debug
kubectl exec
Executes a command inside a running container. Use -it for an interactive TTY shell; omit them for a one-shot command whose stdout you want to capture for scripts.
kubectl exec (POD | TYPE/NAME) [-c CONTAINER] [-it] -- COMMAND [args...] Common flags
| Flag | Purpose |
|---|---|
| -i, --stdin | Keep STDIN open on the container. |
| -t, --tty | Allocate a TTY (combine with -i for shells). |
| -c, --container | Target container in a multi-container pod. |
| -n, --namespace | Namespace of the pod. |
| --quiet | Suppress kubectl's own prompt messages, useful when piping. |
Examples
kubectl exec -it web-abc -- /bin/sh Open an interactive shell inside the pod.
kubectl exec web-abc -- env One-shot: print the container's environment to your terminal.
kubectl exec -it deploy/api -c app -- bash Interactive shell into a named container of a Deployment's pod.
kubectl exec web-abc -- cat /etc/config/app.conf Read a config file from inside the container.
Gotcha
The double-dash (--) separates kubectl flags from the container command; forgetting it makes flags leak into kubectl. -it against a pod without a TTY-capable binary fails immediately.
Related commands
kubectl logs
Prints container logs from a pod, deployment, or other workload. Supports streaming, previous-instance recovery after crashes, and selecting a specific container in multi-container pods.
kubectl cp
Copies files and directories between your local filesystem and a container in a pod. Uses tar under the hood, so the target container must have a working tar binary on its PATH.
kubectl run
Imperatively creates a single Pod running a specified image. In modern kubectl it only produces a Pod (Deployment/Job generators were removed) - use it for ad-hoc debugging containers, not durable workloads.
kubectl port-forward
Forwards one or more local ports to a pod, service, or deployment inside the cluster via the API server. Ideal for reaching internal services (databases, admin UIs) from your laptop without exposing them publicly.
kubectl proxy
Runs a local HTTP proxy that authenticates as your kubeconfig user and forwards requests to the Kubernetes API server. Useful for exploring the raw API with curl or serving the dashboard behind localhost auth.