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
provider "kubernetes" {
config_path = "~/.kube/config"
}
resource "kubernetes_namespace" "example" {
metadata {
name = "example-namespace"
}
}
Delete all#
kubectl delete all --all
kubectl get all
Update and upgrade sudo apt update && sudo apt upgrade -y sudo apt install -y curl gnupg apt-transport-https ca-certificates lsb-release Hostname sudo hostnamectl set-hostname k8s-cp-01 sudo hostnamectl set-hostname k8s-node-01 IP sudo nano /etc/network/interfaces auto eth0 iface eth0 inet static address 172.31.71.121 netmask 255.255.240.0 gateway 172.31.64.1 dns-nameservers 1.1.1.1 8.8.8.8 hosts sudo nano /etc/hosts 172.31.71.121 k8s-cp-01 172.31.71.131 k8s-node-01 Disable SWAP sudo swapoff -a sudo sed -i '/ swap / s/^/#/' /etc/fstab Install Docker / Containerd sudo apt install -y containerd sudo mkdir -p /etc/containerd containerd config default | sudo tee /etc/containerd/config....
Kubernetes Dashboard Installation and Configuration on Windows 11 Prerequisites Windows 11 with Docker and Kubernetes enabled. Helm installed. kubectl installed and configured to use the local Kubernetes cluster. Step 1: Add Kubernetes Dashboard Helm Repository helm repo add kubernetes-dashboard https://kubernetes.github.io/dashboard/ This command adds the Kubernetes Dashboard Helm chart repository.
Step 2: Install Kubernetes Dashboard using Helm helm upgrade --install kubernetes-dashboard kubernetes-dashboard/kubernetes-dashboard --create-namespace --namespace kubernetes-dashboard This command:
Installs or upgrades the kubernetes-dashboard Helm chart....