Kubernetes kubectl Cheat Sheet
1. Basic Cluster and Node Operations
kubectl cluster-info
kubectl get nodes
kubectl describe node < node-name>
kubectl get nodes -o wide
Check Node Conditions (Resource Pressure)
kubectl describe node < node-name> | grep -i pressure
MemoryPressure : High memory usage
DiskPressure : Low disk space
PIDPressure : Too many processes running
kubectl cordon < node-name> # Mark node as unschedulable
kubectl uncordon < node-name> # Allow scheduling on the node again
Drain a Node (Evict Pods Before Maintenance)
kubectl drain < node-name> --ignore-daemonsets --delete-local-data
2. Pod Management & Evictions
kubectl get pods -A # List all pods in all namespaces
kubectl get pods -n < namespace> # List pods in a specific namespace
kubectl get pods -o wide # Show detailed pod information
kubectl describe pod < pod-name> # Show pod details, including events
Check Pod Events & Eviction History
kubectl get events --sort-by=.metadata.creationTimestamp
kubectl describe pod < pod-name> | grep -i eviction
kubectl delete pod < pod-name>
kubectl get pod < pod-name> -o jsonpath=" {.status.qosClass}"
Guaranteed : Requests = Limits
Burstable : Requests < Limits
BestEffort : No requests or limits set
3. Scheduling: Taints, Tolerations, and Affinity
kubectl describe node < node-name> | grep -i taint
kubectl taint nodes < node-name> key=value:NoSchedule
Remove a Taint from a Node
kubectl taint nodes < node-name> key=value:NoSchedule-
Deploy a Pod with a Toleration
tolerations :
- key : " dedicated"
operator : " Equal"
value : " database"
effect : " NoSchedule"
Deploy a Pod with Node Affinity
affinity :
nodeAffinity :
requiredDuringSchedulingIgnoredDuringExecution :
nodeSelectorTerms :
- matchExpressions :
- key : " region"
operator : " In"
values :
- " us-east"
Deploy a Pod with Pod Anti-Affinity
affinity :
podAntiAffinity :
requiredDuringSchedulingIgnoredDuringExecution :
- labelSelector :
matchExpressions :
- key : " tier"
operator : " In"
values :
- " db"
topologyKey : " kubernetes.io/hostname"
helm repo add bitnami https://charts.bitnami.com/bitnami
helm repo update
helm install my-nginx bitnami/nginx
List Installed Helm Releases
helm upgrade my-nginx bitnami/nginx -f values.yaml
Check Node Resource Usage
kubectl top pods --containers
Describe Resource Requests & Limits of a Pod
kubectl describe pod < pod-name> | grep -i " requests\|limits"
kubectl logs < pod-name>
kubectl logs < pod-name> -f # Follow logs in real-time
kubectl logs < pod-name> -c < container-name> # Logs for a specific container
Execute a Command in a Running Pod
kubectl exec -it < pod-name> -- /bin/sh
Ping from One Pod to Another
kubectl exec -it < pod-name> -- ping < target-pod-name>
Get Detailed YAML Output for Any Resource
kubectl get pod < pod-name> -o yaml
kubectl get node < node-name> -o yaml
kubectl create namespace my-namespace
kubectl delete namespace my-namespace
8. Cleanup & Deleting Resources
Delete All Pods in a Namespace
kubectl delete pods --all -n < namespace>
Delete a Specific Deployment
kubectl delete deployment < deployment-name>
Delete a Specific Service
kubectl delete service < service-name>
Delete a Persistent Volume Claim (PVC)
kubectl delete pvc < pvc-name>
Get All Running Pods with Labels
kubectl get pods --show-labels
Check Where Each Pod is Running
Find All Pods in Pending State
kubectl get pods --field-selector=status.phase=Pending
kubectl get nodes --show-labels