Computer Notes/Kubernetes/Kubernetes Command-Line Cheat Sheet
From Cramsession
< Computer Notes | Kubernetes
✍️ Verified Author: Mflavell • Click to view professional profile & credentials
Kubernetes Command-Line Cheat Sheet is a reference guide for standard operations using the kubectl command-line interface to manage Kubernetes clusters, workloads, and node infrastructure.
Cluster Context & Namespaces
| Command | Description |
|---|---|
kubectl config get-contexts |
List all available cluster contexts. |
kubectl config use-context <context-name> |
Switch active cluster context. |
kubectl config set-context --current --namespace=<ns> |
Set default namespace for current context. |
kubectl get ns |
List all namespaces in the cluster. |
Viewing & Inspecting Resources
| Command | Description |
|---|---|
kubectl get pods -A |
List all pods across all namespaces. |
kubectl get pods -o wide |
List pods with IP addresses and node assignments. |
kubectl get all -n <namespace> |
Show all active resources in a specific namespace. |
kubectl describe <resource>/<name> |
View detailed state, events, and configuration. |
kubectl get <resource> <name> -o yaml |
Export resource manifest to YAML format. |
Deploying & Managing Workloads
| Command | Description |
|---|---|
kubectl apply -f <file.yaml> |
Create or update resources declaratively. |
kubectl delete -f <file.yaml> |
Delete resources defined in a YAML manifest. |
kubectl scale deploy/<name> --replicas=<N> |
Scale the replica count of a Deployment. |
kubectl rollout restart deploy/<name> |
Perform a zero-downtime rolling restart. |
kubectl rollout undo deploy/<name> |
Roll back to the previous deployment revision. |
Debugging & Observability
| Command | Description |
|---|---|
kubectl logs -f <pod-name> |
Stream real-time logs from a pod container. |
kubectl logs -p <pod-name> |
View logs from a previous crashed container instance. |
kubectl exec -it <pod-name> -- /bin/sh |
Open an interactive shell inside a running pod. |
kubectl port-forward svc/<name> 8080:80 |
Forward local port 8080 to cluster service port 80. |
kubectl get events --sort-by='.metadata.creationTimestamp' |
List recent cluster events sorted by timestamp. |
kubectl top pods / kubectl top nodes |
Display live CPU and memory metrics. |
Node Operations & Maintenance
| Command | Description |
|---|---|
kubectl get nodes |
List all cluster nodes and their current status. |
kubectl cordon <node-name> |
Mark a node as unschedulable. |
kubectl drain <node-name> --ignore-daemonsets |
Safely evict all pods before node maintenance. |
kubectl uncordon <node-name> |
Mark a node as schedulable again. |
Essential Productivity Shell Shortcuts
Add these shortcuts to your shell profile (~/.bashrc or ~/.zshrc) to speed up workflow:
<syntaxhighlight lang="bash"> alias k='kubectl' alias kgp='kubectl get pods' alias kgs='kubectl get svc' alias kgd='kubectl get deployments' alias kdp='kubectl describe pod' alias kl='kubectl logs -f' </syntaxhighlight>