0
0
Kubernetesdevops~10 mins

Debugging with kubectl debug in Kubernetes - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Debugging with kubectl debug
Identify Pod to Debug
Run kubectl debug command
Create Ephemeral Debug Container
Attach to Debug Container
Run Debugging Commands
Exit Debug Container
Ephemeral Container Removed Automatically
This flow shows how kubectl debug creates a temporary container inside a pod for troubleshooting, lets you run commands, then cleans up automatically.
Execution Sample
Kubernetes
kubectl debug pod/myapp-pod -it --image=busybox
# Runs a debug container inside myapp-pod
# Attach to it interactively
This command starts an interactive debug container inside the existing pod 'myapp-pod' using the busybox image.
Process Table
StepActionkubectl CommandResultNotes
1Identify Pod to debugkubectl get podsPod 'myapp-pod' foundUser selects pod to debug
2Run debug containerkubectl debug pod/myapp-pod -it --image=busyboxEphemeral container created and attachedDebug container runs inside pod
3Run commands inside debug containershShell prompt inside debug containerUser can run troubleshooting commands
4Exit debug containerexitDebug container terminatesEphemeral container removed automatically
5Verify pod statekubectl get podsPod 'myapp-pod' running normallyNo lasting changes to pod
💡 Debug container exits and is removed after user finishes debugging
Status Tracker
VariableStartAfter Step 2After Step 4Final
Pod StateRunning with original containersRunning with ephemeral debug container addedRunning with debug container removedRunning with original containers
Debug ContainerNot presentCreated and runningTerminated and removedNot present
Key Moments - 3 Insights
Why does the debug container disappear after I exit?
The debug container is ephemeral, meaning it only exists during the debug session. As shown in execution_table step 4, when you exit, Kubernetes automatically removes it to keep the pod clean.
Can I change the pod's main containers using kubectl debug?
No, kubectl debug adds a temporary container alongside existing ones without modifying them. The pod state in variable_tracker confirms original containers stay unchanged.
What if the pod is not found when running kubectl debug?
You must first identify the correct pod name using 'kubectl get pods' as in execution_table step 1. If the pod name is wrong, debug cannot start.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what happens at step 2?
AAn ephemeral debug container is created and attached
BThe pod is deleted
CThe main container restarts
DThe debug container exits
💡 Hint
Refer to execution_table row 2 under 'Result' column
At which step does the debug container get removed?
AStep 1
BStep 3
CStep 4
DStep 5
💡 Hint
Check execution_table step 4 'Result' column for container termination
If you run 'kubectl debug' on a non-existing pod, what must you do first?
ACreate a new pod
BUse 'kubectl get pods' to find the correct pod name
CRestart the cluster
DRun debug without pod name
💡 Hint
See key_moments question 3 about identifying pods
Concept Snapshot
kubectl debug lets you add a temporary container inside a running pod
Use: kubectl debug pod/<pod-name> -it --image=<image>
This container is ephemeral and removed after exit
Allows interactive troubleshooting without changing pod
Pod's original containers stay unchanged
Full Transcript
Debugging with kubectl debug means adding a temporary container inside a running pod to troubleshoot. First, you find the pod name with 'kubectl get pods'. Then you run 'kubectl debug pod/myapp-pod -it --image=busybox' to create and attach to a debug container. Inside, you can run commands like a shell. When done, exiting removes the debug container automatically. The original pod containers remain unchanged throughout. This method helps fix issues without restarting or changing the pod permanently.