kubectl exec finds no shell The checkout Pod returns errors, and you want to look inside. You run kubectl exec -it checkout -- sh, and the runtime answers: executable file not found in $PATH.
The image is distroless. It holds the app binary and its libraries, but no shell, no ps and no curl. That keeps it small and gives an attacker less to work with, but exec can only run programs that exist in the image.
Key idea kubectl exec runs a program inside an existing container. If the image has no such program, exec cannot help.
kubectl debug adds an ephemeral container to the Pod kubectl debug -it checkout --image=busybox:1.36 --target=app adds a new container to the running Pod. It does not write pod.spec. It updates the Pod's ephemeralcontainers subresource, and the kubelet starts the extra container next to app.
An ephemeral container shares the Pod's network namespace, so it sees the same IP and can reach the app on localhost. It brings its own tools from its own image. Ephemeral containers are stable since Kubernetes v1.25.
Key idea kubectl describe pod lists it under Ephemeral Containers. Pick a small image with the tools you need, such as busybox or a network toolbox.
--target lets you see the app's processes By default, each container in a Pod has its own process namespace, so ps in the debugger shows only its own processes. --target=app joins the process namespace of the app container.
Now ps shows the app process. You can read /proc/<pid>/environ, list open files in /proc/<pid>/fd, or look at the app's filesystem through /proc/<pid>/root. The runtime must support --target; if it does not, the debugger may start with its own process namespace.
Key idea Without --target you debug the network. With --target you also see the process and its files.
An ephemeral container cannot be removed When you exit the shell, the debugger stops and stays in the Pod as Terminated. It is never restarted, and you cannot change or remove it. Each kubectl debug run adds another entry to the Pod.
Ephemeral containers also cannot set ports, probes or resources. Pod Security admission checks them like normal containers, so in a namespace with the baseline or restricted level, pick a matching --profile.
Watch out The only way to get rid of an ephemeral container is to replace the Pod, for example with kubectl rollout restart on its Deployment.
A crashing container gives you nothing to attach to A new release makes app exit at startup. The Pod is in CrashLoopBackOff, so the container is down most of the time. exec fails, and an ephemeral container with --target has no live process to look at.
Read the last crash first: kubectl logs checkout --previous prints the logs of the container that just died. If the message says a config file is missing or a path is wrong, you need to look at the files the app sees.
Watch out logs --previous is the first stop for a crash loop. Plain kubectl logs shows the new container, which may not have logged anything yet.
--copy-to starts a copy with a different command kubectl debug checkout -it --copy-to=checkout-debug --container=app -- sh creates a new Pod from the original spec, with the app container's command replaced by sh. The image has no sh, so add --set-image=app=busybox:1.36, or add a debug container with --image and keep --share-processes on.
The copy starts, stays up, and you can check mounts, environment and paths the way the crashing app would see them. By default the copy drops the original labels and probes, so Services do not send it traffic and probes do not kill it.
Key idea Use --copy-to when the original keeps crashing or you must change the command or image. The original Pod is left as it is.
port-forward reaches the copy from your machine The copy has no labels, so no Service points at it. To call its HTTP port, run kubectl port-forward pod/checkout-debug 8080:8080. kubectl opens a stream through the API server and the kubelet into the Pod's network namespace.
Now curl localhost:8080 on your laptop reaches the copy, without a Service, Ingress or open firewall port. The same works for the original Pod, and for a tool you start in an ephemeral container, because it shares the Pod's network.
Key idea port-forward lasts as long as the command runs, and it needs RBAC on pods/portforward.
kubectl debug node/ runs a Pod in the host namespaces If the problem is the node itself, run kubectl debug node/worker-1 -it --image=ubuntu. kubectl creates a Pod pinned to that node. It runs in the host IPC, network and PID namespaces, and the node's root filesystem is mounted at /host.
You can read /host/var/log, see every process on the node and test the node's network, with no SSH access. The Pod is not privileged by default, so some reads and chroot /host may fail. --profile=sysadmin gives it root privileges.
Key idea A node debugging Pod is a normal Pod. It gets a name like node-debugger-worker-1-xxxxx, and you must delete it yourself.
Debugging needs its own permissions A teammate with the built-in edit role tries kubectl debug and gets Forbidden: cannot patch resource pods/ephemeralcontainers. That subresource is on purpose not part of the default edit and admin roles, because adding a container to a running Pod is powerful.
Grant patch and update on pods/ephemeralcontainers only to the people who debug. Node debugging needs permission to create Pods, and the namespace must allow host namespaces and hostPath, which the baseline and restricted Pod Security levels block.
Watch out kubectl auth can-i patch pods/ephemeralcontainers -n shop tells you before you try.
Copies and node Pods stay until you delete them kubectl debug does not clean up after you. The copy checkout-debug and the node-debugger Pod keep running and keep using resources. A node Pod with host access is also a security risk if left behind.
Delete them with kubectl delete pod checkout-debug and kubectl delete pod node-debugger-worker-1-xxxxx. Ephemeral containers go away only when their Pod is replaced.
Watch out Use a clear naming pattern for copies and list leftover debug Pods with kubectl get pods -A | grep debug.
Pick the tool by what is broken Container running but image has no tools: add an ephemeral container, with --target to see its processes. Container crashing: read logs --previous, then start a copy with --copy-to and a different command or image. Node problem: kubectl debug node/ with the host filesystem at /host.
Each one needs RBAC, and each one leaves something behind. Ephemeral containers stay in the Pod until it is replaced. Copies and node Pods stay until you delete them.
Summary Debug from outside the image instead of putting a shell into production images.