Kubernetes Cheatsheet
Kubernetes (kubectl) 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.
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 <context-name> # Switch active context to a different cluster
kubectl config set-context --current --namespace=<ns> # 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.
kubectl get namespaces # List all namespaces in the cluster
kubectl create namespace <namespace-name> # Create a new namespace
kubectl get all -n <namespace-name> # List all resources within a specific namespace
kubectl delete namespace <namespace-name> # Delete namespace and all resources within it
Pod Management
Deploy, query, inspect, and manage pods (the basic execution units).
# 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 <pod-name> # View low-level config, state, and event logs for a pod
kubectl get pod <pod-name> -o yaml # Export pod configuration in YAML format
kubectl delete pod <pod-name> # 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.
# Deployments & Scaling
kubectl get deployments # List deployments in active namespace
kubectl describe deployment <deploy-name> # View details of a deployment
kubectl scale deployment <deploy-name> --replicas=5 # Scale replicas up or down to a specific count
kubectl autoscale deployment <d-name> --min=2 --max=10 --cpu-percent=80 # Set CPU-based autoscaling
# Updates & Rollouts
kubectl set image deployment/<d-name> <c>=<image>:<tag> # Update container image in deployment
kubectl rollout status deployment/<d-name> # Monitor the deployment rollout status
kubectl rollout history deployment/<d-name> # View revision history of rollouts
kubectl rollout undo deployment/<d-name> # Rollback deployment to the previous version
kubectl rollout undo deployment/<d-name> --to-revision=3 # Rollback to a specific revision
kubectl rollout restart deployment/<d-name> # Perform a rolling restart of all replica pods
Services & Networking
Expose applications internally or externally via services, port-forwarding, or ingress.
# Querying Services
kubectl get services # List all services in active namespace
kubectl describe service <service-name> # View detailed configurations and endpoints
# Exposing & Forwarding Ports
kubectl expose deployment <d-name> --port=80 --target-port=8080 --type=ClusterIP # Expose deploy as ClusterIP
kubectl expose deployment <d-name> --port=80 --type=LoadBalancer # Expose deploy externally via LoadBalancer
kubectl port-forward pod/<pod-name> 8080:80 # Bind local port 8080 to container port 80 of a pod
kubectl port-forward svc/<svc-name> 8080:80 # Bind local port 8080 to port 80 of a cluster service
# Ingress
kubectl get ingress # List all configured ingresses
kubectl describe ingress <ingress-name> # View detailed ingress rules and hosts
ConfigMaps & Secrets
Manage non-sensitive configurations and encrypted secrets.
# ConfigMaps
kubectl get configmaps # List all ConfigMaps
kubectl describe configmap <cm-name> # Inspect configuration values
kubectl create configmap <name> --from-literal=key=value # Create ConfigMap from command-line values
kubectl create configmap <name> --from-file=config.env # Create ConfigMap from a file
# Secrets
kubectl get secrets # List all Secrets
kubectl describe secret <secret-name> # View secret metadata (values are hidden)
kubectl get secret <name> -o jsonpath='{.data.password}' | base64 --decode # Retrieve and decrypt secret value
kubectl create secret generic <name> --from-literal=db-pass=secretpw # Create generic secret from literal
Logs, Debugging & Troubleshooting
Investigate, debug, and diagnose application or node failures.
# Container Logs
kubectl logs <pod-name> # Print stdout logs for a pod
kubectl logs <pod-name> -c <container-name> # Print logs for a specific container inside a multi-container pod
kubectl logs -f <pod-name> # Tail/follow stdout logs in real-time
kubectl logs --tail=100 <pod-name> # Print only the last 100 log lines
kubectl logs --since=1h <pod-name> # Print logs from the last hour
# Active Debugging
kubectl exec -it <pod-name> -- sh # Execute an interactive shell inside a pod
kubectl exec <pod-name> -- 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.
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).
kubectl get nodes # List all nodes making up the cluster
kubectl describe node <node-name> # Inspect node health, capacity, and resource limits
kubectl cordon <node-name> # Mark node as unschedulable (prevents new pods)
kubectl drain <node-name> --ignore-daemonsets# Safe eviction of all pods from node (for maintenance)
kubectl uncordon <node-name> # Mark node as schedulable again