Kubernetes Cheat Sheet

1. Cluster and Configuration Management

kubectl config get-contexts         # List available contexts
kubectl config use-context CONTEXT  # Switch to a different cluster
kubectl config view                 # Display current configuration

2. Working with Pods

kubectl get pods                     # List all pods in the current namespace
kubectl get pods -A                  # List pods in all namespaces
kubectl describe pod POD_NAME         # Show details of a specific pod
kubectl logs POD_NAME                 # Retrieve logs from a pod (default container)
kubectl logs POD_NAME -c CONTAINER    # Retrieve logs from a specific container
kubectl exec -it POD_NAME -- bash     # Access a running pod interactively
kubectl delete pod POD_NAME           # Delete a pod
kubectl delete pod --all              # Delete all pods in the namespace

3. Deployments and Services

kubectl get deployments               # List all deployments
kubectl get services                   # List all services
kubectl describe deployment DEPLOYMENT # Show details of a specific deployment
kubectl rollout restart deployment DEPLOYMENT  # Restart a deployment
kubectl delete deployment DEPLOYMENT  # Delete a deployment
kubectl get svc                        # List services in the current namespace
kubectl delete svc SERVICE_NAME        # Delete a service

4. Namespace Management

kubectl get namespaces                 # List all namespaces
kubectl create namespace NAME          # Create a new namespace
kubectl delete namespace NAME          # Delete a namespace
kubectl config set-context --current --namespace=NAMESPACE  # Set default namespace

5. ConfigMap and Secrets

kubectl get configmap                  # List ConfigMaps
kubectl create configmap NAME --from-literal=KEY=VALUE  # Create a ConfigMap
kubectl describe configmap NAME         # Show details of a ConfigMap
kubectl delete configmap NAME           # Delete a ConfigMap

kubectl get secrets                     # List Secrets
kubectl create secret generic NAME --from-literal=KEY=VALUE  # Create a Secret
kubectl describe secret NAME             # Show details of a Secret
kubectl delete secret NAME               # Delete a Secret

6. Persistent Volumes and Claims

kubectl get pvc                         # List Persistent Volume Claims
kubectl describe pvc PVC_NAME           # Show details of a PVC
kubectl delete pvc PVC_NAME             # Delete a PVC
kubectl get pv                          # List Persistent Volumes
kubectl delete pv PV_NAME               # Delete a PV

7. Autoscaling and Monitoring

kubectl top pods                        # Display pod resource usage
kubectl top nodes                       # Display node resource usage
kubectl autoscale deployment DEPLOYMENT --cpu-percent=50 --min=1 --max=10  # Autoscale pods

8. Debugging

kubectl describe pod POD_NAME           # Show detailed information of a pod
kubectl logs POD_NAME                   # Retrieve pod logs
kubectl exec -it POD_NAME -- sh         # Access a pod shell
kubectl get events                      # List all cluster events
kubectl get pods --field-selector=status.phase=Failed  # List failed pods

9. Helm (If Using Helm)

helm repo add stable https://charts.helm.sh/stable  # Add Helm repository
helm install NAME CHART_NAME                        # Install a Helm chart
helm upgrade NAME CHART_NAME                        # Upgrade a Helm release
helm list                                           # List installed Helm charts
helm delete NAME                                    # Delete a Helm release

10. Labels and Selectors

kubectl get pods --show-labels             # Show pod labels
kubectl label pod POD_NAME env=prod        # Add a label to a pod
kubectl get pods -l env=prod               # List pods with a specific label
kubectl delete pod -l env=prod             # Delete pods with a specific label

11. RBAC (Role-Based Access Control)

kubectl get roles                         # List roles in the namespace
kubectl get rolebindings                   # List role bindings
kubectl create role NAME --verb=get,list --resource=pods  # Create a role
kubectl create rolebinding NAME --role=ROLE_NAME --user=USER  # Assign a role to a user
kubectl delete rolebinding NAME            # Delete a role binding

12. Backup and Restore

kubectl get all --all-namespaces -o yaml > backup.yaml  # Backup all resources
kubectl apply -f backup.yaml                            # Restore from YAML file

13. Cluster Node Management

kubectl drain NODE_NAME --ignore-daemonsets --delete-emptydir-data  # Evacuate a node
kubectl cordon NODE_NAME                                          # Mark a node as unschedulable
kubectl uncordon NODE_NAME                                        # Mark a node as schedulable again
kubectl delete node NODE_NAME                                     # Remove a node from the cluster

14. Terraform for Kubernetes (If Using Terraform)

provider "kubernetes" {
  config_path = "~/.kube/config"
}

resource "kubernetes_namespace" "example" {
  metadata {
    name = "example-namespace"
  }
}

Delete all

kubectl delete all --all
kubectl get all