Communicate Between Containers in the Same Pod Using a Shared Volume

This page shows how to use a Volume to communicate between two Containers running in the same Pod. See also how to allow processes to communicate by sharing process namespace between containers.

Before you begin

You need to have a Kubernetes cluster, and the kubectl command-line tool must be configured to communicate with your cluster. It is recommended to run this tutorial on a cluster with at least two nodes that are not acting as control plane hosts. If you do not already have a cluster, you can create one by using minikube or you can use one of these Kubernetes playgrounds:

To check the version, enter kubectl version.

Creating a Pod that runs two Containers

In this exercise, you 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:

  1. pods/two-container-pod.yaml
  1. apiVersion: v1
  2. kind: Pod
  3. metadata:
  4. name: two-containers
  5. spec:
  6. restartPolicy: Never
  7. volumes:
  8. - name: shared-data
  9. emptyDir: {}
  10. containers:
  11. - name: nginx-container
  12. image: nginx
  13. volumeMounts:
  14. - name: shared-data
  15. mountPath: /usr/share/nginx/html
  16. - name: debian-container
  17. image: debian
  18. volumeMounts:
  19. - name: shared-data
  20. mountPath: /pod-data
  21. command: ["/bin/sh"]
  22. args: ["-c", "echo Hello from the debian container > /pod-data/index.html"]

In the configuration file, you can see that the Pod has a Volume named shared-data.

The first container listed in the configuration file runs an nginx server. The mount path for the shared Volume is /usr/share/nginx/html. The second container is based on the debian image, and has a mount path of /pod-data. The second container runs the following command and then terminates.

  1. echo Hello from the debian container > /pod-data/index.html

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:

  1. kubectl apply -f https://k8s.io/examples/pods/two-container-pod.yaml

View information about the Pod and the Containers:

  1. kubectl get pod two-containers --output=yaml

Here is a portion of the output:

  1. apiVersion: v1
  2. kind: Pod
  3. metadata:
  4. ...
  5. name: two-containers
  6. namespace: default
  7. ...
  8. spec:
  9. ...
  10. containerStatuses:
  11. - containerID: docker://c1d8abd1 ...
  12. image: debian
  13. ...
  14. lastState:
  15. terminated:
  16. ...
  17. name: debian-container
  18. ...
  19. - containerID: docker://96c1ff2c5bb ...
  20. image: nginx
  21. ...
  22. name: nginx-container
  23. ...
  24. state:
  25. running:
  26. ...

You can see that the debian Container has terminated, and the nginx Container is still running.

Get a shell to nginx Container:

  1. kubectl exec -it two-containers -c nginx-container -- /bin/bash

In your shell, verify that nginx is running:

  1. root@two-containers:/# apt-get update
  2. root@two-containers:/# apt-get install curl procps
  3. root@two-containers:/# ps aux

The output is similar to this:

  1. USER PID ... STAT START TIME COMMAND
  2. root 1 ... Ss 21:12 0:00 nginx: master process nginx -g daemon off;

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:

  1. root@two-containers:/# curl localhost

The output shows that nginx serves a web page written by the debian container:

  1. Hello from the debian container

Discussion

The primary reason that Pods can have multiple containers is to support helper applications that assist a primary application. Typical examples of helper applications are data pullers, data pushers, and proxies. Helper and primary applications often need to communicate with each other. Typically this is done through a shared filesystem, as shown in this exercise, or through the loopback network interface, localhost. An example of this pattern is a web server along with a helper program that polls a Git repository for new updates.

The Volume in this exercise provides a way for Containers to communicate during the life of the Pod. If the Pod is deleted and recreated, any data stored in the shared Volume is lost.

What’s next