Understanding multi-container pod & Init Container in Kubernetes

Tirth Patel
5 min readMar 26, 2021

Let’s start understanding the need of multi-container pod with one example. Before that lets understand what is Multi-Container Pod in kubernetes.

Let’s start by explaining what a Pod is in the first place. A Pod is is the smallest unit that can be deployed and managed by Kubernetes. In other words, if you need to run a single container in Kubernetes, then you need to create a Pod for that container. At the same time, a Pod can contain more than one container, usually because these containers are relatively tightly coupled. How tightly coupled? Well, think of it this way: the containers in a pod represent processes that would have run on the same server in a pre-container world.

And that makes sense, because in many respects, a Pod acts like a single server. For example, each container can access the other containers in the pod as different ports on localhost. So, when there are more than one container present in a Pod is known as multi-container pod.

Now, let’s try to understand by different use cases.

USECASE-1

Suppose you have a HTTPD webserver pod and is deployed on k8s. Now, there is often a need to monitor these webserver using some monitoring tool eg: splunk. Monitoring the pod includes real time analysis of network bandwidth used, how much client come up, catching error or detecting DOS attack etc. So, we need monitoring tool like splunk which has one Agent Program i.e known as Universal Forwarder. So, this program has capability to go inside OS level and hence can monitor our Apache Webserver.

So, in this case we launch one webserver container(let say C1) and a monitoring container (let say C2) inside a single Pod.

Side Car Design

We have to design pod in such a way that C2 will monitor C1. As C2 monitoring container has a agent that can retrieve real time logs and it process locally in the same C2 container. We have to tell the Splunk container that it have to fetch all the logs and metrics from Webserver. It acts as a Helper container. This kind of Multi container pod design is known as Side Car Design. This particular kind of use case is known as Side-car use case.

USECASE-2

Let suppose we have similar kind of design as above. But there is one challenge. Let suppose the log which splunk container collects is not in proper format eg: the login logs contains date, time etc but are not in proper format. So, here we need to format it properly and then store somewhere and analyze later. So, in this use-case the C2 container has ability to fetch all the logs from webserver container and can transform it according to our usecase. After changing format/transforming it will send data to a centralized Monitoring tool. This kind of multi-container pod design is known as Adapter Design.

Adapter Design

USECASE-3

There is often a condition to proxy connections from the application container to other services. So, in this kind of use case Ambassador Design is used. It acts as a Client proxy. Example: There may be a scenario in which the main application connection requests are proxied to multiple Redis Database servers.

Ambassador Design

These are the use cases which leads to design a multi container pod. But, now how two containers communicate to each other inside a pod?

To answer these question, we have three ways by which two container communicate to each other.

  1. Shared Volume

Two containers can communicate to each other with a Shared Volume. Let suppose, you created a PVC and ask for storage. The storage will be mounted to C1 container and C1 can write on the storage. While the other container can only read the storage. So, this way they can communicate.

Shared Volume

2. Shared Memory

Now, every container is basically a Process. So, when one process writes on RAM, any other process can’t read the RAM due to its internal security configured in such way. So, to solve this challenge, we can implement IPC Inter Process Communication through which two process i.e containers here can communicate with each other on RAM i.e memory. The container which writes data is often called as Producer and the other which reads is called Consumer in this design.

Shared Memory

3. Network

As both the containers are launched inside a single pod so both are present in the same network namespace. As they are in same network namespace, they both can communicate to each other using Port number. In kubernetes, IP will only be given to a pod, so can connect on loopback IP on their respective port numbers. So, while exposing any application, we never expose container, we always expose pod and hence we provide — port=<value> parameter.

Network Namespace

Now, in this design there is one challenge. When we launch a multi-container pod both the containers launch together. There is possibility that consumer launch first. But consumer needs data so, producer should run first. So, we want setup such that when C1 container runs and successfully completes then only C2 will run. Here, the use of INIT CONTAINER comes into play.

Let’s understand with one example. Let suppose we want to launch a multi-tier application i.e a wordpress pod and mysql database connected. So, in this case WordPress site cannot be launched without a MySQL Database. This cannot be achieved using Init Container as both are launched in different pods. But with the help of one helper container as Init Container we can setup this.

In a wordpress pod, we can launch two container C1- Wordpress site and C2- helper container which will keep on monitoring MySQL Pod is up and running. So, with the help of helper container as Init Container, i.e when MySQL Pod is completely run then helper function successfully run and then only WordPress site will launch.

Init Container

Hope you would have understood the different use cases and the approach to solve them using Multi Container Pod Designs and Init Container.

Thank You for Reading :)

--

--