Bird
Raised Fist0
Kubernetesdevops~5 mins

StatefulSet ordering and naming in Kubernetes - Commands & Configuration

Choose your learning style10 modes available
Introduction
When you run multiple copies of an app that need stable names and order, StatefulSet helps. It creates pods with unique names and starts or stops them one by one in order.
When you need to run a database cluster where each node must keep its identity.
When you want to deploy an app that requires stable network IDs for each instance.
When pods must start or stop in a specific order to avoid errors.
When you want to keep data on each pod even if it restarts or moves.
When you need predictable pod names for easier management and monitoring.
Config File - statefulset.yaml
statefulset.yaml
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: example-statefulset
  namespace: default
spec:
  serviceName: "example-service"
  replicas: 3
  selector:
    matchLabels:
      app: example
  template:
    metadata:
      labels:
        app: example
    spec:
      containers:
      - name: example-container
        image: nginx:1.23.3
        ports:
        - containerPort: 80
  volumeClaimTemplates:
  - metadata:
      name: example-storage
    spec:
      accessModes: ["ReadWriteOnce"]
      resources:
        requests:
          storage: 1Gi

serviceName: Links pods to a headless service for stable network IDs.

replicas: Number of pod copies to run.

selector: Matches pods managed by this StatefulSet.

template: Defines pod content and labels.

volumeClaimTemplates: Creates persistent storage for each pod with unique names.

Commands
Create the StatefulSet with 3 pods, each with stable names and storage.
Terminal
kubectl apply -f statefulset.yaml
Expected OutputExpected
statefulset.apps/example-statefulset created
Check the pods created by the StatefulSet and see their stable names.
Terminal
kubectl get pods -l app=example
Expected OutputExpected
NAME READY STATUS RESTARTS AGE example-statefulset-0 1/1 Running 0 10s example-statefulset-1 1/1 Running 0 8s example-statefulset-2 1/1 Running 0 6s
→
-l - Filter pods by label to show only those managed by the StatefulSet
Delete pod 1 to see how StatefulSet recreates it with the same name and storage.
Terminal
kubectl delete pod example-statefulset-1
Expected OutputExpected
pod "example-statefulset-1" deleted
Verify the pod example-statefulset-1 is recreated with the same name.
Terminal
kubectl get pods -l app=example
Expected OutputExpected
NAME READY STATUS RESTARTS AGE example-statefulset-0 1/1 Running 0 30s example-statefulset-1 1/1 Running 0 5s example-statefulset-2 1/1 Running 0 28s
→
-l - Filter pods by label to show only those managed by the StatefulSet
Key Concept

StatefulSet ensures pods have stable, unique names and start or stop in order, keeping their storage intact.

Common Mistakes
Deleting pods manually and expecting new pods to have random names.
StatefulSet always recreates pods with the same stable names, so names are predictable, not random.
Understand that pod names include an ordinal index and are stable for each replica.
Using a Deployment instead of StatefulSet for apps needing stable storage and names.
Deployments create pods with random names and do not guarantee stable storage per pod.
Use StatefulSet when you need stable network IDs and persistent storage per pod.
Not defining volumeClaimTemplates, so pods share storage or lose data on restart.
Without volumeClaimTemplates, pods do not get unique persistent volumes, risking data loss.
Always define volumeClaimTemplates in StatefulSet to give each pod its own persistent storage.
Summary
Apply a StatefulSet YAML to create pods with stable names and storage.
Use labels to list pods and see their predictable names with ordinal numbers.
Deleting a pod causes StatefulSet to recreate it with the same name and storage.
StatefulSet manages pod startup and shutdown order to keep app consistency.

Practice

(1/5)
1. What is a key feature of a Kubernetes StatefulSet compared to a Deployment?
easy
A. Pods share the same IP address at all times.
B. Pods have stable, unique network identities and persistent storage.
C. Pods are created and deleted randomly without order.
D. Pods cannot be scaled after creation.

Solution

  1. Step 1: Understand StatefulSet purpose

    StatefulSets provide stable network IDs and persistent storage for pods, unlike Deployments.
  2. Step 2: Compare with Deployment behavior

    Deployments create pods without stable names or guaranteed order, while StatefulSets do.
  3. Final Answer:

    Pods have stable, unique network identities and persistent storage. -> Option B
  4. Quick Check:

    StatefulSet = stable pods and storage [OK]
Hint: StatefulSet = stable pod names and storage [OK]
Common Mistakes:
  • Thinking StatefulSet pods have random names
  • Assuming StatefulSet pods share IPs
  • Believing StatefulSets cannot scale
2. Which of the following is the correct naming pattern for pods created by a StatefulSet named web with 3 replicas?
easy
A. web-01, web-02, web-03
B. web-1, web-2, web-3
C. web-0, web-2, web-4
D. web-0, web-1, web-2

Solution

  1. Step 1: Recall StatefulSet pod naming

    Pods are named as {statefulset-name}-{ordinal} starting at 0.
  2. Step 2: Apply to given example

    For 3 replicas, pods are web-0, web-1, and web-2.
  3. Final Answer:

    web-0, web-1, web-2 -> Option D
  4. Quick Check:

    StatefulSet pods start at 0 [OK]
Hint: Pod names start at 0 and increment by 1 [OK]
Common Mistakes:
  • Starting pod names at 1 instead of 0
  • Skipping ordinals or using even numbers only
  • Using zero-padded numbers like 01
3. Given a StatefulSet named db with 2 replicas, what will be the order of pod termination when scaling down from 2 to 1 replica?
medium
A. db-1 terminates first, then db-0
B. db-0 terminates first, then db-1
C. Both pods terminate simultaneously
D. Pods terminate in random order

Solution

  1. Step 1: Understand StatefulSet termination order

    StatefulSets terminate pods in reverse ordinal order when scaling down.
  2. Step 2: Apply to example

    With pods db-0 and db-1, db-1 (highest ordinal) terminates first.
  3. Final Answer:

    db-1 terminates first, then db-0 -> Option A
  4. Quick Check:

    Pods terminate highest ordinal first [OK]
Hint: Pods terminate highest ordinal first when scaling down [OK]
Common Mistakes:
  • Assuming pods terminate in creation order
  • Thinking pods terminate simultaneously
  • Believing termination order is random
4. You have a StatefulSet with 3 replicas named cache. You notice pods cache-2 is stuck terminating and blocking scale down. What is a likely cause?
medium
A. Pod cache-2 has a finalizer preventing deletion.
B. Pod cache-0 is still running and blocking cache-2.
C. StatefulSet does not support scaling down pods.
D. Pod names must be numeric only without dashes.

Solution

  1. Step 1: Identify common pod termination issues

    Pods stuck terminating often have finalizers or resources blocking deletion.
  2. Step 2: Apply to StatefulSet pod cache-2

    Pod cache-2 likely has a finalizer preventing its deletion, causing the block.
  3. Final Answer:

    Pod cache-2 has a finalizer preventing deletion. -> Option A
  4. Quick Check:

    Finalizers block pod deletion [OK]
Hint: Check for finalizers if pod stuck terminating [OK]
Common Mistakes:
  • Thinking lower ordinal pods block higher ones
  • Believing StatefulSets cannot scale down
  • Misunderstanding pod naming rules
5. You want to deploy a StatefulSet with 3 replicas but require pods to start in parallel instead of sequentially. Which field should you modify to achieve this?
hard
A. Set serviceName to empty string.
B. Set replicaCount: 3 in the pod template.
C. Set podManagementPolicy: Parallel in the StatefulSet spec.
D. Set updateStrategy: RollingUpdate in the StatefulSet spec.

Solution

  1. Step 1: Understand podManagementPolicy in StatefulSets

    By default, StatefulSets create pods sequentially. Setting podManagementPolicy to Parallel allows parallel pod creation.
  2. Step 2: Identify correct field to change

    Changing podManagementPolicy to Parallel in the spec enables parallel pod startup.
  3. Final Answer:

    Set podManagementPolicy: Parallel in the StatefulSet spec. -> Option C
  4. Quick Check:

    podManagementPolicy controls pod start order [OK]
Hint: Use podManagementPolicy: Parallel to start pods together [OK]
Common Mistakes:
  • Changing replicaCount instead of podManagementPolicy
  • Confusing updateStrategy with pod start order
  • Removing serviceName breaks network identity
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard