Kubernetes Cheat Sheet
Quick Setup: Kubectl Alias
Section titled “Quick Setup: Kubectl Alias”# Add to ~/.bashrc, ~/.zshrc, or ~/.profilealias k=kubectlcomplete -o default -F __start_kubectl k
# PowerShell: replace grep with findstr in piped commandsContext & Cluster Configuration
Section titled “Context & Cluster Configuration”# Check current contextkubectl config current-context
# List all contextskubectl config get-contexts
# Switch contextkubectl config use-context <context-name>
# Set default namespace for contextkubectl config set-context --current --namespace=<namespace>
# View full kubeconfigkubectl config viewCluster & Node Information
Section titled “Cluster & Node Information”# Cluster endpoints and serviceskubectl cluster-info
# Component statuses (scheduler, controller-manager, etcd)kubectl get componentstatuses
# List all nodeskubectl get nodes
# Detailed node info (capacity, allocatable, conditions)kubectl describe node <node-name>
# Cordon node (no new pods scheduled)kubectl cordon <node-name>
# Uncordon node (re-enable scheduling)kubectl uncordon <node-name>
# Drain node (safe pod eviction for maintenance)kubectl drain <node-name> --ignore-daemonsets --delete-emptydir-dataPods: Deploy, Debug, Inspect
Section titled “Pods: Deploy, Debug, Inspect”# List all pods across namespaceskubectl get pods --all-namespaces
# Get pods in specific namespace with wide output (node, IP)kubectl get pods -n <namespace> -o wide
# Apply pod/deployment from YAMLkubectl apply -f deployment.yaml
# Delete pod (forces termination after grace period)kubectl delete pod <pod-name> -n <namespace>
# Describe pod (events, status, volumes)kubectl describe pod <pod-name> -n <namespace>
# Get container names and imageskubectl get pod <pod-name> -n <namespace> -o jsonpath='{.spec.containers[*].name}'kubectl get pod <pod-name> -n <namespace> -o jsonpath='{.spec.containers[*].image}'
# Execute command in running podkubectl exec -it <pod-name> -n <namespace> -- /bin/bash
# View pod environment variableskubectl exec <pod-name> -n <namespace> -- env
# Copy file from pod to localkubectl cp <namespace>/<pod-name>:/path/in/pod /local/path
# Stream logskubectl logs <pod-name> -n <namespace> -f
# View logs with timestampkubectl logs <pod-name> -n <namespace> --timestamps=true
# Attach to running process (stdin/stdout passthrough)kubectl attach <pod-name> -i -t -n <namespace>Deployments & Scaling
Section titled “Deployments & Scaling”# Create deploymentkubectl create deployment <name> --image=<image> -n <namespace>
# Scale deploymentkubectl scale deployment <name> --replicas=3 -n <namespace>
# Get current replicaskubectl get deployment <name> -n <namespace>
# Update image in deploymentkubectl set image deployment/<name> <container>=<new-image> -n <namespace>
# Check rollout statuskubectl rollout status deployment/<name> -n <namespace>
# Rollback deployment to previous versionkubectl rollout undo deployment/<name> -n <namespace>
# List ReplicaSets for deploymentkubectl get replicasets -n <namespace>
# List StatefulSets (maintain pod identity)kubectl get statefulsets -n <namespace>Services & Networking
Section titled “Services & Networking”# Expose deployment as service (ClusterIP by default)kubectl expose deployment <name> --port=8080 --target-port=8080 -n <namespace>
# Expose as LoadBalancer or NodePortkubectl expose deployment <name> --type=LoadBalancer --port=80 -n <namespace>
# Get all serviceskubectl get svc --all-namespaces
# Get service details (endpoints, cluster IP, external IP)kubectl describe svc <service-name> -n <namespace>
# Edit servicekubectl edit svc <service-name> -n <namespace>
# Port forward for local debuggingkubectl port-forward svc/<service-name> 8080:8080 -n <namespace>ConfigMaps & Secrets
Section titled “ConfigMaps & Secrets”# Create secret from literal valueskubectl create secret generic <secret-name> --from-literal=key=value -n <namespace>
# Create secret from filekubectl create secret generic <secret-name> --from-file=/path/to/file -n <namespace>
# List secretskubectl get secrets -n <namespace>
# View secret (base64 encoded)kubectl get secret <secret-name> -o jsonpath='{.data}' -n <namespace>
# Decode secret valuekubectl get secret <secret-name> -o jsonpath='{.data.password}' -n <namespace> | base64 -d
# Create ConfigMap from literalkubectl create configmap <config-name> --from-literal=key=value -n <namespace>
# Create ConfigMap from filekubectl create configmap <config-name> --from-file=/path/to/config.yaml -n <namespace>
# List ConfigMapskubectl get configmaps -n <namespace>
# View ConfigMap contentkubectl get configmap <config-name> -o jsonpath='{.data}' -n <namespace>Resource Management & Quotas
Section titled “Resource Management & Quotas”# Describe resource quota for namespacekubectl describe resourcequota -n <namespace>
# Create resource quotakubectl create quota <quota-name> --hard=requests.cpu=10,limits.memory=20Gi -n <namespace>
# Create LimitRange (per-pod limits)kubectl create limitrange <limit-name> --max-cpu=2 --max-memory=1Gi -n <namespace>
# View all limitskubectl get limitrange -n <namespace>Debugging & Logs
Section titled “Debugging & Logs”# Stream logs from podkubectl logs <pod-name> -n <namespace> -f
# View last 50 lines of logskubectl logs <pod-name> -n <namespace> --tail=50
# Logs from all containers in podkubectl logs <pod-name> -n <namespace> --all-containers=true
# Logs from specific containerkubectl logs <pod-name> -c <container-name> -n <namespace>
# Logs from previous pod (if crashed)kubectl logs <pod-name> -n <namespace> --previous
# Execute shell command for debuggingkubectl exec -it <pod-name> -n <namespace> -- /bin/sh
# Run interactive busybox pod for network testingkubectl run -it --rm debug --image=busybox --restart=Never -- sh
# Get pod eventskubectl describe pod <pod-name> -n <namespace>
# Tail specific container logs in podkubectl logs <pod-name> -c my-container -n <namespace> | tail -fDeletion & Cleanup
Section titled “Deletion & Cleanup”# Delete pod immediately (no grace period)kubectl delete pod <pod-name> --grace-period=0 --force -n <namespace>
# Delete all pods in namespacekubectl delete pods --all -n <namespace>
# Delete resources from YAML filekubectl delete -f deployment.yaml
# Delete deployment (cascades to pods)kubectl delete deployment <name> -n <namespace>
# Delete servicekubectl delete svc <service-name> -n <namespace>K9s: Terminal UI for Kubernetes
Section titled “K9s: Terminal UI for Kubernetes”k9s is a terminal-based dashboard for real-time cluster management and live debugging.
K9s Command Quick Reference
Section titled “K9s Command Quick Reference”| Command | Action |
|---|---|
:ctx | Switch context |
:ns | Change namespace |
:alias | View keyboard shortcuts |
:<resource> | Jump to resource type (e.g., :po for pods, :svc for services) |
Ctrl+A | Add/filter resource |
d | Describe selected resource |
e | Edit resource in YAML editor |
Ctrl+K | Delete resource |
l | View logs |
s | Open shell in pod |
/ | Search/filter resources |
h or ? | Show help |
Shift+R | Force refresh |
Ctrl+C | Exit |
K9s Advanced Features
Section titled “K9s Advanced Features”- Snapshots: Save cluster state for comparison (
Shift+S) - Benchmarks: Performance testing for services (
Ctrl+B) - Plugins: Extend k9s with custom commands
- Start k9s:
k9s(connects to current kubectl context)
Applying YAML to Running Container
Section titled “Applying YAML to Running Container”For workflows where you need to apply configuration changes to a running pod:
-
Verify current context and list pods
Terminal window kubectl config current-contextkubectl get pods -
Get container name inside pod
Terminal window kubectl get pod <pod-name> -o=jsonpath='{.spec.containers[*].name}' -
Initialize YAML variable (from file or here-document)
Terminal window yaml=$(cat <<EOFapiVersion: v1kind: ConfigMapmetadata:name: my-configdata:key: valueEOF) -
Apply YAML piped from variable
Terminal window echo "$yaml" | kubectl apply -f - -
Verify pod receives update (e.g., via curl)
Terminal window kubectl exec <pod-name> -c <container> -- curl -sS http://httpbin.org/headers -
Check container logs for confirmation
Terminal window kubectl logs <pod-name> -c <container> | tail -20
Pro Tip: Combine with watch for continuous monitoring:
watch -n 1 'kubectl get pods -n <namespace>'