Skip to the content.

Kubernetes Cheatsheet

A comprehensive quick-reference guide for Kubernetes cluster administration and application management using the `kubectl` CLI. ## Cluster Config & Context Configure and switch between multiple Kubernetes clusters and namespaces. ```bash kubectl config view # Display merged kubeconfig settings kubectl config get-contexts # List all available cluster contexts kubectl config current-context # Display the currently active context kubectl config use-context # Switch active context to a different cluster kubectl config set-context --current --namespace= # Change default namespace for active context kubectl cluster-info # Display address details of master and services ``` --- ## Working with Namespaces Isolate cluster resources and switch namespaces. ```bash kubectl get namespaces # List all namespaces in the cluster kubectl create namespace # Create a new namespace kubectl get all -n # List all resources within a specific namespace kubectl delete namespace # Delete namespace and all resources within it ``` --- ## Pod Management Deploy, query, inspect, and manage pods (the basic execution units). ```bash # Querying Pods kubectl get pods # List pods in default namespace kubectl get pods -A # List pods across ALL namespaces kubectl get pods -o wide # List pods with extended info (IP, Node) kubectl get pods -l app=nginx # Filter pods matching a specific label selector kubectl get pods --watch # List pods and watch for real-time state changes # Inspecting & Deleting Pods kubectl describe pod # View low-level config, state, and event logs for a pod kubectl get pod -o yaml # Export pod configuration in YAML format kubectl delete pod # Terminate and delete a specific pod kubectl delete pods --all # Force delete ALL pods in active namespace ``` --- ## Deployments & Scaling Manage self-healing application deployments and scale replicas. ```bash # Deployments & Scaling kubectl get deployments # List deployments in active namespace kubectl describe deployment # View details of a deployment kubectl scale deployment --replicas=5 # Scale replicas up or down to a specific count kubectl autoscale deployment --min=2 --max=10 --cpu-percent=80 # Set CPU-based autoscaling # Updates & Rollouts kubectl set image deployment/ =: # Update container image in deployment kubectl rollout status deployment/ # Monitor the deployment rollout status kubectl rollout history deployment/ # View revision history of rollouts kubectl rollout undo deployment/ # Rollback deployment to the previous version kubectl rollout undo deployment/ --to-revision=3 # Rollback to a specific revision kubectl rollout restart deployment/ # Perform a rolling restart of all replica pods ``` --- ## Services & Networking Expose applications internally or externally via services, port-forwarding, or ingress. ```bash # Querying Services kubectl get services # List all services in active namespace kubectl describe service # View detailed configurations and endpoints # Exposing & Forwarding Ports kubectl expose deployment --port=80 --target-port=8080 --type=ClusterIP # Expose deploy as ClusterIP kubectl expose deployment --port=80 --type=LoadBalancer # Expose deploy externally via LoadBalancer kubectl port-forward pod/ 8080:80 # Bind local port 8080 to container port 80 of a pod kubectl port-forward svc/ 8080:80 # Bind local port 8080 to port 80 of a cluster service # Ingress kubectl get ingress # List all configured ingresses kubectl describe ingress # View detailed ingress rules and hosts ``` --- ## ConfigMaps & Secrets Manage non-sensitive configurations and encrypted secrets. ```bash # ConfigMaps kubectl get configmaps # List all ConfigMaps kubectl describe configmap # Inspect configuration values kubectl create configmap --from-literal=key=value # Create ConfigMap from command-line values kubectl create configmap --from-file=config.env # Create ConfigMap from a file # Secrets kubectl get secrets # List all Secrets kubectl describe secret # View secret metadata (values are hidden) kubectl get secret -o jsonpath='{.data.password}' | base64 --decode # Retrieve and decrypt secret value kubectl create secret generic --from-literal=db-pass=secretpw # Create generic secret from literal ``` --- ## Logs, Debugging & Troubleshooting Investigate, debug, and diagnose application or node failures. ```bash # Container Logs kubectl logs # Print stdout logs for a pod kubectl logs -c # Print logs for a specific container inside a multi-container pod kubectl logs -f # Tail/follow stdout logs in real-time kubectl logs --tail=100 # Print only the last 100 log lines kubectl logs --since=1h # Print logs from the last hour # Active Debugging kubectl exec -it -- sh # Execute an interactive shell inside a pod kubectl exec -- env # Execute a single command (e.g. env) inside a pod without shells kubectl get events --sort-by='.metadata.creationTimestamp' # View cluster event logs sorted by time kubectl top pods # View real-time CPU and Memory usage of pods kubectl top nodes # View real-time CPU and Memory usage of nodes ``` --- ## Applying & Deleting Declarative Manifests Apply configuration states defined in YAML files. ```bash kubectl apply -f manifest.yaml # Create or update resources declared in a YAML file kubectl apply -f ./directory/ # Apply all YAML configurations within a folder kubectl diff -f manifest.yaml # Preview structural changes before applying them kubectl delete -f manifest.yaml # Delete all resources defined in a YAML file kubectl explain pod.spec # View structural API schema fields and definitions ``` --- ## Cluster Node Administration Administer physical/virtual host nodes in your cluster (Admin rights required). ```bash kubectl get nodes # List all nodes making up the cluster kubectl describe node # Inspect node health, capacity, and resource limits kubectl cordon # Mark node as unschedulable (prevents new pods) kubectl drain --ignore-daemonsets# Safe eviction of all pods from node (for maintenance) kubectl uncordon # Mark node as schedulable again ```