Skip to main content

StatefulSet

A StatefulSet is a Kubernetes controller for applications that need stable identity and storage, such as databases and message brokers. Each pod gets a predictable name, its own persistent volume, and ordered startup and shutdown.

What StatefulSets guarantee

  • Stable names: pods are redis-0, redis-1, redis-2, and keep those names across restarts.
  • Stable DNS: each pod gets its own DNS record through a headless Service.
  • Dedicated storage: each pod has its own PVC from volumeClaimTemplates, which survives pod deletion.
  • Ordered operations: pods start in order and stop in reverse order by default.
flowchart TD H["Headless Service
redis-headless"] --> P0["redis-0"] H --> P1["redis-1"] H --> P2["redis-2"] P0 --> V0[("PVC redis-data-0")] P1 --> V1[("PVC redis-data-1")] P2 --> V2[("PVC redis-data-2")] classDef svc fill:#FFEDD5,color:#1E293B classDef pod fill:#DBEAFE,color:#1E293B classDef pvc fill:#EDE9FE,color:#1E293B class H svc class P0,P1,P2 pod class V0,V1,V2 pvc

Example

YAML
apiVersion: v1
kind: Service
metadata:
name: redis-headless
namespace: production
spec:
clusterIP: None
selector:
app: redis
ports:
- port: 6379
name: redis
YAML
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: redis
namespace: production
spec:
serviceName: redis-headless # must match the headless Service
replicas: 3
selector:
matchLabels:
app: redis
template:
metadata:
labels:
app: redis
spec:
containers:
- name: redis
image: redis:7.2-alpine
volumeMounts:
- name: redis-data
mountPath: /data
volumeClaimTemplates:
- metadata:
name: redis-data
spec:
accessModes: ["ReadWriteOnce"]
storageClassName: gp3-encrypted
resources:
requests:
storage: 10Gi

Operate

Bash
kubectl get pods -l app=redis -n production
kubectl get pvc -l app=redis -n production
kubectl scale statefulset redis --replicas=4 -n production

For a staged upgrade, set updateStrategy.rollingUpdate.partition so only higher-ordinal pods update first. By default PVCs are kept on scale-down. A persistentVolumeClaimRetentionPolicy field can change that.

Deployment or StatefulSet

Question Choose
Pods interchangeable, no per-pod disk Deployment
Stable identity or per-pod storage StatefulSet

Troubleshooting

Problem Cause Fix
redis-1 stays Pending redis-0 not Ready Fix the first pod
DNS names do not resolve serviceName mismatch Match the headless Service name
Orphan PVCs after scale-down Default retention Delete after confirming data
Remember

A StatefulSet gives identity and storage. It does not provide replication or backups. That logic belongs to the application or an operator.

Security

Run database pods as non-root with runAsNonRoot: true, and avoid broad host access.

Common Mistake

Running a database as a Deployment with one shared ReadWriteOnce volume. When it reschedules to another node, the volume may fail to follow and the pod stays stuck.

Frequently Asked Questions

Why can I not just run a database as a Deployment?

A Deployment treats pods as interchangeable with random names and no per-pod storage. Databases need stable identity, a dedicated volume per replica, and ordered startup, which StatefulSets provide.

Does a StatefulSet replicate my data?

No. It guarantees identity and storage only. Replication, such as Postgres streaming replication, is configured in the application or by an operator.

What happens to PVCs when I scale down?

By default they remain, so data is not lost, and you must delete them manually unless a retention policy is set.

Found a mistake, an outdated step or a broken link?