Sub topic 15
Per-replica storage with StatefulSets
This chapter assumes you can write a PersistentVolumeClaim, know what a StorageClass does, and have used a Deployment. If those are unfamiliar, review the earlier chapters on claims and dynamic provisioning first.
Why a Deployment does not fit
A Deployment with one PVC gives every replica the same claim. A ReadWriteOnce volume attaches to one node at a time, so most replicas cannot mount it. Even with a shared-access volume, databases usually need their own data directory per instance.
A StatefulSet solves this. Each replica has a stable name (db-0, db-1, db-2) and gets its own claim, created from a template.
volumeClaimTemplates
The volumeClaimTemplates field holds a PVC blueprint. For each replica, the controller creates a claim named <template-name>-<pod-name>.
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: db
spec:
serviceName: db
replicas: 3
selector:
matchLabels:
app: db
template:
metadata:
labels:
app: db
spec:
containers:
- name: db
image: postgres:16
env:
- name: POSTGRES_PASSWORD
value: example
- name: PGDATA
value: /var/lib/postgresql/data/pgdata
volumeMounts:
- name: data
mountPath: /var/lib/postgresql/data
volumeClaimTemplates:
- metadata:
name: data
spec:
accessModes: ["ReadWriteOnce"]
storageClassName: fast
resources:
requests:
storage: 10GiThe template name data matches the volumeMounts entry. This produces three claims: data-db-0, data-db-1, and data-db-2. Each is 10Gi and is provisioned by the fast StorageClass.
What happens on rescheduling
The claim is tied to the replica's identity, not to a node or a Pod instance. If db-1 is deleted or its node fails, the controller creates a new Pod named db-1. That Pod mounts data-db-1 again, so it sees the same data.
With WaitForFirstConsumer, the first Pod placement decides where the volume is created. After that, a zonal volume such as EBS pins the replica to that zone. If no node in the zone has capacity, the Pod stays Pending.
Common errors
- Claims survive deletion. Deleting the StatefulSet or scaling down does not delete the claims. This protects data, but you pay for unused volumes. Delete the PVCs yourself when you no longer need them. Newer Kubernetes versions offer
persistentVolumeClaimRetentionPolicyto change this behavior. - Editing the template later. You cannot change
volumeClaimTemplateson an existing StatefulSet. To resize, patch each existing PVC individually, which requiresallowVolumeExpansion: trueon the class. - Scaling back up reuses old data. If you scale from 3 to 1 and back to 3,
db-1anddb-2reattach to their old claims. This is useful, but be aware of stale data. - Missing StorageClass. If
fastdoes not exist, the claims stay Pending and the Pods never schedule. Check withkubectl describe pvc data-db-0.
kubectl get pvc -l app=db
kubectl delete pod db-1
kubectl get pod db-1 -o wideThe labels in the Pod template are not copied to the claims by default. If -l app=db returns nothing, list claims with kubectl get pvc instead.
Exercise
You scale a StatefulSet named db from 3 replicas to 2, then delete the StatefulSet. Which PVCs still exist, and why?
Show answer
All three: data-db-0, data-db-1, and data-db-2. By default, StatefulSet controllers do not delete claims on scale-down or on StatefulSet deletion, which protects the data. You must delete the claims manually, or set a persistentVolumeClaimRetentionPolicy.