Skip to content

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.

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

  1. Create a namespace for this demo and make it the default for your context.

    Terminal window
    kubectl create namespace ckad-image
    kubectl config set-context --current --namespace=ckad-image
  2. Write a tiny image that runs as a non-root user.

    mkdir -p img && cat > img/hello.sh <<'EOF'
    #!/bin/sh
    echo "hello from $(hostname), version ${VERSION:-1.0}"
    sleep 3600
    EOF
    cat > img/Dockerfile <<'EOF'
    FROM registry.k8s.io/e2e-test-images/busybox:1.37.0-2
    COPY --chmod=755 hello.sh /hello.sh
    ENV VERSION=1.0
    USER 65534
    ENTRYPOINT ["/hello.sh"]
    EOF
    docker build -t demo/hello:1.0 img/
    kind load docker-image demo/hello:1.0
  3. Run it. kind can’t pull demo/hello from a registry, so the pull policy must not be Always.

    Terminal window
    kubectl run hello --image=demo/hello:1.0 --image-pull-policy=IfNotPresent
    kubectl wait --for=condition=Ready pod/hello --timeout=60s && kubectl logs hello
  4. Change the image, tag it, and swap it in.

    Terminal window
    sed -i 's/VERSION=1.0/VERSION=1.1/' img/Dockerfile
    docker build -t demo/hello:1.1 img/ && kind load docker-image demo/hello:1.1
    kubectl set image pod/hello hello=demo/hello:1.1
    sleep 10; kubectl logs hello
    kubectl run latest --image=demo/hello:latest
    sleep 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:

Terminal window
rm -rf img
kubectl config set-context --current --namespace=default
kubectl delete namespace ckad-image

Practise

  • Build an image with a multi-stage Containerfile, save it with podman save or docker 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

  1. Create a namespace for this demo and make it the default for your context.

    Terminal window
    kubectl create namespace ckad-workloads
    kubectl config set-context --current --namespace=ckad-workloads
  2. 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=8080
    kubectl 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/v1
    kind: DaemonSet
    metadata:
    name: agent
    spec:
    selector:
    matchLabels:
    app: agent
    template:
    metadata:
    labels:
    app: agent
    spec:
    containers:
    - name: agent
    image: registry.k8s.io/e2e-test-images/busybox:1.37.0-2
    command: ["sh", "-c", "echo running on $NODE; sleep 3600"]
    env:
    - name: NODE
    valueFrom:
    fieldRef:
    fieldPath: spec.nodeName
    EOF
  3. Watch how each behaves.

    Terminal window
    sleep 70; kubectl get deployments,daemonsets,jobs,cronjobs
    kubectl get pods -o wide
    kubectl 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:

Terminal window
kubectl config set-context --current --namespace=default
kubectl delete namespace ckad-workloads

Practise

  • Create a CronJob that runs every five minutes, keeps two successful Jobs, and has a activeDeadlineSeconds of 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

  1. Create a namespace for this demo and make it the default for your context.

    Terminal window
    kubectl create namespace ckad-multi
    kubectl config set-context --current --namespace=ckad-multi
  2. 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: v1
    kind: Pod
    metadata:
    name: patterns
    spec:
    initContainers:
    - name: setup
    image: registry.k8s.io/e2e-test-images/busybox:1.37.0-2
    command: ["sh", "-c", "echo 'config ready' > /work/config; sleep 2"]
    volumeMounts:
    - {name: work, mountPath: /work}
    - name: log-shipper
    image: registry.k8s.io/e2e-test-images/busybox:1.37.0-2
    restartPolicy: Always
    command: ["sh", "-c", "touch /work/app.log; tail -f /work/app.log"]
    volumeMounts:
    - {name: work, mountPath: /work}
    containers:
    - name: app
    image: registry.k8s.io/e2e-test-images/busybox:1.37.0-2
    command: ["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: work
    emptyDir: {}
    EOF
    kubectl get pod patterns -w # watch Init:0/2 -> Init:1/2 -> Running, then Ctrl-C
  3. Read each container.

    Terminal window
    kubectl logs patterns -c setup
    kubectl logs patterns -c app
    kubectl logs patterns -c log-shipper --tail=3
    kubectl 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:

Terminal window
kubectl config set-context --current --namespace=default
kubectl delete namespace ckad-multi

Practise

  • Add a log-shipping sidecar that tails a file written by the main container through a shared emptyDir.

On this site

Know

  • emptyDir lives 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

  1. Create a namespace for this demo and make it the default for your context.

    Terminal window
    kubectl create namespace ckad-vols
    kubectl config set-context --current --namespace=ckad-vols
  2. 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: v1
    kind: Pod
    metadata:
    name: vols
    spec:
    containers:
    - name: app
    image: registry.k8s.io/e2e-test-images/busybox:1.37.0-2
    command: ["sh", "-c", "df -h /disk /ram /scratch; sleep 3600"]
    volumeMounts:
    - {name: disk, mountPath: /disk}
    - {name: ram, mountPath: /ram}
    - {name: scratch, mountPath: /scratch}
    volumes:
    - name: disk
    emptyDir: {}
    - name: ram
    emptyDir:
    medium: Memory
    sizeLimit: 32Mi
    - name: scratch
    ephemeral:
    volumeClaimTemplate:
    spec:
    accessModes: ["ReadWriteOnce"]
    resources:
    requests:
    storage: 1Gi
    EOF
    kubectl wait --for=condition=Ready pod/vols --timeout=120s
    kubectl logs vols
    kubectl get pvc
  3. Delete the pod and check what is left.

    Terminal window
    kubectl delete pod vols
    kubectl 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:

Terminal window
kubectl config set-context --current --namespace=default
kubectl delete namespace ckad-vols

On 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.