Skip to content
intermediate Phase 3 · Core Workloads

StatefulSets

Learn StatefulSets for stateful applications requiring stable network identities and persistent storage.

1h
0 problems
Topic Progress 0%

Stable Identity for Stateful Workloads

Stable Identity for Stateful Workloads

Databases and brokers care about who they are: peers must locate each other, and each member owns its own data. Deployments give interchangeable pods with random suffixes; StatefulSets give ordinal identity.

Pods are named <set>-<ordinal> — mysql-0, mysql-1, mysql-2 — and a governing headless Service (clusterIP: None) publishes DNS records such as mysql-0.mysql.ns.svc.cluster.local. Under the default OrderedReady policy, scaling down removes the highest ordinal first, and scaling up creates each pod only after the previous one is Running and Ready.

Each ordinal mounts its own PVC from volumeClaimTemplates, so data follows identity instead of following the node.

Spec Shape

Spec Shape

apiVersion: apps/v1
kind: StatefulSet
metadata: { name: mysql }
spec:
  serviceName: mysql
  replicas: 3
  podManagementPolicy: OrderedReady
  updateStrategy: { type: RollingUpdate }
  selector: { matchLabels: { app: mysql } }
  template:
    metadata: { labels: { app: mysql } }
    spec:
      containers:
      - name: mysql
        image: mysql:8.4
        ports: [{ containerPort: 3306 }]
        volumeMounts: [{ name: data, mountPath: /var/lib/mysql }]
  volumeClaimTemplates:
  - metadata: { name: data }
    spec:
      accessModes: [ReadWriteOnce]
      resources: { requests: { storage: 10Gi } }

serviceName must reference a headless Service; per-pod DNS entries appear only once pods run.

Pitfalls and Interview Angles

Pitfalls and Interview Angles

Pitfall 1 — PVC lifecycle surprises. Deleting the StatefulSet leaves its PVCs behind by default (deliberate: data survives), but recreating the set with the same name rebinds those volumes — including stale schema or single-writer data from a previous incarnation.

Pitfall 2 — careless rolling upgrades. Restarting clustered stores pod-by-pod can still break quorum when combined with parallel pod management or aggressive readiness. For systems like etcd or Kafka, consider OnDelete and roll manually after checking cluster health.

Interview-ready: state the guarantees — stable network identity, stable storage, ordered deployment and termination — explain the headless Service's role, and pair upgrades with PodDisruptionBudgets so drains never empty a quorum.

Quiz

1. What creates per-pod DNS records for a StatefulSet?

Question 1 options

2. What happens to PVCs when you delete a StatefulSet?

Question 2 options

3. Which pod does OrderedReady scaling create first when going 1 → 3?

Question 3 options

Flashcards

Question

StatefulSet guarantees

Answer

Stable network identity, stable per-ordinal storage, ordered creation and termination.

Question

serviceName field

Answer

Points at the governing headless Service that publishes per-pod DNS A/AAAA records.

Question

volumeClaimTemplates

Answer

Generates one PVC per pod (data-mysql-0, ...), bound to that ordinal for life.

Question

OrderedReady vs Parallel

Answer

OrderedReady serializes pod ops by ordinal; Parallel starts/stops all at once (faster, less safe).

Revision Notes

Key Takeaways

  • 1. Ordinal names plus a headless Service give databases stable addresses
  • 2. PVCs outlive the set by default — recreate carefully
  • 3. Roll clustered stores deliberately, often with OnDelete

Interview Tips

  • • Contrast StatefulSet and Deployment guarantees with a concrete example (Kafka, MySQL)
  • • Explain how you would upgrade a 3-member quorum without losing availability

Cheat Sheet

StatefulSet = ordinal identity + headless DNS + PVC per pod. serviceName required; PVCs retained on delete; roll quorums with OnDelete and PDBs.

Continue Learning