0
0
Kubernetesdevops~10 mins

Executing commands in Pods in Kubernetes - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Executing commands in Pods
Start: Identify Pod
Run kubectl exec command
Connect to Pod container
Execute command inside Pod
Receive command output
End
This flow shows how to run a command inside a Kubernetes Pod using kubectl exec, connecting to the container, executing the command, and getting the output.
Execution Sample
Kubernetes
kubectl exec my-pod -- ls /app
kubectl exec -it my-pod -- /bin/bash
These commands run 'ls /app' inside 'my-pod' and open an interactive bash shell inside the pod.
Process Table
StepCommand RunActionResult/Output
1kubectl exec my-pod -- ls /appConnect to 'my-pod' containerConnection established
2kubectl exec my-pod -- ls /appRun 'ls /app' inside podList of files/folders in /app
3kubectl exec -it my-pod -- /bin/bashConnect interactively to 'my-pod'Interactive shell opened
4kubectl exec -it my-pod -- /bin/bashUser types 'pwd' inside shell/ (current directory)
5kubectl exec -it my-pod -- /bin/bashUser types 'exit' to leave shellShell session ends
💡 Commands finish after output or user exits interactive shell
Status Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
Pod ConnectionNoneConnectedConnectedInteractive ConnectedInteractive ConnectedDisconnected
Command OutputNoneNoneFile listNoneCurrent directory pathNone
Key Moments - 2 Insights
Why do we use '--' before the command in 'kubectl exec'?
The '--' tells kubectl that what follows is the command to run inside the pod, not kubectl options. See execution_table steps 1 and 2.
What does the '-it' option do in 'kubectl exec -it'?
The '-i' keeps input open and '-t' allocates a terminal, allowing interactive commands like a shell. See execution_table steps 3 to 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the output after running 'kubectl exec my-pod -- ls /app'?
AConnection established
BList of files/folders in /app
CInteractive shell opened
DShell session ends
💡 Hint
Check execution_table row 2 under 'Result/Output'
At which step does the user exit the interactive shell?
AStep 2
BStep 3
CStep 5
DStep 4
💡 Hint
Look at execution_table row 5 describing 'exit' command
If you remove '-it' from 'kubectl exec -it my-pod -- /bin/bash', what changes in the execution?
AYou cannot interact; command runs non-interactively
BYou still get an interactive shell
CPod connection fails
DCommand runs faster
💡 Hint
Refer to key_moments about '-it' option and execution_table steps 3-5
Concept Snapshot
kubectl exec <pod-name> -- <command>
- Runs a command inside a pod container
- Use '--' to separate kubectl options from command
- Add '-it' for interactive shell sessions
- Output shows command result or shell prompt
- Exit shell with 'exit' command
Full Transcript
Executing commands in Kubernetes pods uses the 'kubectl exec' command. First, identify the pod name. Then run 'kubectl exec pod-name -- command' to execute a command inside the pod. The '--' separates kubectl options from the command to run. For interactive sessions like a shell, add '-it' to keep input open and allocate a terminal. You connect to the pod container, run the command, and receive output. To exit an interactive shell, type 'exit'. This process helps you inspect or manage running pods easily.