Introduction
Storage management in Kubernetes can be challenging, especially when handling stateful applications. Manually provisioning PersistentVolumes (PVs) adds operational overhead and limits scalability. Dynamic Volume Provisioning allows Kubernetes to create storage resources on demand, automating storage allocation for workloads.
In this post, we’ll set up dynamic provisioning using Local Path Provisioner, enabling seamless storage for Kubernetes applications.
Step 1: Deploy Local Path Provisioner
To enable dynamic provisioning, we need a StorageClass that defines how PVs are created. The Local Path Provisioner is a simple and efficient solution for clusters without cloud storage.
Apply Local Path Provisioner (For Minikube or Local Clusters)
kubectl apply -f https://raw.githubusercontent.com/rancher/local-path-provisioner/master/deploy/local-path-storage.yaml
Verify the provisioner is running:
kubectl get pods -n local-path-storage
Expected output:
NAME READY STATUS RESTARTS AGE
local-path-provisioner-xx 1/1 Running 0 1m
Step 2: Create a StorageClass
We need a StorageClass to define the storage provisioning behavior.
storage-class.yaml
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: local-path
provisioner: rancher.io/local-path
volumeBindingMode: WaitForFirstConsumer
reclaimPolicy: Delete
Apply the StorageClass:
kubectl apply -f storage-class.yaml
Verify:
kubectl get storageclass
Expected output:
NAME PROVISIONER RECLAIMPOLICY VOLUMEBINDINGMODE
local-path rancher.io/local-path Delete WaitForFirstConsumer
Step 3: Create a PersistentVolumeClaim (PVC)
A PersistentVolumeClaim (PVC) requests storage dynamically from the StorageClass.
pvc.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: my-dynamic-pvc
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
storageClassName: local-path
Apply the PVC:
kubectl apply -f pvc.yaml
Check the PVC status:
kubectl get pvc
Expected output:
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS
my-dynamic-pvc Bound pvc-xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx 1Gi RWO local-path
Step 4: Deploy a Pod Using the Dynamic Volume
Now, we’ll create a Pod that mounts the dynamically provisioned volume.
pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: storage-test
spec:
containers:
- name: test-container
image: busybox
command: [ "sleep", "3600" ]
volumeMounts:
- mountPath: "/data"
name: test-volume
volumes:
- name: test-volume
persistentVolumeClaim:
claimName: my-dynamic-pvc
Apply the pod:
kubectl apply -f pod.yaml
Verify the pod:
kubectl get pods
Expected output:
NAME READY STATUS RESTARTS AGE
storage-test 1/1 Running 0 1m
Step 5: Verify Data Persistence
Enter the pod and create a test file inside the volume:
kubectl exec -it storage-test -- sh
Inside the container:
echo "Hello, Kubernetes!" > /data/testfile.txt
exit
Now, delete the pod:
kubectl delete pod storage-test
Recreate the pod:
kubectl apply -f pod.yaml
Enter the pod again and check if the file persists:
kubectl exec -it storage-test -- cat /data/testfile.txt
Expected output:
Hello, Kubernetes!
This confirms that the PersistentVolume (PV) remains intact even after pod deletion.
Conclusion
Dynamic Volume Provisioning in Kubernetes eliminates manual storage management by allowing on-demand PV creation. This ensures:
Scalability – Storage is provisioned dynamically as needed.
Automation – No manual intervention required.
Persistence – Data is retained across pod restarts.
By implementing Local Path Provisioner and StorageClasses, Kubernetes clusters can achieve seamless, automated storage provisioning, optimizing performance and resource utilization.
Adopt Dynamic Volume Provisioning today and simplify storage management in your Kubernetes environment!
How do you currently handle persistent storage in Kubernetes? Have you faced challenges with dynamic storage provisioning? Drop your thoughts in the comments! 