Lesson 143 · Section 12: Troubleshooting
Lesson 143: kubectl debug: ephemeral containers, Pod copies and node shells
When kubectl exec is not enough: add an ephemeral debug container to a running Pod with no shell, make a copy of a crashing Pod with a different command or image, and open a shell on a node through a Pod, all with kubectl debug, plus the cleanup each one needs. · 20 min read · 3 min video
Official competencies this lesson covers
Narrated by the local narrator voice. Keys: Space plays and pauses, J and L step, C captions, F full screen.
Why kubectl exec is not always enough
kubectl exec -it <pod> -- sh is the first tool everybody reaches for. It works only when three things are true: the
container is running, its image contains a shell and the tools you need, and the problem is inside that
container. In real clusters, and in exam tasks, all three fail regularly:
- Distroless and scratch images hold the program and nothing else: no
sh, nols, notar(so not evenkubectl cpworks). - A crash-looping container exits before you can attach, and
execinto a container in back-off is refused. - A node problem is outside every container, and you may have no SSH access to the node.
kubectl debug solves each of these with a different trick.
Parts:
- kubectl debug (one command)
- Ephemeral container (same Pod, no restart)
- Pod copy (--copy-to)
- Node shell (node/NAME, /host)
Connections:
- kubectl debug to Ephemeral container: pod
- kubectl debug to Pod copy: pod --copy-to
- kubectl debug to Node shell: node/NAME
| Problem | Mode | Command |
|---|---|---|
| Image has no shell or tools; the Pod runs | Ephemeral container | kubectl debug -it <pod> --image=busybox:1.36 --target=<container> |
| Container crashes at start | Copy with a new command | kubectl debug <pod> -it --copy-to=<pod>-debug --container=<container> -- sh |
| Want the same Pod with a debug build of the image | Copy with a new image | kubectl debug <pod> --copy-to=<pod>-debug --set-image=<container>=<image> |
| Node problem, no SSH | Node shell | kubectl debug node/<node> -it --image=busybox:1.36 --profile=sysadmin |
Ephemeral containers
Parts:
- app container (distroless, no shell)
- debugger (busybox, ephemeral)
- Pod network (same IP, localhost)
- Processes (--target=app)
Connections:
- debugger to app container: sees
- debugger to Pod network: shares
- debugger to Processes: shares
An ephemeral container is a container added to a Pod that is already running, through the Pod's ephemeralcontainers
subresource. It is meant only for inspection:
| Property | What it means for you |
|---|---|
| Added without a restart | The app keeps running; you debug the real, failing process |
| Its own image | Bring busybox, netshoot or any tools image |
| Shares the Pod's network namespace | Same IP; localhost:<port> reaches the app |
Can share one container's process namespace (--target) | ps shows the app's processes; /proc/<pid>/root/ shows the app's filesystem |
| No ports, probes or resources | It is not part of the app, and is never restarted |
| Cannot be removed | It stays in the Pod spec (as terminated) until the Pod is deleted |
Example · Look inside a distroless container
The api Pod answers nothing on the port its Service uses, and its image has no shell.
kubectl -n shop debug -it api-7c5d8-q2w9e --image=busybox:1.36 --target=api
Inside the debug container:
ps # PID 1 is the app, thanks to --target
cat /proc/1/root/etc/api/config.yaml # the app's own files
netstat -tlnp # what is really listening, on which port
wget -qO- -T 2 localhost:8081/healthz
listen: :8081
tcp 0 0 :::8081 :::* LISTEN 1/api
ok
The app listens on 8081, while the Service's targetPort says 8080. Exit the shell and fix the Service. The
ephemeral container stays listed in the Pod as terminated, which is harmless.
Debugging a copy of a Pod
Parts:
- Pod api (CrashLoopBackOff)
- Pod api-debug (command: sh)
- You (run it by hand)
- Fix the owner (Deployment)
Connections:
- Pod api to Pod api-debug: --copy-to
- You and Pod api-debug: shell
- Pod api-debug to Fix the owner: found it
When the container exits immediately, there is nothing to attach to. --copy-to creates a new Pod from the old
one's spec with your changes applied, and leaves the original alone (unless you add --replace).
Example · Find why a container exits at once
api is in CrashLoopBackOff, and kubectl logs --previous shows only exit status 1. Make a copy that runs a shell
instead of the app, in an image that has one:
kubectl -n shop debug api-7c5d8-q2w9e -it --copy-to=api-debug \
--container=api --set-image=api=busybox:1.36 -- sh
Now look at what the app would see:
env | grep -i db # environment from the Pod spec
ls -l /etc/api/ /var/run/secrets/ # mounted ConfigMaps and Secrets
cat /etc/api/config.yaml
DB_HOST=postgres.shop
ls: /etc/api/: No such file or directory
The ConfigMap volume is mounted at /etc/app, but the app reads /etc/api. Fix the mountPath in the Deployment,
then remove the copy:
exit
kubectl -n shop delete pod api-debug
kubectl -n shop edit deploy api # volumeMounts[].mountPath: /etc/api
If the original image does have a shell, leave out --set-image and keep the real image: then you can also run the
app's own command by hand and see its error directly.
Useful flags for copies:
| Flag | Effect |
|---|---|
--copy-to=<name> | Name of the new Pod |
--container=<c> -- <cmd> | Replace that container's command |
--set-image=<c>=<image>, --set-image=*=<image> | Change one or all images |
--image=<image> | Add an extra debug container to the copy |
--share-processes | Share the process namespace in the copy (on by default) |
--same-node | Schedule the copy on the original's node |
--replace | Delete the original Pod |
A shell on a node
kubectl debug node/<name> creates a Pod on that node that runs in the host's network, PID and IPC namespaces,
with the node's root filesystem mounted at /host. It is a way onto a node when you have kubectl but no SSH.
kubectl debug node/node01 -it --image=busybox:1.36 --profile=sysadmin
chroot /host # now you are "on" the node
systemctl status kubelet containerd
journalctl -u kubelet --no-pager | tail -n 20
crictl ps -a
exit; exit
kubectl get pods | grep node-debugger # clean up
kubectl delete pod node-debugger-node01-8qzlm
Without a privileged profile, the Pod is not privileged, and chroot /host or reading some process information can
fail. --profile=sysadmin makes it privileged. The node shell needs the node's kubelet to be working (it starts the
Pod), so for a node that is NotReady because of its kubelet, you still need SSH.
Profiles
--profile sets the security context of the debug container:
| Profile | Use it for |
|---|---|
legacy | The old behaviour, kept for compatibility; newer kubectl versions warn that it is deprecated |
general | Most debugging: sensible defaults for each mode |
baseline, restricted | Namespaces that enforce those Pod Security Standards; the debug container must comply |
netadmin | Network debugging: adds NET_ADMIN and NET_RAW (for tcpdump, iptables) |
sysadmin | Node debugging: privileged |
Pass a profile explicitly so the behaviour does not depend on the kubectl version.
Tricks
Common mistakes
- Forgetting
--target. You get a shell in the Pod, but you cannot see the app's processes or files. - Leaving copies and node debugger Pods running. They are bare Pods and nothing deletes them for you.
- Fixing the copy. The copy is a throwaway. Put the fix into the Deployment (or whatever owns the original Pod).
- Expecting to remove an ephemeral container. You cannot; it disappears with the Pod.
chroot /hostwithout a privileged profile. It fails withOperation not permitted. Use--profile=sysadmin.- Using a node shell for a kubelet that is down. The debug Pod needs that kubelet to start. Use SSH.
Recap
- No shell in the image, Pod running:
kubectl debug -it <pod> --image=busybox:1.36 --target=<container>. - Container dies at once:
kubectl debug <pod> -it --copy-to=<name> --container=<c> -- sh, optionally with--set-image; delete the copy afterwards. - Node problem:
kubectl debug node/<node> -it --image=busybox:1.36 --profile=sysadmin, thenchroot /host; delete the Pod afterwards. - Choose a profile on purpose:
generalfor Pods,netadminfor packet tools,sysadminfor nodes.
Official docs
Quiz: check what you learned
5 questions on this lesson · about 3 min · instant feedback. Missed questions come back in your review queue.
Sign in to take the quiz and keep your score.
Practise: try it on a real cluster
2 hands-on labs for this lesson. You get goals, not steps: any solution that reaches them passes.
Read a file from a running container that has no shell and no programs at all, and read a file that exists only on the node without leaving a debugging pod behind: kubectl debug with --target, node debugging and cleanup.
Investigate · Level 2 · standard · target 15 min
A cluster with three faults at once, one in a worker node, one in the control plane and one in cluster networking, picked at random for each session. Find them with the troubleshooting method, repair them, and ship a release through the repaired cluster.
Fix · Level 3 · challenge · target 40 min
Discussion
Learners ask and answer questions here. Sign in to read and join the discussion.