DEV Community

Cover image for 1.Kubernetes Shared Volumes
Thu Kha Kyawe
Thu Kha Kyawe

Posted on

1.Kubernetes Shared Volumes

Lab Information

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-devops.

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-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/beta.

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-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/cluster.

Volume name should be volume-share of type emptyDir.

After creating the pod, exec into the first container i.e volume-container-devops-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/cluster on the second container volume-container-devops-2 as well, since they are using a shared volume.
Enter fullscreen mode Exit fullscreen mode

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

Lab Solutions

Step 1: Create the Pod YAML configuration

Create a file named volume-share-pod.yaml:

cat > volume-share-pod.yaml << EOF
apiVersion: v1
kind: Pod
metadata:
  name: volume-share-devops
spec:
  containers:
  - name: volume-container-devops-1
    image: fedora:latest
    command: ["/bin/sleep"]
    args: ["3650d"]
    volumeMounts:
    - name: volume-share
      mountPath: /tmp/beta

  - name: volume-container-devops-2
    image: fedora:latest
    command: ["/bin/sleep"]
    args: ["3650d"]
    volumeMounts:
    - name: volume-share
      mountPath: /tmp/cluster

  volumes:
  - name: volume-share
    emptyDir: {}
EOF
Enter fullscreen mode Exit fullscreen mode

Step 2: Create the pod

Apply the configuration to create the pod:

kubectl apply -f volume-share-pod.yaml
Enter fullscreen mode Exit fullscreen mode

Step 3: Verify the pod is running

Check if the pod is created and running:

kubectl get pods volume-share-devops
Enter fullscreen mode Exit fullscreen mode

Step 4: Create test file in first container

Execute into the first container and create the test file:

kubectl exec -it volume-share-devops -c volume-container-devops-1 -- /bin/bash
Enter fullscreen mode Exit fullscreen mode

Once inside the container, create the file:

echo "This is a test file for shared volume" > /tmp/beta/beta.txt
Enter fullscreen mode Exit fullscreen mode

Exit the container:
exit

Step 5: Verify file exists in second container

Check if the file is accessible from the second container:

kubectl exec -it volume-share-devops -c volume-container-devops-2 -- cat /tmp/cluster/beta.txt
Enter fullscreen mode Exit fullscreen mode


Resources & Next Steps
📦 Full Code Repository: KodeKloud Learning Labs
📖 More Deep Dives: Whispering Cloud Insights - Read other technical articles
💬 Join Discussion: DEV Community - Share your thoughts and questions
💼 Let's Connect: LinkedIn - I'd love to connect with you

Credits
• All labs are from: KodeKloud
• I sincerely appreciate your provision of these valuable resources.

Top comments (0)