root access

$ cat ckad-prep/part-02.md

CKAD Prep, Part 2: Breaking Pods on Purpose

CKAD Prep, Part 2: Breaking Pods on Purpose — immutable pod fields, a misspelled image, and a debug pod that deletes itself.

Where we are

Hi, I’m Reza. I’m sharing my preparation for the CKAD exam, practiced on a K3s cluster in my home lab. I am a junior engineer looking for my first SRE, DevOps or Cloud job, and that is why I learn in public. You can see how I think, what breaks, and how I fix it.

In Part 1, I set up the lab and built pods the YAML way: a pod with everything set (A1), command vs args (A2), and pulling facts out of a pod with jsonpath (A3).

This part is about breaking things, on purpose and by accident:

  • A4: change a running pod. One change works, and one is rejected.
  • A5: fix a pod with a misspelled image.
  • A6: run a temporary debug pod that deletes itself.

The idea behind all three: pods are disposable. Almost nothing in a running pod can change. When you need something different, you get a new pod, with a new IP.

The left side is what A4’s first change did. The right side is what its second change needed.

A container restart keeps the pod; a recreate makes a new one. Image change with kubectl apply: same pod UID, same IP 10.42.1.33, RESTARTS goes up by 1, AGE keeps counting. CPU limit change with replace --force: new pod UID, new IP 10.42.1.36, RESTARTS back to 0, AGE starts again.

A4: change an immutable field

The task: change pod web to image nginx:1.26, and its CPU limit to 300m.

I didn’t make a new file. I edited the same manifest from A1, twice. One object, one file. A copy per scenario would leave two files describing the same pod, and they would fight on apply -f for the whole folder.

Change 1: the image, which works

containers:
  - name: web
    image: nginx:1.26      # was nginx:1.27
kubectl apply -f 01-web-pod.yaml

apply compares the file with the live object and sends only the difference. Here that’s only image, which is one of the few mutable pod fields. The API server accepts it.

The kubelet on thor sees the new image, stops the old container, pulls nginx:1.26, and starts a new container inside the same pod. Same name, same UID, same IP.

So RESTARTS went to 1, because the container restarted, not the pod. And AGE stayed at 60m, because the pod object is the original one.

Change 2: the CPU limit, which is rejected

resources:
      limits:
        cpu: 300m          # was 200m
        memory: 128Mi

The same apply gave this:

The Pod "web" is invalid: spec: Forbidden: pod updates may not change fields other than
`spec.containers[*].image`,`spec.initContainers[*].image`,`spec.activeDeadlineSeconds`,
`spec.tolerations` (only additions to existing tolerations),
`spec.terminationGracePeriodSeconds` (allow it to be set to 1 if it was previously negative)

Forbidden comes from the API server’s validation. Nothing on the cluster changed.

The error message is the list of mutable fields. I don’t need to memorize it for the exam. Try the change, and read the error.

Below the message, a diff (- “cpu”: “200m” / + “cpu”: “300m”) shows exactly which field caused it. That helps when you changed several things and only one is forbidden.

This rejection was the lesson of the scenario, not a mistake.

The fix: delete and create in one command

kubectl replace --force --grace-period 0 -f 01-web-pod.yaml
  • replace without --force is rejected too, for the same reason.
  • --force means: delete the live object, then create a new one from the file. The output shows both steps: pod “web” deleted, then pod/web replaced.
  • --grace-period 0 skips the default 30-second graceful shutdown. It’s fine for labs and the exam. In production, it can cut off requests in progress.
  • The result is a new pod: RESTARTS 0, AGE 16s, and a new IP, 10.42.1.33 became 10.42.1.36.

I used replace --force instead of delete + apply, because one command means nothing gets forgotten in between. The outcome is the same.

Another way is kubectl edit pod web. You change the limit, save, and the edit is rejected, but kubectl saves your version to /tmp/kubectl-edit-XXXX.yaml. Then kubectl replace --force -f that file. Same result. But editing my own file keeps the repo correct.

Why the new IP matters

Anything that saved the old IP is now wrong, like web-info.txt from A3. That is the problem Services solve: a stable name and IP in front of pods that come and go. They come later in the lab.

Result

NAME   READY   STATUS    RESTARTS   AGE   IP           NODE
web    1/1     Running   0          16s   10.42.1.36   thor
nginx:1.26
300m

A5: fix a broken pod

The task: create a pod with a misspelled image (ngnix), find the cause, save its status to a file, and fix it.

apiVersion: v1
kind: Pod
metadata:
  name: broken
spec:
  containers:
  - name: broken
    image: ngnix:1.26

ngnix has no registry or user, so the container runtime expands it to docker.io/library/ngnix:1.26. library/ is Docker Hub’s namespace for official images. library/nginx exists. library/ngnix doesn’t.

The API server accepts this pod without complaint. It doesn’t check whether images exist. The failure only shows up on the node, when the kubelet tries to pull.

A small note: the lab says ngnix:1.27, and I used 1.26. Same lesson, but on the exam the tag must match the task exactly. That detail comes back below.

Step 1: what state is it in?

kubectl get pod broken
NAME     READY   STATUS             RESTARTS   AGE
broken   0/1     ImagePullBackOff   0          15s

RESTARTS is 0, because the container never started, so there’s nothing to restart. Compare that with CrashLoopBackOff, where the container starts and then crashes, and RESTARTS goes up.

Step 2: why?

kubectl events --for pod/broken
Normal   Scheduled  Successfully assigned core/broken to thor
Normal   Pulling    Pulling image "ngnix:1.26"
Warning  Failed     Failed to pull image "ngnix:1.26": failed to pull and unpack image
                    "docker.io/library/ngnix:1.26": failed to resolve reference ...:
                    pull access denied, repository does not exist or may require authorization:
                    server message: insufficient_scope: authorization failed
Warning  Failed     Error: ErrImagePull
Normal   BackOff    Back-off pulling image "ngnix:1.26"
Warning  Failed     Error: ImagePullBackOff

Reading it from top to bottom:

  • Scheduled: the scheduler worked. The problem isn’t resources or node placement.
  • Pulling, then Failed: the kubelet reached the registry, and the registry said no.
  • docker.io/library/ngnix:1.26 is the fully expanded name. Reading it carefully is how you spot the typo.
  • “pull access denied … may require authorization” is the confusing part. The real cause is a typo, not a permission problem. Docker Hub won’t confirm whether a repository exists, so it gives the same answer for “private” and “doesn’t exist”.
  • ErrImagePull means the pull just failed. ImagePullBackOff means the kubelet is waiting before the next try. The wait grows (10s, 20s, 40s … up to 5 minutes), so the status flips between the two.

kubectl describe pod broken shows the same events at the bottom, plus the full spec.

Step 3: save the status, before the fix

kubectl get pod broken -o jsonpath='{.status.containerStatuses[0].state.waiting.reason}{"\n"}' > /opt/ckad/00/broken.txt
  • .state has exactly one of waiting, running or terminated. A container that can’t pull its image is waiting, and .reason holds the text you see in STATUS.
  • Once the pod is fixed, .state.waiting disappears, and this query prints an empty line. So the status must be saved before the fix.
  • My first try had no [0]: {.status.containerStatuses.state.waiting.reason}. It printed a blank line. containerStatuses is a list, and jsonpath doesn’t step into a list’s items unless you give an index ([0]) or a wildcard ([*]). The same family as the List problem in Part 1.

Step 4: the fix, and a wrong tag

image is one of the mutable fields from A4, so I fixed the file and applied it. No recreate needed.

 image: nginx:1.27
kubectl apply -f 03-broken-pod.yaml

But my first fix was nginx:1.26, the same tag as the typo. The pod ran fine. But A5 asks for nginx:1.27, so on the exam this answer would score zero. I found it by checking .spec.containers[0].image, then fixed the file and applied again.

This time RESTARTS went to 1, because the container really was running on 1.26, and the image change restarted it, just like A4. The lesson: re-read the task, and compare every name and tag before moving on.

I fixed it with apply, not kubectl set image, so the YAML file stays correct. set image is faster on the exam, but then the live object and the file don’t match, and re-applying the folder later would bring the typo back.

Result

ImagePullBackOff

NAME     READY   STATUS    RESTARTS     AGE
broken   1/1     Running   1 (3s ago)   16m

Three statuses worth knowing

  • ErrImagePull / ImagePullBackOff: the container never started, and RESTARTS stays 0. Typical causes: a wrong name or tag, a private registry, or no network.
  • CrashLoopBackOff: the container started, then exited, and RESTARTS goes up. Typical causes: a bad command, an app error, or missing config.
  • CreateContainerConfigError: the container never started. Typical cause: a missing ConfigMap or Secret key.

The debugging order I use for every broken pod: get (what state?), then events or describe (why?), then fix, then get again.

A6: a temporary debug pod

The task: run a one-shot pod that fetches web‘s nginx page by its pod IP, then deletes itself.

This is the one exception to the lab’s YAML rule. Self-deletion is a kubectl run --rm client feature, and a manifest can’t express it. A throwaway pod also has no reason to be in version control.

kubectl run tmp --image=busybox:1.36 --rm -it --restart=Never -- \
  wget -qO- -T2 $(kubectl get pod web -o jsonpath='{.status.podIP}')

Piece by piece:

  • busybox:1.36 is about 4 MB and has wget, nslookup and sh. That makes it the standard debug image.
  • -it: -i keeps stdin open and -t gives it a terminal. Together they attach my terminal to the container.
  • --rm deletes the pod when the attached session ends. kubectl can only notice the end because it’s attached, which is why --rm without -i is rejected. Without --rm, a tmp pod in Completed state stays behind, and the next kubectl run tmp fails with AlreadyExists.
  • --restart=Never sets restartPolicy: Never: run once, then stop. The default is Always: wget exits, the kubelet restarts it, it exits again, and the session never ends cleanly.
  • -qO-: -q is quiet, and -O- (a capital letter O, then -) writes the page to stdout instead of to a file called index.html.
  • -T2 gives up after 2 seconds. Without it, an unreachable IP makes wget hang for a long time, which wastes exam minutes.
  • $( ... ) is command substitution. My local shell runs it first and puts in 10.42.1.36 before kubectl run starts. I don’t type the IP, because A4 just changed it from .33 to .36. Looking it up each time is always correct.

restartPolicy, in three lines

  • Always (the default): always restarted. For long-running pods and Deployments. I saw this in action: printer and sleeper from Part 1 showed RESTARTS 1 after their sleep 3600 ran out.
  • OnFailure: restarted only after a non-zero exit. For Jobs.
  • Never: never restarted. For one-shot debug pods, and some Jobs.

Problem 1: the digit zero

My first try printed busybox’s usage text:

wget: invalid option -- '0'

I had typed -q0-, with the digit zero, instead of -qO-, with the letter O. They look almost the same in many terminal fonts, which makes it hard to spot. The pod ended as terminated (Error), and --rm still deleted it.

Problem 2: the output showed up twice

The nginx page printed twice, with a warning: couldn't attach ... falling back to streaming logs. That’s not an error. It’s a race:

  1. kubectl creates the pod, waits for it to run, then attaches.
  2. wget finished in milliseconds. By the time kubectl tried to attach, the container was already gone.
  3. So kubectl fell back to showing the logs. The output appears once from the early stream, and once from the logs.

The result is correct. Only the display is noisy. It happened on both runs.

What it proves: pod-to-pod networking

Every pod gets its own IP, and any pod can reach any other pod’s IP directly, on any node, with no NAT. That’s the Kubernetes network model, and Flannel provides it on K3s. Here tmp reached web on 10.42.1.36:80, with no Service involved.

But this isn’t how you reach apps in practice. Pod IPs change whenever a pod is recreated, as A4 showed. That’s why a later part of the lab puts a Service, a stable name and IP, in front of the pods.

Result

The <title>Welcome to nginx!</title> page printed (twice, because of the race), then:

pod "tmp" deleted from core namespace

And kubectl get pods shows only broken, printer, sleeper and web. No tmp left behind.

What I learned

  • Pods are disposable. The image can change on a running pod. Almost nothing else can. For anything else, replace --force makes a new pod, with a new IP.
  • Read the error, don’t memorize the rule. The Forbidden message lists every mutable field.
  • Restart vs recreate: a container restart keeps the UID, the IP and the AGE. A recreate resets all of them.
  • An error message can point the wrong way. “Pull access denied” was a typo, not a permission problem. Read the fully expanded image name.
  • Debugging order: get, then events or describe, then fix, then get again.
  • Save a status before you fix it. Afterwards, the field is gone.
  • Compare every name and tag against the task. A running pod with the wrong tag still scores zero.
  • --rm needs -it, and a doubled output from a fast command is a harmless race.
  • O and 0 look the same. When a command fails for no reason, check the characters.

What is next

That finishes Part A of Lab 00: pods. Next in the lab come the controllers that manage pods for you, starting with ReplicaSets, and later the Services that give pods a stable name.

$ ls -t root-access/ | head -3

CKAD Prep, Part 1: Building Pods the YAML Way ckad-prep · 1/2 11 min Building a Job Board API, Part 32: The Fresh Machine Found Two Bugs job-board-api · 32/32 13 min Building a Job Board API, Part 31: Installed Is Not the Same as Working job-board-api · 31/32 10 min

Leave a comment

Your email address will not be published. Required fields are marked *