Kubernetes is a powerful tool for managing containerized applications, but its complexity can be daunting. Whether you’re deploying a new application, scaling services, or troubleshooting issues, having a Kubernetes cheat sheet at your fingertips can save you time and effort.
This guide expands on the basics, providing detailed tables of commands, advanced tips, and FAQs to help you become a Kubernetes expert.
Essential Kubernetes Commands
1. Cluster Management Commands
| Command | Description |
|---|---|
kubectl cluster-info | Displays information about the Kubernetes cluster. |
kubectl get nodes | Lists all nodes in the cluster. |
kubectl describe node <node-name> | Provides detailed information about a specific node. |
kubectl config view | Displays the current Kubernetes configuration. |
kubectl config use-context <context> | Switches to a different Kubernetes context. |
2. Pod Management Commands
| Command | Description |
|---|---|
kubectl get pods | Lists all pods in the current namespace. |
kubectl describe pod <pod-name> | Provides detailed information about a specific pod. |
kubectl logs <pod-name> | Displays logs for a specific pod. |
kubectl exec -it <pod-name> -- /bin/sh | Opens an interactive shell inside a running pod. |
kubectl delete pod <pod-name> | Deletes a specific pod. |
3. Deployment Commands
| Command | Description |
|---|---|
kubectl create deployment <name> --image=<image> | Creates a new deployment using a specified container image. |
kubectl get deployments | Lists all deployments in the current namespace. |
kubectl describe deployment <name> | Provides detailed information about a specific deployment. |
kubectl scale deployment <name> --replicas=<number> | Scales a deployment to the desired number of replicas. |
kubectl rollout status deployment/<name> | Displays the rollout status of a deployment. |
4. Service Management Commands
| Command | Description |
|---|---|
kubectl get services | Lists all services running in the cluster. |
kubectl describe service <service-name> | Provides detailed information about a specific service. |
kubectl expose deployment <name> --type=LoadBalancer --port=<port> | Exposes a deployment as a service. |
kubectl delete service <service-name> | Deletes a specific service. |
5. Namespace Commands
| Command | Description |
|---|---|
kubectl get namespaces | Lists all namespaces in the cluster. |
kubectl create namespace <name> | Creates a new namespace. |
kubectl delete namespace <name> | Deletes a specific namespace. |
kubectl config set-context --current --namespace=<name> | Switches to a different namespace. |
6. ConfigMap and Secret Commands
| Command | Description |
|---|---|
kubectl create configmap <name> --from-literal=<key>=<value> | Creates a ConfigMap from a literal value. |
kubectl get configmaps | Lists all ConfigMaps in the current namespace. |
kubectl describe configmap <name> | Provides detailed information about a specific ConfigMap. |
kubectl create secret generic <name> --from-literal=<key>=<value> | Creates a Secret from a literal value. |
kubectl get secrets | Lists all Secrets in the current namespace. |
Advanced Kubernetes Commands
1. Resource Monitoring
| Command | Description |
|---|---|
kubectl top nodes | Displays resource usage (CPU, memory) for nodes. |
kubectl top pods | Displays resource usage (CPU, memory) for pods. |
2. Rolling Updates and Rollbacks
| Command | Description |
|---|---|
kubectl set image deployment/<name> <container>=<new-image> | Updates the image of a deployment. |
kubectl rollout history deployment/<name> | Displays the rollout history of a deployment. |
kubectl rollout undo deployment/<name> | Rolls back a deployment to the previous version. |
3. Job and CronJob Commands
| Command | Description |
|---|---|
kubectl create job <name> --image=<image> | Creates a new job. |
kubectl get jobs | Lists all jobs in the current namespace. |
kubectl delete job <name> | Deletes a specific job. |
kubectl create cronjob <name> --image=<image> --schedule="<schedule>" | Creates a new CronJob. |
kubectl get cronjobs | Lists all CronJobs in the current namespace. |
Common Kubernetes Challenges and Solutions
| Challenge | Solution |
|---|---|
| Pods stuck in “Pending” state | Check node resources and taints with kubectl describe node <node-name>. |
| Services not accessible | Verify service type and endpoints with kubectl describe service <name>. |
| Configuration errors | Validate YAML files with kubectl apply -f <file>.yaml --dry-run=client. |
| Resource limits exceeded | Monitor usage with kubectl top and adjust resource requests/limits. |
FAQs About Kubernetes
A pod is the smallest deployable unit in Kubernetes, while a deployment manages the lifecycle of pods, ensuring the desired number of replicas are running.
To access the Kubernetes dashboard, run: bash kubectl proxy. Then, open your browser and navigate to the provided URL.
Yes, Kubernetes can be used for small projects, but it may be overkill for very simple applications. Consider using simpler tools like Docker Compose for smaller setups.
This Kubernetes cheat sheet is your ultimate guide to mastering Kubernetes commands and workflows. With detailed tables, advanced tips, and FAQs, you’ll be able to tackle any challenge with confidence. Bookmark this page for quick reference, and happy orchestrating!