All guides

Debug a running pod with kubectl debug

Distroless images include no shell and no debugging utilities, so kubectl exec alone is not enough to troubleshoot them. The kubectl debug command adds an ephemeral container to a pod, copies a pod with changes, or creates a pod on a node.

A desk with two monitors showing code

Why minimal images need this

A distroless image is a minimal container image. It shrinks the attack surface and the exposure to bugs and vulnerabilities. They do not include a shell or any debugging utilities. In the documentation's example with an image that has no shell, kubectl exec -it <pod-name> -- sh ends with the error that sh was not found in $PATH. kubectl exec is also insufficient when a container has crashed.

kubectl debug supports three actions: add an ephemeral container to a running pod, create a copy of a pod with some attributes changed, and create a pod that runs in a node's host namespaces.

An ephemeral container in the pod

Shell

kubectl debug -it <pod-name> --image=busybox:1.28 --target=<container-name>

This adds an ephemeral container to the running pod, without restarting the pod, and attaches to it. The image is yours to choose. Ephemeral containers are stable since Kubernetes v1.25.

--target targets the process namespace of the named container, so the debug container sees that container's processes. The container runtime must support it. If it does not, two outcomes are possible: the ephemeral container is not started, or it starts in a process namespace of its own, where ps shows none of the other containers' processes.

Ephemeral containers have limits. They may not have ports, probes or resource settings, they are never restarted automatically, and static pods do not support them.

Permission to add one

Adding an ephemeral container uses the pods/ephemeralcontainers subresource. The default admin and edit ClusterRoles leave it out by design, and a user who lacks it gets a Forbidden error from kubectl debug. The documentation's ClusterRole for specific users or groups:

YAML

apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: debug-ephemeralcontainers
rules:
- apiGroups: [""]
  resources: ["pods/ephemeralcontainers"]
  verbs: ["patch", "update"]

The documentation gives the reason for the separate permission: with it, cluster administrators decide explicitly who is allowed to add containers to running pods.

A copy of the pod

kubectl exec cannot help when the application crashes on startup. For that case kubectl debug creates a copy of the pod with configuration values changed. The image and the name of the copy are yours to choose:

Shell

kubectl debug <pod-name> -it --image=busybox:1.28 --share-processes --copy-to=my-debug
kubectl debug <pod-name> -it --copy-to=my-debug --container=<container-name> -- sh

The first command copies the pod and adds a debug container. --share-processes lets the containers in the copy see each other's processes, and under a shared process namespace a container's filesystem is visible to the others through the /proc/$pid/root link. The second command copies the pod and changes the command of the named container to an interactive shell, which is the documentation's answer to a container in CrashLoopBackOff. --set-image changes container images in the copy, for example to a debugging build.

Four flags, all off by default, keep parts of the original in the copy: --keep-labels, --keep-liveness, --keep-readiness and --keep-startup. --same-node schedules the copy on the same node as the original.

A shell on the node

Shell

kubectl debug node/<node-name> -it --image=busybox:1.28

This creates a pod on the node, which helps when you cannot reach the node over SSH. The container shares the host's IPC, network and PID namespaces and finds the node's root filesystem mounted at /host. Because the pod runs unprivileged, some process information may be unreadable and chroot /host may fail. Add --profile=sysadmin if you need a privileged pod. The command does not work when the node is down.

Debug profiles

--profile applies a set of predefined properties, such as the security context, to the debug container. Two official pages disagree on the list. The kubectl reference for v1.37 gives five options with general as the default. The task page Debug Running Pods lists a sixth, legacy, and still calls it the default. The v1.36 changelog explains the gap: the default changed from legacy to general in that release, and legacy is planned to be removed in v1.39. A v1.35.0 client run for this guide still shows legacy as its default.

  • general: a reasonable set of generic properties for each debugging journey.
  • baseline and restricted: compatible with the Pod Security Standard of the same name. Choose the matching one for pods governed by that policy.
  • netadmin: includes network administrator privileges.
  • sysadmin: includes system administrator (root) privileges. In the documentation's example the container is created as privileged.

Because the default depends on the client version, name the profile in the command. --custom takes a YAML or JSON file with a partial container spec to adjust a profile, such as environment variables or capabilities.

Cleanup after a session

  1. Ephemeral containers: you may not change or remove one after you have added it to a pod. The documentation's example ends by deleting the pod.
  2. Copies: the documentation's reminder is to delete the debugging pod when you are finished, with kubectl delete pod and the name of the copy.
  3. Node sessions: kubectl debug generates the pod's name from the node's name, as in node-debugger-mynode-pdx84. The documentation's example lists it with kubectl get pods, in Completed status, and then deletes it.

Sources

Want a second pair of eyes on yours? We do this work for a living.

Book a call

More guides