Debugging Pods: describe, logs, events and ephemeral containers
A working order for finding out why a Pod misbehaves: status, conditions and events, current and previous logs, and kubectl debug for images without a shell.
Debugging Pods: describe, logs, events and ephemeral containers
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- Work outside in: kubectl get for the phase and restart count, describe for conditions, container states and events, logs for what the process said, then a debug container for what's true inside.
- Events are short-lived (an hour by default) and namespaced. kubectl events --for pod/NAME shows one object's history, and the scheduler, kubelet and controllers all write there.
- kubectl logs --previous shows the last crashed instance, which is usually where a CrashLoopBackOff's cause is. Logs are served by the kubelet, so a node problem breaks logs too.
- kubectl debug adds an ephemeral container to a running Pod through the ephemeralcontainers subresource. --target shares the app container's process namespace, so a shell can inspect a distroless app.
- Ephemeral containers can't be removed or restarted and have no ports, probes or resources. They're for inspection, not for running things.
- kubectl debug --copy-to makes a modified copy (another image, a shared process namespace) to experiment on, and kubectl debug node/NAME starts a Pod in the node's namespaces with the host filesystem at /host.
Something is wrong, and you have a Pod name
An alert fires, or a deploy hangs, and all you know is that checkout-7d9f... isn’t working. The tempting move is to guess and change things. The faster move is a fixed order of questions, each answered by one command, from cheapest to most invasive. Most problems are solved in the first two steps. This lesson covers the tools. The next one covers the failures you’ll find with them.
Step 1: get, for the shape of the problem
kubectl get pod -n shop -o wide
The STATUS column is a summary, not a phase: Pending, ContainerCreating, CrashLoopBackOff, ImagePullBackOff, Running, Terminating, Completed. READY (say 0/1) is readiness, RESTARTS counts container restarts, and -o wide shows the node and IP. Already you know which of three worlds you’re in: never scheduled, scheduled but not starting, or started and failing.
Step 2: describe, for conditions, states and events
kubectl describe pod joins the Pod’s status with its recent events. On the lab cluster, for a Pod on a node with no kubelet, the events stopped after one line:
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 2m26s default-scheduler Successfully assigned shop/api to fake-2
On a healthy node you’d next see Pulling, Pulled, Created and Started from the kubelet. That gap is itself the diagnosis: scheduled, and then the node did nothing. Read describe for:
- Conditions:
PodScheduled,Initialized,ContainersReady,Ready, each with a reason when false. - Container state:
Waitingwith a reason (ImagePullBackOff,CreateContainerConfigError),Running, orTerminatedwith exit code and reason. Last State shows the previous run, such asOOMKilledwith exit code 137. - Events: the timeline from scheduler, kubelet and controllers, with warnings repeated as
(x12 over 5m).
kubectl events --for pod/api -n shop shows the same events alone, and kubectl events -n shop --types=Warning across a namespace. Events are API objects with a TTL, an hour by default, so export them if you want history.
Step 3: logs, current and previous
kubectl logs -n shop api -c api --previous # the crashed instance
kubectl logs -n shop deploy/checkout --all-containers --since=10m -f
kubectl logs -n shop -l app=checkout --prefix --max-log-requests=10
--previous is the one people forget. In a crash loop, the current container may have just started and printed nothing. The previous one printed the stack trace.
Logs come from the kubelet, not the API server. The API server proxies the request to the node. On the lab cluster, where the Pod’s node is only an API object, it failed accordingly:
$ kubectl -n shop logs api
Defaulted container "api" out of: api, debugger (ephem)
Error from server: no preferred addresses found; known addresses: []
On a real cluster the same symptom (get works, logs and exec don’t) points at the node or the kubelet’s network path, not the app. The logging lesson covers where these logs live on the node.
Step 4: kubectl debug
Modern images are often distroless: no shell, no ps, no curl. kubectl exec -- sh fails. Ephemeral containers solve that by adding a temporary container, from any image, to a running Pod:
kubectl debug -it -n shop api --image=busybox:1.37 --target=api --profile=general -c debugger
The command patches the Pod’s ephemeralcontainers subresource. Containers are otherwise immutable, which is why it’s a separate subresource with its own RBAC (pods/ephemeralcontainers). On the lab cluster it added this to the Pod spec:
[{"image":"busybox:1.37","name":"debugger","stdin":true,"targetContainerName":"api",
"securityContext":{"capabilities":{"add":["SYS_PTRACE"]}}, ...}]
(Then the command waited for the container to start, which it never would here.) targetContainerName puts the debugger in the app container’s process namespace, so ps shows the app’s processes and /proc/<pid>/root/ is the app’s filesystem. The general profile added SYS_PTRACE so you can attach strace or a debugger. Other profiles are baseline, restricted, netadmin (adds NET_ADMIN and NET_RAW for tcpdump) and sysadmin.
Ephemeral containers have limits by design: no ports, probes or resource limits, and they can’t be removed or restarted. The Pod keeps the entry until it’s deleted. They still pass through admission, so a restricted namespace may reject a netadmin debugger.
Copies and nodes
When you need to change the Pod, not just look inside it, debug a copy:
kubectl debug -n shop api --copy-to=api-debug --set-image=api=busybox:1.37 --share-processes
On the lab cluster this created api-debug with shareProcessNamespace: true and the image swapped. Useful patterns are swapping in an image with tools, or changing the command to sleep infinity so a crash-looping container stays up long enough to inspect.
For node problems, kubectl debug node/fake-1 --image=busybox:1.37 --profile=general creates a Pod pinned to that node with hostPID, hostNetwork and hostIPC set and the node’s root filesystem mounted at /host, so chroot /host gives you a shell on the node without SSH. With --profile=sysadmin the container is privileged. The lab API server, running without --allow-privileged, refused that: securityContext.privileged: Forbidden: disallowed by cluster policy. Delete node debugging Pods when you’re done.
A habit that saves time
Write down what you expected at each step and what you saw. “Scheduled at 10:02, no Pulling event” is a finding. “It’s broken” isn’t. Most time lost in debugging is time spent re-checking things already ruled out.
About the video
The embed is Viktor Farcic’s “How to Debug Kubernetes Applications With Ephemeral Containers”, July 2023, 7 minutes, on DevOps & AI Toolkit. Going by its description and linked command gist, it shows why debugging minimal images is hard and uses kubectl debug with ephemeral containers to get inside one without changing the image. It covers step 4 only. The earlier steps are this lesson’s own. I judged it from the description only, since transcripts couldn’t be fetched and it has one chapter marker. English was judged from its “English (US) original” audio track. The deeper link starts Is it Observable’s troubleshooting guide at its workload-diagnosis chapter.
What was run: the events, the logs error, the ephemeral container added by kubectl debug, the copied Pod, the node-debugging Pod’s host settings and the privileged rejection are real output from a local Kubernetes v1.37.3 API server whose nodes run no containers. No shell actually ran, so inside-the-container behaviour (process namespace, /proc/<pid>/root, profiles’ effects) is from the debugging docs, checked 2026-11-29.
Where this fits
The tools are only half of it. Next, what they usually turn up: troubleshooting common failures.
Check yourself
A Pod is in CrashLoopBackOff and kubectl logs shows nothing useful. What's the next command?
kubectl logs POD --previous (with -c for the container) to read the output of the instance that crashed, and kubectl describe pod to see the last state's exit code and reason, such as OOMKilled or Error with code 1.
Your app image is distroless, with no shell. How do you look at its filesystem and processes while it runs?
kubectl debug -it POD --image=busybox --target=APP. That adds an ephemeral container sharing the app container's process namespace, where ps shows the app's processes and /proc/<pid>/root is its filesystem.
Why can events from yesterday's incident no longer be found?
Events are stored with a TTL, one hour by default (the API server's --event-ttl). For history, ship events to your logging system with an event exporter.
kubectl logs fails with an error about the node while kubectl get pods works fine. Why?
get reads from the API server, but logs and exec are proxied by the API server to the kubelet on the Pod's node. If that kubelet is unreachable or the node has no usable address, the API server can't fetch them.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.