Day-40 : Understanding Storage/Volumes in Kubernetes

Day-40 : Understanding Storage/Volumes in Kubernetes

In Kubernetes, managing storage is crucial for running stateful applications that require persistent data. Unlike stateless applications, which can be easily restarted without any impact, stateful applications need storage that persists beyond the lifecycle of a pod. This is where Kubernetes volumes come into play.

What is a Volume in Kubernetes?

A Volume in Kubernetes is a directory accessible to containers in a pod. Volumes help manage data persistence, ensuring that data remains even if the pod is destroyed or restarted. Volumes can be backed by different storage mediums, such as local storage, NFS, or cloud-based storage like AWS EBS, Google Cloud Persistent Disks, etc.

Types of Volumes in Kubernetes

Some of the commonly used volumes in Kubernetes include:

  • emptyDir: A temporary directory that is created for a pod and exists as long as the pod is running.

  • hostPath: Maps a file or directory from the host node’s filesystem into a pod.

  • persistentVolumeClaim (PVC): Used to request storage resources from a Persistent Volume (PV).

  • configMap: Provides a way to inject configuration data into pods.

Real-Life Example: Persistent Volume and Persistent Volume Claim

Imagine कि आप एक database Kubernetes cluster में चला रहे हैं। Database को storage की जरूरत है ताकि pod के समाप्त होने पर भी data सुरक्षित रहे। इस समस्या का समाधान करने के लिए हम Persistent Volumes (PV) और Persistent Volume Claims (PVC) का उपयोग कर सकते हैं।

Step 1: Create a Persistent Volume (PV)

A Persistent Volume represents a piece of storage in the cluster. Here’s an example of a PV manifest file:

apiVersion: v1
kind: PersistentVolume
metadata:
  name: my-pv
spec:
  capacity:
    storage: 10Gi
  accessModes:
    - ReadWriteOnce
  persistentVolumeReclaimPolicy: Retain
  hostPath:
    path: /mnt/data
  • storage: The size of the volume.

  • accessModes: Specifies how the volume can be accessed (e.g., ReadWriteOnce, ReadOnlyMany).

  • hostPath: The path on the host where the data will be stored.

Command to apply the PV manifest:

kubectl apply -f pv.yaml

Step 2: Create a Persistent Volume Claim (PVC)

A PVC is a request for storage by a user. It allows pods to use storage defined by a Persistent Volume.

Here’s an example of a PVC manifest file:

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: my-pvc
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 5Gi

Command to apply the PVC manifest:

kubectl apply -f pvc.yaml

Step 3: Use the PVC in a Pod

अब आप इस PVC का उपयोग अपने pod में data store करने के लिए कर सकते हैं।

Here’s an example of a pod manifest file that uses the PVC:

apiVersion: v1
kind: Pod
metadata:
  name: my-pod
spec:
  containers:
    - name: my-container
      image: nginx
      volumeMounts:
        - mountPath: "/usr/share/nginx/html"
          name: my-storage
  volumes:
    - name: my-storage
      persistentVolumeClaim:
        claimName: my-pvc
  • mountPath: The path where the volume will be mounted inside the container.

  • persistentVolumeClaim.claimName: The name of the PVC to be used.

Command to apply the pod manifest:

kubectl apply -f pod.yaml

Step 4: Verify the Storage Setup

यह सुनिश्चित करने के लिए कि PVC PV के साथ bound है और pod volume का उपयोग कर रहा है, आप निम्नलिखित commands चला सकते हैं:

kubectl get pv
kubectl get pvc
kubectl describe pod my-pod

These commands will show the status of your Persistent Volumes, Persistent Volume Claims, and the details of your pod.

Another Example: Using emptyDir Volume

मान लीजिए कि आप एक temporary storage space बनाना चाहते हैं जो केवल pod के चलने तक ही उपलब्ध हो। इसके लिए आप emptyDir volume type का उपयोग कर सकते हैं।

Here’s a manifest file for a pod using emptyDir:

apiVersion: v1
kind: Pod
metadata:
  name: emptydir-pod
spec:
  containers:
    - name: my-container
      image: busybox
      command: ["sleep", "3600"]
      volumeMounts:
        - mountPath: "/data"
          name: temp-storage
  volumes:
    - name: temp-storage
      emptyDir: {}

Command to apply the manifest:

kubectl apply -f emptydir-pod.yaml

This pod will have a temporary storage directory mounted at /data, which will be deleted as soon as the pod is terminated.

Conclusion

Kubernetes volumes stateful applications के लिए data management को आसान बनाते हैं। ये आपको pod के lifecycle से परे data को बनाए रखने की अनुमति देते हैं, जिससे pod के समाप्त होने की स्थिति में भी data उपलब्ध रहता है। चाहे आप databases के लिए persistent volumes का उपयोग कर रहे हों या transient data के लिए temporary storage की आवश्यकता हो, Kubernetes आपको एक flexible और powerful system प्रदान करता है।

Volumes को define, use और manage करने का तरीका समझना Kubernetes cluster में reliable और scalable applications को deploy करने के लिए महत्वपूर्ण है। इस blog में दिए गए manifest files और commands का उपयोग करके आप Kubernetes environment में storage management का hands-on अनुभव प्राप्त कर सकते हैं।

Connect and Follow Me on Socials

LINKDIN | GITHUB |TWITTER