CrackOps
Interview PrepTracksLinuxAWS LabSoonInterview RoomSoon
Sign inStart free
CrackOps

Interview prep for the people who keep production up.

[email protected]
ProductInterview PrepTracksDashboardInterview Room
RolesDevOpsSREPlatformCloud
CompanyAboutContactPrivacyTerms
© CrackOps
Linux / Kubernetes / kubectl exec

kubectl exec

Run a command or shell inside a running container

Intermediatekubernetesdebuggingshell

kubectl exec runs a command in a container that is already running, like docker exec but through the Kubernetes API. Use it to check config, environment and connectivity from the pod's point of view, or open an interactive shell with -it.

Syntaxkubectl exec [-it] (POD | TYPE/NAME) [-c CONTAINER] [-n NAMESPACE] -- COMMAND [ARGS...]

Watch it run

The api can't reach its database. See which settings and DNS the running container actually has, without opening a shell.

devops@devops-lab: ~ — zsh — 96×28
➜ ~
Step 1 of 3
kubectl exec -n prod api-7f9c5d4b6-2xkqp -- env | grep ^DB_

env runs inside the container; its output comes back to your terminal and grep runs locally. No -it needed for a one-shot command whose output you pipe.

Anatomy of a command

$
commandkubectl

The Kubernetes CLI.

How it works

kubectl sends a POST to the pod's exec subresource, /api/v1/namespaces/prod/pods/NAME/exec?command=sh&stdin=true&tty=true&container=api, and upgrades the connection to a streaming protocol (WebSocket in current versions, SPDY before 1.31). The API server checks RBAC for create on pods/exec, then proxies the stream to the kubelet on the pod's node. The kubelet asks the container runtime (containerd, via CRI) to start a new process inside the container's existing namespaces and cgroup: same filesystem, network, user and limits as the app. Your keystrokes and its output flow back over that one connection. When the command exits, kubectl exits with the same code. Nothing about the pod spec changes, and anything you change inside the container is lost when it restarts.

One upgraded HTTPS stream from your laptop, through the API server and the node's kubelet, into a new process inside the container.

upgradecan-i?proxy streamExec (CRI)enter namespacesspawnkubectlexec -it -- shAPI serverauthn + RBACkubeletnode :10250containerdCRI runtimeapi containerPID 1: node appsh processnew PID, same nsRBACpods/exec create
1/7
You run kubectl exec

kubectl reads your kubeconfig and sends POST /api/v1/namespaces/prod/pods/NAME/exec with command=sh, stdin and tty, asking to upgrade to a streaming (WebSocket) connection.

← → to step

Options you'll actually use

-i, --stdinPass your stdin to the process (needed for pipes and shells)
-t, --ttyAllocate a terminal (prompts, colours, Ctrl+C)
-c NAMEContainer to run in
-n NSNamespace of the pod
--End of kubectl flags; the rest is the container command
deploy/NAMEExec into one pod of a Deployment
-q, --quietOnly print the remote command's output

Common mistakes

kubectl exec pod ls -la /app

Without -- kubectl treats -la as its own flags; current versions reject the form altogether.

kubectl exec pod -- ls -la /app
Hot-fixing a config file inside a container with exec and vi

The change is lost on the next restart and other replicas never get it; the pod now differs from Git.

Change the ConfigMap or image, then kubectl rollout restart deploy/NAME
kubectl exec -it pod -- sh | tee out.txt

-t with a pipe mixes carriage returns into the output and some commands behave differently on a TTY.

kubectl exec pod -- sh -c 'cmd' > out.txt # no -t when capturing output
Granting developers pods/exec in prod 'just for debugging'

exec is a shell with the pod's service-account token, secrets and network access: effectively full access to that workload.

Give exec only where needed, audit it, and prefer kubectl debug or logs

Interview questions

Practice

Try these on a Linux machine of your own. Each task has a hint and a solution.

  1. 1

    Write the command that prints the environment variables of the container sidecar in pod web-5c7b9d6f4-q2x7z (namespace shop).

  2. 2

    Write a command that tests from inside pod api-7f9c5d4b6-2xkqp (namespace prod) whether the Service redis answers on port 6379, without opening a shell.

  3. 3

    Read this error and say what to run instead: 'exec: "bash": executable file not found in $PATH'

  4. 4

    Compare /etc/resolv.conf on the practice machine with the one inside pod api-7f9c5d4b6-2xkqp (namespace prod): note the nameserver, the search domains and ndots.

Related commands

kubectl logsRead and follow container logskubectl port-forwardReach a pod or Service from your laptopdocker execRun a command inside a running containerkubectl describeExplain an object's state, conditions and events
Previouskubectl logsNextkubectl apply
Linux / Kubernetes / kubectl exec

kubectl exec

Run a command or shell inside a running container

Intermediatekubernetesdebuggingshell

kubectl exec runs a command in a container that is already running, like docker exec but through the Kubernetes API. Use it to check config, environment and connectivity from the pod's point of view, or open an interactive shell with -it.

Syntaxkubectl exec [-it] (POD | TYPE/NAME) [-c CONTAINER] [-n NAMESPACE] -- COMMAND [ARGS...]

Watch it run

The api can't reach its database. See which settings and DNS the running container actually has, without opening a shell.

devops@devops-lab: ~ — zsh — 96×28
➜ ~
Step 1 of 3
kubectl exec -n prod api-7f9c5d4b6-2xkqp -- env | grep ^DB_

env runs inside the container; its output comes back to your terminal and grep runs locally. No -it needed for a one-shot command whose output you pipe.

Anatomy of a command

$
commandkubectl

The Kubernetes CLI.

How it works

kubectl sends a POST to the pod's exec subresource, /api/v1/namespaces/prod/pods/NAME/exec?command=sh&stdin=true&tty=true&container=api, and upgrades the connection to a streaming protocol (WebSocket in current versions, SPDY before 1.31). The API server checks RBAC for create on pods/exec, then proxies the stream to the kubelet on the pod's node. The kubelet asks the container runtime (containerd, via CRI) to start a new process inside the container's existing namespaces and cgroup: same filesystem, network, user and limits as the app. Your keystrokes and its output flow back over that one connection. When the command exits, kubectl exits with the same code. Nothing about the pod spec changes, and anything you change inside the container is lost when it restarts.

One upgraded HTTPS stream from your laptop, through the API server and the node's kubelet, into a new process inside the container.

upgradecan-i?proxy streamExec (CRI)enter namespacesspawnkubectlexec -it -- shAPI serverauthn + RBACkubeletnode :10250containerdCRI runtimeapi containerPID 1: node appsh processnew PID, same nsRBACpods/exec create
1/7
You run kubectl exec

kubectl reads your kubeconfig and sends POST /api/v1/namespaces/prod/pods/NAME/exec with command=sh, stdin and tty, asking to upgrade to a streaming (WebSocket) connection.

← → to step

Options you'll actually use

-i, --stdinPass your stdin to the process (needed for pipes and shells)
-t, --ttyAllocate a terminal (prompts, colours, Ctrl+C)
-c NAMEContainer to run in
-n NSNamespace of the pod
--End of kubectl flags; the rest is the container command
deploy/NAMEExec into one pod of a Deployment
-q, --quietOnly print the remote command's output

Common mistakes

kubectl exec pod ls -la /app

Without -- kubectl treats -la as its own flags; current versions reject the form altogether.

kubectl exec pod -- ls -la /app
Hot-fixing a config file inside a container with exec and vi

The change is lost on the next restart and other replicas never get it; the pod now differs from Git.

Change the ConfigMap or image, then kubectl rollout restart deploy/NAME
kubectl exec -it pod -- sh | tee out.txt

-t with a pipe mixes carriage returns into the output and some commands behave differently on a TTY.

kubectl exec pod -- sh -c 'cmd' > out.txt # no -t when capturing output
Granting developers pods/exec in prod 'just for debugging'

exec is a shell with the pod's service-account token, secrets and network access: effectively full access to that workload.

Give exec only where needed, audit it, and prefer kubectl debug or logs

Interview questions

Practice

Try these on a Linux machine of your own. Each task has a hint and a solution.

  1. 1

    Write the command that prints the environment variables of the container sidecar in pod web-5c7b9d6f4-q2x7z (namespace shop).

  2. 2

    Write a command that tests from inside pod api-7f9c5d4b6-2xkqp (namespace prod) whether the Service redis answers on port 6379, without opening a shell.

  3. 3

    Read this error and say what to run instead: 'exec: "bash": executable file not found in $PATH'

  4. 4

    Compare /etc/resolv.conf on the practice machine with the one inside pod api-7f9c5d4b6-2xkqp (namespace prod): note the nameserver, the search domains and ndots.

Related commands

kubectl logsRead and follow container logskubectl port-forwardReach a pod or Service from your laptopdocker execRun a command inside a running containerkubectl describeExplain an object's state, conditions and events
Previouskubectl logsNextkubectl apply