Home » Backend Dev » kubernetes » 30 Days kubernetes » Day 9: Kubernetes – Persistent Volumes and Storage Classes

Day 9: Kubernetes – Persistent Volumes and Storage Classes

Introduction to Persistent Volumes and Storage Classes

In Kubernetes, managing data persistence is essential for stateful applications. Persistent Volumes (PVs) and Storage Classes provide a framework to decouple storage provisioning from application management. This ensures scalability, flexibility, and consistency in how storage is allocated and consumed.

This guide dives into the concepts of Persistent Volumes, Persistent Volume Claims (PVCs), and Storage Classes, with examples to help you get started.


What Are Persistent Volumes (PVs)?

A Persistent Volume (PV) is a piece of storage in the cluster provisioned by an administrator or dynamically by a Storage Class. It abstracts the underlying storage implementation, offering a consistent way for applications to consume storage.

Key Features of PVs:

  1. Decoupled Storage Management: Separate storage from pod lifecycle.
  2. Dynamic or Static Provisioning: Support for both manual and automated provisioning.
  3. Shared or Exclusive Access: Define access modes for storage sharing.
  4. Reclaim Policies: Control what happens to the volume when a PVC is deleted.

Persistent Volume Claims (PVCs)

A Persistent Volume Claim (PVC) is a request for storage by a user. It specifies the desired size, access mode, and optionally, the Storage Class.

Access Modes in PVCs:

  • ReadWriteOnce (RWO): Mounted by a single node in read/write mode.
  • ReadOnlyMany (ROX): Mounted by multiple nodes in read-only mode.
  • ReadWriteMany (RWX): Mounted by multiple nodes in read/write mode.

Example: Creating a PVC

  1. Create a pvc-example.yaml file:
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: example-pvc
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 10Gi
  1. Apply the PVC:
kubectl apply -f pvc-example.yaml
  1. Check PVC status:
kubectl get pvc

Storage Classes

A Storage Class defines the “class” of storage available in a cluster. It provides a way to describe the types of storage (e.g., SSD, HDD) and allows dynamic provisioning of Persistent Volumes.

Key Features of Storage Classes:

  1. Dynamic Provisioning: Automatically creates PVs for PVCs.
  2. Custom Parameters: Define storage backend-specific parameters.
  3. Reclaim Policies: Configure actions for released PVs (e.g., Retain, Delete, Recycle).

Example: Creating a Storage Class

  1. Create a storageclass-example.yaml file:
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: example-storage-class
provisioner: kubernetes.io/aws-ebs # Change based on your environment
parameters:
  type: gp2
  encrypted: "true"
reclaimPolicy: Delete
mountOptions:
  - debug
  1. Apply the Storage Class:
kubectl apply -f storageclass-example.yaml
  1. Verify the Storage Class:
kubectl get storageclass

Binding PVCs to PVs

PVCs are bound to PVs either manually (static provisioning) or automatically (dynamic provisioning).

Dynamic Provisioning Example

  1. Create a PVC referencing the Storage Class:
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: example-dynamic-pvc
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 20Gi
  storageClassName: example-storage-class
  1. Apply the PVC:
kubectl apply -f example-dynamic-pvc.yaml
  1. Verify the created PV:
kubectl get pv

Using Persistent Volumes in Pods

Volumes are mounted into pods via PVCs.

Example: Mounting a PV in a Pod

  1. Create a pod definition file pod-with-pvc.yaml:
apiVersion: v1
kind: Pod
metadata:
  name: pod-with-pvc
spec:
  containers:
  - name: app-container
    image: nginx
    volumeMounts:
    - mountPath: "/data"
      name: pvc-storage
  volumes:
  - name: pvc-storage
    persistentVolumeClaim:
      claimName: example-pvc
  1. Apply the Pod:
kubectl apply -f pod-with-pvc.yaml
  1. Verify the volume mount:
kubectl exec pod-with-pvc -- df -h

Best Practices for PVs and Storage Classes

  1. Choose Appropriate Access Modes: Ensure the correct access mode is selected for your application needs.
  2. Enable Volume Encryption: Use encrypted volumes for sensitive data.
  3. Set Resource Quotas: Prevent resource overconsumption by setting quotas.
  4. Monitor Usage: Regularly monitor volume usage to avoid over-provisioning.
  5. Test Reclaim Policies: Ensure reclaim policies align with your operational needs.

Conclusion

Persistent Volumes and Storage Classes are foundational for managing data persistence in Kubernetes. By leveraging these tools effectively, you can ensure robust, scalable, and secure storage management for your stateful applications.


References

*** Your support will help me continue to bring new Content. Love Coding *** ❤️


Feedback and Discussion

Have questions or feedback? Comment below! Let’s build a collaborative learning environment. Check out more articles on Node.js, Express.js, and System Design.

Leave a Comment

Your email address will not be published. Required fields are marked *