Thursday, 9 October 2025

Kubernetes Shared Volumes: Multi-Container Pod with emptyDir Volume

Our course you can check :-   Udemy course 

Ques:-   


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. DevOps team is developing a similar template to replicate the scenario. Below you can find more details about it. Create a pod named volume-share-xfusion. 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-xfusion-1, and run a sleep command for it so that it remains in running state. Volume volume-share should be mounted at path /tmp/beta. 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-xfusion-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. Volume name should be volume-share of type emptyDir. After creating the pod, exec into the first container i.e volume-container-xfusion-1, and just for testing create a file beta.txt with any content under the mounted path of first container i.e /tmp/beta. The file beta.txt should be present under the mounted path /tmp/games on the second container volume-container-xfusion-2 as well, since they are using a shared volume.
Ans:-
Here's the complete Kubernetes manifest to create the pod volume-share-xfusion with two containers sharing an emptyDir volume named volume-share:
raj@jumphost ~$ cat pod.yaml apiVersion: v1 kind: Pod metadata: name: volume-share-xfusion spec: volumes: - name: volume-share emptyDir: {} containers: - name: volume-container-xfusion-1 image: ubuntu:latest command: ["sleep", "3600"] volumeMounts: - name: volume-share mountPath: /tmp/beta - name: volume-container-xfusion-2 image: ubuntu:latest command: ["sleep", "3600"] volumeMounts: - name: volume-share mountPath: /tmp/games Create the pod:
raj@jumphost ~$ kubectl apply -f pod.yaml pod/volume-share-xfusion created Verify pod status: raj@jumphost ~$ kubectl get pods volume-share-xfusion NAME READY STATUS RESTARTS AGE volume-share-xfusion 2/2 Running 0 15s Exec into the first container and create the file:
raj@jumphost ~$ kubectl exec -it volume-share-xfusion -c volume-container-xfusion-1 -- bash root@volume-share-xfusion:/# echo "Shared volume test" > /tmp/beta/beta.txt root@volume-share-xfusion:/# cat /tmp/beta/beta.txt Shared volume test root@volume-share-xfusion:/# exit Exec into the second container and verify the file: raj@jumphost ~$ kubectl exec -it volume-share-xfusion -c volume-container-xfusion-2 -- bash root@volume-share-xfusion:/# cat /tmp/games/beta.txt Shared volume test root@volume-share-xfusion:/# You should see the content Shared volume test, confirming that the volume is shared correctly between the containers.

No comments:

Post a Comment