// cheat sheets / kubectl
kubectl quick reference
Essential commands for inspecting, deploying, and debugging applications in your cluster.
Deploy→Inspect Status→View Logs→Debug→Update/Scale→Monitor↺
CommandsIntermediate#kubernetes#kubectl#command-line
| Command | Description |
|---|
kubectl get nodes | Lists the cluster's nodes and whether each is Ready. |
kubectl get pods -A | Show all pods across all namespaces. |
kubectl get svc,deploy,sts | List services, deployments, and stateful sets. |
kubectl cluster-info | Shows the addresses of the control plane and core cluster services. |
| Command | Purpose |
|---|
kubectl get <resource>/<name> -o yaml | Get detailed YAML output for a specific resource. |
kubectl describe pod <pod-name> | Show detailed status, events, and conditions for a pod. |
kubectl describe deploy <deploy-name> | Get details about a deployment, including replica status and events. |
kubectl get events --sort-by='.lastTimestamp' | List recent cluster events, useful for debugging. |
| Command | Action |
|---|
kubectl logs <pod-name> -f | Stream logs from a pod. Add -c <container> for multi-container pods. |
kubectl exec -it <pod-name> -- bash | Opens a shell inside a running pod (use sh if the image has no bash). |
kubectl port-forward <pod-name> 8080:80 | Forward local port 8080 to pod's port 80. |
kubectl cp <pod-name>:/app/file . | Copy a file from a pod to your local machine. |
| Command | Effect |
|---|
kubectl apply -f <file.yaml> | Apply configuration from a file. Creates or updates resources. |
kubectl create -f <file.yaml> | Create resources from a file. Fails if resource already exists. |
kubectl rollout status deploy/<name> | Monitor the status of a deployment rollout. |
kubectl rollout undo deploy/<name> | Rollback a deployment to the previous revision. |
55. Debugging & Troubleshooting
| Command | Use Case |
|---|
kubectl debug -it <resource>/<name> --image=busybox --target=<container> | Create an ephemeral container for debugging. Requires Kubernetes 1.23+. |
kubectl top pod <pod-name> | Show CPU/memory usage for a pod. Requires Metrics Server. |
kubectl get pod <pod-name> -o jsonpath='{.status.containerStatuses[*].state}' | Quickly check container states within a pod. |
kubectl get pod <pod-name> -o wide | See node, IP, and restart count for a pod. |
| Command | Action |
|---|
kubectl delete -f <file.yaml> | Delete resources defined in a file. Use with caution. |
kubectl delete pod <pod-name> --force --grace-period=0 | Force delete a stuck pod immediately. Use with extreme caution. |
kubectl scale deploy/<name> --replicas=3 | Scale a deployment to a specific number of replicas. |
kubectl edit deploy/<name> | Open resource definition in editor for live changes. |
77. Context & Configuration
| Command | Function |
|---|
kubectl config get-contexts | List all available kubectl contexts. |
kubectl config use-context <context-name> | Switch to a different Kubernetes context. |
kubectl config current-context | Display the name of the current context. |
kubectl config view --minify | Show the active kubeconfig configuration. |