Multi container pod with Kubernetes: use case — Share volume,inter-process communication

Welcome to my new topic related to how to run multi container pod!!!!

Let’s discuss some of the key concept, for good understanding.

What’s pod ?

Pod is a small unit that contains a container used in kubernetes for creating and management.Pod is a group of one or more container with share capacity and network resources.

Typically you don’t need to create a pod with single container or multiple container. There are two way for run container inside pod.

- Pod that run a single container: most common in kubernetes use case

- Pod that run multi container : Pod can encapsulate multiple container that communicate together

This guide is base on multi container. let’s jump directly to practical part.

  • Share volume with kubernetes

Application of share volume with kubernetes using yaml file.

apiVersion: v1
kind: Pod
metadata:
name: mc1
spec:
volumes:
- name: html
emptyDir: {}
containers:
- name: 1st
image: nginx
volumeMounts:
- name: html
mountPath: /usr/share/nginx/html

Run yaml file by using kubectl command

kubectl apply -y Pod_share_vol.yml

Running yaml file will create two container inside one pod. one container running nginx and other container running debian.

Check status of pods by kubectl get command

kubectl get pods

Print description of pod mc1

kubectl describe pods mc1

Output will be:

Name:         mc1
Namespace: default
Priority: 0
Node: minikube/192.168.99.101
Start Time: Thu, 25 Mar 2021 08:30:39 +0000
Labels: <none>
Annotations: <none>
Status: Running
IP: 172.17.0.2
IPs:
IP: 172.17.0.2
Containers:
1st:
Container ID: docker://91fc6923b10e9632b2c3c6b019f420b3263c0c5d3753c4d840eeea515c09e1bb
Image: nginx
Image ID: docker-pullable://nginx@sha256:d2925188effb4ddca9f14f162d6fba9b5fab232028aa07ae5c1dab764dca8f9f
Port: <none>
Host Port: <none>
State: Running
Started: Thu, 25 Mar 2021 08:32:51 +0000
Ready: True
Restart Count: 0
Environment: <none>
Mounts:
/usr/share/nginx/html from html (rw)
/var/run/secrets/kubernetes.io/serviceaccount from default-token-bbbxz (ro)
2nd:
Container ID: docker://3f8d4b5adac81d9ad957924645d167f659634f19a5b2cc09c60c576f7ba50021
Image: debian
Image ID: docker-pullable://debian@sha256:9d4ab94af82b2567c272c7f47fa1204cd9b40914704213f1c257c44042f82aac
Port: <none>
Host Port: <none>
Command:
/bin/sh
-c
Args:
while true; do date >> /html/index.html; sleep 1; done
State: Running
Started: Thu, 25 Mar 2021 08:33:10 +0000
Ready: True
Restart Count: 0
Environment: <none>
Mounts:
/html from html (rw)
/var/run/secrets/kubernetes.io/serviceaccount from default-token-bbbxz (ro)
Conditions:
Type Status
Initialized True
Ready True
ContainersReady True
PodScheduled True
Volumes:
html:
Type: EmptyDir (a temporary directory that shares a pod's lifetime)
Medium:
SizeLimit: <unset>
default-token-bbbxz:
Type: Secret (a volume populated by a Secret)
SecretName: default-token-bbbxz
Optional: false
QoS Class: BestEffort
Node-Selectors: <none>
Tolerations: node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 7m39s default-scheduler Successfully assigned default/mc1 to minikube
Normal Pulling 7m38s kubelet Pulling image "nginx"
Normal Pulled 5m28s kubelet Successfully pulled image "nginx" in 2m9.983579993s
Normal Created 5m28s kubelet Created container 1st
Normal Started 5m27s kubelet Started container 1st
Normal Pulling 5m27s kubelet Pulling image "debian"
Normal Pulled 5m9s kubelet Successfully pulled image "debian" in 18.602558106s
Normal Created 5m9s kubelet Created container 2nd
Normal Started 5m8s kubelet Started container 2nd

Notice, two container is running successfully with share volume mount in html repository.

Use kubectl exec command with -c <name_container> argument to connect with each container.

kubectl exec -it <pod_name> -c <container_name> -- bash 

Describe in yaml file , index.html file is a share ressource connect to each container. Let’s connect to each container and verify .

#Connect to 1st container (nginx)
kubectl exec -it mc1 -c 1st -- bash
ls /usr/share/nginx/html/
#Connect to 2nd container (debian)
kubectl exec -it mc1 -c 2nd -- bash
ls /html/
  • Inter-process communication in one pod

Inter-process communication is a share communication message between two container.

In this example, we will set up one pod with two container. One container for producer and second for consumer.

Yaml file for Inter-process communication will be:

apiVersion: v1
kind: Pod
metadata:
name: mcipc
spec:
containers:
- name: producer
image: allingeek/ch6_ipc
command: ["./ipc", "-producer"]
- name: consumer
image: allingeek/ch6_ipc
command: ["./ipc", "-consumer"]
restartPolicy: Never

Run ipc.yml file for creating pod

kubectl apply -f ipc.yml

Use kubectl describe command for description

kubectl describe pods mcipc

output will be:

Name:         mcipc
Namespace: default
Priority: 0
Node: minikube/192.168.99.101
Start Time: Thu, 25 Mar 2021 11:09:33 +0000
Labels: <none>
Annotations: <none>
Status: Succeeded
IP: 172.17.0.5
IPs:
IP: 172.17.0.5
Containers:
producer:
Container ID: docker://ce494c14c71606908d3ca851797f6cf63fab454a60c23a4af763afefe03afcc4
Image: allingeek/ch6_ipc
Image ID: docker-pullable://allingeek/ch6_ipc@sha256:eeeb7255a341751d31fbcfbeef0b27a9f02e61819ac6ab4b95bcdbd223b5f250
Port: <none>
Host Port: <none>
Command:
./ipc
-producer
State: Terminated
Reason: Completed
Exit Code: 0
Started: Thu, 25 Mar 2021 11:09:40 +0000
Finished: Thu, 25 Mar 2021 11:09:44 +0000
Ready: False
Restart Count: 0
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from default-token-bbbxz (ro)
consumer:
Container ID: docker://62e52246cb2b9d45d5040bfd06437cbcaf55d538dace7d38b3e331e6fd558968
Image: allingeek/ch6_ipc
Image ID: docker-pullable://allingeek/ch6_ipc@sha256:eeeb7255a341751d31fbcfbeef0b27a9f02e61819ac6ab4b95bcdbd223b5f250
Port: <none>
Host Port: <none>
Command:
./ipc
-consumer
State: Terminated
Reason: Completed
Exit Code: 0
Started: Thu, 25 Mar 2021 11:09:44 +0000
Finished: Thu, 25 Mar 2021 11:09:44 +0000
Ready: False
Restart Count: 0
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from default-token-bbbxz (ro)
Conditions:
Type Status
Initialized True
Ready False
ContainersReady False
PodScheduled True
Volumes:
default-token-bbbxz:
Type: Secret (a volume populated by a Secret)
SecretName: default-token-bbbxz
Optional: false
QoS Class: BestEffort
Node-Selectors: <none>
Tolerations: node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 72s default-scheduler Successfully assigned default/mcipc to minikube
Normal Pulling 71s kubelet Pulling image "allingeek/ch6_ipc"
Normal Pulled 65s kubelet Successfully pulled image "allingeek/ch6_ipc" in 5.124024775s
Normal Created 65s kubelet Created container producer
Normal Started 65s kubelet Started container producer
Normal Pulling 65s kubelet Pulling image "allingeek/ch6_ipc"
Normal Pulled 61s kubelet Successfully pulled image "allingeek/ch6_ipc" in 4.144985362s
Normal Created 61s kubelet Created container consumer
Normal Started 61s kubelet Started container consumer

Check the logs for each container, means producer create a message queue and consumer receiver entire message

#Logs for producer
kubectl logs mcipc -c producer
#Logs for consumer
kubectl logs mcipc -c consumer

In this post, we have demonstrate share volume and Inter-process communication in multi container pod.

Hopefully you have enjoy this topic, follow me for my next write-up!!!!!!!!!

Cloud / Cybersecurity enthusiast. CehV10, RHCSA, RHCE, Comptia Sec +, ITF +, AWS Architect