Difficulty: Beginner
How do you get a shell inside a running Pod, and what do you do when the container image has no shell?
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.
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.Many production images (distroless, scratch-based Go binaries) ship without sh, so exec fails with executable file not found. Options:
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.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.kubectl debug node/<node> -it --image=ubuntu starts a Pod on the node with the host filesystem mounted at /host.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:
nslookup, wget -qO- http://payments:8080/health.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.
# 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.
kubectl exec, Interactive Shell, kubectl debug, Ephemeral Containers, Distroless Images