Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Running Applications  ›  Lesson

StatefulSets

StatefulSets 12 minA StatefulSet gives each pod a stable name, its own persistent volume claim and ordered startup, for workloads such as databases whose replacements must be the same instance.

StatefulSets

notes-web copies are interchangeable: any pod can serve any request. The database is different. Each database pod has its own data on its own disk, and a replacement must come back with the same name and the same disk. That is what a StatefulSet promises.

The rule

A StatefulSet gives each pod a fixed name, a fixed disk and a turn in line.

Compared with a Deployment, a StatefulSet adds three guarantees:

  • Stable names: pods are notes-db-0, notes-db-1, notes-db-2, not random suffixes. A replaced notes-db-1 is still notes-db-1.
  • Stable storage: volumeClaimTemplates create one PersistentVolumeClaim per pod (data-notes-db-0, …). A replaced pod reattaches to its own claim.
  • Ordered steps: by default pods start in order 0, 1, 2 — each waits for the one before to be ready — and are removed in reverse.

It also needs a headless Service (one with clusterIP: None), named in serviceName, which gives each pod its own DNS name such as notes-db-0.notes-db.notes.svc.cluster.local.

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: notes-db
  namespace: notes
spec:
  serviceName: notes-db
  replicas: 1
  selector:
    matchLabels:
      app: notes-db
  template:
    metadata:
      labels:
        app: notes-db
    spec:
      containers:
        - name: postgres
          image: postgres:16
          volumeMounts:
            - name: data
              mountPath: /var/lib/postgresql/data
  volumeClaimTemplates:
    - metadata:
        name: data
      spec:
        accessModes: ["ReadWriteOnce"]
        resources:
          requests:
            storage: 5Gi

A StatefulSet does not make a database replicate or fail over. It only gives each pod a stable identity; the database software must do the rest.

The shortcut

Ask: "If this pod died, must its replacement be the same one?" Same name and same data needed → StatefulSet. Any copy will do → Deployment.

notes-db-0 crashesStep 1 of 3
  1. Replacement

    The StatefulSet recreates a pod named exactly notes-db-0.

The trap: deleting a StatefulSet (or scaling it down) does not delete its claims by default. That protects data — and leaves storage you may still be paying for.

Drills

Answer fast — trust the rule.

quick drill

A StatefulSet named notes-db has 3 replicas. What are the pod names?

quick drill

Which field gives each StatefulSet pod its own persistent disk?

quick drill

Which workload fits a Deployment rather than a StatefulSet?

quick drill

You scale notes-db from 3 to 1. What happens to data-notes-db-2?

Remember

  • A StatefulSet gives each pod a fixed name, a fixed disk and a turn in line.
  • Must the replacement be the same one? StatefulSet. Any copy? Deployment.
  • Trap: claims outlive the pods; clean them up on purpose.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainWhy not a Deployment?

Explain why notes-db should be a StatefulSet rather than a Deployment. Describe what happens when notes-db-0 crashes, and one thing a StatefulSet does not do for a database.