Multi Container Pods In Kubernetes


Shared Network Namespace

All the containers in the pod share the same network namespace, therefore all containers can communicate with each other on the localhost. For instance, We have two containers in the same pod listening on ports 8080 and 8081 respectively. Container 1 can talk to container 2 on localhost:8080.


Shared Storage Volumes

All the containers can have the same volume mounted so that they can communicate with each other by reading and modifying files in the storage volume.


Shared Process Namespace

Another way for the containers to communicate is with the Shared Process Namespace. With this, the containers inside the pod can signal each other. For this to be enabled, we need to have this setting shareProcessNamespace to true in the pod spec.


Defining a multi-container pod:

vim multiplepod.yml


apiVersion: v1
kind: Pod
metadata:
 name: myfirstpod
 labels:
	label1: harshal
	label2: gaurav
	label3: saurav
spec:
	containers:
      - name: firstcontainer
        image: coolgourav147/nginx-custom
        env:
          - name: myname
            value: Gaurav
          - name: city
            value: Jaipur
        args: ["sleep", "3600"]
      - name: secondcontainer
        image: coolgourav147/nginx-custom


How To Deploy A Multi Container Pod:

To deploy a multi-container command we use the kubectl command given below:

kubectl apply -f multiple-pod.yml


Why Use Multi Container Pods 

Well there are many good reasons why to use them rather than not to use them; here are some of them:

  • The primary purpose of a multi-container Pod is to support co-located, co-managed helper processes for a primary application.
  • With the same network namespace, shared volumes, and the same IPC namespace it is possible for these containers to efficiently communicate, ensuring data locality.
  • They enable you to manage several tightly coupled application containers as a single unit.
  • Another reason is that all containers have the same lifecycle which should run on the same node.