Browse topics

StatefulSets

Version
1.37
Review
Draft

Summary#

A StatefulSet gives each Pod a stable ordinal name such as web-0 and a matching network name through a headless Service. With volumeClaimTemplates, each Pod gets its own PersistentVolumeClaim. A replacement Pod keeps the name and claim.

Syntax#

yaml
spec:
  serviceName: web
  replicas: 2
  volumeClaimTemplates:
    - metadata:
        name: data
      spec:
        accessModes: ["ReadWriteOnce"]
        resources:
          requests:
            storage: 1Gi

The Service named by serviceName sets clusterIP: None. You create it yourself.

Example#

Use a cluster with a default StorageClass that provisions volumes dynamically and has the Delete reclaim policy so cleanup removes those volumes.

sh
(
  set -eu
  ns="ref-statefulsets-$$"
  kubectl create namespace "$ns" >/dev/null
  trap 'kubectl delete namespace "$ns" >/dev/null' EXIT
  kubectl wait -n "$ns" --for=create serviceaccount/default --timeout=60s >/dev/null

  kubectl apply -n "$ns" -f - >/dev/null <<'EOF'
apiVersion: v1
kind: Service
metadata:
  name: web
spec:
  clusterIP: None
  selector:
    app: web
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: web
spec:
  serviceName: web
  replicas: 2
  selector:
    matchLabels:
      app: web
  template:
    metadata:
      labels:
        app: web
    spec:
      terminationGracePeriodSeconds: 1
      containers:
        - name: app
          image: busybox:1.37
          command: ["sleep", "3600"]
          volumeMounts:
            - name: data
              mountPath: /data
  volumeClaimTemplates:
    - metadata:
        name: data
      spec:
        accessModes: ["ReadWriteOnce"]
        resources:
          requests:
            storage: 16Mi
EOF
  kubectl rollout status -n "$ns" statefulset/web --timeout=180s >/dev/null
  kubectl get -n "$ns" pods -l app=web -o name --sort-by=.metadata.name
  kubectl get -n "$ns" persistentvolumeclaims -o name --sort-by=.metadata.name

  kubectl exec -n "$ns" web-0 -- sh -c 'echo "written by web-0" > /data/note'
  kubectl delete -n "$ns" pod web-0 >/dev/null
  kubectl wait -n "$ns" --for=create pod/web-0 --timeout=60s >/dev/null
  kubectl wait -n "$ns" --for=condition=Ready pod/web-0 --timeout=120s >/dev/null
  kubectl exec -n "$ns" web-0 -- sh -c 'hostname; cat /data/note'
)

Expected output#

Text
pod/web-0
pod/web-1
persistentvolumeclaim/data-web-0
persistentvolumeclaim/data-web-1
web-0
written by web-0

The replacement web-0 has the same hostname and reads the file written by its predecessor.

Common mistakes#

Deleting a StatefulSet or scaling it down does not delete its claims by default. Old data returns when the same ordinal is created again, and the storage stays allocated. Delete the claims deliberately when the data is no longer needed.

Version applicability#

The example targets Kubernetes 1.37 and uses apps/v1. The persistentVolumeClaimRetentionPolicy field, which can delete claims on scale-down or deletion, is stable since 1.32. Its default is Retain.

Sources#

Attribution
DevLookup contributors
License
CC-BY-4.0