Difficulty: Intermediate
Explain PersistentVolume (PV), PersistentVolumeClaim (PVC) and StorageClass. How does dynamic provisioning work?
A PersistentVolume (PV) is a piece of real storage in the cluster, such as an AWS EBS volume or an NFS share, represented as a cluster-wide API object. A PersistentVolumeClaim (PVC) is a namespaced request for storage ("I need 20Gi, ReadWriteOnce") that a Pod mounts. A StorageClass describes how to create PVs on demand, so when a PVC asks for that class, a CSI driver provisions a new volume and binds it automatically.
The design separates two roles. Cluster admins (or the cloud) deal with the physical storage; application developers just ask for capacity and an access mode without knowing whether it is EBS gp3, a GCE Persistent Disk or Ceph. The Pod references the PVC, never the PV, so the same manifest works across clusters with different backends.
storageClassName. The class's provisioner (for example ebs.csi.aws.com) creates a new volume, a PV object is created for it, and the PVC is bound. This is what almost every managed cluster uses today.If a PVC omits storageClassName, the cluster's default StorageClass is used (marked with the annotation storageclass.kubernetes.io/is-default-class: "true").
| Field | Values | What it means |
|---|---|---|
accessModes | ReadWriteOnce, ReadOnlyMany, ReadWriteMany, ReadWriteOncePod | RWO is one node; RWOP (stable since 1.29) is one Pod; RWX needs shared storage like NFS or EFS |
persistentVolumeReclaimPolicy | Delete, Retain | What happens to the disk when the PVC is deleted. Recycle is deprecated |
volumeBindingMode | Immediate, WaitForFirstConsumer | When to provision. WaitForFirstConsumer waits until a Pod is scheduled so the disk is created in the same zone |
allowVolumeExpansion | true/false | Whether you can grow a PVC by editing its size |
A PV moves through phases Available, Bound, Released and Failed.
Immediate binding, the disk might be created in ap-south-1a while the Pod gets scheduled in 1b, leaving it stuck. Use WaitForFirstConsumer.Delete. Deleting the namespace deletes the PVC and the disk. For a payments database, use a class with reclaimPolicy: Retain and take snapshots.volumeClaimTemplates to create one PVC per replica (data-postgres-0, data-postgres-1). Scaling down does not delete those PVCs by default.apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: gp3-retain
provisioner: ebs.csi.aws.com
parameters:
type: gp3
reclaimPolicy: Retain # keep the disk if the PVC is deleted
volumeBindingMode: WaitForFirstConsumer # create disk in the Pod's zone
allowVolumeExpansion: true
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: orders-db-data
spec:
storageClassName: gp3-retain
accessModes: [ReadWriteOnce]
resources:
requests:
storage: 20Gi
---
apiVersion: v1
kind: Pod
metadata:
name: orders-db
spec:
containers:
- name: postgres
image: postgres:16
volumeMounts:
- name: data
mountPath: /var/lib/postgresql/data
volumes:
- name: data
persistentVolumeClaim:
claimName: orders-db-data
The Pod only knows the claim name. The StorageClass decides the disk type, where and when it is created, and whether it survives PVC deletion.
kubectl get storageclass # (default) marks the default class
kubectl get pvc -n payments # STATUS should be Bound
kubectl get pv # cluster-scoped, shows RECLAIM POLICY
kubectl describe pvc orders-db-data -n payments # events explain Pending claims
A PVC stuck in Pending usually means no matching PV, no default class, or WaitForFirstConsumer waiting for a Pod.
PersistentVolume, PersistentVolumeClaim, StorageClass, Dynamic Provisioning, Access Modes, Reclaim Policy