MultiContainer: K8S
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
controlleris used to manage thePodinstances. - 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)
Refer to code
Basic Structure
| Field | Description |
|---|---|
| apiVersion | Versioned schema for representation of the object. |
| kind | The object (Persistent entity) being represented. Typically categorised in three groups (details here) 1. Objects 2. List 3. Simple |
| metadata | Every object must have metadata that can define 1. namespace 2. name 3. unique ID |
| spec | A 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 yamlIn 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:16ZIf 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>->TrueLike Pod, a container also has lifecycle and possible states: Waiting, Running, Terminated.
NAME READY STATUS RESTARTS AGE
myepserver 1/1 Running 0 63m