StatefulSets and stable identity
Give each replica a stable name and its own claim, then see what a replacement Pod inherits.
What you will understand
- Explain which parts of ledger-1’s identity persist when the Pod is replaced.
- Connect the headless Service, per-Pod DNS record, and generated storage claim.
- Predict the order of creation and rollout under the default StatefulSet policy.
Builds on: Snapshots and storage recovery
Which parts of a replica’s identity stay stable
Imagine an application with three ledger members. Each writes to its own disk and needs to find two specific peers. If the second member restarts, it needs its previous data and the other members need a familiar address to reconnect. A StatefulSet manages Pods with stable ordinal identities. An ordinal is the numbered slot in the set. Here the slots are ledger-0, ledger-1, and ledger-2. They share a Pod template, but each slot can represent a distinct application member.
When Kubernetes replaces ledger-1, the new Pod gets the same name and can use the same claim. It receives a new UID, runs a new process, and can receive a new IP address. In the identity scene, follow the name through the replacement and spot which state changes. Software using that name still needs to reconnect and recover its own state.
What stays the same when ledger-1 is replaced?
The replacement has the same StatefulSet ordinal and can mount the old claim. It has a new UID and can have a different IP.
Assign ordinals and claims
- StatefulSet ledgerledger-0ordinal 0
- StatefulSet ledgerledger-1ordinal 1
- StatefulSet ledgerledger-2ordinal 2
The controller creates predictable ordinal names instead of interchangeable random Deployment identities.
Publish peer discovery
- Headless ServiceDNSmember addresses
- ledger-1DNSledger-1.ledger
The headless Service and StatefulSet subdomain support member names. DNS publication still depends on endpoint readiness settings.
Stop the original process
- StatefulSet ledgerledger-1member missing
The old ledger-1 Pod ends. Its logical ordinal remains part of the desired state even though that Pod UID is gone.
Replace the Pod at the same ordinal
- StatefulSet ledgerledger-1replace ordinal 1
- ledger-1DNSupdated address
The replacement is again ledger-1, with a new UID and potentially a new IP. Clients resolve the stable name to current addresses.
Default ordinals starting at zero and a matching same-namespace headless Service.
Explore another case
Give each member a name and a claim
A headless Service has clusterIP: None. With a selector matching the StatefulSet’s Pods, it can publish addresses for the members instead of giving callers one virtual IP. In the same namespace, a client can use a per-Pod name such as ledger-1.ledger. A fully qualified example is ledger-1.ledger.default.svc.cluster.local, assuming the default namespace and cluster domain. By default, readiness affects which addresses are published. A workload that needs to discover peers during startup can use publishNotReadyAddresses deliberately. Clients then need to handle peers that answer DNS but are still initializing. A cached failed lookup can also delay discovery shortly after creation.
Save both objects below as ledger.yaml. The StatefulSet refers to its headless Service through serviceName: ledger and uses the cluster’s default StorageClass. The claim template named data creates one PVC per ordinal, including data-ledger-1. The writer records its hostname in the mounted volume each time it starts. This example uses the Service for stable naming and the writer for storage verification.
apiVersion: v1
kind: Service
metadata:
name: ledger
spec:
clusterIP: None
selector: {app: ledger}
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: ledger
spec:
serviceName: ledger # Matching headless Service must exist.
replicas: 3
selector:
matchLabels: {app: ledger}
template:
metadata:
labels: {app: ledger}
spec:
containers:
- name: writer
image: busybox:1.37.0
command: ["sh", "-c", "hostname >> /data/starts; sleep 3600"]
volumeMounts:
- name: data
mountPath: /data
volumeClaimTemplates:
- metadata:
name: data
spec:
accessModes: [ReadWriteOnce]
resources:
requests:
storage: 1GiOpen Inspect the storage handoff to follow the replacement of ordinal 1. Compare the Pod UID, the PVC, and the file across the two lifetimes.
Match each ordinal with its own data
The StatefulSet creates a distinct claim from the template for each member. The default ordered rollout advances after the current ordinal is ready.
Create one claim per member
- StatefulSetClaims 0 / 1 / 2per-ordinal PVCs
The template produces distinct claims such as data-ledger-0. The controller is not cloning one shared writable filesystem for every Pod.
Mount a replacement’s existing claim
- Claims 0 / 1 / 2ledger-1data-ledger-1
When ledger-1 is replaced, its new instance references the ordinal-1 claim rather than another member disk.
Update the highest ordinal first
- StatefulSetledger-2new template
- ledger-2Readiness checkpointready?
A default rolling update replaces ledger-2 before lower ordinals and observes its readiness checkpoint.
Wait for readiness before proceeding
- ledger-2Highest ordinal
- Readiness checkpointledger-1proceed downward
When the updated highest member is ready, the controller can update the next lower member. Application replication remains the application responsibility.
Default OrderedReady management and default rolling-update behavior; retained PVCs.
Inspect the storage handoff
Replace ledger-1 while its claim data-ledger-1 stays bound. Follow the Pod name, Pod UID, persistent volume, and file across the replacement.
From API serverTo kubectl
Record the Pod and claim
Read the UID of ledger-1, the UID of data-ledger-1, and the PV named in the claim. The writer has already recorded a start in /data/starts.
- Before
- Unrecorded
- After
- Pod UID, PVC UID, and PV name recorded
Read the UID of ledger-1, the UID of data-ledger-1, and the PV named in the claim. The writer has already recorded a start in /data/starts.
The storage backend is healthy and the original claim is retained. The kubelet can remount the bound volume on an eligible node. The file comes from that volume.
Reveal prediction answer
The name identifies the ordinal. Compare the UID to identify the Pod object, then compare the PVC UID and PV name to verify storage continuity.
Save the Pod UID and volume binding, replace only ledger-1, and compare both with the file read from the replacement.
Replace ledger-1 and check the evidenceExplore another case
Replace one ledger member and verify its disk
Run this on the disposable kind workbench with a working default StorageClass and a fresh ledger-trace namespace. The checks verify that the Pod UID changes, the same PVC uses the same PV, and the replacement appends a line to the same file.
The StatefulSet lab compares the StatefulSet’s stable name with a DaemonSet’s node coverage and a Job’s completion.
(
set -euo pipefail
k() { kubectl --context kind-kv-lab -n ledger-trace "$@"; }
kubectl --context kind-kv-lab create namespace ledger-trace
k apply -f ledger.yaml
k rollout status statefulset/ledger --timeout=180s
old_pod_uid=$(k get pod ledger-1 -o jsonpath='{.metadata.uid}')
old_claim_uid=$(k get pvc data-ledger-1 -o jsonpath='{.metadata.uid}')
volume=$(k get pvc data-ledger-1 -o jsonpath='{.spec.volumeName}')
old_line_count=$(k exec ledger-1 -- sh -c 'wc -l < /data/starts')
printf 'Original Pod UID: %s\nPVC UID: %s\nPV: %s\n' "$old_pod_uid" "$old_claim_uid" "$volume"
k exec ledger-1 -- cat /data/starts
k delete pod ledger-1 --wait=true
k wait --for=create pod/ledger-1 --timeout=120s
k wait --for=condition=Ready pod/ledger-1 --timeout=180s
new_pod_uid=$(k get pod ledger-1 -o jsonpath='{.metadata.uid}')
test "$new_pod_uid" != "$old_pod_uid"
test "$(k get pvc data-ledger-1 -o jsonpath='{.metadata.uid}')" = "$old_claim_uid"
test "$(k get pvc data-ledger-1 -o jsonpath='{.spec.volumeName}')" = "$volume"
test "$(k exec ledger-1 -- sh -c 'wc -l < /data/starts')" -gt "$old_line_count"
printf 'Replacement Pod UID: %s\n' "$new_pod_uid"
k exec ledger-1 -- cat /data/starts
)Remove the namespace you created for this exercise when you finish.
kubectl --context kind-kv-lab delete namespace ledger-traceUnderstand rollout order and application recovery
With the default OrderedReady management policy, the controller creates from lowest ordinal upward and scales down in reverse order. The default rolling update works from highest ordinal downward and waits for each updated Pod to become ready before proceeding. In this example, a broken update can stall at ledger-2. Check its Pod and events first. If reverting the template leaves a failed update stuck, the supported recovery can require deleting the affected Pod after the revert so it is recreated.
The application remains responsible for copying records to peers, deciding which member accepts writes, and recovering from data loss. The sample writer simply appends a line to its own volume.
Check your understanding
ledger-1 comes back with the same name. How can you tell it used the old disk and that Kubernetes created a new Pod?
Confirm it references data-ledger-1 and can read the original file. Compare Pod UIDs before and after deletion. The claim and file demonstrate storage continuity, while the UID identifies a new object lifetime.
Primary documentation
Go to the source
Reviewed 2026-10-27 · Kubernetes 1.37 · Related local exercise