Tools & How-To

Kubernetes Ephemeral Containers: Debug Distroless and Crashing Pods Without Rebuilding

Use kubectl debug and ephemeral containers to inspect running Kubernetes Pods when exec is unavailable, including distroless images, RBAC requirements, security profiles and cleanup limitations.

Sunil Kumar8 min read
Server rack with blinking green status lights, representing Kubernetes cluster operations and live workload troubleshooting

Debugging a production Kubernetes Pod can be difficult when the application container has crashed, repeatedly restarts or uses a minimal distroless image with no shell and no diagnostic utilities. Rebuilding the image just to add curl, ps or a shell creates a new artifact and may change the conditions that caused the failure. Kubernetes ephemeral containers provide another option: an operator can add a temporary debugging container to an existing Pod and inspect the environment without replacing the application container.

Ephemeral containers are useful, but they are not ordinary sidecars. They are created through a dedicated API subresource, have important restrictions, are not automatically restarted and cannot be removed individually after they are added. They also create a privileged operational path that should be controlled with RBAC and admission policy. This guide explains how to use them deliberately and how to avoid turning a troubleshooting session into a security or availability problem.

What is an ephemeral container?

An ephemeral container is a temporary container added to an existing Pod for user-initiated activities such as debugging. It is useful when the application container does not contain the tools needed to inspect processes, DNS, network connections or mounted files. The feature has been stable since Kubernetes 1.25.

Unlike a normal container in a Pod specification, an ephemeral container is added through the Pod’s ephemeralcontainers subresource. Kubernetes does not treat it as a regular application component. It has no resource or scheduling guarantees, is not automatically restarted when it exits and cannot be removed or changed after it has been added. It is therefore a troubleshooting tool, not a way to run a permanent helper service.

When kubectl exec is not enough

kubectl exec starts a process inside an existing container. That works when the container has a shell or the required executable and when the application process is still available. It often fails with distroless images because those images intentionally omit shells and package managers. It can also be ineffective when the container exits too quickly, when the process is stuck during startup or when the application image lacks network diagnostic tools.

An ephemeral container can use a separate debugging image with utilities while sharing the Pod’s network namespace. With the appropriate target process namespace support, it can also inspect processes in the target container. This lets an operator investigate the live Pod rather than changing the production image first.

Start with a safe inspection workflow

Before attaching a debugger, identify the namespace, Pod, application container and current rollout. Record the Pod’s owner, node, restart count and recent events. A deployment may be replacing Pods while you investigate, so note the Pod UID and timestamp as well as its name.

kubectl get pods -n production -o wide
kubectl describe pod myapp-7b8c9d6f5f-abcde -n production
kubectl logs myapp-7b8c9d6f5f-abcde -n production -c app --previous
kubectl get pod myapp-7b8c9d6f5f-abcde -n production -o yaml

These commands are read-only. They help determine whether the issue is a container crash, failed readiness check, image-pull problem, node pressure, volume mount failure or application-level error. Do not jump directly to a privileged debugging profile before checking the basic evidence.

Add an ephemeral debug container

When the target Pod is running but the application image has no shell, use kubectl debug with a trusted debugging image. The following example assumes the application container is named app:

kubectl debug -it pod/myapp-7b8c9d6f5f-abcde \
  -n production \
  --image=busybox:1.36 \
  --target=app

Use an approved image from your organisation’s registry and pin it to a digest where possible. The example tag is illustrative; production teams should use a reviewed image version that matches their security policy. The target option asks the runtime to place the debug container in the target container’s process namespace when supported. If the runtime does not support process namespace targeting, the debug container may start without visibility into the application’s processes.

After the command starts, use tools available in the debug image to inspect the Pod’s network environment, DNS resolution and mounted paths. For example, you might check resolver configuration, test a service name with a DNS client, inspect available routes or verify whether an expected mount exists. Keep the session read-only wherever possible. Avoid editing application files, changing permissions or killing processes unless the incident commander has approved that action.

Debugging common production problems

Distroless image has no shell

Use an ephemeral container to provide diagnostic utilities rather than replacing the production image with a larger build. Inspect environment-independent facts first: DNS configuration, mounted volumes, network reachability and the Pod’s process state. Remember that a debug container does not automatically share every filesystem view or process namespace; verify the target and runtime behavior.

Application repeatedly crashes

When the container exits before exec can attach, inspect previous logs and Pod events first. If the Pod remains alive because another container is running, an ephemeral container may still help inspect shared Pod-level resources. If the whole Pod is terminating or being replaced, create a controlled copy of the workload for reproduction rather than assuming the ephemeral session will survive.

Network connection fails

A debug container can help test DNS and TCP reachability from the Pod’s network namespace. Compare the result with a known-good Pod in the same namespace and node. A successful DNS lookup does not prove the application can connect: NetworkPolicy, service endpoints, TLS, proxy settings and application credentials may still be involved.

Volume or configuration appears missing

Review the Pod specification and mount list before changing anything. The debug container may not have the same container-specific mounts as the application container. Do not assume that seeing a path in the debug container proves the application container can read it with the same permissions.

RBAC: control who can add debug containers

Adding an ephemeral container requires permission on the pods/ephemeralcontainers subresource. The Kubernetes documentation explains that this permission is intentionally separate from ordinary Pod editing and is not included in the default user-facing admin and edit ClusterRoles. A user who can patch a Pod object may still be unable to add an ephemeral container.

Check the operator’s permissions before the incident:

kubectl auth can-i patch pods/ephemeralcontainers -n production
kubectl auth can-i update pods/ephemeralcontainers -n production

If a dedicated role is required, grant only the verbs and namespace scope needed for the approved debugging workflow. Avoid giving every developer broad cluster-admin access just to make kubectl debug work. Record who started a debug session and review the audit trail after the incident.

Admission controls also matter. Kubernetes documentation warns that some admission plugins and security policies do not recognize ephemeral containers. Do not assume that a policy which validates ordinary Pod containers automatically inspects the ephemeralcontainers subresource. Test your exact Pod Security Admission version and custom webhooks, and make sure your policy engine explicitly evaluates the debug image, security context, capabilities and target namespace before the request is accepted.

Choose a debugging profile carefully

kubectl debug supports profiles that influence the security context of the debug container. A profile with elevated capabilities can expose more host or process information, but it also increases risk. Prefer the least-privileged profile that can answer the question. If a restricted or baseline Pod Security policy applies, choose a compatible profile and test it before an outage.

Do not use a privileged debugging container as the default because it is convenient. If the issue truly requires host-level access, document why, obtain approval, use a short-lived session and capture the commands executed. A node debug session is different from a Pod debug session: kubectl debug node/mynode can create a Pod that uses host namespaces and mounts the node filesystem under /host. That is a high-impact operation and should be reserved for authorised node troubleshooting.

Important limitations and cleanup

Ephemeral containers cannot define ports, liveness probes or readiness probes, and Pod resource allocations are immutable, so the ephemeral container cannot request its own CPU and memory resources in the same way as a regular application container. It will not be automatically restarted. Once added, it cannot be removed or changed individually from that Pod specification.

For that reason, plan cleanup before starting the session. When troubleshooting is complete, exit the shell, record findings, and allow the owning workload to replace the Pod through its normal rollout process when appropriate. Do not delete a production Pod solely to remove a debug container unless the workload owner has approved the replacement and availability impact is understood. A Pod managed by a Deployment may be recreated, but a standalone Pod may not be.

Common mistakes to avoid

  • Using an untrusted image: debug images run inside a production Pod context. Use a reviewed image from a controlled registry.
  • Assuming the target process namespace works everywhere: verify container-runtime support and inspect the resulting session.
  • Granting cluster-admin to solve RBAC errors: grant the narrow pods/ephemeralcontainers permissions instead.
  • Leaving sessions undocumented: record the operator, image, reason, commands and outcome.
  • Expecting the debug container to be restarted: it is not a permanent workload container.
  • Trying to remove it with kubectl edit: the API does not support removing an ephemeral container from an existing Pod.
  • Ignoring admission policies: make sure policy engines evaluate the ephemeralcontainers subresource.

Build an operational runbook

Teams should prepare a short runbook before they need it. Define who can request a session, which debug images are approved, which namespaces allow the feature, which security profiles are acceptable and where findings must be stored. Include a command to verify RBAC, a method to identify the target container and a checklist for preserving evidence. Rehearse the workflow in staging with the same container runtime and admission controls used in production.

For related cluster controls, read FutureTechie’s Kubernetes NetworkPolicy guide, Kubernetes secrets management guide and Kubernetes autoscaling guide. These topics cover traffic, credentials and capacity; ephemeral containers provide a separate operational debugging mechanism.

Official Kubernetes references

Bottom line: ephemeral containers are a practical way to investigate Pods that cannot be debugged with kubectl exec, especially when production images are intentionally minimal. Use a trusted image, grant narrow RBAC permissions, respect Pod Security admission and plan for the fact that the container cannot be removed individually. A rehearsed, audited workflow makes this capability useful without weakening the cluster’s security model.

Share this article

Written by

Sunil Kumar

Founder of FutureTechie and BoltPane. Writes clear explainers on AI, cybersecurity, gadgets and developer tools for curious readers who want signal over hype.

106 articles View profile

Get the tech briefing

AI, gadgets, software and cybersecurity — distilled into one short briefing every weekday.

Leave a Reply

Your email address will not be published. Required fields are marked *