Kubernetes Shared Volumes Kodekloud

 

Question: 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. Below you can find more details about it.

    Create a pod named volume-share-datacenter.

    For the first container, use image fedora with latest tag only and remember to mention the tag i.e fedora:latest, container should be named as volume-container-datacenter-1, and run a sleep command for it so that it remains in running state. Volume volume-share should be mounted at path /tmp/blog.

    For the second container, use image fedora with the latest tag only and remember to mention the tag i.e fedora:latest, container should be named as volume-container-datacenter-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/cluster.

    Volume name should be volume-share of type emptyDir.

    After creating the pod, exec into the first container i.e volume-container-datacenter-1, and just for testing create a file blog.txt with any content under the mounted path of first container i.e /tmp/blog.

    The file blog.txt should be present under the mounted path /tmp/cluster on the second container volume-container-datacenter-2 as well, since they are using a shared volume.

Note: The kubectl utility on jump_host has been configured to work with the kubernetes cluster.


Solution:

Create a pod.yaml according to question. mountPath and image name can be changed

vi pod.yaml

apiVersion: v1
kind: Pod
metadata:
name: volume-share-datacenter
labels:
name: app
spec:
volumes:
- name: volume-share
emptyDir: {}
containers:
- name: volume-container-datacenter-1
image: fedora:latest
command: ["/bin/bash", "-c", "sleep 10000"]
volumeMounts:
- name: volume-share
mountPath: /tmp/blog
- name: volume-container-datacenter-2
image: fedora:latest
command: ["/bin/bash", "-c", "sleep 10000"]
volumeMounts:
- name: volume-share
mountPath: /tmp/cluster

Then apply it

kubectl apply -f pod.yaml

kubectl get po

Exec into the first container and create a mentioned txt file under the mentioned mountpath

kubectl exec -it volume-share-datacenter -c volume-container-datacenter-1  /bin/bash

cd /tmp/blog

vi blog.txt

Now exec into second container and go to mentioned path to check if the txt file exists or not

kubectl exec -it volume-share-datacenter -c volume-container-datacenter-2  /bin/bash

cd /tmp/cluster

ls

If you find the file then task is done.

 




Share on Google Plus

About Ashadullah Shawon

I am Ashadullah Shawon. I am a Software Engineer. I studied Computer Science and Engineering (CSE) at RUET. I Like To Share Knowledge. Learn More: Click Here
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment