LinuxCommandLibrary

kubectl-exec

executes a command in a container running in a pod

TLDR

Execute command in pod

$ kubectl exec [pod-name] -- [command]
copy
Open interactive shell
$ kubectl exec -it [pod-name] -- /bin/sh
copy
Execute in specific container
$ kubectl exec [pod-name] -c [container] -- [command]
copy
Execute bash in pod
$ kubectl exec -it [pod-name] -- /bin/bash
copy

SYNOPSIS

kubectl exec [options] pod-name -- command [args...]

DESCRIPTION

kubectl exec runs a command directly inside a container that is part of a running Kubernetes pod, functioning similarly to `docker exec`. It establishes a connection to the container's runtime environment through the Kubernetes API server and kubelet, allowing you to inspect files, run diagnostics, or open an interactive shell session.
The double dash (--) is required to separate kubectl flags from the command and its arguments that should be passed to the container. When combined with -i (stdin) and -t (tty) flags, it provides a fully interactive terminal session. For pods with multiple containers, the -c flag lets you specify which container to target.

PARAMETERS

-c, --container name

Container name in multi-container pod.
-i, --stdin
Pass stdin to container.
-t, --tty
Allocate TTY.
-n, --namespace name
Kubernetes namespace.

SEE ALSO

> TERMINAL_GEAR

Curated for the Linux community

Copied to clipboard

> TERMINAL_GEAR

Curated for the Linux community