Sub topic 5
Seeing the application's processes with --target
This chapter assumes you can attach an ephemeral debug container with kubectl debug, and that you know containers in a pod share a network namespace but not necessarily a process namespace. You should also be comfortable reading ps output and navigating /proc.
The problem: an isolated process view
By default, each container in a pod has its own PID namespace. A PID namespace is the set of processes a container can see. If you attach a plain debug container, it sees only its own processes:
kubectl debug -it web-7d4b9c-abcde --image=busybox:1.36# inside the debug container
ps auxYou will see only your shell and ps. The application is running in the same pod, but it is invisible. You can't inspect its process, read its command line, or browse its files. The --target flag fixes this.
Attaching with --target
--target names the container you want to inspect. Kubectl records that name in the ephemeral container's targetContainerName field. The container runtime then starts the debug container inside the target's process namespace.
kubectl debug -it web-7d4b9c-abcde --image=busybox:1.36 --target=appNow ps shows the application:
# inside the debug container
ps auxPID USER TIME COMMAND
1 appuser 0:12 /usr/local/bin/server --port=8080
27 root 0:00 sh
34 root 0:00 ps auxThe application is usually PID 1, because the debug container joined the target's namespace instead of getting a new one.
Browsing the application's filesystem
The debug container does not share the application's mount namespace. It has its own root filesystem from the debug image. Because you can see the application's PID, you can reach its filesystem through /proc:
# inside the debug container
ls /proc/1/root/
cat /proc/1/root/etc/app/config.yaml/proc/<pid>/root is a view of that process's root filesystem. This works on a distroless image that has no shell or ls, because the tools come from your debug image.
Other useful entries for PID 1 (or whichever PID ps shows):
| Path | What it shows |
|---|---|
| `/proc/1/cmdline` | The exact command line, with arguments separated by null bytes |
| `/proc/1/environ` | The environment variables the process started with |
| `/proc/1/cwd` | A link to the process's working directory |
| `/proc/1/fd/` | Open file descriptors, including sockets and log files |
To read the command line in a readable form:
tr '\0' ' ' < /proc/1/cmdline; echoPermission errors
Reading another process's /proc entries requires matching privileges. If the application runs as a non-root user and your debug container runs as root, access works. If the application runs as root and your debug container runs as a non-root user, you will see Permission denied. Some /proc/<pid>/root and environ reads can also be blocked by the runtime's default security profile or by missing capabilities.
If you hit this, choose a debugging profile that grants more access, or run the debug container as the same user as the application. Profiles are covered in a later chapter. Grant only what the task needs.
Verifying what was added
Ephemeral containers are part of the pod spec. You can confirm what you attached and which container it targets:
kubectl get pod web-7d4b9c-abcde \
-o jsonpath='{.spec.ephemeralContainers[*].targetContainerName}'If you attach several debug containers over time, each one gets its own entry. Use --container=NAME on kubectl debug to give the debug container a predictable name instead of an auto-generated one. That makes it easier to reconnect with kubectl attach if your session drops.
Common mistakes
- Forgetting
--target, then concluding the application has crashed becausepsshows nothing. - Using the wrong container name. In a multi-container pod,
--targetmust match a name inspec.containers. Runkubectl get pod NAME -o jsonpath='{.spec.containers[*].name}'to list them. - Expecting to see the application's files directly at
/. You see your debug image's filesystem there. Go through/proc/<pid>/root. - Assuming
--targetchanges the application. It doesn't. It only affects what the debug container can see.
Exercise
You attach kubectl debug -it mypod --image=busybox:1.36 --target=app. Running ps aux shows only your shell and ps, and kubectl printed a warning about the runtime. What is the likely cause, and how can you still read the config file at /etc/app/config.yaml in the app container once you can see its processes?
Show answer
The container runtime does not support process namespace targeting, so the debug container got its own PID namespace and cannot see the application. When targeting works, read the file through the process root, for example cat /proc/1/root/etc/app/config.yaml, using the PID that ps shows for the application.