Skip to content

Kubernetes Pod exec debugging pattern

The Kubernetes Pod exec debugging pattern is a troubleshooting and experimentation technique that involves executing commands directly inside a running container. This is typically accomplished using kubectl exec to gain shell access or to run specific utilities within the Pod's environment.^[400-devops-07-monitoring-and-observability-k8s-istio-samples-sleep-readme.md]

A common implementation of this pattern involves deploying a dedicated "utility" or "sleep" container—such as an Ubuntu image with networking tools like curl installed—into the cluster.^[400-devops-07-monitoring-and-observability-k8s-istio-samples-sleep-readme.md] Once deployed, this Pod serves as a secure execution environment (a "jump host") within the network, allowing developers to invoke other services and test connectivity from an internal perspective.^[400-devops-07-monitoring-and-observability-k8s-istio-samples-sleep-readme.md]

Usage Workflow

  1. Identify the target pod: Retrieve the name of the running Pod you wish to access, often by selecting a Pod with a specific label using kubectl get [Pod](<./pod.md>) -l <label>.^[400-devops-07-monitoring-and-observability-k8s-istio-samples-sleep-readme.md]
  2. Execute the command: Run kubectl exec with the appropriate flags (e.g., -it for interactive TTY access) and the target container name (-c) to invoke a shell or command.^[400-devops-07-monitoring-and-observability-k8s-istio-samples-sleep-readme.md]
  3. Run diagnostics: From the interactive session, use the installed tools (e.g., curl) to send requests to neighboring services or external endpoints to verify behavior.^[400-devops-07-monitoring-and-observability-k8s-istio-samples-sleep-readme.md]

Example command sequence:

export SLEEP_POD=$([kubectl](<./kubectl.md>) get [Pod](<./pod.md>) -l app=sleep -o jsonpath={.items..metadata.name})
[kubectl](<./kubectl.md>) exec -it $SLEEP_POD -c sleep -- curl http://ratings.default.svc.cluster.local:9080/ratings/1
^[400-devops-07-monitoring-and-observability-k8s-istio-samples-sleep-readme.md]

Sources

^[400-devops-07-monitoring-and-observability-k8s-istio-samples-sleep-readme.md]