📖 Scenario: You are managing a Kubernetes cluster that runs a database application. You want to deploy multiple instances of this database using a StatefulSet. StatefulSets give each pod a unique name and ensure they start in order, which is important for your database to work correctly.
🎯 Goal: Learn how to create a StatefulSet with 3 replicas, understand how Kubernetes names each pod, and see the order in which pods start.
📋 What You'll Learn
Create a StatefulSet YAML manifest with 3 replicas
Set the container image to nginx:latest
Use a volumeClaimTemplates section for persistent storage
Observe pod names and startup order
💡 Why This Matters
🌍 Real World
StatefulSets are used to deploy applications that need stable network IDs and persistent storage, like databases or clustered services.
💼 Career
Understanding StatefulSets is important for Kubernetes administrators and DevOps engineers managing stateful applications in production.
Progress0 / 4 steps
1
Create a StatefulSet YAML with 3 replicas
Create a YAML file named statefulset.yaml with a StatefulSet resource named web. Set replicas to 3. Use nginx:latest as the container image. Include a volumeClaimTemplates section with a persistent volume claim named www requesting 1Gi storage.
Kubernetes
Hint
Remember to set replicas to 3 and include volumeClaimTemplates with the correct storage size.
2
Add a headless service for the StatefulSet
Add a YAML manifest for a headless Service named nginx with clusterIP: None and selector app: nginx. This service is required for the StatefulSet to manage network identities.
Kubernetes
Hint
The headless service must have clusterIP: None and select pods with label app: nginx.
3
Deploy the StatefulSet and check pod names
Use the command kubectl apply -f statefulset.yaml to deploy the StatefulSet and Service. Then use kubectl get pods -l app=nginx to list the pods. Notice the pod names are web-0, web-1, and web-2.
Kubernetes
Hint
Use kubectl apply -f statefulset.yaml to deploy and kubectl get pods -l app=nginx to see pod names.
4
Observe pod startup order and print pod names
Write a command to print the pod names in order using kubectl get pods -l app=nginx -o custom-columns=NAME:.metadata.name --no-headers. This shows pods named web-0, web-1, and web-2 in startup order.
Kubernetes
Hint
Use kubectl get pods -l app=nginx -o custom-columns=NAME:.metadata.name --no-headers to list pod names.
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
Step 1: Understand StatefulSet purpose
StatefulSets provide stable network IDs and persistent storage for pods, unlike Deployments.
Step 2: Compare with Deployment behavior
Deployments create pods without stable names or guaranteed order, while StatefulSets do.
Final Answer:
Pods have stable, unique network identities and persistent storage. -> Option B
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
Step 1: Recall StatefulSet pod naming
Pods are named as {statefulset-name}-{ordinal} starting at 0.
Step 2: Apply to given example
For 3 replicas, pods are web-0, web-1, and web-2.
Final Answer:
web-0, web-1, web-2 -> Option D
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
Step 1: Understand StatefulSet termination order
StatefulSets terminate pods in reverse ordinal order when scaling down.
Step 2: Apply to example
With pods db-0 and db-1, db-1 (highest ordinal) terminates first.
Final Answer:
db-1 terminates first, then db-0 -> Option A
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
Step 1: Identify common pod termination issues
Pods stuck terminating often have finalizers or resources blocking deletion.
Step 2: Apply to StatefulSet pod cache-2
Pod cache-2 likely has a finalizer preventing its deletion, causing the block.
Final Answer:
Pod cache-2 has a finalizer preventing deletion. -> Option A
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
Step 1: Understand podManagementPolicy in StatefulSets
By default, StatefulSets create pods sequentially. Setting podManagementPolicy to Parallel allows parallel pod creation.
Step 2: Identify correct field to change
Changing podManagementPolicy to Parallel in the spec enables parallel pod startup.
Final Answer:
Set podManagementPolicy: Parallel in the StatefulSet spec. -> Option C
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 unknownstrongwhile earning +1 Grip toward your certificate.