CKAD — Logging & Debugging Lab

2 minute read

Lab 1: Debug a Failing Pod

Practice the 5-step debugging workflow on a Pod with a bug.

# Create a Pod with a bug (wrong command)
kubectl run buggy --image=busybox:1.36 -- /bin/sh -c "cat /nonexistent-file"

# Step 1: Check status
kubectl get pod buggy
# Expected: CrashLoopBackOff or Error

# Step 2: Check events
kubectl describe pod buggy | tail -15

# Step 3: Check logs
kubectl logs buggy
# Expected: cat: can't open '/nonexistent-file': No such file or directory

# Step 4: Check previous container logs
kubectl logs buggy --previous

# Step 5: Fix it
kubectl delete pod buggy
kubectl run buggy --image=busybox:1.36 -- /bin/sh -c "echo 'fixed!' && sleep 3600"

# Verify
kubectl logs buggy
# Expected: fixed!

Lab 2: Use kubectl debug

Debug a Pod that has no debugging tools installed.

# Create a minimal Pod without debugging tools
kubectl run minimal --image=nginx:1.27

# Try exec — limited tools
kubectl exec minimal -- ls /usr/bin/curl
# Error: No such file or directory

# Use kubectl debug to add a debug container
kubectl debug minimal -it --image=busybox:1.36 --target=minimal -- /bin/sh

# Inside the debug container you can:
# - Check network: wget -qO- localhost:80
# - Check filesystem: ls /proc/1/root/
# - Check processes: ps aux

Practice Questions

Q1: A Pod named api-server is in CrashLoopBackOff. What commands do you use to diagnose the issue?

Show Answer ```bash # 1. Check current status kubectl get pod api-server # 2. View events and conditions kubectl describe pod api-server # 3. Check logs from the crashed container kubectl logs api-server --previous # 4. Check the exit code kubectl get pod api-server -o jsonpath='{.status.containerStatuses[0].lastState.terminated.exitCode}' ``` The exit code tells you what happened: 1 = app error, 137 = OOMKilled, 139 = segfault.

Q2: How do you view logs from all containers in a Pod named multi-pod?

Show Answer ```bash kubectl logs multi-pod --all-containers=true ``` To follow all container logs in real time: ```bash kubectl logs multi-pod --all-containers=true -f ```

Q3: A Pod is stuck in Pending state. The describe output shows “0/3 nodes are available: 3 Insufficient memory.” What should you do?

Show Answer The Pod's memory request exceeds what is available on all nodes. Options: 1. **Reduce the memory request** in the Pod spec 2. **Free up resources** by deleting unused Pods 3. **Add more nodes** to the cluster 4. **Check for Pods that are over-requesting** with `kubectl top pods` ```bash # Check what the Pod is requesting kubectl get pod my-pod -o jsonpath='{.spec.containers[0].resources.requests.memory}' # Check node capacity kubectl describe nodes | grep -A5 "Allocated resources" ```

Q4: What is the command to find the API version of the Ingress resource?

Show Answer ```bash kubectl api-resources | grep ingress # Expected: ingresses ing networking.k8s.io/v1 true Ingress # Or use explain kubectl explain ingress # KIND: Ingress VERSION: networking.k8s.io/v1 ```

Q5: You need to debug a running Pod but the container has no shell or debugging tools. How do you do it?

Show Answer Use `kubectl debug` to create a debug container: ```bash # Add an ephemeral debug container kubectl debug my-pod -it --image=busybox:1.36 --target=my-container # Or create a copy with a different image kubectl debug my-pod -it --image=busybox:1.36 --copy-to=debug-pod --share-processes ``` The `--share-processes` flag lets you see processes from the original container using `ps aux`.

← Previous: Probes & Health Checks Next: ConfigMaps & Secrets →

Updated: