Quick Reference

Cheatsheets

Practical command references for Linux, networking, servers, containers, databases, and more.

Cheatsheet#kubernetes-cheatsheet

Kubernetes (kubectl) Cheatsheet

Kubernetes (K8s) is an open-source system for automating deployment, scaling, and management of containerized applications. kubectl is the command-line tool used to interact with the K8s API.

Installation

# Debian / Ubuntu
sudo apt install -y apt-transport-https ca-certificates curl
curl -fsSL https://pkgs.k8s.io/core:/stable:/v1.30/deb/Release.key | sudo gpg --dearmor -o /etc/apt/keyrings/kubernetes-apt-keyring.gpg
echo "deb [signed-by=/etc/apt/keyrings/kubernetes-apt-keyring.gpg] https://pkgs.k8s.io/core:/stable:/v1.30/deb/ /" | sudo tee /etc/apt/sources.list.d/kubernetes.list
sudo apt update && sudo apt install kubectl
 
# macOS
brew install kubectl
 
# Snap (any Linux)
sudo snap install kubectl --classic
 
# Verify installation
kubectl version --client

Cluster Info and Context

kubectl cluster-info                   # Display cluster endpoint and services
kubectl get nodes                      # List all nodes in the cluster
kubectl get nodes -o wide              # List nodes with details (IP, OS, kernel)
kubectl config get-contexts            # List all available contexts (clusters)
kubectl config current-context         # Show the currently active context
kubectl config use-context <name>      # Switch to a different context
kubectl config set-context --current --namespace=dev  # Set default namespace

Namespaces

kubectl get namespaces                 # List all namespaces
kubectl create namespace dev           # Create a new namespace
kubectl delete namespace dev           # Delete a namespace (and all resources in it)
 
# Run any command in a specific namespace
kubectl get pods -n kube-system        # List pods in 'kube-system' namespace
kubectl get all -n dev                 # List all resources in 'dev' namespace

Managing Pods

kubectl get pods                       # List pods in current namespace
kubectl get pods -A                    # List pods across all namespaces
kubectl get pods -o wide               # Show extra details (IP, Node)
kubectl get pods -w                    # Watch for changes in real-time
kubectl describe pod <pod-name>        # Show detailed info about a pod
kubectl delete pod <pod-name>          # Delete a pod (it will be recreated by its controller)
kubectl run nginx --image=nginx        # Quick-create a pod (for testing)

Deployments and Scaling

kubectl get deployments                # List all deployments
kubectl create deployment nginx --image=nginx  # Create a simple deployment
kubectl scale deployment nginx --replicas=3    # Scale to 3 replicas
kubectl rollout status deployment/nginx        # Check rollout progress
kubectl rollout history deployment/nginx       # View rollout revision history
kubectl rollout undo deployment/nginx          # Rollback to the previous version
kubectl rollout undo deployment/nginx --to-revision=2  # Rollback to a specific revision
kubectl delete deployment nginx        # Delete a deployment

Services and Networking

kubectl get services                   # List all services
kubectl get svc                        # Short alias for 'get services'
kubectl expose deployment nginx --port=80 --type=ClusterIP    # Internal-only service
kubectl expose deployment nginx --port=80 --type=NodePort     # Expose on each node's IP
kubectl expose deployment nginx --port=80 --type=LoadBalancer  # Expose via cloud load balancer
kubectl port-forward pod/nginx-abc123 8080:80  # Forward local port 8080 to pod port 80
kubectl port-forward svc/nginx 8080:80         # Forward to a service

ConfigMaps and Secrets

# Create a ConfigMap from literal values
kubectl create configmap app-config --from-literal=APP_ENV=production --from-literal=LOG_LEVEL=info
 
# Create a ConfigMap from a file
kubectl create configmap app-config --from-file=config.yaml
 
# Create a Secret from literal values
kubectl create secret generic db-creds --from-literal=username=admin --from-literal=password=secret
 
# View ConfigMaps and Secrets
kubectl get configmaps
kubectl get secrets
kubectl describe configmap app-config
kubectl get secret db-creds -o jsonpath='{.data.password}' | base64 -d  # Decode a secret value

Logs and Debugging

kubectl logs <pod-name>                # View logs of a pod
kubectl logs -f <pod-name>             # Follow logs in real-time
kubectl logs <pod-name> -c <container> # Logs for a specific container in a multi-container pod
kubectl logs --previous <pod-name>     # Logs from the previous container instance (after crash)
kubectl exec -it <pod-name> -- /bin/bash   # Open a shell inside a pod
kubectl exec -it <pod-name> -- /bin/sh     # Use sh if bash is not available
kubectl top pods                       # Show CPU/memory usage per pod (requires metrics-server)
kubectl top nodes                      # Show CPU/memory usage per node

Applying and Managing Resources

kubectl apply -f manifest.yaml         # Create or update resources from a YAML file
kubectl apply -f ./manifests/          # Apply all YAML files in a directory
kubectl delete -f manifest.yaml        # Delete resources defined in a YAML file
kubectl get all                        # List all standard resources in current namespace
kubectl get all -A                     # List all standard resources across all namespaces
 
# Generate YAML without applying (useful for creating templates)
kubectl create deployment nginx --image=nginx --dry-run=client -o yaml > deployment.yaml