Day 4: Workloads at Exam Speed
Goal: You roll Deployments out and back, configure applications and pick the right workload type – all imperatively and fast. · Duration: approx. 60 min · Prerequisite: Day 2
Briefing
CKA: Workloads and Scheduling (15 %) – rolling updates and rollbacks, ConfigMaps and Secrets, self-healing workloads. You know the basics; today speed is what counts.
| Type | Purpose | Create with |
|---|---|---|
| Deployment | stateless apps | k create deployment |
| DaemonSet | one Pod per node | rework Deployment YAML |
| StatefulSet | stable identity, own storage | YAML |
| Job / CronJob | one-off / scheduled | k create job / cronjob |
mkdir -p ~/cka/tag04 && cd ~/cka/tag04
k create ns tag04
k config set-context --current --namespace=tag04Drill 1: Rolling Update with a Change Cause
k create deployment shop --image=nginx:1.28 --replicas=3
k rollout status deployment shop
k set image deployment/shop nginx=nginx:1.29
k annotate deployment shop kubernetes.io/change-cause="nginx 1.29"
k rollout status deployment shop
k rollout history deployment shopExpected (abridged):
deployment.apps/shop image updated
Waiting for deployment "shop" rollout to finish: 1 out of 3 new replicas ...
deployment "shop" successfully rolled out
REVISION CHANGE-CAUSE
1 <none>
2 nginx 1.29
The container is named nginx because
k create deployment names it after the image. You have to
specify that name with k set image.
Save your progress? Sign up for free
Drill 2: A Broken Rollout and the Way Back
k set image deployment/shop nginx=nginx:9.99
k annotate deployment shop kubernetes.io/change-cause="kaputtes Image" --overwrite
k get pods -l app=shopExpected (after about 20 s):
NAME READY STATUS RESTARTS AGE
shop-59456556cd-ktjpx 1/1 Running 0 25s
shop-59456556cd-l2bdq 1/1 Running 0 26s
shop-59456556cd-mzxp2 1/1 Running 0 23s
shop-6fbc884fbc-jzvlq 0/1 ImagePullBackOff 0 20s
The old Pods keep running – the rolling update stalls, but the app is not down.
k rollout undo deployment shop
k rollout status deployment shop
k rollout history deployment shop
k get deployment shop -o jsonpath='{.spec.template.spec.containers[0].image}{"\n"}'Expected:
deployment.apps/shop rolled back
deployment "shop" successfully rolled out
REVISION CHANGE-CAUSE
1 <none>
3 kaputtes Image
4 nginx 1.29
nginx:1.29
Revision 2 has disappeared: a rollback copies the old revision as the
new revision 4. To go back to a specific revision, use
k rollout undo deployment shop --to-revision=1.
Save your progress? Sign up for free
Drill 3: Mounting a ConfigMap and a Secret
k create configmap app-config --from-literal=MODE=prod --from-literal=LOG_LEVEL=info
k create secret generic db-cred --from-literal=DB_PASSWORD=lab-password
k run app --image=busybox:1.37 $do --command -- sh -c 'sleep 3600' > app.yamlExtend the container part in app.yaml so that it looks
like this:
apiVersion: v1
kind: Pod
metadata:
name: app
spec:
containers:
- name: app
image: busybox:1.37
command: ["sh", "-c", "sleep 3600"]
envFrom:
- configMapRef:
name: app-config
env:
- name: DB_PASSWORD
valueFrom:
secretKeyRef:
name: db-cred
key: DB_PASSWORD
volumeMounts:
- name: cfg
mountPath: /etc/app
volumes:
- name: cfg
configMap:
name: app-configk apply -f app.yaml
k wait --for=condition=Ready pod/app
k exec app -- env | grep -E 'MODE|LOG_LEVEL|DB_PASSWORD' | sort
k exec app -- ls /etc/appExpected:
DB_PASSWORD=lab-password
LOG_LEVEL=info
MODE=prod
LOG_LEVEL
MODE
envFrom imports all keys as variables, and the volume
stores each key as a file. Secrets are only base64-encoded, not
encrypted:
k get secret db-cred -o jsonpath='{.data.DB_PASSWORD}' | base64 -d; echoExpected: lab-password
Save your progress? Sign up for free
Drill 4: DaemonSet from a Deployment YAML
There is no k create daemonset. The fast way: generate a
Deployment YAML and rework it.
k create deployment logger --image=busybox:1.37 $do -- sh -c 'sleep 3600' > ds.yaml
sed -i -e 's/^kind: Deployment/kind: DaemonSet/' -e '/replicas:/d' \
-e '/strategy:/d' -e '/^status:/d' ds.yaml
k apply -f ds.yaml
k get ds logger
k get pods -l app=logger -o custom-columns=NAME:.metadata.name,NODE:.spec.nodeNameExpected:
daemonset.apps/logger created
NAME DESIRED CURRENT READY UP-TO-DATE AVAILABLE NODE SELECTOR AGE
logger 2 2 2 2 2 <none> 15s
NAME NODE
logger-6gdgk worker1
logger-k8fnp worker2
Only two Pods: cp1 carries a taint (Day 5 and the
Challenge).
Save your progress? Sign up for free
Drill 5: StatefulSet with a Headless Service
cat <<'EOF' | k apply -f -
apiVersion: v1
kind: Service
metadata:
name: db
spec:
clusterIP: None
selector:
app: db
ports:
- port: 80
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: db
spec:
serviceName: db
replicas: 3
selector:
matchLabels:
app: db
template:
metadata:
labels:
app: db
spec:
containers:
- name: nginx
image: nginx:1.29
EOF
k rollout status statefulset db
k get pods -l app=dbExpected:
partitioned roll out complete: 3 new pods have been updated...
NAME READY STATUS RESTARTS AGE
db-0 1/1 Running 0 6s
db-1 1/1 Running 0 4s
db-2 1/1 Running 0 2s
Each Pod has a stable DNS name through the headless Service:
k run dnstest --image=busybox:1.37 --rm -it --restart=Never -- \
nslookup db-1.db.tag04.svc.cluster.localExpected (abridged):
Name: db-1.db.tag04.svc.cluster.local
Address: 192.168.235.143
Delete db-1 – it comes back with the same name:
k delete pod db-1
k wait --for=condition=Ready pod/db-1
k get pods -l app=dbExpected:
NAME READY STATUS RESTARTS AGE
db-0 1/1 Running 0 15s
db-1 1/1 Running 0 1s
db-2 1/1 Running 0 11s
Save your progress? Sign up for free
Drill 6: Job and CronJob
k create job pi --image=busybox:1.37 -- sh -c 'echo 3.14159'
k wait --for=condition=Complete job/pi
k logs job/pi
k create cronjob tick --image=busybox:1.37 --schedule='*/1 * * * *' -- dateExpected:
job.batch/pi created
job.batch/pi condition met
3.14159
cronjob.batch/tick created
After a little over a minute:
k get jobsExpected:
NAME STATUS COMPLETIONS DURATION AGE
pi Complete 1/1 4s 56s
tick-29915854 Complete 1/1 5s 33s
Save your progress? Sign up for free
Drill 7: Self-Healing with Probes
cat <<'EOF' | k apply -f -
apiVersion: v1
kind: Pod
metadata:
name: healthy
spec:
containers:
- name: app
image: busybox:1.37
command: ["sh", "-c", "touch /tmp/ok; sleep 3600"]
livenessProbe:
exec:
command: ["test", "-f", "/tmp/ok"]
periodSeconds: 5
failureThreshold: 2
readinessProbe:
exec:
command: ["test", "-f", "/tmp/ok"]
periodSeconds: 5
EOF
k wait --for=condition=Ready pod/healthy
k exec healthy -- rm /tmp/ok
k get events --field-selector involvedObject.name=healthy --sort-by=.lastTimestampExpected (after about 20 s, abridged):
26s Normal Started pod/healthy Container started
17s Warning Unhealthy pod/healthy Liveness probe failed:
17s Normal Killing pod/healthy Container app failed liveness probe, ...
The restart creates /tmp/ok again, and the Pod becomes
healthy:
k wait --for=condition=Ready pod/healthy --timeout=90s
k get pod healthyExpected:
NAME READY STATUS RESTARTS AGE
healthy 1/1 Running 1 (1s ago) 42s
Liveness restarts the container; readiness only takes it out of the Service.
Save your progress? Sign up for free
Exercise: Speed Drills
- Scale
shopto 5 replicas. Target time: 10 s - Trigger the CronJob
tickonce by hand right now as the Jobmanual. Target time: 20 s - Set
maxSurge: 1andmaxUnavailable: 0forshop. Target time: 60 s - Generate the YAML of a Secret
api-keywithKEY=sk-lab-1234. Target time: 20 s
Solution
k scale deployment shop --replicas=5
k create job manual --from=cronjob/tick
k patch deployment shop \
-p '{"spec":{"strategy":{"rollingUpdate":{"maxSurge":1,"maxUnavailable":0}}}}'
k create secret generic api-key --from-literal=KEY=sk-lab-1234 $doExpected (excerpt):
deployment.apps/shop scaled
job.batch/manual created
deployment.apps/shop patched
apiVersion: v1
data:
KEY: c2stbGFiLTEyMzQ=
kind: Secret
Save your progress? Sign up for free
Checkpoint
k get deployment shop -o jsonpath='{.spec.strategy.rollingUpdate}{"\n"}'Expected:
{"maxSurge":1,"maxUnavailable":0}
k get statefulset dbExpected: READY shows
3/3.
k exec app -- cat /etc/app/MODE; echoExpected: prod
Challenge
The DaemonSet logger should also run on
cp1.
Hint: The Pod has to tolerate the control plane’s taint.
Solution
cat > tol.yaml <<'EOF'
spec:
template:
spec:
tolerations:
- key: node-role.kubernetes.io/control-plane
operator: Exists
effect: NoSchedule
EOF
k patch ds logger --patch-file tol.yaml
k rollout status ds logger
k get pods -l app=logger -o custom-columns=NAME:.metadata.name,NODE:.spec.nodeNameExpected:
daemonset.apps/logger patched
daemon set "logger" successfully rolled out
NAME NODE
logger-2z6l7 cp1
logger-mgntl worker1
logger-vnmnj worker2
Clean up:
k config set-context --current --namespace=default
k delete ns tag04Save your progress? Sign up for free
Exam Tip
k set imageneeds the container name, not the Deployment name. Look it up withk get deploy shop -o jsonpath='{.spec.template.spec.containers[*].name}'.- DaemonSet: generate Deployment YAML, change
kind, deletereplicasandstrategy. - Remember these docs pages: “Configure a Pod to Use a ConfigMap”, “Configure Liveness, Readiness and Startup Probes”.
Enterprise Note
- In production, rollouts run through GitOps (Argo CD, Flux);
rollout undois the emergency lever, and afterwards the state belongs in Git. - No plaintext Secrets in Git – use External Secrets, Sealed Secrets or OpenBao.
- For databases, use an operator (CloudNativePG) instead of your own StatefulSet.
- Use probes with care: a liveness probe that is too strict restarts Pods under load and makes outages worse.