Operate and shipLesson 5 of 10
- Metrics, logs, events and traces, not reconciled
- Horizontal pod autoscaling, not reconciled
- Right-sizing and in-place resize, not reconciled
- A troubleshooting method, not reconciled
- kubectl debug and ephemeral containers, not reconciled
- Debugging from the node: crictl, journalctl and nsenter, not reconciled
- Upgrades and version skew, not reconciled
- etcd, backup and disaster recovery, not reconciled
- GitOps with Argo CD, not reconciled
- Progressive delivery, not reconciled
kubectl debug and ephemeral containers
Minimal images leave nothing to exec into, and crash loops never stay up. kubectl debug brings tools in as an ephemeral container in a running pod, a modified copy of a pod, or a pod in a node's host namespaces.
Shows the lesson one block at a time. Arrow keys step, number keys jump to a scene, and Escape returns to the page where you were.
Reconcile, in 7 scenes
Why ephemeral containers
Many production images are minimal. A distroless or scratch image holds the application binary, its certificates and little else: no shell, no ps, no curl, no package manager. That is good for attack surface and image size, and it makes kubectl exec useless, because exec can only start a program that exists in the container’s filesystem. traefik/whoami is such an image:
apiVersion: v1
kind: Pod
metadata:
name: payments
labels:
app: payments
spec:
containers:
- name: app
image: traefik/whoami:v1.12.0
ports:
- containerPort: 80
kubectl get pods
NAME READY STATUS RESTARTS AGE invoicer 0/1 CrashLoopBackOff 3 (1s ago) 40s payments 1/1 Running 0 40s
kubectl exec payments -- sh
OCI runtime exec failed: exec failed: unable to start container process: exec: "sh": executable file not found in $PATH: unknown command terminated with exit code 126
kubectl debug payments --image=busybox:1.37 --target=app
Defaulting debug container name to debugger-hws5k.
Adding a shell to the image would mean a rebuild, a rollout and replacement pods, which also destroys the state you wanted to inspect. An ephemeral containerA container added to a running pod through the pods/ephemeralcontainers subresource, for inspection; it has no ports, probes or resources, and cannot be changed or removed once added. solves this. It is a container with its own image that shares an existing pod’s network namespace, and can mount the pod’s volumes, without restarting anything. Ephemeral containers have been stable since Kubernetes 1.25.
They are not a general way to change a pod. The API accepts them only through the ephemeralcontainers subresource, not through an ordinary pod update, and only for inspection: no ports, no probes, no resource requests or limits. The pod’s spec lists them under spec.ephemeralContainers and its status under status.ephemeralContainerStatuses.
Attaching
kubectl debug is the client for that subresource. The interactive form is the one you will use most:
kubectl debug -it pod/payments --image=busybox:1.37 --target=app
Under the hood, kubectl reads the pod, appends an entry to spec.ephemeralContainers with your image, command and targetContainerName, and sends the result to the ephemeralcontainers subresource. The kubelet on the pod’s node notices the added entry and starts the container in the pod’s existing sandbox; nothing else in the pod is touched. kubectl describe pod then lists it under Ephemeral Containers, with its state. The container is named debugger-<suffix> unless you name it with --container, and kubectl waits for it to start before attaching your terminal. Exiting the shell ends its process, and the entry stays in the pod as a terminated container. Running kubectl debug again adds another one; kubectl attach payments -c debugger-hws5k -it reattaches to one that is still running.
--target is what makes it useful against a single-process container. Without it, the debug container gets its own process namespace inside the pod: it shares the network, so wget localhost:80 reaches the app, but ps shows only the debugger’s own shell. With --target=app, the container joins the app container’s process namespace, when the runtime supports it, and you can see the application’s processes, send them signals, and read their open files and environment under /proc/<pid>/. The application’s filesystem is visible too, at /proc/<pid>/root, which is how you read a config file from a distroless container whose own image has no cat.
--target takes a container name, not a process. Find it with kubectl get pod payments -o jsonpath='{.spec.containers[*].name}'; a pod created by kubectl run names its container after the pod.
The image is your toolbox, so pick it for the question. busybox has a shell, ps, wget, nslookup and netstat, enough to see whether the app is running, which ports it listens on, and whether it can resolve and reach its dependencies. nicolaka/netshoot adds tcpdump, dig, curl, ss and iperf for network work. A distribution image such as debian lets you install whatever is missing, at the cost of a larger pull on a node that may already be struggling. Pin the tag, as you would for any image you run in production.
Once inside, the debugger tests the same hops as the troubleshooting method. wget -qO- localhost:80 checks that the app answers in its own pod; nslookup and wget against a Service name check the path out of it, and kubectl get endpointslices -l kubernetes.io/service-name=<service> shows what that Service routes to (the core/v1 Endpoints API is deprecated as of Kubernetes 1.33). If traffic arrives through an ingress controller, debug its pods the same way; ingress-nginx was retired on 2026-03-24 and gets no further fixes, so treat it as a hop to replace with a Gateway API implementation. For slowness, remember that kubectl top reports usage only: throttling happens only at the CPU limit and shows in cAdvisor’s container_cpu_cfs_throttled_periods_total, not in anything the debugger’s top prints.
Sharing a process namespace does not share a filesystem. Your debugger runs from busybox’s root, and the app’s volumes appear in the debug container only if you mount them, which the general profile does not do.
Profiles
Every debug container needs some security context, and the right amount depends on what you are doing. --profile picks a preset:
| Profile | What it sets |
|---|---|
general | The default from kubectl 1.36. For an ephemeral container, adds SYS_PTRACE so you can trace the target’s processes. |
baseline | Settings that pass the Pod Security baseline level, without adding capabilities. |
restricted | Settings that pass the restricted level: non-root, all capabilities dropped, RuntimeDefault seccomp, no privilege escalation. |
netadmin | Adds NET_ADMIN and NET_RAW, for tcpdump, iptables and network configuration. |
sysadmin | Runs the container privileged. |
A profile is applied to whatever kubectl creates, so the same name adapts to the target. For a copied pod, general also turns on process sharing; for a node debugging pod, it is the profile that requests the host namespaces and the /host mount, without making the container privileged.
Up to kubectl 1.35, the default was legacy, which kept the behaviour kubectl debug had before profiles existed. kubectl 1.36 made general the default and dropped legacy from the help text; it still accepts --profile=legacy with a deprecation warning, and plans to remove it in 1.39. The kubernetes.io task page on debugging running pods describes legacy as the default, which matches kubectl 1.35 and earlier, so match what you read to the kubectl version you run.
When a preset is close but not exact, --custom takes a file with a partial container spec, in JSON or YAML, and merges it over the profile. The flag is GA from kubectl 1.32. It can add environment variables, volume mounts or security context, but cannot change the container’s name, image, command, lifecycle or volume devices. A common use is the restricted profile with an image whose default user is root, which the kubelet refuses to start under runAsNonRoot; the custom file supplies a user:
env:
- name: TARGET_URL
value: http://localhost:80/
securityContext:
runAsUser: 65532
kubectl debug -it pod/payments --image=busybox:1.37 --target=app --profile=restricted --custom=debug-restricted.yaml
Copying a pod
An ephemeral container needs a pod that stays up. A container that crashes on start is gone again before you can look, and adding containers to it does not stop the crash. For that, debug a copy. --copy-to creates another pod from the original’s spec with your changes, and leaves the original alone:
kubectl logs invoicer --previous
{"time":"2026-11-19T09:01:08.516Z","level":"INFO","msg":"starting web","version":"1.2.0","pid":1}
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x18 pc=0x6b2f3a]
goroutine 1 [running]:
main.(*Config).listenAddr(...)
/src/cmd/web/config.go:42
main.main()
/src/cmd/web/main.go:27 +0x3akubectl debug invoicer --copy-to=invoicer-debug --image=busybox:1.37 --share-processes --container=shell -- sleep 3600
kubectl get pods --show-labels
NAME READY STATUS RESTARTS AGE LABELS invoicer 0/1 CrashLoopBackOff 3 (11s ago) 50s app=invoicer invoicer-debug 1/2 CrashLoopBackOff 1 (3s ago) 10s <none> payments 1/1 Running 0 50s app=payments
The original keeps cycling meanwhile. Its STATUS reads CrashLoopBackOff, a container reason, while the pod’s phase stays Running; the restart delay starts at 10 seconds, doubles to a cap of 300 seconds, and resets after 10 minutes of healthy running. Phase is Pending while images pull, and Terminating is only how kubectl displays a pod with a deletion timestamp.
The copy has the crashing app container and an added shell container, in one process namespace thanks to --share-processes. The app still crashes, but the shell stays up next to it, and you can inspect the volumes and environment the app starts with. The copy carries no labels: kubectl strips them, and the probes, unless you pass --keep-labels, --keep-liveness, --keep-readiness or --keep-startup. That keeps the copy out of the Service’s EndpointSlices and away from the ReplicaSet that owns the original.
Three changes cover most crash loops:
- Change the command.
kubectl debug invoicer -it --copy-to=invoicer-debug --container=app -- shreplaces the app container’s command with a shell, so the container starts and waits. Run the real entrypoint by hand and watch it fail. This needs an image that hassh. - Change the image.
--set-image=app=ghcr.io/kx-labs/web:1.1runs the copy with a different tag, to test whether a release introduced the crash;--set-image=*=busybox:1.37swaps every container. Debug images pulled from Docker Hub count against its limits: 100 pulls per six hours for anonymous clients per IPv4 address or IPv6 /64, 200 for authenticated personal accounts. - Add a container. As in the transcript, for an image with no shell.
A copy is a test bench, not a fix. Once it shows the cause, a missing variable or a bad tag, correct the Deployment or the ConfigMap that the real pods come from, watch the rollout, and delete the copy. Changes made inside the copy die with it.
--same-node schedules the copy on the original’s node, for problems that depend on the node. --replace deletes the original once the copy exists, which is rarely what you want for a pod a controller manages: the controller makes another.
Node debugging
Some problems live below the pods: a full disk, a kubelet that keeps restarting, a runtime that will not pull, a network interface that dropped. kubectl debug node/<name> creates a pod pinned to that node, in the host’s network, PID and IPC namespaces, with the node’s root filesystem mounted at /host:
kubectl debug node/kx-worker2 --image=busybox:1.37 -- sleep 3600
Creating debugging pod node-debugger-kx-worker2-sd5z8 with container debugger on node kx-worker2.
kubectl get pods -l app.kubernetes.io/managed-by=kubectl-debug -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES node-debugger-kx-worker2-sd5z8 1/1 Running 0 5s 172.18.0.4 kx-worker2 <none> <none>
kubectl get node kx-worker2 -o wide
NAME STATUS ROLES AGE VERSION INTERNAL-IP EXTERNAL-IP OS-IMAGE KERNEL-VERSION CONTAINER-RUNTIME kx-worker2 Ready <none> 70s v1.37.0 172.18.0.4 <none> Debian GNU/Linux 13 (trixie) 6.12.54-linuxkit (amd64) containerd://2.3.4
kubectl delete pods -l app.kubernetes.io/managed-by=kubectl-debug
pod "node-debugger-kx-worker2-sd5z8" deleted from default namespace
The pod’s IP is the node’s IP, because it shares the host network namespace. With -it instead of a command you get a shell, and the node is under /host: logs in /host/var/log, the kubelet’s configuration in /host/var/lib/kubelet, container logs in /host/var/log/pods. Because the PID namespace is the host’s, ps lists every process on the node, the kubelet and containerd included.
A typical session answers the questions the troubleshooting method raised about the node. Is the disk full? df -h /host and du under /host/var/lib/containerd and /host/var/log/pods. Is the kubelet running, and what did it say? ps shows the process, and on nodes that log to files, /host/var/log/kubelet.log holds what it said. Reading the systemd journal with the node’s own tools, chroot /host journalctl -u kubelet, needs a privileged pod: the node debugging documentation notes that chroot /host fails in the default one, so start the pod with --profile=sysadmin for that. Can the node reach the API server or a registry? The network namespace is the host’s, so wget and nslookup from the pod behave as they would from the node.
When a node is too broken to start any pod, kubectl debug cannot help: the pod needs a working kubelet and runtime. Fall back to the node log query API through the API server, kubectl get --raw "/api/v1/nodes/<node>/proxy/logs/?query=kubelet", which also needs the kubelet to answer, or to the machine’s own access path, such as SSH or a cloud provider’s serial console.
The pod is not privileged. Reading most files works; reading some process information, chroot /host, changing kernel settings and loading modules fail. Pass --profile=sysadmin when you need a privileged container. Even the default node debugging pod uses the host namespaces and a hostPath volume, which the baseline and restricted levels both forbid, so Pod Security Admission refuses it in a namespace that enforces either; run node debugging from a namespace at the privileged level. The pod is labelled app.kubernetes.io/managed-by=kubectl-debug, which is how you find it again.
Limits
Ephemeral containers trade flexibility for safety, and the rules are worth knowing before an incident:
- They cannot be removed or changed. Once added, the entry stays in the pod spec for the pod’s life. Its process ends when you exit, but the pod keeps the record until it is deleted.
- No ports, probes or resources. Ephemeral containers declare no
resources, so their usage is not reserved; a heavy tool can push the pod towards its limits or the node towards pressure. - Not for static pods. The kubelet manages static pods from files, and the API cannot add containers to them.
- RBAC. Adding one needs permission on the
pods/ephemeralcontainerssubresource, which the defaulteditandadminClusterRoles do not grant. Grant it deliberately, because an ephemeral container with--targetcan read the target’s environment and memory, Secrets included. - Pod Security Admission still applies. The API server evaluates the pod as it would be with the added container. A
sysadmindebugger in a baseline namespace is rejected, and so is a container running as root in a restricted namespace; thebaselineandrestrictedprofiles exist to fit those namespaces.
Cleanup
Debugging leaves objects behind, and they keep consuming resources and confusing the next person:
- Delete copied pods:
kubectl delete pod invoicer-debug. They have no labels and no owner, so no controller ever removes them. - Delete node debugging pods:
kubectl delete pods -l app.kubernetes.io/managed-by=kubectl-debug. They run on the host’s namespaces until you do. - Leave ephemeral containers alone; they cannot be deleted. If a pod’s record of debugging matters, for example because the debugger read Secrets, replace the pod:
kubectl rollout restarton its Deployment creates fresh pods without them.
kubectl --context kind-kx run whoami --image=traefik/whoami:v1.12.0 --port=80kubectl --context kind-kx wait --for=condition=Ready pod/whoamikubectl --context kind-kx debug -it pod/whoami --image=busybox:1.37 --target=whoami -- pskubectl --context kind-kx get pod whoami -o jsonpath='{.spec.ephemeralContainers[*].name}'kubectl --context kind-kx debug whoami -it --copy-to=whoami-debug --image=busybox:1.37 --share-processes -- shkubectl --context kind-kx debug node/kx-worker -it --image=busybox:1.37kubectl --context kind-kx delete pod whoami whoami-debugkubectl --context kind-kx delete pods -l app.kubernetes.io/managed-by=kubectl-debug
For your own kind cluster kx: copy a command and paste it into your terminal. Run in the simulator runs the same command in this page's simulated cluster instead; nothing on this page touches your machine. kubectl run names the container after the pod, so the target is whoami. In the node shell, try ls /host/var/log/pods and ps; exit when done. Run each -it command on its own and exit before the next.
Actual state
Check your understanding
Your first answer to each check is the one that counts. After a miss you can answer again; passing every check reconciles the lesson.
Lesson not yet reconciled
Desired: 4 objectives, 0 metReconciled: 4 of 4 objectives met
- Attach an ephemeral container to a running pod and target another container's process namespace. (not yet met)
- Choose a debug profile, from general, the default from kubectl 1.36, to baseline, restricted, netadmin or sysadmin. (not yet met)
- Debug a copy of a pod with a changed image or command. (not yet met)
- Open a shell on a node with kubectl debug node/<name>. (not yet met)
Pass every check above to reconcile this lesson.
Every check has passed, so the desired state is met. First-try accuracy: 0 of 5. This lesson's review cards are in your queue.
Practice
Put this lesson to work on the simulated cluster. Everything runs in your browser, with nothing to install.
Changed since the notes
This lesson draws on an earlier set of study notes. Where the documentation contradicts a claim in them, qualifies it or has moved past it, the claim is listed here with the corrected statement and the source it was checked against.
Show 10 corrections
The notes saidkubectl get endpoints my-service
Correctedcore/v1 Endpoints is deprecated as of Kubernetes 1.33, and kubectl prints 'v1 Endpoints is deprecated in v1.33+; use discovery.k8s.io/v1 EndpointSlice'. Use kubectl get endpointslices -l kubernetes.io/service-name=my-service.
The notes saidkubectl get pods -n ingress-nginx / ingress-nginx controller as the example
CorrectedKubernetes SIG Network announced the retirement of ingress-nginx on Nov 11, 2025. Maintenance ended in March 2026, and the project gets no further releases or security fixes. Use a Gateway API implementation or another maintained controller (on EKS, the AWS Load Balancer Controller).
The notes saidDocker Hub rate limits pulls
CorrectedTrue. Docker documents the limits per 6-hour window, not hourly: 100 pulls for unauthenticated users per IPv4 address or IPv6 /64, 200 for authenticated Personal accounts, and unlimited for Pro, Team and Business. Authenticate pulls or mirror images (e.g. through an ECR pull-through cache).
The notes saidCrashLoopBackOff backoff behavior
CorrectedThe default backoff starts at 10s and doubles (20s, 40s, …) up to 300s, and resets once a container runs for 10 minutes without problems. KubeletCrashLoopBackOffMax (alpha 1.32, beta and on by default from 1.35) lets a node lower the cap through the kubelet's maxContainerRestartPeriod. ReduceDefaultCrashLoopBackOffDecay (alpha since 1.33) shortens the defaults only when enabled.
Sourcekubernetes.io/docs/concepts/workloads/pods/pod-lifecycle
The notes saidkubectl debug behavior
Correctedkubectl debug --profile accepts legacy, general, baseline, restricted, netadmin and sysadmin. kubectl 1.36 made general the default profile (previously legacy); the kubernetes.io task pages may still describe legacy.
The notes said'kubectl debug node/… creates a privileged pod'
CorrectedConfirmed. The node debugging pod shares the node's IPC, network and PID namespaces and mounts the node's root filesystem at /host, but it isn't privileged. Use --profile=sysadmin for a privileged container.
Sourcekubernetes.io/docs/tasks/debug/debug-cluster/kubectl-node-debug
The notes said'Pods using more than requests might be throttled (CPU)'
CorrectedConfirmed. CFS throttling happens only when a container reaches its CPU limit. Above its request, a container competes for spare CPU in proportion to its request (CPU weight); a container with no CPU limit isn't throttled by CFS quota.
Sourcekubernetes.io/docs/concepts/configuration/manage-resources-containers
The notes saidThe metrics.k8s.io API (kubectl get --raw …/pods/my-pod) and kubectl top show usage only, not throttling
CorrectedConfirmed. metrics.k8s.io (and kubectl top) reports only CPU and memory usage for autoscaling. To see throttling, use cAdvisor's container_cpu_cfs_throttled_periods_total relative to container_cpu_cfs_periods_total (kubelet /metrics/cadvisor, Prometheus or Container Insights).
Sourcekubernetes.io/docs/tasks/debug/debug-cluster/resource-metrics-pipeline
The notes said'kubectl debug my-pod --image=busybox' without --target=<container> can't see the app's processes
CorrectedConfirmed. An ephemeral container joins the target container's process namespace only when you pass --target=<container> (and the runtime supports it); otherwise ps shows only the debug container's processes.
Sourcekubernetes.io/docs/tasks/debug/debug-application/debug-running-pod
The notes said'Terminating' is a kubectl display state, not a pod phase
CorrectedConfirmed. Pod phase is only Pending, Running, Succeeded, Failed or Unknown. kubectl shows 'Terminating' when deletionTimestamp is set; ContainerCreating, ErrImagePull and CrashLoopBackOff are container state reasons, and the phase stays Pending during image pulls.
Sourcekubernetes.io/docs/concepts/workloads/pods/pod-lifecycle
Every correction across the guide, by note and topic.
Sources
Checked against kubernetes.io and AWS documentation for Kubernetes 1.37 on .
- kubernetes.io/docs/concepts/workloads/pods/ephemeral-containers
- kubernetes.io/docs/tasks/debug/debug-application/debug-running-pod
- kubernetes.io/docs/reference/kubectl/generated/kubectl_debug
- kubernetes.io/docs/tasks/debug/debug-cluster/kubectl-node-debug
- kubernetes.io/docs/tasks/configure-pod-container/share-process-namespace
- kubernetes.io/docs/concepts/security/pod-security-admission
- kubernetes.io/docs/concepts/security/pod-security-standards
- kubernetes.io/docs/tasks/debug/debug-application/debug-pods
- kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle
- kubernetes.io/blog/2025/04/24/endpoints-deprecation
kubectl debug and ephemeral containers
↓ ↑ step, 1 to 7 scene, Esc leave