Kubernetes
kubectl Advanced
Advanced kubectl commands for debugging and cluster management.
36 commands
Windows
MacOS
Linux
#orchestration
#debugging
Debugging
Show detailed pod information
kubectl describe pod my-pod
View logs for specific container
kubectl logs my-pod -c my-container
View logs from previous container instance
kubectl logs my-pod --previous
Open shell in running pod
kubectl exec -it my-pod -- /bin/sh
Create debug container in pod
kubectl debug my-pod --image=busybox
Show pod resource usage sorted by memory
kubectl top pods --sort-by=memory
Show events for a specific pod
kubectl events --for pod/my-pod
Resource Management
kubectl apply -f manifest.yaml --dry-run=server # Server-side dry run
Show diff before applying changes
kubectl diff -f manifest.yaml
kubectl patch deployment my-dep -p '{"spec":{"replicas":3}}' # Patch a resource inline
Add label to a pod
kubectl label pods my-pod env=prod
Add annotation to a pod
kubectl annotate pod my-pod note="test"
List all available resource types
kubectl api-resources
Rollouts
kubectl rollout status deployment/my-dep # Watch rollout progress
kubectl rollout history deployment/my-dep # View rollout history
Rollback to previous revision
kubectl rollout undo deployment/my-dep
kubectl rollout undo deployment/my-dep --to-revision=2 # Rollback to specific revision
kubectl rollout restart deployment/my-dep # Restart all pods in deployment
Pause a rollout
kubectl rollout pause deployment/my-dep
Port Forwarding
Forward local port to pod port
kubectl port-forward pod/my-pod 8080:80
Forward local port to service port
kubectl port-forward svc/my-svc 8080:80
kubectl port-forward deploy/my-dep 8080:80 # Forward local port to deployment
Run kubectl proxy to API server
kubectl proxy
Contexts
List all available contexts
kubectl config get-contexts
Show current context
kubectl config current-context
Switch to a different context
kubectl config use-context my-cluster
kubectl config set-context --current --namespace=dev # Set default namespace
Show current context config only
kubectl config view --minify
JSONPath
kubectl get pods -o jsonpath='{.items[*].metadata.name}' # Get all pod names
kubectl get nodes -o jsonpath='{.items[*].status.addresses[0].address}' # Get node IPs
kubectl get pods -o jsonpath='{range .items[*]}{.metadata.name}{"\n"}{end}' # List pods one per line
kubectl get pod my-pod -o jsonpath='{.status.phase}' # Get pod phase
kubectl get pods -o custom-columns=NAME:.metadata.name,STATUS:.status.phase # Custom columns output
Quick Commands
Create debug container in a running pod
kubectl debug my-pod --image=busybox
Rollback deployment to previous revision
kubectl rollout undo deployment/my-dep
Forward local port to service port
kubectl port-forward svc/my-svc 8080:80