Day 3: Choosing the Right Workload
Goal: You can tell from the task whether a Deployment, DaemonSet, StatefulSet, Job or CronJob is called for, and you build each one in a few minutes. · Duration: approx. 75 min · Prerequisite: Day 2
Briefing
CKAD: Application Design and Build (20 %) – “Choose and use the right workload resource”. In practice you almost never create pods directly; a controller creates them and keeps them alive. The only question is which one:
runs continuously? ── yes ──> one per node? ── yes ──> DaemonSet
| └─ no ─> stable ID/data? ── yes ──> StatefulSet
| └─ no ─> Deployment
└─ no ──> once ──> Job · on a schedule ──> CronJob
Drill 1: Overview and decision guide
k create ns tag03
k config set-context --current --namespace=tag03
mkdir -p ~/ckad/tag03 && cd ~/ckad/tag03
k api-resources --api-group=apps
k api-resources --api-group=batchExpected:
NAME SHORTNAMES APIVERSION NAMESPACED KIND
controllerrevisions apps/v1 true ControllerRevision
daemonsets ds apps/v1 true DaemonSet
deployments deploy apps/v1 true Deployment
replicasets rs apps/v1 true ReplicaSet
statefulsets sts apps/v1 true StatefulSet
NAME SHORTNAMES APIVERSION NAMESPACED KIND
cronjobs cj batch/v1 true CronJob
jobs batch/v1 true Job
| Resource | Typical use | Pod names | k create? |
|---|---|---|---|
| Deployment | stateless API, web frontend | random | yes |
| DaemonSet | log agent, node exporter | random, 1/node | no |
| StatefulSet | database, queue, cluster software | name-0, -1 … |
no |
| Job | migration, batch, export | random | yes |
| CronJob | report, backup, cleanup | name-<time>-… |
yes |
Save your progress? Sign up for free
Drill 2: Deployment
notes-api from Day 2, three replicas.
k create deployment has no --env, so go via
the file:
k create deployment notes --image=172.16.0.126:30500/notes-api:1.0 --replicas=3 \
--port=8080 $do > notes.yaml
vim notes.yamlAdd below name: notes-api:
env:
- name: NOTES_API_KEY
value: lab-key-123k apply -f notes.yaml
k rollout status deployment/notes
k get rs,pods -o wideExpected (shortened):
deployment "notes" successfully rolled out
NAME DESIRED CURRENT READY AGE
replicaset.apps/notes-7b6866d9dd 3 3 3 4s
NAME READY STATUS RESTARTS AGE IP NODE
pod/notes-7b6866d9dd-97tnm 1/1 Running 0 4s 192.168.235.142 worker1
pod/notes-7b6866d9dd-d8l8h 1/1 Running 0 4s 192.168.235.139 worker1
pod/notes-7b6866d9dd-rj5gb 1/1 Running 0 4s 192.168.189.112 worker2
A pod gets lost – the ReplicaSet replaces it immediately:
k delete pod notes-7b6866d9dd-97tnm
k get pods -l app=notesExpected:
pod "notes-7b6866d9dd-97tnm" deleted from tag03 namespace
NAME READY STATUS RESTARTS AGE
notes-7b6866d9dd-d8l8h 1/1 Running 0 8s
notes-7b6866d9dd-fqrws 0/1 ContainerCreating 0 3s
notes-7b6866d9dd-rj5gb 1/1 Running 0 8s
Tip: Had you run
k create deploymentfirst and thenk set env, there would be two rollouts – with pods briefly crashing without an API key in between.
Save your progress? Sign up for free
Drill 3: DaemonSet
There is no k create for DaemonSets. The exam way:
generate Deployment YAML, change kind, delete
replicas, strategy and
status.
k create deployment node-agent --image=busybox:1.37 $do -- sleep infinity > ds.yaml
sed -i 's/^kind: Deployment/kind: DaemonSet/' ds.yaml
k apply -f ds.yamlExpected (shortened):
Error from server (BadRequest): error when creating "ds.yaml": DaemonSet in version "v1"
cannot be handled as a DaemonSet: strict decoding error: unknown field "spec.replicas",
unknown field "spec.strategy"
So delete the replicas, strategy and
status lines with vim ds.yaml. Afterwards the
file looks like this:
apiVersion: apps/v1
kind: DaemonSet
metadata:
labels:
app: node-agent
name: node-agent
spec:
selector:
matchLabels:
app: node-agent
template:
metadata:
labels:
app: node-agent
spec:
containers:
- command:
- sleep
- infinity
image: busybox:1.37
name: busyboxk apply -f ds.yaml
k get ds node-agent
k get pods -l app=node-agent -o custom-columns=NAME:.metadata.name,NODE:.spec.nodeNameExpected:
daemonset.apps/node-agent created
NAME DESIRED CURRENT READY UP-TO-DATE AVAILABLE NODE SELECTOR AGE
node-agent 2 2 2 2 2 <none> 8s
NAME NODE
node-agent-m976s worker1
node-agent-xdvk6 worker2
Why only two? The control plane node has a taint:
k describe node cp1 | grep TaintsExpected:
Taints: node-role.kubernetes.io/control-plane:NoSchedule
If the agent should run there too, add under
template.spec in ds.yaml:
tolerations:
- key: node-role.kubernetes.io/control-plane
operator: Exists
effect: NoSchedulek apply -f ds.yaml
k rollout status ds/node-agent
k get ds node-agentExpected (shortened):
daemon set "node-agent" successfully rolled out
NAME DESIRED CURRENT READY UP-TO-DATE AVAILABLE NODE SELECTOR AGE
node-agent 3 3 3 3 3 <none> 89s
Save your progress? Sign up for free
Drill 4: StatefulSet with a headless Service
A StatefulSet needs a headless Service (clusterIP: None)
for stable DNS names:
k create service clusterip db --clusterip=None --tcp=80:80Via volumeClaimTemplates, each pod gets its own PVC from
the default StorageClass local-path:
cat > sts.yaml <<'EOF'
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: db
spec:
serviceName: db
replicas: 2
selector:
matchLabels:
app: db
template:
metadata:
labels:
app: db
spec:
containers:
- name: web
image: nginx:1.29
ports:
- containerPort: 80
volumeMounts:
- name: data
mountPath: /usr/share/nginx/html
volumeClaimTemplates:
- metadata:
name: data
spec:
accessModes: ["ReadWriteOnce"]
resources:
requests:
storage: 100Mi
EOF
k apply -f sts.yaml
k get pods -l app=db -wExpected (shortened, stop with Ctrl+C):
NAME READY STATUS RESTARTS AGE
db-0 0/1 Pending 0 0s
db-0 0/1 ContainerCreating 0 6s
db-0 1/1 Running 0 8s
db-1 0/1 Pending 0 0s
db-1 0/1 ContainerCreating 0 7s
db-1 only starts once db-0 is running. The
PVCs are named <template>-<pod>:
k get pvcExpected (shortened):
NAME STATUS VOLUME CAPACITY ACCESS MODES
data-db-0 Bound pvc-136e1c71-ec46-4556-8ff9-29fe94443ca3 100Mi RWO
data-db-1 Bound pvc-e53db033-9fa8-4cd1-bc15-1984f4b96b59 100Mi RWO
Save your progress? Sign up for free
Drill 5: Identity and data persist
k exec db-0 -- sh -c 'echo "ich bin $(hostname)" > /usr/share/nginx/html/index.html'
k exec db-1 -- sh -c 'echo "ich bin $(hostname)" > /usr/share/nginx/html/index.html'
k run client --image=busybox:1.37 -- sleep 3600
k exec client -- wget -qO- db-1.db
k exec client -- nslookup db.tag03.svc.cluster.localExpected (shortened):
ich bin db-1
Name: db.tag03.svc.cluster.local
Address: 192.168.235.149
Name: db.tag03.svc.cluster.local
Address: 192.168.189.125
The headless Service returns the pod IPs directly, and
db-1.db always reaches the same pod. Now delete
db-0:
k delete pod db-0
k get pod db-0 -o wide
k exec db-0 -- cat /usr/share/nginx/html/index.htmlExpected (shortened):
pod "db-0" deleted from tag03 namespace
NAME READY STATUS RESTARTS AGE IP NODE
db-0 1/1 Running 0 2s 192.168.189.66 worker2
ich bin db-0
Same name, new IP, same data. When you scale down, the PVCs stay:
k scale sts db --replicas=1
k get pods,pvc -l app=dbExpected (shortened):
statefulset.apps/db scaled
NAME READY STATUS RESTARTS AGE
pod/db-0 1/1 Running 0 8s
NAME STATUS VOLUME
persistentvolumeclaim/data-db-0 Bound pvc-136e1c71-...
persistentvolumeclaim/data-db-1 Bound pvc-e53db033-...
Save your progress? Sign up for free
Drill 6: Job with completions and parallelism
k create job batch --image=busybox:1.37 $do \
-- sh -c 'echo Teil fertig auf $(hostname); sleep 3' > job.yaml
vim job.yamlAdd directly below the first spec::
completions: 6
parallelism: 2
backoffLimit: 2k apply -f job.yaml
k get pods -l job-name=batch
k wait --for=condition=complete job/batch --timeout=120s
k get job batchExpected (shortened):
NAME READY STATUS RESTARTS AGE
batch-smhxk 0/1 Completed 0 7s
batch-xx5jx 1/1 Running 0 7s
job.batch/batch condition met
NAME STATUS COMPLETIONS DURATION AGE
batch Complete 6/6 26s 27s
Six successful pods, never more than two at a time.
k create job sets restartPolicy: Never; for
Jobs, only Never and OnFailure are
allowed.
Save your progress? Sign up for free
Drill 7: When Jobs fail
backoffLimit limits failed attempts,
activeDeadlineSeconds the total runtime:
k apply -f - <<'EOF'
apiVersion: batch/v1
kind: Job
metadata:
name: fail
spec:
backoffLimit: 2
template:
spec:
restartPolicy: Never
containers:
- name: fail
image: busybox:1.37
command: ["sh", "-c", "echo versuche; exit 1"]
---
apiVersion: batch/v1
kind: Job
metadata:
name: slow
spec:
activeDeadlineSeconds: 10
template:
spec:
restartPolicy: Never
containers:
- name: slow
image: busybox:1.37
command: ["sleep", "60"]
EOFAfter one minute:
k get jobs fail slow
k get pods -l job-name=fail
k describe job fail | tail -1
k describe job slow | tail -1Expected (shortened):
NAME STATUS COMPLETIONS DURATION AGE
fail Failed 0/1 51s 51s
slow Failed 0/1 50s 50s
NAME READY STATUS RESTARTS AGE
fail-2t2np 0/1 Error 0 51s
fail-kz7pd 0/1 Error 0 38s
fail-vgsts 0/1 Error 0 16s
Warning BackoffLimitExceeded 12s job-controller Job has reached the specified
backoff limit
Warning DeadlineExceeded 9s job-controller Job was active longer than specified
deadline
Three pods: one attempt plus two retries, with a growing pause in
between. The controller deleted the pod of slow once the
deadline had passed.
Save your progress? Sign up for free
Drill 8: CronJob
k create cronjob report --image=busybox:1.37 --schedule="*/1 * * * *" $do \
-- sh -c 'date; sleep 90' > cj.yaml
vim cj.yamlAdd below schedule: (at the same level):
concurrencyPolicy: Forbid
successfulJobsHistoryLimit: 2
failedJobsHistoryLimit: 1k apply -f cj.yaml
k create job report-manual --from=cronjob/reportA run takes 90 s, the interval is 60 s. After a good two minutes:
k get cj report
k get events --field-selector involvedObject.name=report --sort-by=.lastTimestampExpected (shortened):
NAME SCHEDULE TIMEZONE SUSPEND ACTIVE LAST SCHEDULE AGE
report */1 * * * * <none> False 1 56s 2m37s
REASON OBJECT MESSAGE
SuccessfulCreate cronjob/report Created job report-29916944
UnexpectedJob cronjob/report Saw a job that the controller did not create or
forgot: report-manual
JobAlreadyActive cronjob/report Not starting job because prior execution is running
and concurrency policy is Forbid
SawCompletedJob cronjob/report Saw completed job: report-29916944, condition: ...
Forbid skips the run as long as the previous one is
still running; Replace would cancel it, Allow
(the default) would start in parallel. The CronJob does not count the
manual Job as one of its own. The history: the controller deletes
successful Jobs beyond two (event SuccessfulDelete). To
pause it:
k patch cronjob report -p '{"spec":{"suspend":true}}'
k get cj reportExpected:
cronjob.batch/report patched
NAME SCHEDULE TIMEZONE SUSPEND ACTIVE LAST SCHEDULE AGE
report */1 * * * * <none> True 1 71s 2m52s
Save your progress? Sign up for free
Exercise: Speed Drills
- Job
hellowithbusybox:1.37that runsecho hallosuccessfully three times in a row. Target time: 60 s - CronJob
cleanup,echo wegevery 15 minutes, suspended right away. Target time: 45 s - Run the CronJob
reportonce by hand right now as Jobreport-now. Target time: 15 s - Scale the Deployment
notesto one replica. Target time: 10 s
Solution
k create job hello --image=busybox:1.37 $do -- echo hallo > hello.yaml
sed -i 's/^spec:$/spec:\n completions: 3/' hello.yaml
k apply -f hello.yaml
k create cronjob cleanup --image=busybox:1.37 --schedule="*/15 * * * *" -- echo weg
k patch cronjob cleanup -p '{"spec":{"suspend":true}}'
k create job report-now --from=cronjob/report
k scale deployment notes --replicas=1Expected (shortened):
job.batch/hello created
cronjob.batch/cleanup created
cronjob.batch/cleanup patched
job.batch/report-now created
deployment.apps/notes scaled
Save your progress? Sign up for free
Checkpoint
k get job hello -o jsonpath='{.status.succeeded}'Expected: 3
k get ds node-agent -o jsonpath='{.status.numberReady}'Expected: 3
k get pvc -l app=dbExpected: data-db-0 and
data-db-1, both Bound – the StatefulSet
controller copies the selector labels to the PVCs.
k get cj -o custom-columns=NAME:.metadata.name,SUSPEND:.spec.suspendExpected: cleanup and
report, both true.
Challenge
A Job parts should process four parts in parallel; each
pod prints its part number (0 to 3).
Hint: completionMode: Indexed sets the
variable JOB_COMPLETION_INDEX in each pod.
Solution
k apply -f - <<'EOF'
apiVersion: batch/v1
kind: Job
metadata:
name: parts
spec:
completions: 4
parallelism: 4
completionMode: Indexed
template:
spec:
restartPolicy: Never
containers:
- name: part
image: busybox:1.37
command: ["sh", "-c", "echo Teil $JOB_COMPLETION_INDEX"]
EOF
k wait --for=condition=complete job/parts --timeout=90s
for p in $(k get pods -l job-name=parts -o name); do k logs $p; done | sortExpected:
job.batch/parts created
job.batch/parts condition met
Teil 0
Teil 1
Teil 2
Teil 3
Clean up:
k config set-context --current --namespace=default
k delete ns tag03The PVCs, and with them the local-path volumes, disappear together with the namespace.
Save your progress? Sign up for free
Exam Tip
- DaemonSets and StatefulSets cannot be created with
k create– generate Deployment YAML and rework it. For DaemonSets, deletereplicasandstrategy, otherwise the API reports “strict decoding error: unknown field”. - Job fields (
completions,parallelism,backoffLimit,activeDeadlineSeconds) belong underspec, not underspec.template.spec.k explain job.spechelps. - A CronJob that should run “right now”:
k create job <name> --from=cronjob/<cj>.
Enterprise Note
- Databases as StatefulSets are usually run via an operator (CloudNativePG, Strimzi) that handles backups, failover and upgrades.
- DaemonSets for logs and monitoring are provided by the platform; application teams rarely build them themselves.
- CronJobs get
timeZone,startingDeadlineSecondsand alerts on failed Jobs – a silent backup job that has been failing for weeks is a classic.