Challenge - StatefulSet
- Docker & Kubernetes course
- Advanced
- Lesson 67/70
- 5 min
Write a StatefulSet with a volumeClaimTemplate.
Challenge: a three-node cluster
You'll run Redis as a three-replica StatefulSet: each replica will have its own disk and stable DNS name; the replicas will start in parallel (they don't need to wait for each other). Then you'll test the identity and persistence: when a pod is deleted, it should come back with the same name and the same data.
redis-0.redis redis-1.redis redis-2.redis
data-redis-0 data-redis-1 data-redis-2Practice The StatefulSet
Write a StatefulSet YAML: name redis, serviceName: redis, replicas: 3, podManagementPolicy: Parallel; selector and template label app: redis; container named redis, image redis:7, mounts data at /data; a template named data in volumeClaimTemplates, ReadWriteOnce, 1Gi.
Starter code
# buraya yazSample solution
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: redis
spec:
serviceName: redis
replicas: 3
podManagementPolicy: Parallel
selector:
matchLabels:
app: redis
template:
metadata:
labels:
app: redis
spec:
containers:
- name: redis
image: redis:7
volumeMounts:
- name: data
mountPath: /data
volumeClaimTemplates:
- metadata:
name: data
spec:
accessModes: ["ReadWriteOnce"]
resources:
requests:
storage: 1GiExpected output
statefulset.apps/redis created
Practice Test persistence
Write four commands: run redis-cli set anahtar deger in redis-1; delete the redis-1 pod; list the pods labelled app=redis; run redis-cli get anahtar in redis-1.
Starter code
# buraya yazSample solution
kubectl exec redis-1 -- redis-cli set anahtar deger
kubectl delete pod redis-1
kubectl get pods -l app=redis
kubectl exec redis-1 -- redis-cli get anahtarExpected output
OK pod "redis-1" deleted NAME READY STATUS RESTARTS AGE redis-0 1/1 Running 0 10m redis-1 1/1 Running 0 6s redis-2 1/1 Running 0 10m deger
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 What is the benefit of choosing podManagementPolicy: Parallel in this scenario?
- The disks merge
- DNS turns off
- All three pods start at once without waiting; startup is faster Correct answer
- Data is shared
Explanation: Stable identities and disks are still kept; only the ordering isn't waited for.
2 Start the pods in parallel.
spec:
serviceName: redis
podManagementPolicy: ____
spec:
serviceName: redis
podManagementPolicy: Parallel
Explanation: Parallel doesn't wait for order.
3 Match.
| Term | Meaning |
|---|---|
redis-0 |
Stable name |
data-redis-0 |
Persistent disk |
redis-0.redis |
DNS name |
Parallel |
Start at once |