liveBook · Manning

Kubectl exec

As explained in Kubernetes in Action, Second Edition

kubectl exec is a kubectl command that executes a command inside a running container in a pod.

kubectl exec runs a command inside a container in a running pod. It helps inspect an application from inside without logging in to the node hosting the pod. section 5.3.4

Use kubectl exec for one-off commands or an interactive shell:

  • Run a process listing with `kubectl exec kiada -- ps aux`. section 5.3.4
  • Test the application internally with `kubectl exec kiada -- curl -s localhost:8080`. section 5.3.4
  • Open an interactive shell with `kubectl exec -it kiada -- bash`; `-i` passes standard input and `-t` allocates a terminal. section 5.3.4
  • Inspect files, network interfaces, or connectivity from that shell using tools available inside the container. section 5.3.4

For a pod with multiple containers, select the target with `-c` or `--container`. Without that option, kubectl exec uses the first container in the pod manifest. section 5.4.3

$ kubectl exec kiada -- ps aux
USER  PID %CPU %MEM    VSZ   RSS TTY STAT START TIME COMMAND
root    1  0.0  1.3 812860 27356 ?   Ssl  11:54 0:00 node app.js   #1
root  120  0.0  0.1  17500  2128 ?   Rs   12:22 0:00 ps aux     #2

A process-listing example shows the command running inside the container alongside the application process. section 5.3.4

How Kubernetes in Action, Second Edition covers it

How deep it goes
Explains one-off commands, interactive shells, terminal flags, and multi-container selection, but not command execution internals.
Examples
Demonstrates process listing, internal curl requests, interactive bash, file inspection, network inspection, and connectivity tests.
What it assumes
Assumes a running pod, a target container, and the required command or shell available in that container.
What you can do after
Operate kubectl exec to inspect running containers, test applications, open shells, and target containers in multi-container pods.
How it connects
Connects containers, pods, debugging, interactive shells, and ephemeral containers.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Kubernetes in Action, Second Edition. The section links point to the parts of the book it draws on.