54/100 Kubernetes Shared Volumes

We are working on an application that will be deployed on multiple containers within a pod on Kubernetes cluster. There is a requirement to share a volume among the containers to save some temporary data. The Nautilus DevOps team is developing a similar template to replicate the scenario.

  1. Create a pod named volume-share-devops.
  2. For the first container, use image ubuntu with latest tag only and remember to mention the tag i.e ubuntu:latest, container should be named as volume-container-devops-1, and run a sleep command for it so that it remains in running state. Volume volume-share should be mounted at path /tmp/official.
  3. For the second container, use image ubuntu with the latest tag only and remember to mention the tag i.e ubuntu:latest, container should be named as volume-container-devops-2, and again run a sleep command for it so that it remains in running state. Volume volume-share should be mounted at path /tmp/games.
  4. Volume name should be volume-share of type emptyDir.
  5. After creating the pod, exec into the first container i.e volume-container-devops-1, and just for testing create a file official.txt with any content under the mounted path of first container i.e /tmp/official.
  6. The file official.txt should be present under the mounted path /tmp/games on the second container volume-container-devops-2 as well, since they are using a shared volume.

First create the volume-share-devops.yaml file:

apiVersion: v1
kind: Pod
metadata:
  name: volume-share-devops
spec:
  containers:
    - name: volume-container-devops-1
      image: ubuntu:latest
      command: ["sleep", "3600"]
      volumeMounts:
        - name: volume-share
          mountPath: /tmp/official

    - name: volume-container-devops-2
      image: ubuntu:latest
      command: ["sleep", "3600"]
      volumeMounts:
        - name: volume-share
          mountPath: /tmp/games

  volumes:
    - name: volume-share
      emptyDir: {}

Create the pod with the following command:

thor@jumphost ~$ kubectl apply -f volume-share-devops.yaml
pod/volume-share-devops created

Next verify the pod is running:

thor@jumphost ~$ kubectl get pods
NAME                  READY   STATUS    RESTARTS   AGE
volume-share-devops   2/2     Running   0          55s

Create the file inside the first container by exec'ing into the container:

thor@jumphost ~$ kubectl exec -it volume-share-devops -c volume-container-devops-1 -- bash

# This will place you inside the container so you can create the file:
root@volume-share-devops:/# 

root@volume-share-devops:/# echo "This is shared data" > /tmp/official/official.txt
ls /tmp/official
official.txt

exit

Verify from the second container that we can see the official.txt file:

thor@jumphost ~$ kubectl exec -it volume-share-devops -c volume-container-devops-2 -- bash
root@volume-share-devops:/# ls /tmp/games
official.txt
root@volume-share-devops:/# cat /tmp/games/official.txt
This is shared data