Kubernetes Cheat Sheet: 50+ Essential Commands
Introduction
Kubernetes (K8s) is a powerful open-source container orchestration platform that automates the deployment, scaling, and management of containerized applications. It helps developers and operations teams efficiently manage complex applications across multiple environments, including cloud, on-premises, and hybrid infrastructures.
In this guide, we’ll walk through the fundamentals of Kubernetes and explain how to set up a Kubernetes environment from scratch.
Why Use Kubernetes?
Before diving into the setup, let’s understand why Kubernetes is widely adopted:
- Automated Deployment & Scaling: Easily scale applications up or down based on demand.
- Load Balancing & Service Discovery: Manages traffic distribution across containers.
- Self-Healing: Restarts failed containers and replaces unresponsive instances automatically.
- Resource Optimization: Ensures efficient use of compute and storage resources.
- Portable & Cloud-Agnostic: Works on any infrastructure, including AWS, Azure, Google Cloud, and on-premises.
Getting Started with Kubernetes
Read this article: Introduction to Kubernetes and installation
1. Cluster Management
kubectl cluster-info # Display cluster information
kubectl config get-contexts # List available contexts
kubectl config use-context <name> # Switch to a specific context
kubectl config current-context # Show current context
kubectl get nodes # List all nodes in the cluster
kubectl describe node <node-name> # Describe a specific node
kubectl cordon <node-name> # Mark a node as unschedulable
kubectl uncordon <node-name> # Mark a node as schedulable
kubectl drain <node-name> # Safely evict all pods from a node
kubectl delete node <node-name> # Remove a node from the cluster
2. Pod Management
kubectl get pods # List all pods in the default namespace
kubectl get pods -A # List all pods across all namespaces
kubectl get pods -o wide # Show more details including node assignments
kubectl describe pod <pod-name> # Show detailed information about a pod
kubectl logs <pod-name> # Get logs from a pod
kubectl logs -f <pod-name> # Stream logs from a pod
kubectl exec -it <pod-name> -- /bin/sh # Open a shell in a running container
kubectl delete pod <pod-name> # Delete a pod
kubectl apply -f <file>.yaml # Create/update a pod using a YAML file
kubectl run <pod-name> --image=<image> # Create a pod with a specific image
3. Deployment Management
kubectl get deployments # List all deployments
kubectl describe deployment <name> # Show details of a deployment
kubectl delete deployment <name> # Delete a deployment
kubectl scale deployment <name> --replicas=3 # Scale a deployment to 3 replicas
kubectl rollout status deployment <name> # Show rollout status
kubectl rollout history deployment <name> # Show rollout history
kubectl rollout undo deployment <name> # Rollback to the previous version
kubectl apply -f <deployment>.yaml # Apply a deployment from YAML
4. Service Management
kubectl get services # List all services
kubectl describe service <name> # Show details of a service
kubectl delete service <name> # Delete a service
kubectl expose deployment <name> --type=LoadBalancer --port=80 # Expose a deployment as a service
5. Namespace Management
kubectl get namespaces # List all namespaces
kubectl create namespace <name> # Create a new namespace
kubectl delete namespace <name> # Delete a namespace
6. ConfigMaps and Secrets
kubectl get configmaps # List all ConfigMaps
kubectl describe configmap <name> # Show details of a ConfigMap
kubectl delete configmap <name> # Delete a ConfigMap
kubectl create configmap <name> --from-literal=key=value # Create a ConfigMap
kubectl get secrets # List all secrets
kubectl describe secret <name> # Show details of a secret
kubectl delete secret <name> # Delete a secret
kubectl create secret generic <name> --from-literal=username=admin --from-literal=password=secret # Create a secret
7. Persistent Volumes and Storage
kubectl get pv # List all PersistentVolumes
kubectl get pvc # List all PersistentVolumeClaims
kubectl describe pv <name> # Show details of a PersistentVolume
kubectl describe pvc <name> # Show details of a PersistentVolumeClaim
kubectl delete pvc <name> # Delete a PersistentVolumeClaim
kubectl apply -f <pvc-file>.yaml # Create a PersistentVolumeClaim from a YAML file
8. Ingress Management
kubectl get ingress # List all ingress resources
kubectl describe ingress <name> # Show details of an ingress
kubectl delete ingress <name> # Delete an ingress resource
kubectl apply -f <ingress>.yaml # Create/update an ingress resource
9. Debugging and Monitoring
kubectl top nodes # Show CPU/memory usage of nodes
kubectl top pods # Show CPU/memory usage of pods
kubectl events --sort-by='.lastTimestamp' # Show cluster events sorted by time
kubectl get pods --field-selector=status.phase=Running # List only running pods
10. Custom Resource Definitions (CRDs)
kubectl get crds # List all CRDs
kubectl describe crd <name> # Show details of a CRD
kubectl delete crd <name> # Delete a CRD
kubectl apply -f <crd>.yaml # Create/update a CRD
11. Jobs and CronJobs
kubectl get jobs # List all jobs
kubectl describe job <name> # Show details of a job
kubectl delete job <name> # Delete a job
kubectl apply -f <job>.yaml # Create/update a job
kubectl get cronjobs # List all CronJobs
kubectl delete cronjob <name> # Delete a CronJob
kubectl apply -f <cronjob>.yaml # Create/update a CronJob
12. Role-Based Access Control (RBAC)
kubectl get roles # List all roles
kubectl describe role <name> # Show details of a role
kubectl delete role <name> # Delete a role
kubectl get rolebindings # List all role bindings
kubectl describe rolebinding <name> # Show details of a role binding
kubectl delete rolebinding <name> # Delete a role binding
13. Helm (Package Manager for Kubernetes)
helm repo add stable https://charts.helm.sh/stable # Add Helm chart repository
helm repo update # Update repo information
helm search repo <chart-name> # Search for a chart
helm install <release-name> <chart-name> # Install a Helm chart
helm list # List all Helm releases
helm uninstall <release-name> # Uninstall a Helm release
Conclusion
This cheat sheet covers 50+ essential Kubernetes commands to help manage clusters, pods, deployments, services, and more. Keep practicing these commands to streamline your Kubernetes operations!