↓ Skip to main content
Kubernetes

MultiContainer: K8S

·2 mins

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 share storage or other resources. (Storage and Networking)
  • Pods can support multiple cooperating processes.
  • Workload resources is used to manage the multiple pods. (DaemonSet, Deployment, SatefulSet)
From the official documentation
From the official documentation
Refer to code

Basic Structure
#

FieldDescription
apiVersionVersioned schema for representation of the object.
kindThe object (Persistent entity) being represented. Typically categorised in three groups (details here) 1. Objects 2. List 3. Simple
metadataEvery object must have metadata that can define 1. namespace 2. name 3. unique ID
specA complete description of the desired state.

Example
#

apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: null
  labels:
    app: my-pod
    author: samarthya
  name: myepserver
spec:
  containers:
  - env:
    - name: SERVERPORT
      value: "9090"
    image: samarthya/epserver:2.0
    name: myepserver
    ports:
    - containerPort: 9090
    resources: {}
  dnsPolicy: ClusterFirst
  restartPolicy: Always
status: {}
k api-resources — gives the complete list of resources supported

The YAML (from example above) in form of a command

k run myepserver --image samarthya/epserver:2.0 --env SERVERPORT=9090 --port=9090 -l'app=my-pod,author=samarthya'

I could have generated the same YAML using

k run myepserver --image samarthya/epserver:2.0 --env SERVERPORT=9090 --port=9090 -l'app=my-pod,author=samarthya' --dry-run=client -o yaml
In terms of Docker concepts, a Pod is similar to a group of Docker containers with shared namespaces and shared filesystem volumes.

Lifecycle of a POD
#

When we use k get pods to see the pods running the status field defined is the Phase associated with the Pod and can be either of Pending, Running, Succeeded, Failed or Unknown.

k get pod myepserver -o=jsonpath="{range .status.conditions[*]}{'\n'}{.type}{'\t'}{.lastTransitionTime}{end}"
Initialized 2020-08-27T08:41:06Z
Ready 2020-08-27T08:41:16Z
ContainersReady 2020-08-27T08:41:16Z

If you are wondering about the jsonpath used above; it is a very powerful way to parse and get the desired information bit.

Example: To get the host IP you can use a command like below.

k get pod myepserver -o=jsonpath="{.status.hostIP}{'\n'}"

To get a restart policy

k get pod myepserver -o=jsonpath="{.spec.restartPolicy}{'\n'}"
k get pod myepserver -o=jsonpath="{range .status.conditions[*]}{'\n'}{.type}{'\t'}{.lastTransitionTime}{'\t'}{.lastProbeTime}{'->'}{.status}{end}{'\n'}"
Initialized     2020-08-27T08:41:06Z    <nil>->True
Ready   2020-08-27T08:41:16Z    <nil>->True
ContainersReady 2020-08-27T08:41:16Z    <nil>->True
PodScheduled    2020-08-27T08:41:06Z    <nil>->True
Like Pod, a container also has lifecycle and possible states: Waiting, Running, Terminated.
NAME         READY   STATUS    RESTARTS   AGE
myepserver   1/1     Running   0          63m

More..
#

Author
Samarthya
Engineer, learner, tinkerer. This is my trophy shelf of everyday work and leisure.

Related

Kubectl - A revisit

·3 mins
It is always fun learning and trying out some new tech. In this blog, I will try and capture my experience of running my first pod using kubectl. If you wish to learn how to install a typical master, worker cluster of K8S I recommend going through this blog.

Helpful links for K8S

·1 min
Always remember to refer the API and elements for the YAML structure API Overview — official K8S documentation The Kuberenetes documentation that has text descriptions and good explanatory examples.

Deployments: K8S

·1 min
A declarative way of maintaining the desired state. Continuing my example from the last blog --- # Pod deployment apiVersion: apps/v1 kind: Deployment metadata: name: go-server-deployment spec: replicas: 2 selector: matchLabels: app: my-go-server template: metadata: labels: app: my-go-server spec: containers: - name: go-server image: samarthya/epserver:2.0 resources: limits: memory: "256Mi" cpu: "1024m" env: - name: SERVERPORT value: "9090" ports: - containerPort: 9090 # Reference - https://kubernetes.io/docs/concepts/workloads/controllers/deployment/ In my deployment, I have two replicas configured, which can be increased to three using the command like the one below.