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

← All kubectl commands · Docker vs Kubernetes