Skip to content

Debugging Pods with kubectl — describe, logs, exec, and ephemeral containers

Diagnose Pod failures fast — get, describe, logs, exec, port-forward, and kubectl debug for distroless containers.

When a Pod misbehaves, the diagnostic path is short and consistent: get, describe, logs, exec, sometimes debug. This article is the runbook — what each command shows, the order to run them in, and the kubectl features (ephemeral debug containers, --previous, port-forward) that turn a stuck workload into a one-minute fix or a clear next step.

How to verify

The first thing you need to know is the Pod’s high-level state and recent events:

kubectl get pods -n team-payments -l app=web
kubectl describe pod web-7fdb6f9b56-2vp8x -n team-payments
kubectl get events -n team-payments --sort-by=.lastTimestamp | tail -20

The Pod’s status column tells you the failure class (CrashLoopBackOff, ImagePullBackOff, OOMKilled, Pending, ContainerCreating, Init:Error). describe shows the recent events for that Pod specifically — most diagnosis ends here.

For a richer view of the Pod’s spec and status:

kubectl get pod web-7fdb6f9b56-2vp8x -n team-payments -o yaml | yq '.status'

What’s happening

kubectl is a thin client over the Kubernetes API. Every debug command is a read against an object (get, describe) or a connection through the API server to the kubelet (logs, exec, port-forward, debug). The API server keeps the state; the kubelet on each node runs the containers and surfaces logs, terminal sessions, and ephemeral containers when asked. None of this requires SSH access to the nodes.

Distinct failure shapes need distinct tools:

  • Pending Pod: the scheduler couldn’t place it. describe shows 0/4 nodes are available: insufficient cpu or a taint mismatch. No container exists, so logs is empty.
  • ContainerCreating / ImagePullBackOff: the kubelet is trying but failing — image pull error, missing Secret for the registry, image name typo. describe events show the pull error verbatim.
  • CrashLoopBackOff: the container started and exited. logs --previous shows the last exit’s output; without --previous you get the current attempt, which may not have started yet.
  • Running but broken: exec for a shell, port-forward to hit the app from your machine, logs -f to tail.
  • Distroless / scratch container: no shell to exec into. kubectl debug attaches an ephemeral container sharing the Pod’s namespaces.

The procedure

  1. Get and describe. This catches 80% of failures:

    kubectl get pod web-7fdb6f9b56-2vp8x -n team-payments -o wide
    kubectl describe pod web-7fdb6f9b56-2vp8x -n team-payments

    The Events section at the bottom of describe is where most failures explain themselves.

  2. Logs, current and previous. --previous is essential for CrashLoopBackOff Pods:

    kubectl logs web-7fdb6f9b56-2vp8x -n team-payments
    kubectl logs web-7fdb6f9b56-2vp8x -n team-payments --previous
    kubectl logs web-7fdb6f9b56-2vp8x -n team-payments -c init-db
    kubectl logs -f deploy/web -n team-payments --tail=200

    -f follows; --tail limits initial output; -c selects a specific container in a multi-container Pod; --previous shows logs from the previous attempt.

  3. Exec into a running container for a shell — works when the image has /bin/sh or /bin/bash:

    kubectl exec -it web-7fdb6f9b56-2vp8x -n team-payments -- /bin/sh
    kubectl exec web-7fdb6f9b56-2vp8x -n team-payments -- env | sort
  4. Port-forward to talk to the Pod directly without going through the Service or ingress:

    kubectl port-forward -n team-payments pod/web-7fdb6f9b56-2vp8x 8080:8080
    # then from another terminal:
    curl -sv http://localhost:8080/healthz
  5. Attach an ephemeral debug container when the running container has no shell (distroless, scratch, hardened minimal image):

    kubectl debug -n team-payments \
      web-7fdb6f9b56-2vp8x \
      -it --image=nicolaka/netshoot \
      --target=web

    Inside the debug container you share the target’s process and network namespaces — ps, ss, curl localhost all work.

  6. Debug a node by running a privileged Pod that chroots into it — useful when you suspect the runtime, the kubelet, or kernel state:

    kubectl debug node/ip-10-0-0-12 -it --image=ubuntu
    # inside:
    # chroot /host
    # systemctl status containerd
    # journalctl -u kubelet -n 100
  7. Run an interactive copy of a Pod with overrides — useful for poking at a deploy with extra env vars or commands without modifying the real Pod:

    kubectl debug -n team-payments \
      web-7fdb6f9b56-2vp8x \
      --copy-to=web-debug --container=web \
      --image=registry.example.com/web:1.4.2 \
      -- /bin/sh

Common pitfalls

  • kubectl logs without --previous on a CrashLoopBackOff Pod returns nothing or partial output — the container died before producing logs in the current attempt; --previous is what you want.
  • exec requires a shell in the image — distroless or scratch images have nothing to exec; use kubectl debug.
  • kubectl debug --target=<container> only works if process.shareProcessNamespace is enabled or the runtime supports it (containerd does); without that, the debug container sees its own process tree.
  • kubectl get pod -o yaml returns the full object including managed fields and ConfigMap-projected volumes — pipe through yq '.status' or yq '.spec' to narrow.
  • Events older than the etcd compaction window (default ~1 hour) are gone — diagnose recent failures fast or rely on a log aggregator.

We operate the kubectl debug workflow for clients with ephemeral containers enabled, distroless images everywhere, and runbooks that walk new engineers through describe → logs --previous → debug without surprises. See /en/services/managed-operations/.