Home > Back-end >  Communicate Between Containers in the Same Pod Using a Shared Volume
Communicate Between Containers in the Same Pod Using a Shared Volume

Time:11-03

I am trying to learn Kubernetes. I was going through a link but not able to understand one thing.

URL: https://kubernetes.io/docs/tasks/access-application-cluster/communicate-containers-same-pod-shared-volume/

Mentioned in the doc:

Notice that the second container writes the index.html file in the root directory of the nginx server.

How is this HTML file created in the root directory of the Nginx server? As we are executing this command in the different container i.e. debian-container.

CodePudding user response:

In the example from the link you posted you have:

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"]

You have two containers which share a volume, named shared-data. Each container decides where does it want to mount the volume:

  • nginx-container is mounting it to /usr/share/nginx/html (also known as Nginx's default root directory)
  • debian-container is mounting it to /pod-data.

Since shared volume is like a shared directory on a computer or in a cloud, everything present in the volume is available in both containers under the mountPath path.

In the example, the debian-container writes index.html file to a shared space, which can then be used by nginx-container.

This is a well-known pattern in Kubernetes, called Sidecar Container Pattern. In this case debian-container is a sidecar container. It extends and enhances the functionality of main container (nginx-container) - provides index.html file in this simple example.


Resources:

  • Related