Sub topic 4
Attaching a debug container with kubectl debug
This chapter assumes you can use kubectl get, describe, logs and exec. It also assumes you know what a pod and a container are, and that you have a cluster running Kubernetes 1.25 or later. The previous chapters explained why kubectl exec is limited and what ephemeral containers are. Here you will use them.
The problem in practice
Many production images are distroless or built FROM scratch. They contain the application and nothing else. Running a shell in one fails:
kubectl exec -it web-7d4b9c-abcde -c app -- sh
# OCI runtime exec failed: exec: "sh": executable file not found in $PATHkubectl exec can only start a program that already exists inside the target container's image. If the image has no shell, there is nothing to start.
The basic command
kubectl debug solves this by adding a new container, with its own image, to the running pod:
kubectl debug -it web-7d4b9c-abcde --image=busybox:1.36 --target=appEach part of the command does one job:
-itkeeps stdin open and allocates a terminal, so you get an interactive shell. Without it, the container starts but you have nothing to type into.--imageis the image for the debug container. It does not have to match the application image. Pick one that carries the tools you need, such asbusyboxfor basic utilities ornicolaka/netshootfor network tools.--targetnames the container whose process namespace the debug container should join. The next chapter covers what that gives you. The option only works if the container runtime supports it.
The pod's existing containers are not restarted. The debug container starts beside them and shares the pod's network namespace, so localhost inside it reaches the application's ports.
What happens behind the scenes
kubectl debug sends an update to the pod's ephemeralcontainers subresource. The API server records the new container in spec.ephemeralContainers, and the kubelet on the pod's node starts it. You can confirm this:
kubectl get pod web-7d4b9c-abcde -o jsonpath='{.spec.ephemeralContainers[*].name}'
kubectl describe pod web-7d4b9c-abcdeThe first command prints the container name. Because you did not choose one, Kubernetes generated a name such as debugger-x7k2p. The describe output has an "Ephemeral Containers" section with the image, the state and the target.
To pick your own name, add --container:
kubectl debug -it web-7d4b9c-abcde --image=busybox:1.36 --target=app --container=dbgA fixed name is easier to refer to in later commands.
Leaving and returning
Typing exit ends the shell, and the debug container stops with it. Kubernetes does not delete it. It stays in the pod spec in a terminated state. If you need another session, start a new debug container. The generated names differ each time, so you can run the command repeatedly.
If you lose your terminal while the container is still running, reattach with:
kubectl attach -it web-7d4b9c-abcde -c dbgCommon errors
| Symptom | Likely cause |
|---|---|
| `forbidden: ... cannot patch resource "pods/ephemeralcontainers"` | Your account lacks RBAC permission on that subresource. |
| `unknown flag: --profile` or the `debug` command is missing | Your kubectl client is older than the feature. Upgrade it. |
| The debug container starts but `ps` shows only its own processes | `--target` was omitted, or the runtime does not support it. |
| `ImagePullBackOff` on the debug container | The node cannot pull the image. Check the name, tag and registry access. |
| Reusing a container name fails | Ephemeral containers cannot be changed or removed once added. Use a new name. |
Ephemeral containers cannot be edited, restarted or removed from a pod. They also cannot define ports, probes or resource requests. If you need any of those, you need a different technique, which a later chapter covers.
Summary of the workflow
- Confirm that
kubectl execfails or lacks the tools you need. - Run
kubectl debug -it POD --image=IMAGE --target=CONTAINER. - Investigate from the debug shell.
- Type
exit. - Verify with
kubectl describe pod PODif you need a record of what you added.
Exercise
A pod named api-5c8d-xyz1 runs a container called server built on a distroless image. Write the command that adds a busybox:1.36 debug container named dbg with an interactive terminal, joined to the server container's process namespace. Then say which command lists the names of ephemeral containers on that pod.
Show answer
kubectl debug -it api-5c8d-xyz1 --image=busybox:1.36 --target=server --container=dbg To list the names: kubectl get pod api-5c8d-xyz1 -o jsonpath='{.spec.ephemeralContainers[*].name}'
Exercise
You typed exit in a debug session and the container stopped. Can you restart that same ephemeral container? What do you do instead?
Show answer
No. Ephemeral containers cannot be restarted or removed. Run kubectl debug again to add a new debug container, optionally with a new --container name.