Code BeautifierDev Tools

Kubernetes kubectl CLI Cheat Sheet

Essential kubectl commands for cluster inspection, pods, deployments, services, debugging, and config

Cluster & Context Management

Inspect cluster state and switch namespaces/contexts

Display current kubeconfig context
kubectl config current-context
Switch to different context
kubectl config use-context <cluster-name>
Set default namespace for current context
kubectl config set-context --current --namespace=<ns>
Display cluster info and endpoints
kubectl cluster-info

Pod Management & Debugging

List, inspect, log, and troubleshoot active pods

List pods with IP and node placement
kubectl get pods -o wide -n <namespace>
Tail real-time logs from pod container
kubectl logs -f <pod-name> -c <container-name> --tail=100
Interactive shell inside running pod
kubectl exec -it <pod-name> -- /bin/sh
Describe pod events & failure reasons
kubectl describe pod <pod-name>
Forward local port to remote pod
kubectl port-forward pod/<pod-name> 8080:80

Deployments, Services & Rollouts

Manage application workloads and traffic routing

Scale deployment replicas
kubectl scale deployment/<app> --replicas=3
Trigger zero-downtime rolling restart
kubectl rollout restart deployment/<app>
Check rollout status / history
kubectl rollout status deployment/<app>
kubectl rollout history deployment/<app>
Rollback to previous revision
kubectl rollout undo deployment/<app>
List all services and ClusterIPs
kubectl get svc -A

Resource Manifests & Cleanups

Apply manifest file or URL
kubectl apply -f ./deployment.yaml
Extract resource manifest as clean YAML
kubectl get deployment/<app> -o yaml > app.yaml
Delete all pods in Error or Evicted state
kubectl delete pods --field-selector=status.phase=Failed -A
Top CPU and Memory resource usage
kubectl top nodes
kubectl top pods -A