Skip to content

Debugging Guide

Essential Commands

View Pods

# List all pods
kubectl get pods -n test-staging

# Watch pods (live updates)
kubectl get pods -n test-staging -w

# More details
kubectl get pods -n test-staging -o wide

Check Logs

# View logs
kubectl logs deployment/myapp -n test-staging

# Follow logs (like tail -f)
kubectl logs -f deployment/myapp -n test-staging

# Previous container logs (after crash)
kubectl logs pod/myapp-xxx --previous -n test-staging

# Specific container in pod
kubectl logs pod/myapp-xxx -c web -n test-staging

Describe Resources

# Pod details and events
kubectl describe pod myapp-xxx -n test-staging

# Deployment details
kubectl describe deployment myapp -n test-staging

# Service details
kubectl describe service myapp -n test-staging

Execute Commands

# Open shell in pod
kubectl exec -it pod/myapp-xxx -n test-staging -- /bin/sh

# Run command
kubectl exec pod/myapp-xxx -n test-staging -- ls -la

# Check connectivity
kubectl exec pod/myapp-xxx -n test-staging -- wget -O- http://another-service

Port Forwarding

# Forward local port to pod
kubectl port-forward pod/myapp-xxx 8080:3000 -n test-staging
# Access at http://localhost:8080

# Forward to service
kubectl port-forward service/myapp 8080:80 -n test-staging

Common Debugging Scenarios

Pod Won't Start

# Check pod status
kubectl get pod myapp-xxx -n test-staging

# Check events
kubectl describe pod myapp-xxx -n test-staging | grep -A20 Events

# Common issues:
# - ImagePullBackOff: Can't pull Docker image
# - CrashLoopBackOff: App crashes on startup
# - Pending: Waiting for resources

Can't Access Service

# Check service endpoints
kubectl get endpoints myapp -n test-staging

# Check service configuration
kubectl get service myapp -n test-staging -o yaml

# Test from another pod
kubectl run test --rm -it --image=busybox -- wget -O- http://myapp

High Memory/CPU Usage

# Check resource usage
kubectl top pods -n test-staging
kubectl top nodes

# Check resource limits
kubectl describe pod myapp-xxx -n test-staging | grep -A5 Limits

Useful One-Liners

# Delete all pods (forces restart)
kubectl delete pods --all -n test-staging

# Get pod IPs
kubectl get pods -n test-staging -o jsonpath='{range .items[*]}{.metadata.name}{"\t"}{.status.podIP}{"\n"}{end}'

# Check recent events
kubectl get events -n test-staging --sort-by='.lastTimestamp'

# Export logs to file
kubectl logs deployment/myapp -n test-staging > myapp.log

Next Steps