You want to look into a container, but kubectl exec finds no shell. Many slim images consist of a single program. Instead of rebuilding the image, you attach a debug container with tools to the same pod.
Symptom
kubectl -n laden exec $A -- sh -c true error: Internal error occurred: … exec: "sh": executable file not found in $PATH
The pods run traefik/whoami, an image that consists of a single program.
Diagnosis
Put a debug container into the target container's process namespace and read its log.
kubectl -n laden debug -i $A --image=busybox:1.37.0 --target=whoami --profile=general -- \
sh -c 'ps; tr "\0" "\n" < /proc/1/environ | grep DB_; ls /proc/1/root/'
Targeting container "whoami". If you don't see processes from this container it may be because the container runtime doesn't support this feature.
Defaulting debug container name to debugger-m8snk.
kubectl -n laden logs $A -c debugger-m8snk
PID USER TIME COMMAND
1 root 0:00 /whoami --port 8080
Causes
no shell in the imageDistroless or scratch image
The image contains only the program itself, no shell and no tools. That is intended: smaller attack surface, smaller image.
output missingDebug commands finish too fast
Often the commands are done before kubectl has connected. Their output is then in the debug container's log – the second line names it.
Fix
kubectl -n laden debug -it $A --image=busybox:1.37.0 --target=whoami -- sh
Don't: Build a shell into the production image just to be able to debug. And note – anyone allowed to create ephemeral containers can read secrets from environment variables via /proc/1/environ.
Kubernetes tools, Day 7: logs and debugging – stern, kubectl debug, ephemeral containers and a container without a shell.
Go to day 7 →
FAQ
What does --target do?
It puts the debug container into the named container's process namespace. You then see its processes, and /proc/1/root shows its file system.
Does the debug container stay in the pod?
Yes. Ephemeral containers cannot be removed; they only disappear with the pod.