kubectl 1.37 reference · Debugging

kubectl exec Command (kubectl)

Run a command in a container of a running pod, for example a shell, a database client or a quick check.

kubectl 1.37

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

kubectl exec [-it] POD|TYPE/NAME [-c CONTAINER] -- COMMAND [ARGS...]

Parameters

ParameterDescription
-it Interactive terminal.
-c CONTAINER Choose the container.
-- COMMAND The command after a double dash.

Examples

Run a command
kubectl create namespace rc-ref-exec > /dev/null
trap 'kubectl delete namespace rc-ref-exec --wait=false > /dev/null' EXIT
kubectl -n rc-ref-exec run app --image=busybox:1.37 --env=MODE=demo -- sleep 3600 > /dev/null
kubectl -n rc-ref-exec wait --for=condition=Ready pod/app --timeout=120s > /dev/null
kubectl -n rc-ref-exec exec app -- sh -c 'echo MODE=$MODE; hostname'

Output

MODE=demo
app

Run this on your computer — see “Get Started” for setup.

Exec into a deployment's pod
kubectl create namespace rc-ref-exec2 > /dev/null
trap 'kubectl delete namespace rc-ref-exec2 --wait=false > /dev/null' EXIT
kubectl -n rc-ref-exec2 create deployment cache --image=redis:8-alpine > /dev/null
kubectl -n rc-ref-exec2 rollout status deployment/cache --timeout=120s > /dev/null
kubectl -n rc-ref-exec2 exec deploy/cache -- redis-cli ping

Output

PONG

Run this on your computer — see “Get Started” for setup.

Was this page helpful?