Debugging and Troubleshooting Pods

1. Viewing Pod Logs

kubectl logs POD
kubectl logs POD -c container
kubectl logs -l app=web --max-log-requests=10 --tail=50

2. Following Log Streams

kubectl logs -f POD
kubectl logs -f POD --since=10m --timestamps

3. Viewing Previous Container Logs

kubectl logs POD --previous
Note: Use after a CrashLoopBackOff to see why the prior instance died.

4. Executing Commands in Pods

kubectl exec -it POD -- sh
kubectl exec POD -c sidecar -- env
kubectl exec POD -- cat /etc/hosts

5. Creating Debug Containers

kubectl debug POD -it --image=busybox --target=app
kubectl debug node/NODE -it --image=ubuntu

6. Using Ephemeral Containers

PropertyDetail
StableSince 1.25
No restartCannot specify probes/resources
PID shareUse --target to share process ns
SurvivesUntil pod deleted

7. Viewing Pod Events

kubectl describe pod POD | tail -30
kubectl get events --field-selector involvedObject.name=POD --sort-by=.lastTimestamp

8. Using Port Forwarding

kubectl port-forward pod/POD 8080:80
kubectl port-forward svc/web 8080:80 --address 0.0.0.0

9. Inspecting API Objects

kubectl get pod POD -o yaml
kubectl get pod POD -o json | jq '.status.containerStatuses[].lastState'

10. Checking Cluster Events

kubectl get events -A --sort-by=.lastTimestamp | tail -30
kubectl get events -A --field-selector type=Warning

11. Troubleshooting Networking

kubectl run debug --rm -it --image=nicolaka/netshoot -- bash
# inside:
nslookup web.prod.svc.cluster.local
curl -v http://web:8080
mtr web
SymptomCause
DNS failCoreDNS down; check kube-dns Service
Service unreachableNo endpoints (selector mismatch)
CNI errorsCheck CNI daemonset logs

12. Using Node Debug Pods

kubectl debug node/NODE -it --image=ubuntu
# inside: chroot /host    # access node FS
kubectl get --raw "/api/v1/nodes/NODE/proxy/metrics"