0
0
Kubernetesdevops~10 mins

kubectl exec for container access in Kubernetes - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - kubectl exec for container access
User runs kubectl exec command
kubectl sends exec request to API Server
API Server forwards request to kubelet on node
kubelet executes command inside container
Command output sent back to user terminal
This flow shows how the kubectl exec command lets you run commands inside a container by sending a request through the Kubernetes API to the node running the container.
Execution Sample
Kubernetes
kubectl exec -it my-pod -- /bin/bash
This command opens an interactive bash shell inside the container of the pod named 'my-pod'.
Process Table
StepActionCommand/RequestResult/Output
1User runs kubectl execkubectl exec -it my-pod -- /bin/bashExec request sent to API Server
2API Server receives requestForward exec request to kubeletRequest forwarded to node's kubelet
3kubelet executes command/bin/bash inside containerBash shell started inside container
4User interacts with shellCommands typed in shellShell executes commands and returns output
5User exits shellexit commandShell session ends, connection closed
💡 User exits shell or connection is closed, ending the exec session
Status Tracker
VariableStartAfter Step 1After Step 3After Step 5
kubectl exec sessionnonerequest sentshell runningsession closed
Key Moments - 3 Insights
Why do we need the -- before the command in kubectl exec?
The -- tells kubectl that what follows is the command to run inside the container, not an option to kubectl itself. See execution_table step 1.
What does the -it option do in kubectl exec?
-i keeps the input open so you can type commands, and -t allocates a terminal so the shell works interactively. This is why you get a usable bash shell in step 3.
Can kubectl exec run commands in containers of pods with multiple containers?
Yes, but you must specify the container name with -c option. Otherwise, kubectl exec defaults to the first container.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what happens at step 3?
AThe API Server forwards the exec request to kubelet
BThe kubelet executes the command inside the container
CUser types commands in the shell
DThe exec session ends
💡 Hint
Check the 'Action' and 'Result/Output' columns at step 3 in execution_table
At which step does the user start interacting with the shell inside the container?
AStep 4
BStep 1
CStep 2
DStep 5
💡 Hint
Look for when 'User interacts with shell' in the execution_table
If you omit the -it option, what would change in the execution?
Akubectl exec would fail to send the request
BYou would still get an interactive shell
CThe command would run but you cannot interact with it
DThe API Server would reject the request
💡 Hint
Recall the explanation in key_moments about the purpose of -it option
Concept Snapshot
kubectl exec lets you run commands inside a container.
Use: kubectl exec -it <pod> -- <command>
- -i keeps input open
- -t allocates a terminal
-- separates kubectl options from the command
Useful for debugging or accessing container shells.
Full Transcript
The kubectl exec command allows you to run commands inside a container of a Kubernetes pod. When you run kubectl exec -it my-pod -- /bin/bash, kubectl sends an exec request to the Kubernetes API Server. The API Server forwards this request to the kubelet on the node where the pod runs. The kubelet then executes the command inside the container, starting a bash shell. The user can interact with this shell until they exit, which closes the session. The -i and -t options keep the input open and allocate a terminal for interactive use. The -- separates kubectl options from the command to run inside the container. This command is useful for debugging or accessing container environments directly.