Sub topic 6
Per-Pod storage with volumeClaimTemplates
This chapter assumes you know Pods, Services, and applying YAML with kubectl. It also assumes you have seen PersistentVolumes (PVs) and PersistentVolumeClaims (PVCs). A PVC is a request for storage, and a PV is the storage that satisfies it.
Why a shared claim is not enough
A Deployment can mount one PVC into its Pods. With ReadWriteOnce access, only one node can mount that volume at a time. All replicas would also share the same files, which breaks databases and other systems where each replica owns its own data.
A StatefulSet solves this with volumeClaimTemplates. The controller uses the template to create one PVC for each Pod. Each Pod gets its own volume, and the volume stays tied to that Pod's identity.
The template in the manifest
This is the full example from the earlier chapters. The part to focus on is volumeClaimTemplates.
apiVersion: v1
kind: Service
metadata:
name: web
labels:
app: web
spec:
clusterIP: None
selector:
app: web
ports:
- name: web
port: 80
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: web
spec:
serviceName: web
replicas: 3
selector:
matchLabels:
app: web
template:
metadata:
labels:
app: web
spec:
containers:
- name: nginx
image: nginx:1.25
ports:
- name: web
containerPort: 80
volumeMounts:
- name: www
mountPath: /usr/share/nginx/html
volumeClaimTemplates:
- metadata:
name: www
spec:
accessModes: ["ReadWriteOnce"]
resources:
requests:
storage: 1GiThe template name www must match the name in volumeMounts. You do not declare a volumes entry for it. The controller adds that entry to each Pod.
How claims are named and bound
For each Pod, the controller creates a PVC named <template name>-<StatefulSet name>-<ordinal>. In this example, the claims are www-web-0, www-web-1, and www-web-2.
kubectl apply -f web.yaml
kubectl get pods -l app=web -w
kubectl get pvc -l app=webEach claim is bound to its own PV. If the cluster has a default StorageClass with dynamic provisioning, the PVs are created for you. To pick a class, set storageClassName in the template's spec. Without a default StorageClass or a matching PV, the claims stay Pending and the Pods stay Pending with them.
The diagram below shows the mapping.
Storage follows the identity
The claim name is derived from the ordinal. If web-1 is deleted or its node fails, the replacement Pod is also called web-1. It finds the existing claim www-web-1 and mounts it again. The data survives, even if the new Pod runs on a different node, as long as the volume can attach there.
You can see this by writing a file and deleting the Pod:
kubectl exec web-1 -- sh -c 'echo hello > /usr/share/nginx/html/index.html'
kubectl delete pod web-1
kubectl wait --for=condition=Ready pod/web-1 --timeout=120s
kubectl exec web-1 -- cat /usr/share/nginx/html/index.htmlThe last command prints hello.
Claims outlive the StatefulSet by default
Deleting a StatefulSet does not delete its PVCs. This is deliberate, because the data is usually more valuable than the Pods. Scaling down behaves the same way: web-2 is removed, but www-web-2 stays. If you scale back up, the new web-2 reattaches to the old data.
The persistentVolumeClaimRetentionPolicy field changes this behavior:
spec:
persistentVolumeClaimRetentionPolicy:
whenDeleted: Retain
whenScaled: DeleteEach setting accepts Retain (the default) or Delete. This example keeps claims when the StatefulSet is deleted and removes the claims of Pods that are scaled away. Whether the PV itself is then deleted depends on the PV's reclaim policy. The next chapters cover scaling in more detail.
Common errors
| Symptom | Likely cause |
|---|---|
| Pods and claims stay `Pending` | No default StorageClass, or no PV matches the request |
| Pod cannot start after moving nodes | A zonal volume cannot attach in another zone |
| Edited `storage` size is rejected | `volumeClaimTemplates` is immutable on an existing StatefulSet |
| Old data appears after recreating the StatefulSet | Retained claims from the previous StatefulSet were reused |
Exercise
You scale the StatefulSet web from 3 replicas to 2 with the default retention policy. Which Pod is removed, what happens to its PVC, and what happens when you scale back to 3?
Show answer
Pod web-2 is removed because the highest ordinal goes first. The claim www-web-2 is retained under the default whenScaled: Retain. When you scale back to 3, the new web-2 mounts www-web-2 again and sees its earlier data.