PersistentVolumes & StorageClasses
Persistent storage in Kubernetes is split into three objects so that developers don't need to know which disk technology the cluster uses:
| Object | Scope | Who creates it | Meaning |
|---|---|---|---|
| PersistentVolumeClaim (PVC) | Namespace | Developer | "I need 10 GiB of fast, read-write-once storage" |
| PersistentVolume (PV) | Cluster | Admin, or automatically | A real piece of storage (cloud disk, NFS share, local SSD) |
| StorageClass | Cluster | Admin | A recipe for creating PVs on demand (type, replication, reclaim policy) |
The PVC is a request, the PV is the supply, and the binding connects exactly one claim to one volume.
Dynamic provisioning
Most clusters have a default StorageClass. You only write the claim; a provisioner (a CSI driver) creates the disk and the PV for you:
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: fast
provisioner: ebs.csi.aws.com
parameters:
type: gp3
reclaimPolicy: Delete
volumeBindingMode: WaitForFirstConsumer # create the disk in the zone where the Pod lands
allowVolumeExpansion: true
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: uploads
spec:
storageClassName: fast
accessModes: [ReadWriteOnce]
resources:
requests:
storage: 10Gi# in the Pod spec
volumes:
- name: uploads
persistentVolumeClaim:
claimName: uploadskubectl get storageclass
kubectl get pvc,pvSample output:
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS
persistentvolumeclaim/uploads Bound pvc-3f2b7c9e-6a1d-4c58-9e0b-2d7a41f0c6b3 10Gi RWO fastAccess modes
| Mode | Short | Meaning |
|---|---|---|
ReadWriteOnce |
RWO | Mounted read-write by one node (several Pods on that node may share it) |
ReadWriteOncePod |
RWOP | Read-write by exactly one Pod |
ReadOnlyMany |
ROX | Read-only by many nodes |
ReadWriteMany |
RWX | Read-write by many nodes (NFS, CephFS, EFS, Azure Files; not block disks) |
How binding works
For static PVs (created by an admin), the controller finds the smallest available PV that satisfies the claim's class, access modes and size. A PVC never gets a smaller volume, but it may get a bigger one. This model implements that matching:
GI = 1024 ** 3
def size(text):
units = {"Mi": 1024 ** 2, "Gi": GI, "Ti": 1024 * GI}
return int(text[:-2]) * units[text[-2:]]
pvs = [
{"name": "pv-nfs-100g", "capacity": "100Gi", "modes": {"ReadWriteMany", "ReadOnlyMany"}, "class": "nfs", "claim": None},
{"name": "pv-ssd-20g", "capacity": "20Gi", "modes": {"ReadWriteOnce"}, "class": "local-ssd", "claim": None},
{"name": "pv-ssd-50g", "capacity": "50Gi", "modes": {"ReadWriteOnce"}, "class": "local-ssd", "claim": None},
{"name": "pv-ssd-5g", "capacity": "5Gi", "modes": {"ReadWriteOnce"}, "class": "local-ssd", "claim": None},
]
def bind(claim):
fits = [pv for pv in pvs
if pv["claim"] is None
and pv["class"] == claim["class"]
and set(claim["modes"]) <= pv["modes"]
and size(pv["capacity"]) >= size(claim["request"])]
if not fits:
return f"{claim['name']}: Pending (no matching volume)"
best = min(fits, key=lambda pv: size(pv["capacity"]))
best["claim"] = claim["name"]
return f"{claim['name']}: Bound to {best['name']} ({best['capacity']})"
claims = [
{"name": "db-data", "class": "local-ssd", "modes": ["ReadWriteOnce"], "request": "10Gi"},
{"name": "logs", "class": "local-ssd", "modes": ["ReadWriteOnce"], "request": "10Gi"},
{"name": "shared", "class": "nfs", "modes": ["ReadWriteMany"], "request": "50Gi"},
{"name": "big", "class": "local-ssd", "modes": ["ReadWriteOnce"], "request": "1Ti"},
]
for c in claims:
print(bind(c))db-data: Bound to pv-ssd-20g (20Gi)
logs: Bound to pv-ssd-50g (50Gi)
shared: Bound to pv-nfs-100g (100Gi)
big: Pending (no matching volume)
db-data asked for 10 GiB and got the 20 GiB volume (the smallest that fits); the next 10 GiB claim got the 50 GiB one. With dynamic provisioning you get exactly the size you asked for instead.
Reclaim policies and the PV lifecycle
What happens to the disk when the PVC is deleted is set by persistentVolumeReclaimPolicy (copied from the StorageClass's reclaimPolicy):
| Policy | Effect |
|---|---|
Delete (default for dynamic) |
The PV and the underlying disk are deleted |
Retain |
The PV becomes Released; data stays until an admin cleans up or reuses it manually |
def lifecycle(policy):
states = ["Available", "Bound (claim created)"]
if policy == "Delete":
states += ["claim deleted -> volume and disk deleted"]
else:
states += ["claim deleted -> Released (data kept)",
"admin removes claimRef -> Available again"]
return states
for policy in ["Delete", "Retain"]:
print(f"{policy}: " + " -> ".join(lifecycle(policy)))Delete: Available -> Bound (claim created) -> claim deleted -> volume and disk deleted
Retain: Available -> Bound (claim created) -> claim deleted -> Released (data kept) -> admin removes claimRef -> Available again
Warning: With the default
Deletepolicy,kubectl delete pvc(or deleting a namespace) destroys the data. For important databases use a StorageClass withreclaimPolicy: Retain, and take backups or VolumeSnapshots regardless.
Expanding a volume
If the StorageClass has allowVolumeExpansion: true, edit the PVC's spec.resources.requests.storage to a bigger value. Most CSI drivers grow the filesystem online. Shrinking is not supported.
kubectl patch pvc uploads -p '{"spec":{"resources":{"requests":{"storage":"20Gi"}}}}'Test yourself
Which object does a developer usually create to get persistent storage?
- PersistentVolume
- PersistentVolumeClaim
- StorageClass
- CSIDriver
A claim requests 10Gi. Available PVs: 5Gi, 20Gi, 50Gi (all matching otherwise). Which is bound?
- 5Gi
- 20Gi
- 50Gi
- None
Which access mode lets Pods on many nodes write to the same volume?
- ReadWriteOnce
- ReadOnlyMany
- ReadWriteMany
- ReadWriteOncePod