Persistent Volumes β Storage That Survives
A PersistentVolume (PV) is a piece of storage in the cluster that has been provisioned by an administrator or dynamically provisioned using StorageClasses. It exists independently of any Pod.
A PersistentVolumeClaim (PVC) is a request for storage by a user. Pods use PVCs to claim PVs β like filling out a storage request form.
PV and PVC Workflow
Admin provisions storage User creates a PVC
βββββββββββββββββββββββ βββββββββββββββββββββββ
β PersistentVolume β β PersistentVolume β
β (PV) βββββββββββΆβ Claim (PVC) β
β 10Gi SSD, RWO β binds β "I need 5Gi" β
βββββββββββββββββββββββ ββββββββββββ¬βββββββββββ
β
Pod uses the PVC
βββββββββββ΄ββββββββββ
β volumeMounts: β
β name: data β
β mountPath: /dataβ
βββββββββββββββββββββ
Creating a PersistentVolume
apiVersion: v1
kind: PersistentVolume
metadata:
name: my-pv
spec:
capacity:
storage: 10Gi
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Retain
storageClassName: standard
hostPath:
path: /mnt/data
accessModes β ReadWriteOnce (one node), ReadOnlyMany (many nodes), ReadWriteMany (many nodes, not all storage supports this).
reclaimPolicy β Retain (keep data when PVC is deleted) or Delete (delete the storage).
Creating a PersistentVolumeClaim
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: my-pvc
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 5Gi
storageClassName: standard
Kubernetes matches this PVC to a PV with sufficient capacity and compatible access modes.
Using a PVC in a Pod
apiVersion: v1
kind: Pod
metadata:
name: my-app
spec:
containers:
- name: app
image: my-app
volumeMounts:
- name: storage
mountPath: /var/data
volumes:
- name: storage
persistentVolumeClaim:
claimName: my-pvc
The data in /var/data persists even if the Pod is deleted. A new Pod can claim the same PVC and access the data.
Dynamic Provisioning
# With a StorageClass, PVs are created on demand
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: fast
provisioner: kubernetes.io/aws-ebs
parameters:
type: gp3
reclaimPolicy: Delete
volumeBindingMode: WaitForFirstConsumer
Dynamic provisioning eliminates the need for admins to manually create PVs. When a PVC references a StorageClass, the storage is automatically provisioned.