Bird
Raised Fist0
Kubernetesdevops~20 mins

Debugging with kubectl debug in Kubernetes - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Kubectl Debug Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
Output of kubectl debug creating ephemeral container
What is the output when running this command to add an ephemeral debug container to a running pod named webapp in namespace prod?
Kubernetes
kubectl debug -n prod -it webapp --image=busybox --target=webapp -- sh
Aephemeralcontainers "webapp-debugger" created in pod webapp
BPod/webapp created in namespace prod
CError from server (NotFound): pods "webapp" not found
DWarning: image busybox not found locally, pulling...
Attempts:
2 left
💡 Hint
Think about what kubectl debug does when adding ephemeral containers.
Troubleshoot
intermediate
2:00remaining
Troubleshooting kubectl debug failure due to missing target
You run kubectl debug -it mypod --image=busybox --target=app but get an error. What is the most likely cause?
AThe image busybox is not available in the cluster
BThe pod mypod does not exist in the current namespace
CThe container named app does not exist in pod mypod
Dkubectl debug requires --container flag instead of --target
Attempts:
2 left
💡 Hint
Check if the container name matches exactly inside the pod.
Configuration
advanced
2:00remaining
Configuring a pod to allow ephemeral containers
Which pod spec snippet correctly enables ephemeral containers for debugging?
A
spec:
  enableEphemeralContainers: true
B
metadata:
  annotations:
    ephemeral-containers.kubernetes.io/enabled: "true"
C
spec:
  ephemeralContainers:
  - name: debug
    image: busybox
DNo special configuration is needed; ephemeral containers are enabled by default
Attempts:
2 left
💡 Hint
Consider the default Kubernetes behavior for ephemeral containers.
Best Practice
advanced
2:00remaining
Best practice for debugging a pod with kubectl debug
What is the recommended approach to debug a running pod without disrupting its main containers?
ADelete the pod and recreate it with a debug container
BAdd an ephemeral container using kubectl debug
CModify the pod spec to add a permanent debug container
DUse kubectl exec to run commands inside the main container
Attempts:
2 left
💡 Hint
Think about non-disruptive debugging methods.
🧠 Conceptual
expert
2:00remaining
Understanding ephemeral container lifecycle
Which statement about ephemeral containers added via kubectl debug is TRUE?
AEphemeral containers are intended for temporary debugging and do not restart automatically
BEphemeral containers restart automatically if they exit
CEphemeral containers share the same lifecycle as the pod's main containers
DEphemeral containers require pod restart to be removed
Attempts:
2 left
💡 Hint
Consider how ephemeral containers behave compared to normal containers.

Practice

(1/5)
1. What is the main purpose of the kubectl debug command in Kubernetes?
easy
A. To delete a pod and recreate it with debug options
B. To permanently change the pod's container image
C. To scale the number of pod replicas for debugging
D. To add a temporary container with debugging tools to a running pod

Solution

  1. Step 1: Understand the purpose of kubectl debug

    This command adds a temporary container to a running pod for debugging without changing the original pod.
  2. Step 2: Compare other options

    Options A, B, and D describe permanent or unrelated actions, not the temporary debugging feature.
  3. Final Answer:

    To add a temporary container with debugging tools to a running pod -> Option D
  4. Quick Check:

    Temporary debug container = C [OK]
Hint: Remember: debug adds temporary tools, no permanent pod change [OK]
Common Mistakes:
  • Thinking debug changes pod image permanently
  • Confusing debug with pod scaling
  • Assuming debug deletes pods
2. Which of the following is the correct syntax to start a debug session on a pod named webapp-123?
easy
A. kubectl debug --pod webapp-123 --image=busybox
B. kubectl debug webapp-123 -image=busybox
C. kubectl debug pod/webapp-123 --image=busybox
D. kubectl debug pod webapp-123 --image=busybox

Solution

  1. Step 1: Recall correct kubectl debug syntax

    The correct syntax uses kubectl debug pod/NAME --image=IMAGE to specify the pod and debug container image.
  2. Step 2: Analyze options

    kubectl debug pod/webapp-123 --image=busybox matches the correct syntax exactly. Options A, B, and C have syntax errors or missing slashes.
  3. Final Answer:

    kubectl debug pod/webapp-123 --image=busybox -> Option C
  4. Quick Check:

    Correct syntax includes 'pod/' prefix = D [OK]
Hint: Use 'pod/NAME' format with --image option for debug [OK]
Common Mistakes:
  • Omitting 'pod/' prefix before pod name
  • Using spaces instead of slash between 'pod' and name
  • Misplacing --image option
3. What will be the output of this command if the pod api-server-1 does not exist?

kubectl debug pod/api-server-1 --image=busybox
medium
A. Error: pods "api-server-1" not found
B. A new debug container starts successfully
C. Pod is deleted and recreated with debug image
D. Command runs but no debug container is added

Solution

  1. Step 1: Understand pod existence requirement

    The kubectl debug command requires the target pod to exist to add a debug container.
  2. Step 2: Predict command behavior if pod missing

    If the pod does not exist, kubectl returns an error indicating the pod was not found.
  3. Final Answer:

    Error: pods "api-server-1" not found -> Option A
  4. Quick Check:

    Missing pod causes 'not found' error = A [OK]
Hint: Check pod exists before debug; else 'not found' error [OK]
Common Mistakes:
  • Assuming debug creates pod if missing
  • Expecting debug to run silently without pod
  • Confusing debug with pod creation commands
4. You run kubectl debug pod/myapp --image=busybox but get an error: error: container busybox not found. What is the likely cause?
medium
A. The debug container image is missing or misspelled
B. The pod does not exist
C. The pod already has a container named busybox
D. kubectl debug requires root privileges

Solution

  1. Step 1: Analyze the error message

    The error says 'container busybox not found', which usually means the image name is incorrect or missing.
  2. Step 2: Check common causes

    The debug container image is missing or misspelled fits because a misspelled or missing image causes this error. The pod existence or privileges are unrelated.
  3. Final Answer:

    The debug container image is missing or misspelled -> Option A
  4. Quick Check:

    Image name errors cause 'container not found' message [OK]
Hint: Verify debug image name spelling and availability [OK]
Common Mistakes:
  • Assuming pod absence causes this error
  • Thinking debug needs root privileges
  • Confusing container name with image name
5. You want to debug a running pod backend-0 but need to run a shell with root privileges inside the debug container. Which command correctly achieves this?
hard
A. kubectl debug pod/backend-0 --image=busybox --target=backend-0 -- sh
B. kubectl debug pod/backend-0 --image=busybox --privileged -- sh
C. kubectl debug pod/backend-0 --image=busybox --container=debugger -- sh
D. kubectl debug pod/backend-0 --image=busybox --as-root -- sh

Solution

  1. Step 1: Identify how to run debug container with root

    The --privileged flag allows the debug container to run with root privileges.
  2. Step 2: Check command correctness

    kubectl debug pod/backend-0 --image=busybox --privileged -- sh uses --privileged and runs shell sh, which is correct. Other options misuse flags or do not enable root.
  3. Final Answer:

    kubectl debug pod/backend-0 --image=busybox --privileged -- sh -> Option B
  4. Quick Check:

    Use --privileged to get root shell in debug container [OK]
Hint: Add --privileged flag to run debug container as root [OK]
Common Mistakes:
  • Using nonexistent flags like --as-root
  • Confusing --target with privilege escalation
  • Omitting --privileged when root shell needed