0
0
Kubernetesdevops~10 mins

StatefulSets for stateful applications in Kubernetes - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - StatefulSets for stateful applications
Define StatefulSet YAML
kubectl apply StatefulSet
Controller creates Pod 0
Pod 0 gets stable network ID & storage
Controller creates Pod 1
Pod 1 gets stable network ID & storage
Pods run with stable identity
Scaling up/down maintains order & identity
Pods terminate gracefully preserving data
Shows how Kubernetes StatefulSet creates pods one by one with stable network IDs and storage, maintaining order and identity for stateful apps.
Execution Sample
Kubernetes
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: web
spec:
  serviceName: "nginx"
  replicas: 2
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx
        volumeMounts:
        - name: www
          mountPath: /usr/share/nginx/html
  volumeClaimTemplates:
  - metadata:
      name: www
    spec:
      accessModes: [ "ReadWriteOnce" ]
      resources:
        requests:
          storage: 1Gi
Defines a StatefulSet named 'web' with 2 replicas, each pod gets stable network ID and storage.
Process Table
StepActionPod CreatedPod IdentityStorage VolumeStatus
1Apply StatefulSet YAMLNoneNoneNoneStatefulSet resource created
2Controller creates first podweb-0web-0.nginxPVC web-0Pending -> Running
3First pod readyweb-0web-0.nginxPVC web-0Running
4Controller creates second podweb-1web-1.nginxPVC web-1Pending -> Running
5Second pod readyweb-1web-1.nginxPVC web-1Running
6Scale down to 1 replicaweb-1 deletedweb-1.nginx removedPVC web-1 retainedweb-0 running
7Scale up to 2 replicasweb-1 recreatedweb-1.nginxPVC web-1 reusedRunning
💡 Pods created in order with stable network IDs and storage; scaling preserves identity and data.
Status Tracker
VariableStartAfter Step 2After Step 4After Step 6After Step 7
PodsNone[web-0][web-0, web-1][web-0][web-0, web-1]
Pod IdentitiesNone[web-0.nginx][web-0.nginx, web-1.nginx][web-0.nginx][web-0.nginx, web-1.nginx]
Storage PVCsNone[PVC web-0][PVC web-0, PVC web-1][PVC web-0, PVC web-1][PVC web-0, PVC web-1]
Key Moments - 3 Insights
Why does the controller create pods one at a time instead of all at once?
The controller creates pods sequentially to ensure each pod gets a stable network ID and storage before the next pod starts, as shown in steps 2 and 4 of the execution_table.
What happens to the storage volumes when a pod is deleted during scale down?
The storage volumes (PVCs) are retained even if the pod is deleted, preserving data for when the pod is recreated, as seen in step 6 where PVC web-1 remains after pod deletion.
How does the StatefulSet maintain pod identity after scaling down and up?
When scaling down, pods are deleted but their identities and storage are preserved. When scaling up, pods are recreated with the same network IDs and storage volumes, as shown in steps 6 and 7.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 4. Which pod is created and what is its network identity?
APod web-2 with network ID web-2.nginx
BPod web-0 with network ID web-0.nginx
CPod web-1 with network ID web-1.nginx
DNo pod created at step 4
💡 Hint
Check the 'Pod Created' and 'Pod Identity' columns at step 4 in the execution_table.
At which step does the StatefulSet controller delete a pod during scaling down?
AStep 6
BStep 5
CStep 3
DStep 7
💡 Hint
Look for the row mentioning pod deletion in the 'Action' column in the execution_table.
If the number of replicas was increased to 3 initially, how would the variable_tracker's 'Pods' row change after step 4?
A[web-0, web-1, web-2]
B[web-0, web-1]
C[web-0]
DNone
💡 Hint
The controller creates pods one at a time; after step 4 only two pods exist as per execution_table.
Concept Snapshot
StatefulSets manage stateful apps by creating pods in order.
Each pod gets a stable network ID and persistent storage.
Pods start one by one; scaling preserves pod identity and data.
Storage volumes (PVCs) are retained even if pods are deleted.
Use StatefulSets for apps needing stable network and storage.
Full Transcript
StatefulSets in Kubernetes help run stateful applications by creating pods one at a time. Each pod gets a stable network name and persistent storage volume. The controller applies the StatefulSet YAML, then creates pod web-0 with identity web-0.nginx and storage PVC web-0. Once web-0 is running, it creates web-1 similarly. When scaling down, pods are deleted but their storage volumes remain. Scaling up recreates pods with the same identity and storage. This ensures data is preserved and pods have stable network IDs, which is important for databases and other stateful apps.