Creating multi-container pods in Kubernetes

Filed Under: Random
Kubernetes Multi Container Pod

Hello, readers! This article will focus on Creating a multi-container pod in Kubernetes with examples and scenarios.

So, let us begin!! 馃檪


Kubernetes Pod – Crisp Overview

A Kubernetes pod is the smallest instance of a running application. That is, we pack the application within a container. The container represents the configuration files and all the necessary components of the application.

A pod encapsulates the container and runs itself as the live instance of the application altogether. It is the smallest deployable unit of the application within the cluster to function.


Kubernetes pods are app instances, not containers.

Let us brush up on our basic understanding of containers and Pods and then try to analyze the answer to the above question.

A Kubernetes Container contains all the configurations of the applications, the code, and the necessary libraries that would be needed for the application to run.

As containers don’t appear directly in Kubernetes infrastructure, they are coupled and encapsulated into a Pod.

To manage a container (application), we need additional information regarding the container such as the liveliness probe of the container, restart policy that decides the policy with regards to the termination of the container. To manage and present this information within a container would have been too messy to assist.

That is the reason Kubernetes came up with the concept of Pods which provides this additional information about the containers at run time.


How can we have multiple containers in a single Kubernetes Pod?

By default, all the containers that we plan to reside within a pod run on a logical host. That is, they all share the same IP address, port space, network namespace and can even use shared volumes for ephemeral storage.

Obeying the concept of microservices, we can have multiple containers of the same application within a pod expressing one process per container as the concept. Having all the configurations within the same container will increase the load and violate the microservices policy.

Thus, the containers can communicate efficiently within a single pod and share storage. Having multiple containers within a pod makes it easy and reusable.


Creation of a multi-container Pod

Let us now create a multi-container pod. We will be creating a pod with two containers: nginx and busybox.

Though we have multiple containers configured within the same pod, they can definitely utilize the same or different volume shares according to the requirement. We can customize the functioning and configuration such as resources, volume mounts, secret, etc for every container individually.

apiVersion: v1
kind: Pod
metadata:
  name: multi-pod
spec:
  volumes:
  - name: data-vol
    emptyDir: {}
  containers:
  - name: nginx-shell
    image: nginx
    volumeMounts:
    - name: data-vol
      mountPath: /usr/nginx/data
  - name: busybox-shell
    image: busybox
    volumeMounts:
    - name: data-vol1
      mountPath: /usr/busybox/data
kubectl apply -f pod.yaml -n demo_ns
kubectl get po -n demo_ns

Application specific scenario for multi container pods

The main reason for us to have a multi-container pod is to manage multiple microservices at the same time in a parallel manner.

One of the scenarios can be the use of the container as a sidecar. Usually, sidecars help the main container with some specific processes. For example, we can have a sidecar container to route the traffic of the particular application component to the backend service.


Conclusion

By this, we have come to the end of this topic. Feel free to comment below, in case you come across any questions.

For more such posts related to Kubernetes, Stay tuned with us.

Till then, Happy learning!! 馃檪

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content