Multi-container pods in Kubernetes and communication between them using a shared Volume
In this blog I will show how to create multi-container pods in Kubernetes and establish communication between them using a shared Volume
You need a few things.
- An existing Kubernetes Cluster or minikube.
- kubectl binary locally installed.
Creating a Pod that runs two Containers
In this blog, We create a Pod that runs two Containers. The two containers share a Volume that they can use to communicate. Here is the configuration file for the Pod:
Having multiple containers in a single Pod makes it relatively straightforward for them to communicate with each other. They can do this using Shared volumes.
In this example, we define a volume named shared-data. Its type is emptyDir, which means that the volume is first created when a Pod is assigned to a node, and exists as long as that Pod is running on that node.As the name says, it is initially empty. The 1st container runs nginx server and has the shared volume mounted to the directory /usr/share/nginx/html.The 2nd container uses the Debian image and has the shared volume mounted to the directory /pod-data.
Notice that the second container writes the index.html file in the root directory of the nginx server.
Create the Pod and the two Containers:
View information about the Pod and the Containers:
Here is a portion of the output:
You can see that the debian Container has terminated, and the nginx Container is still running.
Get a shell to nginx Container:
In your shell, verify that nginx is running:
The output is similar to this:
Recall that the debian container created the index.html file in the nginx root directory. Use curl to send a GET request to the nginx server:
The output shows that nginx serves a web page written by the debian container:
welcome to brainupgrade
This article demonstrated how to create multi-container pod in Kubernetes and establish communication between them using a shared Volume. Although this is a very basic example but perfect example to understand the concept of multi-container pod in Kubernetes.
About Brain Upgrade Academy
We, at Brain Upgrade, offer Kubernetes Consulting services to our clients including Up Skilling (training) of clients teams thus facilitate efficient utilization of Kubernetes Platform. To know more on the Kubernetes please visit www.brainupgrade.in/blog and register on www.brainupgrade.in/enroll to equip yourself with Kubernetes skills.
Why Brain Upgrade
We at Brain Upgrade, partner with our customers in the digital transformation of their businesses by providing:
Technology Consulting in product development, IoT, DevOps, Cloud, Containerization, Big Data Analysis with a heavy focus on Open source technologies. Training the IT workforce on the latest cloud technologies such as Kubernetes, Docker, AI, ML, etc.
You may want to register for the upcoming trainings on https://brainupgrade.in/enroll