Sub topic 2
What ephemeral containers are
This chapter explains the working model of ephemeral containers: what they are, how they differ from regular containers, and the errors you will meet first. It assumes you know pods, containers and namespaces, and that you are comfortable with kubectl get, describe, logs and exec. A cluster at version 1.25 or later is recommended, because ephemeral containers are stable from that version.
The problem they solve
A regular container is defined in spec.containers or spec.initContainers. Kubernetes treats a pod's container list as immutable after creation, apart from a few fields such as the image. You cannot add a new container to a running pod by editing its spec.
That is a problem when you need a shell and the application image has none. Minimal and distroless images ship only the application and its runtime. Running kubectl exec on one fails like this:
kubectl exec -it web-7d4b9c-abcde -c app -- sh
# OCI runtime exec failed: exec: "sh": executable file not found in $PATHRebuilding the image with debugging tools is a poor answer. It changes what you are testing, and recreating the pod can make the problem disappear before you have seen it.
The working model
An ephemeral container is a special container that you add to a pod that is already running. It exists only for troubleshooting. Key properties:
- It is stored in the pod's
spec.ephemeralContainersfield, separate fromspec.containers. - It is added through a dedicated API subresource,
pods/ephemeralcontainers, not through a normal pod update. - It shares the pod's network namespace and can mount the pod's volumes, so it sees the same IP address, ports and shared files as the application.
- It can optionally target another container's process namespace, so you can see that container's processes. A later chapter covers this.
- It has no ports, probes or resource guarantees, and the kubelet never restarts it.
- It cannot be removed or changed once added. It stays in the pod spec until the pod is deleted.
The last point matters. Ephemeral means "not part of the pod's intended workload". It does not mean the container disappears on its own.
Inspecting the result
After you add one, the container appears in the pod's spec. You can read it back with:
kubectl get pod web-7d4b9c-abcde -o jsonpath='{.spec.ephemeralContainers[*].name}'
kubectl describe pod web-7d4b9c-abcdeThe first command prints the generated container names, such as debugger-x7k2p. The describe output has an Ephemeral Containers section showing the image, state and any target container.
The application container keeps running while you work. Its process is not restarted, so its in-memory state, open connections and the bug you are chasing are still there.
Comparing the container types
| Property | Regular container | Init container | Ephemeral container |
|---|---|---|---|
| Defined in | `spec.containers` | `spec.initContainers` | `spec.ephemeralContainers` |
| Can be added to a running pod | No | No | Yes |
| Restarted by the kubelet | Per restart policy | Per restart policy | Never |
| Probes and ports | Yes | Limited | No |
| Purpose | Run the workload | Prepare the pod | Troubleshoot |
Common errors
- Feature not available. On clusters older than 1.23 the feature is off by default, and it is stable only from 1.25. If the API rejects the request, check the server version with
kubectl version. - Forbidden. Adding an ephemeral container needs permission on the
pods/ephemeralcontainerssubresource. Havingupdateonpodsis not enough. A later chapter shows the Role. - Trying to edit with
kubectl edit. You cannot changespec.ephemeralContainersthis way. Usekubectl debug, which calls the subresource for you. - Expecting cleanup. Exiting your shell stops the container, but its entry stays in the pod spec and shows as terminated. Only deleting the pod removes it.
- Resource limits. Ephemeral containers have no resource settings of their own. Heavy tools can still compete with the application for the pod's resources, so run them sparingly on busy pods.
Exercise
A pod runs a distroless image and kubectl exec -it POD -- sh fails. You add an ephemeral container with kubectl debug, exit the shell, and later notice the container is still listed in kubectl describe pod. Why is it still listed, and how do you remove it?
Show answer
Ephemeral containers are recorded in spec.ephemeralContainers, and that list cannot be edited or shrunk after a container is added. Exiting the shell only stops the container, so it shows as terminated. The only way to remove the entry is to delete the pod, for example by letting its controller recreate it.
Exercise
Name two differences between an ephemeral container and a regular container in the same pod.
Show answer
Any two of these: it is added to a running pod through the pods/ephemeralcontainers subresource; it has no ports, probes or resource guarantees; the kubelet never restarts it; it cannot be changed or removed once added.