Skip to content
devops.academy

Exercises — Module 7: Kubernetes

Environment: kind cluster (free, disposable — kind delete cluster and start fresh anytime). Install kubectl, kind, helm. Alias k=kubectl and enable shell completion NOW — you'll type kubectl 500 times this module. All YAML goes in a Git repo.


Part A — Warm-Up Drills

Drill 1 — Cluster orientation. kind create cluster. Then answer with commands: how many nodes? what version? what pods already run in kube-system and which architecture component is each (match to the module's diagram)? ✓ You can point at api-server, etcd, scheduler, controller-manager, kubelet-managed pods.

Drill 2 — Pod lifecycle reps (until fluid). kubectl run tmp --image=nginx → get → describe (read the Events!) → logs → exec sh → delete. Then the same via a YAML file you write from memory (apiVersion/kind/metadata/spec skeleton). ✓ You can write a minimal pod manifest without looking.

Drill 3 — The describe habit. Create a pod with image nginx:doesnotexist. Don't google — read kubectl describe Events bottom-up. Name the status. Fix by editing image. Repeat with a container command exit 1 — name THAT status, read logs --previous. ✓ ImagePullBackOff and CrashLoopBackOff diagnosed from Events/logs alone.

Drill 4 — Label selector fluency. Create 3 pods labeled app=a, app=b, app=a,tier=web. Practice: get by -l app=a; by -l 'app in (a,b)'; by -l app=a,tier=web; delete by label. ✓ Selectors feel like SQL WHERE clauses now — they run everything in K8s.

Drill 5 — Namespace reps. Create ns dev and prod; run a pod in each; list pods per-ns and across all (-A); set your context default ns to dev (kubectl config set-context --current --namespace=dev); try to reach a service across namespaces by its full DNS name. ✓ You know why kubectl get pods "loses" pods (wrong ns) — top beginner confusion, defused.


Part B — Guided Labs

Lab 1 — Deployment Mechanics (90 min)

  1. Write a Deployment for your Module 5 app: 3 replicas, resource requests/limits, readiness probe on /health. Apply.
  2. Watch the hierarchy: kubectl get deploy,rs,pods — kill a pod (delete pod), watch the ReplicaSet replace it within seconds. Sit with this — it's the reconciliation loop live.
  3. Scale to 5 imperatively; then set 2 in YAML and apply — which wins, and what does that teach about declarative config?
  4. Rolling update: set image to a v2 tag while running kubectl get pods -w in a second terminal AND a curl loop against the service in a third. Narrate what you see: surge pod, readiness gate, old pod termination.
  5. rollout history, then rollout undo. Then break on purpose: set a nonexistent image — observe the rollout STALL (old pods keep serving!); undo. ✓ You witnessed why maxUnavailable:0 + readiness = zero downtime, and that a bad rollout doesn't take down the old version.

Lab 2 — Services & the Empty-Endpoints Classic (60 min)

  1. ClusterIP Service for the app. From a throwaway pod (kubectl run tmp -it --rm --image=busybox -- sh): wget the service by name; by FQDN; nslookup it.
  2. The classic: change the Service selector to a typo. Curl fails. Diagnose with kubectl get endpoints (empty!) — fix — endpoints repopulate.
  3. Second variant: correct selector, but break the readiness probe path. Endpoints empty again — same symptom, different cause. Write the 2-step triage: endpoints empty → check selector, then check readiness.
  4. Expose as NodePort; curl it via the kind node's IP (docker inspect the kind node or use kind's port mappings). Note why NodePort is demo-grade. ✓ The triage note — this is the single most common K8s networking ticket.

Lab 3 — Config, Secrets & the 12-Factor Wiring (45 min)

  1. Move your app's config to a ConfigMap (envFrom) and DB password to a Secret (env valueFrom). Redeploy; verify inside the pod with exec env.
  2. Decode the Secret from the API (get secret -o jsonpath | base64 -d) — feel how "not really secret" it is; write the two hardening options from memory.
  3. Change a ConfigMap value; is the running pod's env updated? (No.) Trigger a rollout restart and verify. Note the operational implication.
  4. Mount the same ConfigMap as a file instead; observe that FILE mounts do update in place (eventually).

Lab 4 — Stateful: Postgres with PVC (60 min)

  1. StatefulSet (1 replica) for postgres:16 + PVC via kind's default StorageClass + ClusterIP service db.
  2. Point your app Deployment at db:5432 via Secret/ConfigMap. Write rows through the app.
  3. kubectl delete pod db-0 — watch it return with the SAME name and SAME data. Then delete the StatefulSet but not the PVC; recreate; data still there. ✓ You can articulate pod-ephemeral vs PVC-persistent and why StatefulSet (stable identity) fits databases.

Lab 5 — Ingress + HPA under Load (90 min)

  1. Install nginx-ingress via Helm (kind needs the extraPortMappings config from kind's docs — set that up).
  2. Ingress routing app.localtest.me → your service (localtest.me resolves to 127.0.0.1 — no /etc/hosts needed). Curl through it.
  3. Install metrics-server (kind needs the --kubelet-insecure-tls patch). kubectl top pods works.
  4. HPA: min 2, max 8, CPU target 60%. Load with ab -n 20000 -c 100 through the ingress while watching kubectl get hpa -w and pods scale out, then (after load stops) scale in. ✓ Screenshot/record of replicas climbing and settling — autoscaling witnessed end to end.

Lab 6 — RBAC & NetworkPolicy (60 min)

  1. Create a ServiceAccount viewer; Role allowing get/list pods+logs in dev; RoleBinding. Test with kubectl auth can-i get pods --as=system:serviceaccount:dev:viewer -n dev (yes) and delete deploy (no) and anything in prod (no).
  2. Generate a kubeconfig/token for it and actually run a kubectl get pods as viewer.
  3. NetworkPolicy: default-deny ingress in the namespace, then allow only app→db:5432. Verify: app still writes rows; a busybox pod's nc -zv db 5432 now fails. (kind's default CNI enforces policies via kindnet? If not, create the cluster with Calico — follow kind docs.) ✓ can-i outputs and the nc failure captured in your notes.

Lab 7 — Helm-ify Everything (90 min)

  1. helm create myapp; strip it down; port your Deployment/Service/Ingress/HPA into templates with values for image tag, replicas, host.
  2. helm template to inspect output; helm install to dev ns; helm upgrade --set image.tag=v2; helm history; helm rollback.
  3. values-dev.yaml vs values-prod.yaml (replicas 1 vs 3, different host); install both into their namespaces from ONE chart.
  4. Install something big from the public registry (e.g., bitnami/postgresql) and skim what a production-grade chart parameterizes. ✓ One chart, two environments, versioned rollbacks — the raw-YAML pain is gone and you know exactly what Helm replaced.

Part C — Challenges

C1 — Scheduling puzzle. Label one kind worker node disk=fast (create a multi-node kind cluster via config). Force a pod there with nodeSelector; then taint the node and watch new pods avoid it; add a toleration to get back on. Verify placements with -o wide.

C2 — CronJob + Job. A CronJob every 2 minutes that runs a pg_dump of your lab DB to an emptyDir (or PVC), plus a one-off Job that prints row counts. Inspect completed pods' logs; set history limits.

C3 — Canary by hand. Two Deployments (v1 ×3 pods, v2 ×1 pod) sharing ONE Service selector label. Curl 20 times — roughly 75/25 split? Shift the ratio. Write how Ingress/Gateway API would do this more precisely.

C4 — Debug toolbox pod. Build a personal debug image (curl, dig, nc, psql) and a one-liner alias to drop it into any namespace. You'll use this forever.

C5 — Resource starvation. Set a pod's memory limit to 16Mi (too small) — observe OOMKilled restarts in describe. Then request more CPU than the cluster has — observe Pending + the Events explaining why. Two different failure grammars, learned.

C6 — Read a real manifest cold. kubectl get deploy -n ingress-nginx -o yaml (the controller you installed) — annotate 15 fields you now recognize and 3 you don't; look those 3 up.


Part D — Break & Fix Scenarios (create, walk away, come back "on call")

D1. Wrong image tag on a Deployment update. Symptom: rollout stuck. Diagnose (describe pod, rollout status), fix (undo or correct).

D2. Readiness path typo'd to /healthz (app serves /health). Symptom: all pods Running but 0/1 Ready, service dead, endpoints empty.

D3. Secret deleted while pods reference it. New pods: CreateContainerConfigError. Read describe, recreate secret.

D4. PVC pending forever: request a StorageClass that doesn't exist. Read the PVC's Events; fix.

D5. Quota jail: apply a ResourceQuota (2 pods max) to a namespace, then scale to 5. Where does the error appear (hint: NOT the deployment status — check ReplicaSet events)? This one teaches where K8s hides errors.

D6. DNS breaks: scale coredns to 0 (-n kube-system). Watch name-based everything fail while IP connectivity works (Module 4 D2's twin, cluster edition). Restore.


Part E — Interview Rehearsal (out loud)

  1. Explain Kubernetes architecture — what runs where and what happens when a node dies.
  2. I kubectl apply a Deployment with 3 replicas. Narrate everything until pods serve traffic (apiserver→etcd→controller→scheduler→kubelet→kube-proxy).
  3. Pods are Running but the service returns errors. Full triage tree.
  4. Liveness vs readiness — and the outage caused by confusing them.
  5. Deployment vs StatefulSet vs DaemonSet — one workload each.
  6. How does a rolling update achieve zero downtime? What can still go wrong?
  7. How do you do RBAC for a read-only developer? How do you verify without their creds?
  8. What does Helm add over kubectl apply? What does rollback actually do?

Done when

Drill 3 statuses are reflexes, Lab 2's triage note exists, Lab 5's autoscale was witnessed, all six D scenarios solved from Events/logs, and E2's narration flows without pauses.