In Kubernetes, managing persistent storage is crucial for running stateful applications. To effectively handle this, Kubernetes provides three key resources: Persistent Volumes (PVs), Persistent Volume Claims (PVCs), and StorageClasses. Let's clarify their roles clearly.
1. Persistent Volume (PV)
A Persistent Volume (PV) represents a storage resource within your Kubernetes cluster. This resource exists independently from the lifecycle of pods, meaning your data remains safe even if pods are deleted or recreated.
Key characteristics:
- Created and managed by cluster administrators.
- Can be provisioned statically (manually) or dynamically (automatically).
- Ensures data persistence beyond the lifecycle of a pod.
Example of a PV (static provisioning):
apiVersion: v1
kind: PersistentVolume
metadata:
name: example-pv
spec:
capacity:
storage: 20Gi
accessModes:
- ReadWriteOnce
hostPath:
path: "/mnt/data"
2. Persistent Volume Claim (PVC)
A Persistent Volume Claim (PVC) is how users request storage from Kubernetes. Think of it as a ticket you give to Kubernetes asking for storage with certain specifications (size, access mode, storage class).
Key characteristics:
- Created by users or developers.
- Acts as the bridge connecting pods to persistent volumes.
- Kubernetes automatically finds and binds a suitable PV to fulfill the PVC.
Example of a PVC:
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: example-pvc
spec:
storageClassName: standard
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 10Gi
3. StorageClass
StorageClasses simplify dynamic provisioning of persistent volumes. They define how Kubernetes should automatically create volumes when requested by a PVC, specifying the type of storage, performance, and management policies.
Key characteristics:
- Created by the cluster administrator.
- Automates the provisioning process, avoiding manual PV creation.
- Provides flexibility in storage type and retention policies.
Example of a StorageClass:
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: standard
provisioner: kubernetes.io/aws-ebs
parameters:
type: gp2
reclaimPolicy: Retain
How These Work Together:
- StorageClass defines automatic provisioning rules.
- PVC requests specific storage from Kubernetes based on those rules.
- Kubernetes dynamically provisions a PV that matches the PVC requirements.
- Pods can then reliably use this persistent storage via the PVC, ensuring their data remains intact and accessible throughout the pod lifecycle.
Understanding these concepts ensures you can confidently deploy and maintain stateful applications in Kubernetes, keeping your data secure and consistent.