CKAD Exam preparation -Application Design and Build -Part3

Balasekar Natarajan
2 min readDec 18, 2021

In continuation with part 2 here, we will progress on the Application Design and Build section with Understanding Multi-Pod container design.

Multi-Container Pod

A Pod can encapsulate an application composed of multiple co-located containers that are tightly coupled and need to share resources. The Pod wraps these containers, storage resources, and an ephemeral network identity together as a single unit.

Run pod with multiple container and sample command,

  1. Dry-run to save a template file as multipod.yaml
$ kubectl run multipod --image busybox -o yaml --dry-run  --'/bin/sh' '-c' 'date;sleeo 3;date;' > multipod.yaml

2. Edit the file to add another container under the container section, vi multipod.yaml and exit using ESC :x

- name: pod2
image: busybox
command: [sh, -c, echo message from pod2 ]

Complete file multipod.yaml

apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
labels:
run: multipod
name: multipod
spec:
containers:
- args:
- /bin/sh
- -c
- date;sleep3; date
image: busybox
name: pod1
resources: {}
- name: pod2
image: busybox
command: [sh, -c, echo message from pod2 ]
dnsPolicy: ClusterFirst
restartPolicy: Always
status: {}

3. Create the pod on the cluster

$ kubectl create -f multipod.yaml
pod/multipod created

4. To check logs ,

$ kubectl logs multipod pod1 // logs from pod1
$ kubectl logs multipod pod2 // logs from pod2

5. To describe the pod in cluster

$ kubectl describe pod multipod

Init Containers:

Init containers are exactly like regular containers, except:

  • Init containers always run to completion.
  • Each init container must complete successfully before the next one starts.

The initcontainer could be used to validate other services, perform actions that are required before the normal container start up

Sample template:

apiVersion: v1
kind: Pod
metadata:
name: myapp-pod
labels:
app: myapp
spec:
containers:
- name: myapp-container
image: busybox:1.28
command: ['sh', '-c', 'echo The app is running! && sleep 3600']
initContainers:
- name: init-myservice
image: busybox:1.28
command: ['sh', '-c', "until nslookup myservice.$(cat /var/run/secrets/kubernetes.io/serviceaccount/namespace).svc.cluster.local; do echo waiting for myservice; sleep 2; done"]
- name: init-mydb
image: busybox:1.28
command: ['sh', '-c', "until nslookup mydb.$(cat /var/run/secrets/kubernetes.io/serviceaccount/namespace).svc.cluster.local; do echo waiting for mydb; sleep 2; done"]

The above code lookup the service availability of myservice and mydb before starting the app container.

Browser Bookmarks for exam:

Init Container: https://kubernetes.io/docs/concepts/workloads/pods/init-containers/#init-containers-in-use

Remove initContainers: in the above example to make it an template for sidecar if required

--

--

Balasekar Natarajan

I am a Software Engineer, Vivid Technical & Technology story reader. My interest are Cloud, Containerization, Linux and yet to read longlist. Views are my own.