How to create a multi-container pod

Saiyam Pathak avatar
By Saiyam Pathak
Director of Technical Evangelism


Learn the ins-and-outs of what a multi-container pod is and how you can create a simple use case of it.


What is a multi-container pod?

In this lecture, we'll be discussing multi-container pods. A multi-container pod is running one or more containers inside the same pod. Now, there can be multiple use cases for that. One of the most popular ones is the sidecar pattern. A sidecar can be used to log the main application logs, or there can be a helper container that can be used to act as a reverse proxy and host the static files from the main container, or it can be used to send out network traffic to the outside world. So, there can be these multiple scenarios for the multi-container pod.

How can we create a multi-container pod?

In this particular scenario, we have container C1 in which there is Nginx and container C2 in which there is Alpine, and they are using the same shared volume. So one container will be writing to the shared volume, and another will read from the shared volume.

Let's see this in action. We will use the command cat multi-container.yaml. I have the file already created, the YAML file. The main part starts from the spec section. Next, we have the volume, which is the empty directory. The empty directory is creating a directory of the node as soon as the pod gets created, and its content gets emptied when the pod dies. Finally, in the container section, you have two containers, and this is the most important thing for multi-container pods. Containers are an array, and you can define multiple containers.

You have an nginx container with the nginx image. Now it has used the shared data volumes to mount onto this particular path, and another container named Alpine uses the Alpine image, and the shared data volume is mounted to the mem-info path. So in this particular example, we are just appending the date and the proc mem info to the index.html file, which we are reading from the Nginx container.

Let's deploy this container. First, we will create a pod using the command kubectl apply -f multi-container.yaml, and once the pod is created, you can verify it by using kubectl get pods. Here we will see two out of two because there are two containers. Therefore, two out of two containers of that particular pod are running. We can see the logs through the kubectl logs -f multi-container command. Now, we cannot see the pod logs directly because we have to specify the container's name. To do that, we'll modify the command as kubectl logs -f multi-container -c nginx-container, and you can see it logs up the container.

Now, we'll verify the pods using the command kubectl get pods and exec into one of the containers inside the multi-container pod, which is the nginx container, through the kubectl exec -it multi-container -c nginx-container sh command. Let's open the shell. As I told you, the communication happens via localhost, and as a result, all the resources are shared between the containers inside the running pod. We will curl the localhost through the curl localhost command, and we can see that the date and memory are present here. If we do it again, we will see the next time. It means the data is getting appended to that particular HTML file from one container and getting read by another. This was a very simple use case of a multi-container pod. Thank you for watching. See you in the next lecture.

Don't stop now, check out your next lesson