How to use a Volume to communicate between two Containers running in the same Kubernetes-Pod

+1 vote
How to establish communication between containers in the same Kubernetes-Pod Using a Shared Volume?
Jul 23, 2019 in Kubernetes by Karan
• 19,610 points
2,360 views

1 answer to this question.

+1 vote

Create a Pod that runs two Containers

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:

pods/two-container-pod.yaml 

apiVersion: v1
kind: Pod
metadata:
  name: two-containers
spec:

  restartPolicy: Never

  volumes:
  - name: shared-data
    emptyDir: {}

  containers:

  - name: nginx-container
    image: nginx
    volumeMounts:
    - name: shared-data
      mountPath: /usr/share/nginx/html

  - name: debian-container
    image: debian
    volumeMounts:
    - name: shared-data
      mountPath: /pod-data
    command: ["/bin/sh"]
    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.

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:

kubectl apply -f https://k8s.io/examples/pods/two-container-pod.yaml
  • View information about the Pod and the Containers:

kubectl get pod two-containers --output=yaml
  • Here is a portion of the output:

apiVersion: v1
kind: Pod
metadata:
  ...
  name: two-containers
  namespace: default
  ...
spec:
  ...
  containerStatuses:

  - containerID: docker://c1d8abd1 ...
    image: debian
    ...
    lastState:
      terminated:
        ...
    name: debian-container
    ...

  - containerID: docker://96c1ff2c5bb ...
    image: nginx
    ...
    name: nginx-container
    ...
    state:
      running:
    ...
  • You can see that the debian Container has terminated, and the nginx Container is still running.

  • Get a shell to nginx Container:

kubectl exec -it two-containers -c nginx-container -- /bin/bash
  • In your shell, verify that nginx is running:

root@two-containers:/# apt-get update
root@two-containers:/# apt-get install curl procps
root@two-containers:/# ps aux
  • The output is similar to this:

USER       PID  ...  STAT START   TIME COMMAND
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:

root@two-containers:/# curl localhost
  • The output shows that nginx serves a web page written by the debian container:

Hello from the debian container

The Volume in this example 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.

answered Jul 23, 2019 by Sirajul
• 59,230 points

Related Questions In Kubernetes

0 votes
1 answer

How to use gravitational teleport in a container/kubernetes environment?

You can use teleport to augment kubernetes ...READ MORE

answered Jun 28, 2018 in Kubernetes by ajs3033
• 7,300 points
2,225 views
0 votes
1 answer

How to configure a Pod to use the updated configMap?

If the config map is mounted into ...READ MORE

answered Jul 18, 2019 in Kubernetes by Sirajul
• 59,230 points
791 views
0 votes
1 answer

Configure a Kubernetes-pod to use persistent volume for storage.

The output shows that the PersistentVolume has ...READ MORE

answered Jul 23, 2019 in Kubernetes by Sirajul
• 59,230 points
2,533 views
+1 vote
1 answer
0 votes
3 answers

Error while joining cluster with node

Hi Kalgi after following above steps it ...READ MORE

answered Jan 17, 2019 in Others by anonymous
14,576 views
+4 votes
1 answer

Installing Web UI (Dashboard):kubernetes-dashboard on main Ubuntu 16.04.6 LTS (Xenial Xerus) server

Follow these steps: $ kubeadm reset $ kubeadm init ...READ MORE

answered Apr 12, 2019 in Kubernetes by Kalgi
• 52,360 points

reshown Apr 12, 2019 by Kalgi 6,053 views
0 votes
1 answer

how can i access two containers that are inside a pod from the browser with IP address?

Just do port forward. kubectl port-forward [nginx-pod-name] 80:80 kubectl ...READ MORE

answered Jul 18, 2019 in Kubernetes by Sirajul
• 59,230 points
1,612 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP