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.
- Create a pod named
volume-share-devops. - For the first container, use image
ubuntuwithlatesttag only and remember to mention the tag i.eubuntu:latest, container should be named asvolume-container-devops-1, and run asleepcommand for it so that it remains in running state. Volumevolume-shareshould be mounted at path/tmp/official. - For the second container, use image
ubuntuwith thelatesttag only and remember to mention the tag i.eubuntu:latest, container should be named asvolume-container-devops-2, and again run asleepcommand for it so that it remains in running state. Volumevolume-shareshould be mounted at path/tmp/games. - Volume name should be
volume-shareof typeemptyDir. - After creating the pod, exec into the first container i.e
volume-container-devops-1, and just for testing create a fileofficial.txtwith any content under the mounted path of first container i.e/tmp/official. - The file
official.txtshould be present under the mounted path/tmp/gameson the second containervolume-container-devops-2as 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 createdNext verify the pod is running:
thor@jumphost ~$ kubectl get pods
NAME READY STATUS RESTARTS AGE
volume-share-devops 2/2 Running 0 55sCreate 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
exitVerify 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