{"id":976,"date":"2020-08-27T11:18:17","date_gmt":"2020-08-27T11:18:17","guid":{"rendered":"https:\/\/blog.samarthya.me\/wps\/?p=976"},"modified":"2020-08-27T11:18:18","modified_gmt":"2020-08-27T11:18:18","slug":"multicontainer-k8s","status":"publish","type":"post","link":"https:\/\/blog.samarthya.me\/wps\/2020\/08\/27\/multicontainer-k8s\/","title":{"rendered":"MultiContainer: K8S"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\"><a href=\"https:\/\/kubernetes.io\/docs\/reference\/generated\/kubernetes-api\/v1.18\/#pod-v1-core\">POD<\/a><\/h2>\n\n\n\n<p>Smallest deployment unit you can manage in K8S, is a <code>Pod<\/code>. A pod may have one or more containers.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Points to remember<\/h3>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/kubernetes.io\/docs\/reference\/generated\/kubernetes-api\/v1.18\/#pod-v1-core\">Pod<\/a> is a specification for container.<\/li><li>Pods do not self-heal.<\/li><li>In K8S, a <code>controller<\/code> is used to manage the <code>Pod<\/code> instances.<\/li><li>Pod is a group of one or more containers<\/li><li>It may share storage or other resources. (Storage and Networking)<\/li><li>Pods can support multiple cooperating processes.<\/li><li>Workload resources is used to manage the multiple pods. (DaemonSet, <a href=\"https:\/\/kubernetes.io\/docs\/concepts\/workloads\/controllers\/deployment\/\">Deployment<\/a>, <a href=\"https:\/\/kubernetes.io\/docs\/concepts\/workloads\/controllers\/statefulset\/\">SatefulSet<\/a>)<\/li><\/ul>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img fetchpriority=\"high\" decoding=\"async\" src=\"https:\/\/d33wubrfki0l68.cloudfront.net\/aecab1f649bc640ebef1f05581bfcc91a48038c4\/728d6\/images\/docs\/pod.svg\" alt=\"\" width=\"456\" height=\"485\"\/><figcaption>From the official <a href=\"https:\/\/d33wubrfki0l68.cloudfront.net\/aecab1f649bc640ebef1f05581bfcc91a48038c4\/728d6\/images\/docs\/pod.svg\">documentation<\/a><\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-pullquote has-background has-luminous-vivid-orange-background-color is-style-solid-color\"><blockquote class=\"has-text-color has-black-color\"><p>Refer to <a href=\"https:\/\/github.com\/kubernetes\/community\/blob\/master\/contributors\/devel\/sig-architecture\/api-conventions.md#resources\">code<\/a><\/p><\/blockquote><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Basic Structure<\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-subtle-pale-blue-background-color has-background\"><thead><tr><th>Field<\/th><th>Description<\/th><\/tr><\/thead><tbody><tr><td>apiVersion<\/td><td>Versioned schema for representation of the object.<\/td><\/tr><tr><td><a href=\"https:\/\/github.com\/kubernetes\/community\/blob\/master\/contributors\/devel\/sig-architecture\/api-conventions.md#types-kinds\">kind<\/a><\/td><td>The object (Persistent entity) being represented. Typically categorised in three groups (details <a href=\"https:\/\/github.com\/kubernetes\/community\/blob\/master\/contributors\/devel\/sig-architecture\/api-conventions.md#types-kinds\">here<\/a>)<br>1. Objects<br>2. List<br>3. Simple<\/td><\/tr><tr><td><a href=\"https:\/\/github.com\/kubernetes\/community\/blob\/master\/contributors\/devel\/sig-architecture\/api-conventions.md#metadata\">metadata<\/a><\/td><td>Every object must have <a href=\"https:\/\/kubernetes.io\/docs\/reference\/generated\/kubernetes-api\/v1.18\/#objectmeta-v1-meta\">metadata<\/a> that can define<br>1. namespace<br>2. <a href=\"https:\/\/kubernetes.io\/docs\/concepts\/overview\/working-with-objects\/names\/\">name<\/a><br>3. unique ID<\/td><\/tr><tr><td><a href=\"https:\/\/git.k8s.io\/community\/contributors\/devel\/sig-architecture\/api-conventions.md#spec-and-status\">spec<\/a><\/td><td>A complete description of the desired <a href=\"https:\/\/kubernetes.io\/docs\/reference\/generated\/kubernetes-api\/v1.18\/#podspec-v1-core\">state<\/a>.<br><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\">Example<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>apiVersion: v1\nkind: Pod\nmetadata:\n  creationTimestamp: null\n  labels:\n    app: my-pod\n    author: saurabh\n  name: myepserver\nspec:\n  containers:\n  - env:\n    - name: SERVERPORT\n      value: \"9090\"\n    image: samarthya\/epserver:2.0\n    name: myepserver\n    ports:\n    - containerPort: 9090\n    resources: {}\n  dnsPolicy: ClusterFirst\n  restartPolicy: Always\nstatus: {}<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-pullquote has-background has-luminous-vivid-orange-background-color is-style-solid-color\"><blockquote class=\"has-text-color has-black-color\"><p>k api-resources<\/p><cite>gives the complete list of resources supported<\/cite><\/blockquote><\/figure>\n\n\n\n<p>The YAML (from example above) in form of a command<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>k run myepserver --image samarthya\/epserver:2.0 --env SERVERPORT=9090 --port=9090 -l'app=my-pod,author=saurabh'<\/code><\/pre>\n\n\n\n<p>I could have generated the same YAML using<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>k run myepserver --image samarthya\/epserver:2.0 --env SERVERPORT=9090 --port=9090 -l'app=my-pod,author=saurabh' --dry-run=client -o yaml<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-pullquote is-style-default\" style=\"border-color:#ff6900\"><blockquote><p>In terms of Docker concepts, a Pod is similar to a group of Docker containers with shared namespaces and shared filesystem volumes.<\/p><\/blockquote><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Lifecycle of a POD<\/h3>\n\n\n\n<p>When we use <code>k get pods<\/code> to see the pods running the status field defined is the Phase associated with the <code>Pod<\/code> and can be either of <code>Pending<\/code>, <code>Running<\/code>, <code>Succeeded<\/code>, <code>Failed<\/code> or <code>Unknown<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>k get pod myepserver -o=jsonpath=\"{range .status.conditions&#91;*]}{'\\n'}{.type}{'\\t'}{.lastTransitionTime}{end}\"<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>Initialized 2020-08-27T08:41:06Z\nReady 2020-08-27T08:41:16Z\nContainersReady 2020-08-27T08:41:16Z<\/code><\/pre>\n\n\n\n<p>If you are wondering about the <code>jsonpath<\/code> used above; it is a very powerful way to parse and get the desired information bit.<\/p>\n\n\n\n<p>Example: To get the host IP you can use a command like below.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>k get pod myepserver -o=jsonpath=\"{.status.hostIP}{'\\n'}\"<\/code><\/pre>\n\n\n\n<p>To get a restart policy<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>k get pod myepserver -o=jsonpath=\"{.spec.restartPolicy}{'\\n'}\"<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>k get pod myepserver -o=jsonpath=\"{range .status.conditions&#91;*]}{'\\n'}{.type}{'\\t'}{.lastTransitionTime}{'\\t'}{.lastProbeTime}{'->'}{.status}{end}{'\\n'}\"<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>Initialized     2020-08-27T08:41:06Z    &lt;nil>->True\nReady   2020-08-27T08:41:16Z    &lt;nil>->True\nContainersReady 2020-08-27T08:41:16Z    &lt;nil>->True\nPodScheduled    2020-08-27T08:41:06Z    &lt;nil>->True<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-pullquote is-style-solid-color\"><blockquote><p>Like Pod, a container also has lifecycle and possible states: Waiting, Running, Terminated.<\/p><\/blockquote><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code>NAME         READY   STATUS    RESTARTS   AGE\nmyepserver   1\/1     Running   0          63m<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">More..<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/kubernetes.io\/docs\/concepts\/workloads\/pods\/init-containers\/\">Init containers<\/a><\/li><li><a href=\"https:\/\/kubernetes.io\/docs\/concepts\/workloads\/pods\/ephemeral-containers\/\">Ephemeral containers.<\/a><\/li><li><a href=\"https:\/\/kubernetes.io\/docs\/concepts\/containers\/container-lifecycle-hooks\/\">Container lifecycle hooks.<\/a><\/li><\/ul>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>POD Smallest deployment unit you can manage in K8S, is a Pod. A pod may have one or more containers. Points to remember Pod is a specification for container. Pods do not self-heal. In K8S, a controller is used to manage the Pod instances. Pod is a group of one or more containers It may [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":996,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"footnotes":""},"categories":[34],"tags":[18,102,101],"class_list":["post-976","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technical","tag-k8s","tag-multicontainer","tag-pods"],"_links":{"self":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/posts\/976","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/comments?post=976"}],"version-history":[{"count":0,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/posts\/976\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/media\/996"}],"wp:attachment":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/media?parent=976"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/categories?post=976"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/tags?post=976"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}