Executing in Pod

Difficulty: Beginner

Question

How do you get a shell inside a running Pod, and what do you do when the container image has no shell?

Answer

Run kubectl exec -it <pod> -- sh (or bash if the image has it). -i keeps stdin open, -t allocates a terminal, and everything after -- is the command to run inside the container. If the Pod has several containers, add -c <container>. If the image is distroless or minimal with no shell, use kubectl debug to attach an ephemeral debug container that shares the Pod's namespaces.

How exec works

kubectl exec sends a request to the API server's pods/exec subresource. The API server forwards it to the kubelet on the Pod's node, which asks the container runtime to start a new process inside the existing container. The process runs with the container's filesystem, environment variables and network namespace. Nothing is restarted, and when you exit only that extra process ends.

Useful forms:

  • kubectl exec -it api-xyz -- sh for an interactive shell.
  • kubectl exec api-xyz -- env to run a single command without a TTY.
  • kubectl exec -it api-xyz -c envoy -- sh to pick the sidecar.
  • kubectl exec -it deploy/api -- sh to let kubectl pick a Pod from a Deployment.

When there is no shell

Many production images (distroless, scratch-based Go binaries) ship without sh, so exec fails with executable file not found. Options:

  1. Ephemeral debug container (stable since 1.25): kubectl debug -it <pod> --image=busybox:1.36 --target=<container>. With --target, it shares the target container's process namespace so you can see its processes and reach its filesystem via /proc/<pid>/root.
  2. Debug copy: kubectl debug <pod> -it --copy-to=api-debug --container=api --image=myorg/api:debug creates a copy of the Pod with a modified image, useful when the original keeps crashing.
  3. Node debugging: kubectl debug node/<node> -it --image=ubuntu starts a Pod on the node with the host filesystem mounted at /host.

Exec is for debugging, not fixing

Changes made inside a container are lost when it restarts and cannot be traced in Git. If you find yourself editing config with vi in production, fix the manifest or ConfigMap and roll out instead. Common legitimate uses:

  • Check DNS and connectivity: nslookup, wget -qO- http://payments:8080/health.
  • Inspect mounted config and secrets paths.
  • Run a one-off admin command, like a Django or Rails console, during an incident.

Security

create on pods/exec is a powerful permission: it lets a user read any secret mounted in that Pod and act with the Pod's ServiceAccount. Restrict it in production namespaces with RBAC and enable audit logging so every exec is recorded.

What happens on kubectl exec. exec starts a new process inside an existing container through the API server and kubelet.
  1. kubectl → API server: POST pods/exec with command sh
  2. API server → kubelet: RBAC passes, forward exec request
  3. kubelet → Container: Runtime starts sh in container
  4. Container → kubectl: stdin and stdout streamed back

Code examples

Exec and debug commands

# Interactive shell (try bash if sh is not enough)
kubectl exec -it api-7d9f8c6b5-x2k4q -n orders -- sh

# Single command, no TTY
kubectl exec api-7d9f8c6b5-x2k4q -- printenv DATABASE_HOST

# Pick a container in a multi-container Pod
kubectl exec -it api-7d9f8c6b5-x2k4q -c envoy -- sh

# Distroless image: attach an ephemeral debug container
kubectl debug -it api-7d9f8c6b5-x2k4q \
  --image=busybox:1.36 --target=api

# Debug the node itself (host filesystem at /host)
kubectl debug node/ip-10-0-1-23 -it --image=ubuntu:24.04

exec runs inside the existing container; kubectl debug adds a temporary container with your own tools when the image has none.

Key points

  • kubectl exec -it <pod> -- sh; -c picks a container; everything after -- is the command.
  • exec starts an extra process in the running container; nothing restarts.
  • Distroless images have no shell: use kubectl debug with an ephemeral container.
  • Changes made via exec are lost on restart; fix manifests instead.
  • pods/exec is a sensitive RBAC permission; restrict and audit it.

Common follow-up questions

  • What are ephemeral containers and why were they added?
  • How would you debug a container that crashes too fast to exec into?
  • How do you copy a file out of a Pod?
  • Why is pods/exec considered a privileged permission?

Interview tips

  • Explain what -i, -t and -- mean rather than just reciting the command.
  • Mention kubectl debug for distroless images; it shows you know modern practice.
  • Add that exec is for investigation, not for changing production state.

Concepts covered

kubectl exec, Interactive Shell, kubectl debug, Ephemeral Containers, Distroless Images

More Kubernetes & Orchestration questions