8. Kubernetes Storage Deep Dive: PV, PVC, and StorageClasses

8. Kubernetes Storage Deep Dive: PV, PVC, and StorageClasses

ยท

3 min read

Storage is an integral part of any application deployment, and Kubernetes provides powerful abstractions to manage storage efficiently. In this post, weโ€™ll explore Persistent Volumes (PV), Persistent Volume Claims (PVC), and StorageClasses with a step-by-step example.


๐Ÿ—‚๏ธ Understanding Kubernetes Storage

What is Kubernetes Storage?

Kubernetes separates storage from compute by introducing resources like Persistent Volumes and Persistent Volume Claims, ensuring dynamic and scalable storage provisioning for your workloads.

  • Persistent Volume (PV): A cluster-wide resource that provides storage abstraction.

  • Persistent Volume Claim (PVC): A request for storage by users that binds to a PV.

  • StorageClass: Defines the type of storage (e.g., SSD, HDD) and dynamic provisioning rules.


๐Ÿ“ฆ Persistent Volume (PV)

A Persistent Volume (PV) is a piece of storage provisioned in the cluster. It can be backed by storage types like NFS, AWS EBS, GCP PD, or local storage.

Example: Creating a Persistent Volume

Hereโ€™s a YAML file to create a PV:

apiVersion: v1
kind: PersistentVolume
metadata:
  name: local-pv
  labels:
    app: local
spec:
  storageClassName: local-storage
  capacity:
    storage: 1Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: "/mnt/data"
  persistentVolumeReclaimPolicy: Retain

Apply the Persistent Volume:

kubectl apply -f PersistentVolume.yml -n nginx

Verify Persistent Volume:

kubectl get pv -n nginx


๐Ÿ“„ Persistent Volume Claim (PVC)

A Persistent Volume Claim (PVC) is a request for storage. It specifies storage size and access mode requirements.

Example: Creating a Persistent Volume Claim

Hereโ€™s the YAML for a PVC:

apiVersion: v1  
kind: PersistentVolumeClaim  
metadata:  
  name: local-pvc
spec:  
  storageClassName: local-storage 
  accessModes:  
    - ReadWriteOnce  
  resources:  
    requests:  
      storage: 1Gi

Apply the Persistent Volume Claim:

kubectl apply -f PersistentVolumeClaim.yml -n nginx

Check Persistent Volume(It should Bound):

kubectl get pv -n nginx


๐Ÿš€ Using Storage in a Deployment

After creating PV and PVC, letโ€™s attach the storage to a pod.

Example: Nginx Deployment with Persistent Storage

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  namespace: nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx-app
  template:
    metadata:
      labels:
        app: nginx-app
    spec:
      containers:
        - name: nginx-container
          image: nginx:latest
          volumeMounts:
            - mountPath: "/var/www/html"
              name: my-volume
      volumes:
        - name: my-volume
          persistentVolumeClaim:
            claimName: local-pvc

Apply the Deployment manifest:

kubectl apply -f deployment.yml -n nginx


๐Ÿ› ๏ธ Validating Persistent Storage

  1. Verify Pod Deployment:

     kubectl get pods -n nginx -o wide
    

  2. Exec into the docker container(ie.node):

     docker exec -it container-id bash
    
  3. Check Persistent Data:
    Navigate to the mounted volume:

     ls 
     cd mnt/data
    

Exit the container

    exit

๐ŸŒŸ Conclusion

With Persistent Volumes, Persistent Volume Claims, and StorageClasses, Kubernetes ensures seamless storage integration for both stateful and stateless applications. The flexibility of dynamically provisioning storage using StorageClasses makes Kubernetes storage highly scalable and efficient.

Now that you've mastered storage basics, letโ€™s deploy your next stateful application with confidence. ๐ŸŽ‰

Do you have questions or specific scenarios to discuss? Let me know in the comments below! ๐Ÿš€

ย