☑️Day 40: Exploring Volume in Kubernetes🚀

🔹Table of Contents :

  • Introduction to Volumes in Kubernetes

  • Types of Volumes in Kubernetes

  • Hands-On Task: Using emptyDir Volume

  • Applying the YAML Configuration

  • Working with the Volume

  • Stopping and Restarting the Pod

  • Key Commands Used

  • Real-Time Scenarios for emptyDir Volumes

  • Key Learnings


✅What are Volumes in Kubernetes?

  • Volumes in Kubernetes provide persistent storage that can be shared among containers in a pod.

  • Unlike container storage (which is ephemeral), Kubernetes volumes last for the lifetime of the pod.

  • When containers restart, the data in the volume remains, making volumes essential for stateful applications.


✅Types of Volumes in Kubernetes:

  • emptyDir: A simple volume that is created when a pod starts. It is empty initially, and it gets deleted when the pod is removed. However, if a container within the pod restarts, the data is preserved.

  • hostPath: Mounts a directory from the node’s filesystem to the pod.

  • Persistent Volumes (PV) and Persistent Volume Claims (PVC): Used for persistent storage beyond the lifecycle of a pod.

For this session, I focused on emptyDir, which is used to temporarily store data for the lifetime of a pod.


1. emptyDir Volume

The emptyDir volume is created when a Pod is assigned to a Node and exists as long as the Pod is running. It is typically used for temporary storage, such as caches or scratch data that you don't mind losing after the Pod is deleted.

Real-Time Scenarios:

  • Data Processing: In data processing applications, where a Pod needs temporary storage for intermediate files, emptyDir can be used to store these files temporarily.

  • Caching: Applications that generate cache files while running can use emptyDir to store cache data, as the cache doesn’t need to persist after the Pod is deleted.

  • Session Data: In web applications, emptyDir can be used to store session data during the Pod's runtime, which can be lost without impacting the overall application.

Task 1: Creating a Pod with an emptyDir Volume

Here's how I created a Pod that uses an emptyDir volume:

Step-by-Step Instructions
  1. Create the pod1.yaml file:

     apiVersion: v1
     kind: Pod
     metadata:
       name: pod1
     spec:
       containers:
       - name: myfirstcontainer
         image: nginx:1.23
         volumeMounts:
         - mountPath: /data
           name: emptydirvolume
       volumes:
       - name: emptydirvolume
         emptyDir: {}
    
    • mountPath is where the volume will be mounted inside the container.

    • emptyDir creates an empty directory when the Pod starts.

  2. Apply the YAML file:

     kubectl apply -f pod1.yaml
    
  3. Access the Pod:

     kubectl exec -it pod1 -- /bin/bash
    
  4. Create files in the volume:

    Inside the container, I created a file and a series of numbered files:

     touch /data/data.txt
     touch /data/file{1..5}.txt
    
  5. Verify the files:

     ls /data
    
  6. Stop and Restart the Pod:

    I stopped the service, and upon restarting, the files were deleted, but the /data directory persisted because of the emptyDir configuration.

  7. Delete the Pod:

     kubectl delete pod pod1
    

2. hostPath Volume

The hostPath volume mounts a directory or file from the host Node’s filesystem into a Pod. This can be useful when we want to access specific files on the host machine or store persistent data locally on the Node itself.

Real-Time Scenarios:

  • Log Storage: hostPath is commonly used to store application logs on the host machine, so logs persist even after the Pod is deleted.

  • Persistent Data Storage: For applications requiring persistent storage on a local machine, like databases running in Kubernetes, hostPath can be used to map host storage to the container.

  • Shared Configuration Files: If you need to share configuration files between Pods, you can use hostPath to mount the same directory from the host to multiple Pods.

Task 2: Creating a Pod with a hostPath Volume

This time, I created a Pod that uses the hostPath volume, mapping a directory from the host machine to the container.

Step-by-Step Instructions
  1. Create the pod2.yaml file:

     apiVersion: v1
     kind: Pod
     metadata:
       name: pod2
     spec:
       containers:
       - name: myhostcontainer
         image: nginx:1.23
         volumeMounts:
         - mountPath: /usr/share/nginx/html
           name: hostpathvolume
       volumes:
       - name: hostpathvolume
         hostPath:
           path: /data/nginx
           type: DirectoryOrCreate
    
    • hostPath mounts the directory /data/nginx from the host into the container at /usr/share/nginx/html.
  2. Apply the YAML file:

     kubectl apply -f pod2.yaml
    
  3. Access the Pod:

     kubectl exec -it pod2 -- /bin/bash
    
  4. Create a file in the mounted directory:

    Inside the container, I created an HTML file that would reflect the contents of the host’s /data/nginx folder:

     echo "<h1>Hello from Kubernetes hostPath</h1>" > /usr/share/nginx/html/index.html
    
  5. Access the HTML file in the browser:

    To access this, I had to expose the pod using a NodePort service (optional but useful in this scenario).

     kubectl expose pod pod2 --type=NodePort --port=80
    

    Then, I accessed the service via http://<Node_IP>:<NodePort> to view the HTML page.


✅Key Differences: emptyDir vs hostPath

  • emptyDir: Temporary storage that lasts as long as the Pod exists but gets deleted once the Pod is gone.

  • hostPath: Mounts a specific directory from the Node into the Pod, allowing access to persistent data on the host machine.


✅Real-Time Scenarios

  • emptyDir: Ideal for use cases like temporary logs, cache files, or intermediate data processing that does not need to persist across Pod restarts.

  • hostPath: Commonly used when you need to store persistent logs, local database data, or configuration files that must remain accessible even after a Pod is terminated.


✅Commands Recap:

  • kubectl apply -f pod1.yaml

  • kubectl exec -it pod1 -- /bin/bash

  • kubectl delete pod pod1

  • kubectl apply -f pod2.yaml

  • kubectl expose pod pod2 --type=NodePort --port=80


✅Key Learnings:

  • emptyDir is useful for temporary storage in Kubernetes.

  • Data inside an emptyDir volume is preserved across container restarts but is lost if the pod is deleted.

  • The /data directory remains even if the pod is deleted because of the volume definition in the YAML file.


🚀Thanks for joining me on Day 40! Let’s keep learning and growing together!

Happy Learning! 😊

#90DaysOfDevOps

💡
Follow for more updates on LinkedIn , Github and Twitter(X)
10
Subscribe to my newsletter

Read articles from Kedar Pattanshetti directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Kedar Pattanshetti
Kedar Pattanshetti