Skip to main content
⚡ OpenDevDocs
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/sh

2. Run Database Migration Inside App Pod

kubectl exec api-deployment-abc12 -- npx prisma migrate deploy

3. Test Network Connectivity to Another Service

kubectl exec web-app-abc12 -- wget -qO- http://backend-api:8080/healthz

Common Options

OptionShorthandDescription
--stdin-iPass standard input (stdin) to the container.
--tty-tAllocate a pseudo-TTY for terminal color and interactive input.
--container-cTarget container name in multi-container pods.

Warnings

On this page