Skip to content
LF Cert Trainer

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

1/9kubectl debug
0:00 / 2:38

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, no ls, no tar (so not even kubectl cp works).
  • A crash-looping container exits before you can attach, and exec into 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.

Three things kubectl debug can dopodpod --copy-tonode/NAMEkubectl debugone commandEphemeral containersame Pod, no restartPod copy--copy-toNode shellnode/NAME, /host
Three things kubectl debug can do. Point it at a Pod and it adds an ephemeral container to that Pod. Add --copy-to and it makes a modified copy instead. Point it at a node and it starts a Pod in the node's namespaces with the node's disk at /host.

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
ProblemModeCommand
Image has no shell or tools; the Pod runsEphemeral containerkubectl debug -it <pod> --image=busybox:1.36 --target=<container>
Container crashes at startCopy with a new commandkubectl debug <pod> -it --copy-to=<pod>-debug --container=<container> -- sh
Want the same Pod with a debug build of the imageCopy with a new imagekubectl debug <pod> --copy-to=<pod>-debug --set-image=<container>=<image>
Node problem, no SSHNode shellkubectl debug node/<node> -it --image=busybox:1.36 --profile=sysadmin

Ephemeral containers

An ephemeral container joins a running Podseessharessharesapp containerdistroless, no shelldebuggerbusybox, ephemeralPod networksame IP, localhostProcesses--target=app
An ephemeral container joins a running Pod. The debug container shares the Pod's network and volumes. With --target it also shares the process namespace of one container, so you can see its processes and read its files through /proc.

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:

PropertyWhat it means for you
Added without a restartThe app keeps running; you debug the real, failing process
Its own imageBring busybox, netshoot or any tools image
Shares the Pod's network namespaceSame 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 resourcesIt is not part of the app, and is never restarted
Cannot be removedIt 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

Debugging a crash loop on a copy--copy-toshellfound itPod apiCrashLoopBackOffPod api-debugcommand: shYourun it by handFix the ownerDeployment
Debugging a crash loop on a copy. A container that exits at once cannot be exec'd into. A copy with the command replaced by a shell stays up, so you can look around with the same image, env and volumes, and run the real command by hand.

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:

FlagEffect
--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-processesShare the process namespace in the copy (on by default)
--same-nodeSchedule the copy on the original's node
--replaceDelete 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:

ProfileUse it for
legacyThe old behaviour, kept for compatibility; newer kubectl versions warn that it is deprecated
generalMost debugging: sensible defaults for each mode
baseline, restrictedNamespaces that enforce those Pod Security Standards; the debug container must comply
netadminNetwork debugging: adds NET_ADMIN and NET_RAW (for tcpdump, iptables)
sysadminNode 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 /host without a privileged profile. It fails with Operation 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, then chroot /host; delete the Pod afterwards.
  • Choose a profile on purpose: general for Pods, netadmin for packet tools, sysadmin for 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.