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
Verify Pod Deployment:
kubectl get pods -n nginx -o wide
Exec into the docker container(ie.node):
docker exec -it container-id bash
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! ๐