CodeBlanks Logo Icon CodeBlanks

Summary - StatefulSet

Combine YAML and volumeClaimTemplate to write a StatefulSet.

Summary: StatefulSet

In this group:

  • A StatefulSet is for stateful applications: stable ordinal names, stable network names, persistent storage per pod.
  • serviceName points at a headless service (clusterIP: None); pod DNS is db-0.db.
  • volumeClaimTemplates creates a PVC (data-db-0) for each pod; PVCs remain when the pod and StatefulSet are deleted.
  • OrderedReady starts in order and scales down in reverse; Parallel starts without waiting.
  • Updates proceed in reverse; partition enables canary updates.
kubectl get sts,pvc -l app=db
kubectl scale sts db --replicas=5

Practice The headless service

Write a Service YAML: name db; clusterIP: None; selector app: db; port 5432.

Starter code

terminal
# buraya yaz

Sample solution

terminal
apiVersion: v1
kind: Service
metadata:
  name: db
spec:
  clusterIP: None
  selector:
    app: db
  ports:
    - port: 5432

Expected output

service/db created
NAME   TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)    AGE
db     ClusterIP   None         <none>        5432/TCP   1s

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 What is the reason to choose a Deployment instead of a StatefulSet for a web API?

  • A Deployment needs less YAML
  • Replicas are interchangeable; no stable identity or per-pod disk is needed Correct answer
  • StatefulSets are slow
  • StatefulSets can't scale

Explanation: Stateless applications update faster and more flexibly with a Deployment.

2 Scale the StatefulSet.

Question
kubectl scale ____ db --replicas=5
Answer
kubectl scale sts db --replicas=5

Explanation: A StatefulSet is also scaled with scale.

3 Match.

Term Meaning
StatefulSet Stateful
Deployment Stateless
headless Pod DNS records
partition Canary

All lessons in Advanced