Storage Classes & Dynamic Volume Provisioning in Kubernetes

Storage Classes & Dynamic Volume Provisioning in Kubernetes

Kubernetes provides Persistent Volumes (PVs) and Persistent Volume Claims (PVCs) to manage persistent storage, but manual provisioning of storage is inefficient—especially in cloud environments where storage needs to scale dynamically.

?? How do you ensure storage is created only when needed and scaled automatically?

?? How do you allow different types of storage for different applications in Kubernetes?

This is where Storage Classes & Dynamic Volume Provisioning come in! ??


?? What is a Storage Class in Kubernetes?

A StorageClass defines how storage should be dynamically provisioned in Kubernetes. Instead of manually creating Persistent Volumes (PVs), Kubernetes automatically provisions them when a Persistent Volume Claim (PVC) requests storage.

?? Think of a StorageClass as a template that describes the type of storage you want to use.

Why Use Storage Classes?

? Automates storage provisioning (no need to manually create PVs).

? Supports different storage backends (EBS, Azure Disk, NFS, CephFS, etc.).

? Enables performance tuning by defining parameters like IOPS, volume size, etc.

? Allows multiple storage profiles (fast SSD, slow HDD, high-availability storage).


?? How Dynamic Volume Provisioning Works

1?? A PVC requests storage and specifies a StorageClass.

2?? Kubernetes automatically provisions a Persistent Volume (PV) based on the StorageClass parameters.

3?? The PVC binds to the dynamically created PV, and the pod can now use it.

4?? When the PVC is deleted, the storage is either retained, deleted, or recycled based on the reclaim policy.

?? This eliminates the need for static PV creation and optimizes storage usage.


?? Types of Storage Classes in Kubernetes

Storage Type Best For Access Modes AWS EBS (gp2, gp3, io1) Cloud-based block storage RWO (Single-node access)

Azure Disk (Standard, Premium, Ultra) High-performance cloud storage RWO

GCE Persistent Disk Google Cloud block storage RWO

NFS (Network File System) Shared storage for multiple pods RWX (Multi-node access)

CephFS / GlusterFS Distributed file storage RWX

?? Cloud block storage (AWS EBS, Azure Disk) supports RWO, while NFS/CephFS allows RWX (shared storage).


?? Scenario 1: Dynamic Volume Provisioning on AWS EBS

?? Overview

We will deploy a StorageClass for AWS EBS, create a PVC that requests storage dynamically, and mount it in a pod.

?? Use Case:

? Dynamically provision cloud storage (AWS EBS) when needed.

? Eliminate manual PV creation and automate storage allocation.


Step 1: Define a StorageClass for AWS EBS

Create a file named aws-storageclass.yaml:

apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: aws-ebs
provisioner: kubernetes.io/aws-ebs
parameters:
  type: gp2  # Can be gp3, io1 (for high-performance)
  fsType: ext4
reclaimPolicy: Delete
allowVolumeExpansion: true        

Apply the StorageClass:

kubectl apply -f aws-storageclass.yaml        

? Now, AWS EBS volumes can be dynamically created when requested.


Step 2: Create a PVC That Uses the StorageClass

Create a file named aws-pvc.yaml:

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: aws-dynamic-pvc
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 5Gi
  storageClassName: aws-ebs        

Apply it:

kubectl apply -f aws-pvc.yaml        

Check PVC status:

kubectl get pvc        

? AWS EBS automatically provisions a PV and binds it to this PVC!


Step 3: Mount the PVC in a Pod

Now, let’s create a pod that uses this dynamically provisioned volume.

Create a file named aws-pvc-pod.yaml:

apiVersion: v1
kind: Pod
metadata:
  name: ebs-test-pod
spec:
  containers:
  - name: busybox
    image: busybox
    command: [ "sleep", "3600" ]
    volumeMounts:
    - mountPath: "/data"
      name: storage
  volumes:
  - name: storage
    persistentVolumeClaim:
      claimName: aws-dynamic-pvc        

Apply it:

kubectl apply -f aws-pvc-pod.yaml        

? The pod now has dynamically provisioned storage from AWS EBS!


?? Scenario 2: Resizing a Dynamically Provisioned PVC

By default, Kubernetes does not allow PVCs to be resized unless explicitly enabled in the StorageClass.

?? Use Case:

? Applications running out of storage need volume expansion.


Step 1: Enable Volume Expansion in StorageClass

Modify aws-storageclass.yaml:

allowVolumeExpansion: true        

Apply the changes:

kubectl apply -f aws-storageclass.yaml        

Step 2: Resize the PVC

Edit the PVC and increase the storage size:

kubectl edit pvc aws-dynamic-pvc        

Modify:

resources:
  requests:
    storage: 10Gi        

Check if the PVC was resized:

kubectl get pvc        

? Success! The volume has been expanded dynamically.


?? Scenario 3: Multi-Pod Shared Storage Using NFS (RWX)

?? Overview

Some applications (e.g., web servers, shared logs) require multiple pods to access the same storage. Since AWS EBS and Azure Disk do not support RWX, we will use NFS as a shared storage solution.

?? Use Case:

? Multiple pods accessing shared storage (e.g., WordPress uploads, logs, shared caches).


Step 1: Create an NFS StorageClass

apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: nfs-storage
provisioner: nfs-provisioner
parameters:
  path: "/exports"
  server: "<NFS_SERVER_IP>"
reclaimPolicy: Retain
allowVolumeExpansion: true        

Apply it:

kubectl apply -f nfs-storage.yaml        

Step 2: Create a PVC That Uses RWX Access Mode

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: shared-storage
spec:
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 5Gi
  storageClassName: nfs-storage        

Apply it:

kubectl apply -f shared-storage.yaml        

? Multiple pods can now share the same volume!


?? Key Takeaways

  • Storage Classes enable dynamic provisioning of storage in Kubernetes.
  • Cloud storage solutions (AWS EBS, Azure Disk) are provisioned automatically when requested via PVCs.
  • RWX volumes (NFS, CephFS) allow multiple pods to share the same storage.
  • PVCs can be resized dynamically when allowVolumeExpansion: true is enabled.


?? Let’s Discuss!

Are you using dynamic storage provisioning in Kubernetes? Have you implemented RWX shared storage for multiple pods? Let’s discuss in the comments!

Follow Bavithran M for more DevOps, Kubernetes, and cloud-native insights.

Found this useful? Share it with your network!

RISWAN RAJA A

?? Cloud DevOps | ?? Azure | ??? Terraform | ?? Docker | ?? Kubernetes | ?? Infrastructure Automation Enthusiast | ?? Driving Scalability & Innovation

2 周

Insightful

Bavithran M

Senior Cloud & DevOps Engineer | AWS & Azure Certified | Kubernetes & Automation Advocate | Training | Mentoring | Uplifting Many IT Professionals

2 周

#connections

赞
回复

要查看或添加评论,请登录

Bavithran M的更多文章

社区洞察