Sub topic 1
Why kubectl exec is not always enough
This chapter covers the first step in the plan: why kubectl exec and kubectl logs sometimes cannot get you the information you need. It sets up the problem that ephemeral containers solve in the next chapters.
Prerequisites
This chapter assumes you already have the following:
- Basic Kubernetes concepts: pods, containers, and namespaces.
- Comfort using
kubectl get,describe,logs, andexec. - Familiarity with container images and a Linux shell.
- Access to a Kubernetes cluster, version 1.25 or later recommended.
What kubectl exec actually does
kubectl exec asks the container runtime on the node to start a new process inside an existing container. That process shares the container's filesystem, network, and process namespace. It runs the binary you name, such as sh.
This model has one hard requirement. The binary you ask for must exist in the container's image. The command also needs a running container to start the process in. When either condition fails, exec fails with it.
Failure 1: the image has no shell
Many production images are built to be small. Distroless images and images built FROM scratch contain the application and little else. They have no sh, no ls, no ps, and no package manager. This reduces size and attack surface, but it also removes your debugging tools.
Trying to open a shell in such a container fails:
kubectl exec -it web-7d4b9c-abcde -c app -- sh
# OCI runtime exec failed: exec: "sh": executable file not found in $PATHThe error comes from the container runtime, not from Kubernetes. The runtime looked for sh in the container's filesystem and did not find it.
You cannot fix this by installing tools into the running container. There is no package manager, and a container's filesystem should not be changed by hand in production anyway.
Failure 2: the container is not running
exec only works on a running container. If the application crashes on startup, the pod enters CrashLoopBackOff. The container exits before you can attach, or it restarts so often that your session is cut off.
In that state, kubectl exec returns an error such as container not found or says the container is not running. You can still read the container's output with kubectl logs --previous, but logs show only what the application chose to print. If the crash is caused by a missing file, a wrong permission, or an unreachable dependency, the logs may not say so.
Failure 3: the cluster blocks it or you cannot change the pod
Sometimes the shell exists, but you should not use it. Reasons include:
- The pod runs with a read-only root filesystem, so you cannot write tools or scratch files.
- You do not want to alter a pod that is serving production traffic.
- Your RBAC permissions allow
geton pods but notcreateonpods/exec.
The tempting workarounds and their costs
Teams often try one of these first:
| Workaround | Problem |
|---|---|
| Rebuild the image with a shell and debug tools | Slow, and the debug image differs from the production image |
| Edit the Deployment to add a sidecar | Restarts the pod, which can erase the failing state you wanted to inspect |
| Ship debug tools in every production image | Larger images and a bigger attack surface |
| SSH to the node and use the container runtime directly | Needs node access, which many users do not have, and it bypasses Kubernetes access control |
Each option either changes the thing you are trying to observe or takes away the reasons you chose a minimal image.
What you actually need
Putting the failures together gives a short list of requirements for a better tool:
- Bring your own tools, without baking them into the application image.
- Attach to a pod that is already running, without restarting it.
- See the application container's processes and filesystem.
- Work even when the application image has no shell.
- Be controlled by its own RBAC permission.
Kubernetes provides this through ephemeral containers, added to a pod with kubectl debug. The next chapter introduces them.
Quick checks before you move on
When exec fails, these commands tell you which failure you have:
# Is the container running, or crash-looping?
kubectl get pod web-7d4b9c-abcde
# Why did the last run end?
kubectl describe pod web-7d4b9c-abcde
# What did the previous instance print?
kubectl logs web-7d4b9c-abcde -c app --previous
# Am I allowed to exec at all?
kubectl auth can-i create pods/exec -n devThe Last State section of describe shows the exit code and reason of the previous run. The can-i command answers the permission question without side effects.
Exercise
A pod runs a distroless image and is in the Running state. kubectl exec -it POD -- sh fails with "executable file not found in $PATH". Which part of the exec model explains the failure, and why does editing the Deployment to add a debug sidecar make the investigation harder?
Show answer
exec starts a new process from a binary that must already exist in the container's filesystem. A distroless image has no sh, so the runtime cannot start it. Editing the Deployment changes the pod template, which replaces the pod with a new one. The failing state you wanted to inspect is lost, and the new pod may behave differently.
Exercise
A container is in CrashLoopBackOff. Name two commands that help you diagnose the failure without exec, and say what each shows.
Show answer
kubectl logs POD -c CONTAINER --previous shows the output of the last terminated instance. kubectl describe pod POD shows the exit code, the termination reason under Last State, and recent events such as failed probes or image pull problems.