MultiContainer: K8S

Saurabh Sharma

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

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: saurabh
  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=saurabh'

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=saurabh' --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..