Kubernetes Commands
devopskubernetesReferenceBeginner
kubectl exec
Execute commands or open an interactive terminal session inside a running container within a Kubernetes Pod.
Verified: 2026-10-01
Tested on:kubernetes 1.31
Platforms:
all
#kubernetes#kubectl#exec#bash#debugging
Command
kubectl exec -it web-app-abc12 -- /bin/sh
Short Description
kubectl exec executes commands directly inside an active container in a Pod, allowing interactive shell sessions and runtime debugging.
Syntax
kubectl exec (POD | TYPE/NAME) [-c CONTAINER] [FLAGS] -- COMMAND [args...]Examples
1. Open Interactive Shell Session
kubectl exec -it web-app-559d7b489d-abc12 -- /bin/sh2. Run Database Migration Inside App Pod
kubectl exec api-deployment-abc12 -- npx prisma migrate deploy3. Test Network Connectivity to Another Service
kubectl exec web-app-abc12 -- wget -qO- http://backend-api:8080/healthzCommon Options
| Option | Shorthand | Description |
|---|---|---|
--stdin | -i | Pass standard input (stdin) to the container. |
--tty | -t | Allocate a pseudo-TTY for terminal color and interactive input. |
--container | -c | Target container name in multi-container pods. |