CKAD: Application Design and Build
20% of the CKAD exam, 4 competencies. Back to the CKAD overview.
Each competency lists what to know and the official documentation to read, then a demo you can run. Demos run on the demo lab, a throwaway kind cluster on your own machine; set it up once before you start.
Define, build and modify container images
Section titled “Define, build and modify container images”Know
- A Dockerfile or Containerfile builds layers; each instruction adds one. Multi-stage builds keep build tools out of the final image.
- Tag images explicitly; never rely on
latest.
Read
Demo: Build an image, load it into kind, change it
-
Create a namespace for this demo and make it the default for your context.
Terminal window kubectl create namespace ckad-imagekubectl config set-context --current --namespace=ckad-image -
Write a tiny image that runs as a non-root user.
mkdir -p img && cat > img/hello.sh <<'EOF'#!/bin/shecho "hello from $(hostname), version ${VERSION:-1.0}"sleep 3600EOFcat > img/Dockerfile <<'EOF'FROM registry.k8s.io/e2e-test-images/busybox:1.37.0-2COPY --chmod=755 hello.sh /hello.shENV VERSION=1.0USER 65534ENTRYPOINT ["/hello.sh"]EOFdocker build -t demo/hello:1.0 img/kind load docker-image demo/hello:1.0 -
Run it. kind can’t pull
demo/hellofrom a registry, so the pull policy must not be Always.Terminal window kubectl run hello --image=demo/hello:1.0 --image-pull-policy=IfNotPresentkubectl wait --for=condition=Ready pod/hello --timeout=60s && kubectl logs hello -
Change the image, tag it, and swap it in.
Terminal window sed -i 's/VERSION=1.0/VERSION=1.1/' img/Dockerfiledocker build -t demo/hello:1.1 img/ && kind load docker-image demo/hello:1.1kubectl set image pod/hello hello=demo/hello:1.1sleep 10; kubectl logs hellokubectl run latest --image=demo/hello:latestsleep 15; kubectl get pod latest
What you should see: The first log says version 1.0, and after set image the container restarts and says 1.1. The latest pod fails with ErrImagePull: an image tagged latest, or with no tag, defaults to imagePullPolicy: Always, so the kubelet tries a registry instead of using the loaded image.
Clean up:
rm -rf imgkubectl config set-context --current --namespace=defaultkubectl delete namespace ckad-imagePractise
- Build an image with a multi-stage Containerfile, save it with
podman saveordocker save, and inspect its layers.
Choose and use the right workload resource (Deployment, DaemonSet, CronJob, etc.)
Section titled “Choose and use the right workload resource (Deployment, DaemonSet, CronJob, etc.)”Know
- Deployment: stateless replicas. StatefulSet: stable identity and storage. DaemonSet: one pod per node. Job: run to completion. CronJob: Jobs on a schedule.
Read
Demo: Four workload types side by side
-
Create a namespace for this demo and make it the default for your context.
Terminal window kubectl create namespace ckad-workloadskubectl config set-context --current --namespace=ckad-workloads -
One of each.
Terminal window kubectl create deployment web --image=registry.k8s.io/e2e-test-images/agnhost:2.66.1 --replicas=2 -- /agnhost netexec --http-port=8080kubectl create job once --image=registry.k8s.io/e2e-test-images/busybox:1.37.0-2 -- sh -c 'echo job ran; sleep 5'kubectl create cronjob tick --image=registry.k8s.io/e2e-test-images/busybox:1.37.0-2 --schedule='*/1 * * * *' -- sh -c 'date'kubectl apply -f - <<'EOF'apiVersion: apps/v1kind: DaemonSetmetadata:name: agentspec:selector:matchLabels:app: agenttemplate:metadata:labels:app: agentspec:containers:- name: agentimage: registry.k8s.io/e2e-test-images/busybox:1.37.0-2command: ["sh", "-c", "echo running on $NODE; sleep 3600"]env:- name: NODEvalueFrom:fieldRef:fieldPath: spec.nodeNameEOF -
Watch how each behaves.
Terminal window sleep 70; kubectl get deployments,daemonsets,jobs,cronjobskubectl get pods -o widekubectl logs job/once
What you should see: The Deployment keeps 2 interchangeable pods, the DaemonSet runs exactly one pod per worker (the control plane is tainted), the Job runs to Completed and stops, and the CronJob has created a Job of its own. Pick by the question “how many, where, and does it finish?”
Clean up:
kubectl config set-context --current --namespace=defaultkubectl delete namespace ckad-workloadsPractise
- Create a CronJob that runs every five minutes, keeps two successful Jobs, and has a
activeDeadlineSecondsof 60.
On this site
Understand multi-container Pod design patterns (e.g. sidecar, init and others)
Section titled “Understand multi-container Pod design patterns (e.g. sidecar, init and others)”Know
- Init containers run to completion before the app starts. Sidecars run alongside it; native sidecars are init containers with
restartPolicy: Always. - Containers in a pod share the network namespace and can share volumes.
Read
Demo: An init container and a native sidecar
-
Create a namespace for this demo and make it the default for your context.
Terminal window kubectl create namespace ckad-multikubectl config set-context --current --namespace=ckad-multi -
The init container prepares a file; the sidecar is an init container with
restartPolicy: Always, so it starts first and keeps running.Terminal window kubectl apply -f - <<'EOF'apiVersion: v1kind: Podmetadata:name: patternsspec:initContainers:- name: setupimage: registry.k8s.io/e2e-test-images/busybox:1.37.0-2command: ["sh", "-c", "echo 'config ready' > /work/config; sleep 2"]volumeMounts:- {name: work, mountPath: /work}- name: log-shipperimage: registry.k8s.io/e2e-test-images/busybox:1.37.0-2restartPolicy: Alwayscommand: ["sh", "-c", "touch /work/app.log; tail -f /work/app.log"]volumeMounts:- {name: work, mountPath: /work}containers:- name: appimage: registry.k8s.io/e2e-test-images/busybox:1.37.0-2command: ["sh", "-c", "cat /work/config; while true; do echo \"app wrote $(date +%T)\" >> /work/app.log; sleep 3; done"]volumeMounts:- {name: work, mountPath: /work}volumes:- name: workemptyDir: {}EOFkubectl get pod patterns -w # watch Init:0/2 -> Init:1/2 -> Running, then Ctrl-C -
Read each container.
Terminal window kubectl logs patterns -c setupkubectl logs patterns -c appkubectl logs patterns -c log-shipper --tail=3kubectl get pod patterns -o jsonpath='{range .status.initContainerStatuses[*]}{.name}{" "}{.state}{"\n"}{end}'
What you should see: setup ran once and exited. log-shipper is still running and prints the lines app writes to the shared emptyDir. Native sidecars (GA since v1.33) start before the app, keep running beside it, and stop after it, which old-style sidecars in containers could not guarantee.
Clean up:
kubectl config set-context --current --namespace=defaultkubectl delete namespace ckad-multiPractise
- Add a log-shipping sidecar that tails a file written by the main container through a shared
emptyDir.
On this site
Utilize persistent and ephemeral volumes
Section titled “Utilize persistent and ephemeral volumes”Know
emptyDirlives as long as the pod; PVCs outlive it. Generic ephemeral volumes give per-pod storage from a StorageClass.
Read
Demo: Ephemeral, memory-backed, and generic ephemeral volumes
-
Create a namespace for this demo and make it the default for your context.
Terminal window kubectl create namespace ckad-volskubectl config set-context --current --namespace=ckad-vols -
One pod with an emptyDir on disk, one in memory, and a generic ephemeral volume that gets its own PVC.
Terminal window kubectl apply -f - <<'EOF'apiVersion: v1kind: Podmetadata:name: volsspec:containers:- name: appimage: registry.k8s.io/e2e-test-images/busybox:1.37.0-2command: ["sh", "-c", "df -h /disk /ram /scratch; sleep 3600"]volumeMounts:- {name: disk, mountPath: /disk}- {name: ram, mountPath: /ram}- {name: scratch, mountPath: /scratch}volumes:- name: diskemptyDir: {}- name: ramemptyDir:medium: MemorysizeLimit: 32Mi- name: scratchephemeral:volumeClaimTemplate:spec:accessModes: ["ReadWriteOnce"]resources:requests:storage: 1GiEOFkubectl wait --for=condition=Ready pod/vols --timeout=120skubectl logs volskubectl get pvc -
Delete the pod and check what is left.
Terminal window kubectl delete pod volskubectl get pvc
What you should see: /ram is a tmpfs of 32Mi that counts against the container’s memory. The ephemeral volume created a PVC named vols-scratch, owned by the pod, and deleting the pod deletes the claim too. Use a normal PVC when the data must survive the pod.
Clean up:
kubectl config set-context --current --namespace=defaultkubectl delete namespace ckad-volsOn this site
Domain and competency names are from the CNCF CKAD curriculum (v1.37), cncf/curriculum, licensed CC BY 4.0. Summaries, demos, and exercises are original to this site.