# List resources
kubectl get pods
kubectl get pods -n kube-system # specific namespace
kubectl get pods --all-namespaces # all namespaces (alias: -A)
kubectl get pods -o wide # extra columns (node, IP)
# Get a specific resource
kubectl get pod my-pod
kubectl get pod my-pod -o yaml # full YAML output
kubectl get pod my-pod -o json # full JSON output
# Watch for changes
kubectl get pods -w
# Get all resource types at once
kubectl get all -n <namespace>
# Filter by label
kubectl get pods -l app=my-app
kubectl get pods -l 'env in (prod, staging)'
# Sort output
kubectl get pods --sort-by='.metadata.creationTimestamp'
kubectl get pods --sort-by='.status.containerStatuses[0].restartCount'
# Delete from file
kubectl delete -f manifest.yaml
# Delete by name
kubectl delete pod my-pod
kubectl delete deployment my-deployment
# Delete all pods with a label
kubectl delete pods -l app=my-app
# Force-delete a stuck terminating pod
kubectl delete pod my-pod --grace-period=0 --force
# Delete everything in a namespace
kubectl delete all --all -n my-namespace
# Forward local port to pod
kubectl port-forward pod/my-pod 8080:80
# Forward to a service
kubectl port-forward service/my-service 8080:80
# Forward in background
kubectl port-forward service/my-service 8080:80 &
# Pod CPU/memory usage (requires metrics-server)
kubectl top pods
kubectl top pods -n kube-system
kubectl top pods --sort-by=memory
# Node CPU/memory usage
kubectl top nodes
# Inject ephemeral debug container (K8s 1.23+)
kubectl debug -it my-pod --image=nicolaka/netshoot:v0.13 --target=my-container
# Copy pod and add debug container
kubectl debug my-pod -it --copy-to=debug-pod --image=busybox:1.36
# Get events for a specific object
kubectl get events --field-selector involvedObject.name=my-pod
# All events, newest last
kubectl get events --sort-by='.lastTimestamp'
# Warning events only
kubectl get events --field-selector type=Warning