StatefulSets and Persistent Volumes: Running Stateful Apps on Kubernetes
How StatefulSets give pods stable identities and storage, how PersistentVolumes, PVCs and StorageClasses work, and when to run databases on Kubernetes.
Published December 2, 2026
Deployments treat pods as interchangeable cattle, which is perfect for stateless services. Databases, message brokers and clustered systems need something different: stable network identities, stable storage that follows a specific pod, and ordered startup and shutdown. StatefulSets and persistent volumes provide exactly that.
The concept
A StatefulSet creates pods with predictable names (db-0, db-1, db-2) and, together with a headless Service, stable DNS names (db-0.db.prod.svc.cluster.local). Each pod gets its own PersistentVolumeClaim from a volumeClaimTemplate, and if a pod is rescheduled to another node, it reattaches to the same volume. Pods are created, updated and deleted in order by default.
Storage is abstracted in three layers. A StorageClass describes a type of storage (SSD, replicated, a CSI driver) and provisions volumes dynamically. A PersistentVolumeClaim (PVC) is a request for storage of a certain size and access mode. A PersistentVolume (PV) is the actual provisioned disk bound to that claim. Reclaim policies decide whether data is deleted or retained when a claim is removed.
Storage layers
Deployment vs StatefulSet
| Deployment | StatefulSet | |
|---|---|---|
| Pod names | Random suffix | Ordinal: app-0, app-1 |
| Storage | Shared or none | Per-pod PVC that follows the pod |
| Network identity | Via Service only | Stable per-pod DNS (headless Service) |
| Ordering | Parallel | Ordered create/update/delete |
| Use for | Stateless APIs, workers | Databases, Kafka, ZooKeeper, Elasticsearch |
Should you run databases on Kubernetes?
- Is a managed database available? Prefer it for most teams
- Need it in-cluster? Use a mature operator (CloudNativePG, etc.)
- Plan backups and restore tests from day one
- Use topology spread and PodDisruptionBudgets
- Choose StorageClasses with appropriate performance and replication
Practical examples
StatefulSet with per-pod storage
Requires a headless Service named in serviceName.
apiVersion: apps/v1
kind: StatefulSet
metadata: { name: redis }
spec:
serviceName: redis
replicas: 3
selector: { matchLabels: { app: redis } }
template:
metadata: { labels: { app: redis } }
spec:
containers:
- name: redis
image: redis:7.4
volumeMounts: [{ name: data, mountPath: /data }]
volumeClaimTemplates:
- metadata: { name: data }
spec:
accessModes: [ReadWriteOnce]
storageClassName: fast-ssd
resources: { requests: { storage: 20Gi } }Inspect claims and volumes
See bindings and reclaim policies.
kubectl get pvc -l app=redis
kubectl get pv -o custom-columns=NAME:.metadata.name,CLAIM:.spec.claimRef.name,POLICY:.spec.persistentVolumeReclaimPolicyExpand a volume
Works if the StorageClass allows volume expansion.
kubectl patch pvc data-redis-0 -p '{"spec":{"resources":{"requests":{"storage":"50Gi"}}}}'Common mistakes
- Running production databases on Kubernetes without backups and tested restores.
- Using ReadWriteOnce volumes and expecting multiple pods to share them.
- Delete reclaim policy on important data, losing disks when PVCs are deleted.
- Ignoring zone placement, so a volume in one zone cannot attach to a pod rescheduled in another.
Key takeaways
- StatefulSets give stable names, DNS and per-pod storage.
- StorageClass → PVC → PV provisions and binds disks.
- Prefer managed databases or mature operators for stateful systems.
- Back up, test restores and mind zones and reclaim policies.
FAQ
Does deleting a StatefulSet delete its volumes?
By default the PVCs remain; recent Kubernetes versions offer a persistentVolumeClaimRetentionPolicy to control this.
What is ReadWriteMany?
An access mode allowing many nodes to mount a volume read-write, supported by some storage types like NFS or certain CSI drivers.
What does an operator add?
Operators encode database-specific operations: failover, backups, upgrades and scaling, beyond what a StatefulSet does.