Sub topic 5
Refresher: PersistentVolumes and Claims
This chapter assumes you know Pods, Deployments, and Services, and that you can apply YAML with kubectl. It reviews the storage objects that StatefulSets build on. The next chapter shows how a StatefulSet creates one claim per Pod.
Why Pods need separate storage objects
A container's filesystem disappears when the container restarts. A volume of type emptyDir lasts as long as the Pod, but it is lost when the Pod is deleted. A database needs data that outlives the Pod.
Kubernetes splits this problem into two roles:
- A PersistentVolume (PV) is a piece of storage in the cluster, such as a cloud disk or an NFS export. It is a cluster-level object and does not belong to a namespace.
- A PersistentVolumeClaim (PVC) is a request for storage by a user. It lives in a namespace and states a size, an access mode, and optionally a storage class.
A Pod never references a PV directly. It references a PVC, and Kubernetes binds the PVC to a matching PV. This keeps the Pod manifest independent of the storage vendor.
How a claim becomes a volume
Most clusters use dynamic provisioning. You do not create PVs by hand. A StorageClass describes how to create them, for example which disk type to use. When a PVC names a StorageClass, a provisioner creates a PV that fits the request and binds the two together.
Here is a standalone claim and a Pod that uses it:
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: data
spec:
accessModes: ["ReadWriteOnce"]
resources:
requests:
storage: 1Gi
---
apiVersion: v1
kind: Pod
metadata:
name: writer
spec:
containers:
- name: shell
image: busybox:1.36
command: ["sh", "-c", "date >> /data/log.txt && sleep 3600"]
volumeMounts:
- name: data
mountPath: /data
volumes:
- name: data
persistentVolumeClaim:
claimName: dataBecause the claim names no StorageClass, the cluster's default StorageClass is used, if one exists. Check the result:
kubectl get pvc data
kubectl get pv
kubectl get storageclassA healthy claim shows the status Bound. A claim stuck in Pending usually means that no default StorageClass exists, that no PV matches the request, or that the provisioner failed.
Access modes
The access mode says how many nodes may mount the volume at once.
| Mode | Meaning |
|---|---|
| `ReadWriteOnce` | Read and write by a single node. Several Pods on that node may share it. |
| `ReadOnlyMany` | Read-only by many nodes. |
| `ReadWriteMany` | Read and write by many nodes. Needs a storage backend that supports it, such as NFS. |
| `ReadWriteOncePod` | Read and write by a single Pod. |
Block disks from cloud providers typically support only ReadWriteOnce. This fits StatefulSets well, because each Pod gets its own disk and does not share it.
Reclaim policy and the volume's lifetime
Deleting a Pod does not delete its claim. Deleting a claim may or may not delete the data. That depends on the PV's reclaim policy:
Delete: the PV and its backing disk are removed when the claim is deleted. This is the usual default for dynamically provisioned volumes.Retain: the PV and the data stay after the claim is deleted. An administrator must clean up or reuse the volume manually.
You can check the policy with kubectl get pv, which has a RECLAIM POLICY column.
Common errors
- PVC stays
Pending. Runkubectl describe pvc NAMEand read the events. Typical causes are a missing StorageClass or a provisioner that lacks permissions. - Pod stays
Pendingwith a volume node-affinity conflict. A zonal disk was created in one zone, and the Pod was scheduled into another. A StorageClass withvolumeBindingMode: WaitForFirstConsumeravoids this by delaying provisioning until the Pod is placed. - A volume does not mount on a second node. A
ReadWriteOncevolume is still attached to the old node. This is relevant to node failures, covered later in this tutorial. - A claim cannot be resized. Expansion works only if the StorageClass sets
allowVolumeExpansion: true.
Exercise
You delete a Pod that mounts a PVC, and the Deployment or controller creates a replacement. Is the data still there? What changes if you delete the PVC itself and the PV has reclaim policy Delete?
Show answer
Yes, the data is still there. Deleting a Pod does not delete the claim, so the replacement Pod can mount the same PVC. If you delete the PVC and the PV's reclaim policy is Delete, the PV and its backing disk are removed, and the data is lost.
Exercise
Why does a Pod reference a PVC and not a PV directly?
Show answer
The PVC separates the request for storage from the storage implementation. The Pod manifest stays the same across clusters and storage vendors. The PVC is also namespaced, so each team manages its own claims, while PVs are cluster-wide.