Exercise 1: Stateful Applications
Task 1 - Understanding StatefulSets
StatefulSets are Kubernetes controllers that manage the deployment and scaling of a set of pods, providing guarantees about the ordering and uniqueness of these pods.
Key characteristics of StatefulSets:
- Stable, unique network identifiers
- Stable, persistent storage
- Ordered, graceful deployment and scaling
- Ordered, automated rolling updates
Task 2 - Deploy MongoDB Using StatefulSets
Create the MongoDB StatefulSet manifest:
$statefulsetYaml = @" apiVersion: v1 kind: Service metadata: name: mongodb-service labels: app: mongodb spec: clusterIP: None selector: app: mongodb ports: - port: 27017 targetPort: 27017 --- apiVersion: apps/v1 kind: StatefulSet metadata: name: mongodb spec: serviceName: "mongodb-service" replicas: 3 selector: matchLabels: app: mongodb template: metadata: labels: app: mongodb spec: containers: - name: mongodb image: k8sonazureworkshoppublic.azurecr.io/mongo:4.4 ports: - containerPort: 27017 name: mongodb resources: requests: memory: "256Mi" cpu: "250m" limits: memory: "512Mi" cpu: "500m"cat << EOF | kubectl apply -f - apiVersion: v1 kind: Service metadata: name: mongodb-service labels: app: mongodb spec: clusterIP: None selector: app: mongodb ports: - port: 27017 targetPort: 27017 --- apiVersion: apps/v1 kind: StatefulSet metadata: name: mongodb spec: serviceName: "mongodb-service" replicas: 3 selector: matchLabels: app: mongodb template: metadata: labels: app: mongodb spec: containers: - name: mongodb image: k8sonazureworkshoppublic.azurecr.io/mongo:4.4 ports: - containerPort: 27017 name: mongodb resources: requests: memory: "256Mi" cpu: "250m" limits: memory: "512Mi" cpu: "500m" EOFThis manifest defines:
- A headless service (
clusterIP: None) - A StatefulSet with 3 replicas
- Resource
requests andlimits
- A headless service (
Verify the StatefulSet deployment:
kubectl get statefulsetkubectl get statefulsetYou should see output similar to:
You may need to wait a few minutes for all pods to start.
Task 3 - Observe StatefulSet Behavior
List the pods created by the StatefulSet:
kubectl get pods -l app=mongodbkubectl get pods -l app=mongodbNotice the predictable pod names:
mongodb-0,mongodb-1,mongodb-2
Task 4 - Test StatefulSet Ordering
Delete a pod to observe the recreation behaviour:
kubectl delete pod mongodb-1kubectl delete pod mongodb-1Watch the pod recreation:
kubectl get pods -l app=mongodb --watchkubectl get pods -l app=mongodb --watchNotice that Kubernetes recreates the exact same pod with the same name, maintaining the StatefulSet’s guarantee of identity.
Task 5 - Scale the StatefulSet
Scale the StatefulSet to 4 replicas:
kubectl scale statefulset mongodb --replicas=4kubectl scale statefulset mongodb --replicas=4Observe the scaling process:
kubectl get pods -l app=mongodbkubectl get pods -l app=mongodbNotice that pods are created one at a time, in order.
Scale back down to 2 replicas:
kubectl scale statefulset mongodb --replicas=2kubectl scale statefulset mongodb --replicas=2Observe the termination order:
kubectl get pods -l app=mongodbkubectl get pods -l app=mongodbNotice that pods are terminated in reverse order (highest to lowest index).
Task 6 - Clean Up
Delete the StatefulSet and associated resources:
kubectl delete statefulset mongodb kubectl delete service mongodb-service
