Kubernetes4 min read· 3 practice problems

Deployment vs StatefulSet: the differences, when to use each, and what breaks when you pick wrong

A Deployment runs interchangeable stateless pods; a StatefulSet gives each pod a stable name, DNS record and its own PVC, and handles them in order. See when to use each and how Multi-Attach errors, stuck rollouts and node failures show up.

On this page
  1. Short answer: Deployment pods are interchangeable, StatefulSet pods have a fixed name and their own disk
  2. Side by side
  3. How it works in practice
  4. When to use which
  5. Failure 1: a database on a Deployment and the rollout hangs
  6. Failure 2: a StatefulSet update stops at db-2
  7. Failure 3: after a node dies, only the StatefulSet pod stays put
  8. What not to do
  9. The order to work in
  10. Quick diagnostic checklist

Short answer: Deployment pods are interchangeable, StatefulSet pods have a fixed name and their own disk

Deployment pods are identical: kill any one and another takes its place. StatefulSet pods such as db-0 and db-1 keep a stable name, DNS record and PVC, and are created and updated in order. If each pod holds its own data or pods find each other by name, use a StatefulSet.

Side by side

  • Name — Deployment: checkout-7d9f8c6b5-x2k4p, changes every time · StatefulSet: db-0, always the same
  • DNS — Deployment: anonymous pods behind a Service · StatefulSet: db-0.db.shop.svc.cluster.local
  • Storage — Deployment: every pod points at the same PVC · StatefulSet: one PVC per pod
  • Order — Deployment: all at once · StatefulSet: by default (OrderedReady) 0→1→2, each after the previous is Ready
  • Updates — Deployment: several pods at a time · StatefulSet: one at a time, highest number first

How it works in practice

# Key fields only (apiVersion, selector and template omitted)
kind: Service
metadata: {name: db}
spec:
  clusterIP: None          # headless: a DNS name per pod
  selector: {app: db}
---
kind: StatefulSet
metadata: {name: db}
spec:
  serviceName: db          # must match the Service name above
  volumeClaimTemplates:
  - metadata: {name: data}
    spec:
      accessModes: ["ReadWriteOnce"]
      resources: {requests: {storage: 20Gi}}
$ kubectl get pvc -n shop
NAME        STATUS   VOLUME        CAPACITY   ACCESS MODES
data-db-0   Bound    pvc-3f1c...   20Gi       RWO
data-db-1   Bound    pvc-8a72...   20Gi       RWO

There is one PVC per pod. Delete db-1 and it returns under the same name with the same data-db-1; the IP may change, the DNS name does not. Scaling down keeps the PVCs by default (where supported, persistentVolumeClaimRetentionPolicy changes this). If peer discovery fails with NXDOMAIN or no such host, check that serviceName matches the Service and its CLUSTER-IP is None.

When to use which

  • APIs and workers that keep state in a database or cache → Deployment
  • Databases and brokers where each pod needs its own data (PostgreSQL replicas, Kafka) → StatefulSet
  • Clusters whose members find each other by name (etcd, ZooKeeper) → StatefulSet plus a headless Service

Failure 1: a database on a Deployment and the rollout hangs

$ kubectl describe pod orders-db-6d4f8b7c9-q7w2z -n shop
  Warning  FailedAttachVolume  Multi-Attach error for volume "pvc-8e2a4c1b" Volume is already used by pod(s) orders-db-5b7c9d8f6-k5m2x

A rolling update starts the new pod before removing the old one. If the new pod lands on another node, the ReadWriteOnce volume is still attached to the old node and the new pod sits in ContainerCreating. On the same node it works, which is why this fails only sometimes. Move to a StatefulSet or use strategy.type: Recreate. If it hangs after attaching, see volume attached but rollout stuck.

Failure 2: a StatefulSet update stops at db-2

$ kubectl get pods -n shop -l app=db
NAME   READY   STATUS             RESTARTS      AGE
db-0   1/1     Running            0             12d
db-1   1/1     Running            0             12d
db-2   0/1     CrashLoopBackOff   6 (30s ago)   9m

By design: until the updated db-2 is Ready, db-1 is left alone. Check why with kubectl logs db-2 --previous. One rollback trap: even after rollout undo, the StatefulSet keeps waiting for the broken db-2, so delete that pod yourself. Next time, set partition: 2 to update db-2 alone first.

Failure 3: after a node dies, only the StatefulSet pod stays put

kubectl get pods -o wide shows db-1 stuck in Terminating on the dead node, and no new db-1. About five minutes after a node stops responding, its pods are marked for deletion. A Deployment replaces them at once; a StatefulSet waits for proof the old pod is gone, so two db-1 pods never run together. Only when you are sure the node is off, run kubectl delete pod db-1 --grace-period=0 --force.

What not to do

  • Force-deleting a StatefulSet pod on a node that may still be alive — you can get two copies of db-1.
  • Scaling up a Deployment to make a database "highly available" — the pods just share one PVC.

The order to work in

  1. No per-pod data or fixed names → Deployment
  2. Otherwise → StatefulSet plus a headless Service with the matching name
  3. Stuck rollout: on a Deployment look for Multi-Attach; on a StatefulSet start with the highest-numbered pod

For a failure that ties a StatefulSet pod to its PVC, try PVC expansion succeeds in the API but filesystem size never changes.

Quick diagnostic checklist

  • When a rollout is stuck, first check whether it is a Deployment or a StatefulSet and whether it uses a volume.
  • If the new pod sits in ContainerCreating, look for Multi-Attach error in the kubectl describe pod events.
  • Move a Deployment that shares one ReadWriteOnce PVC to a StatefulSet, or set strategy.type: Recreate.
  • If a StatefulSet update stops, check the highest-numbered pod's readiness and its kubectl logs --previous.
  • After a rollout undo on a StatefulSet, delete the broken pod yourself so it is recreated.
  • If peer discovery returns NXDOMAIN, compare serviceName with the headless Service name and check CLUSTER-IP is None.
  • After scaling down, check with kubectl get pvc that old PVCs remain; scaling back up reattaches that data.
  • If a StatefulSet pod is stuck in Terminating after a node failure, force-delete it only once the node is confirmed off.

Practice with this guide

Reviewed problems about the same failure. Write the cause, recovery, and prevention yourself, then compare with the model answer.

Read next