Essential Kubectl Commands: The Ultimate Kubernetes Cheat Sheet
Key Takeaways
- kubectl is the core tool for managing Kubernetes clusters, enabling you to inspect, deploy, troubleshoot, and scale workloads from the command line. Mastering flags like
-n,-o, and--dry-runis essential. - Use a consistent workflow for debugging:
get→describe→logs. This structured approach surfaces issues faster and reduces downtime. - Favor declarative over imperative commands by using YAML manifests with
kubectl applyto promote version control and repeatability.
Kubernetes (K8s) is the industry standard for orchestrating containerized applications, but managing it can be complex. At the center of every Kubernetes workflow is kubectl, the command-line utility used to interact with clusters, deploy applications, and troubleshoot issues.
This reference provides the most useful kubectl commands, organized by category with practical examples to help you master Kubernetes operations.
Kubernetes at a Glance: Key Concepts
Before diving into commands, understand these core building blocks:
- Pods: The smallest deployable units in Kubernetes.
- Deployments: Manage sets of pods and apply declarative updates.
- Services: Expose your applications internally or externally.
- Namespaces: Provide logical isolation for resources.
- Nodes: Worker machines where workloads run.
- Clusters: Collections of nodes.
- Contexts: Define environments (e.g., prod, staging) for easy switching.
- YAML: The primary format for defining Kubernetes objects.
Inspecting and Understanding Your Cluster
Use these commands to list and describe resources running in your cluster.
| Command | Description | Example |
|---|---|---|
kubectl get pods | List pods in current namespace | kubectl get pods |
kubectl get pods -n | List pods in specific namespace | kubectl get pods -n kube-system |
kubectl describe pod | Detailed pod information | kubectl describe pod nginx-pod |
kubectl get pods -o wide | Show additional info (IPs, nodes) | kubectl get pods -o wide |
Deploying and Managing Applications
| Command | Description | Example |
|---|---|---|
kubectl apply -f | Create/update from YAML | kubectl apply -f app.yaml |
kubectl scale | Scale deployment replicas | kubectl scale deployment web --replicas=5 |
kubectl rollout undo | Roll back a deployment | kubectl rollout undo deployment/web |
Debugging and Troubleshooting
Follow the get → describe → logs workflow. If pods fail, check for ImagePullBackOff or CrashLoopBackOff states.
Logs and Exec
| Command | Description | Example |
|---|---|---|
kubectl logs -f | Stream pod logs | kubectl logs -f api-pod |
kubectl exec -it | Open shell inside container | kubectl exec -it mypod -- /bin/bash |
kubectl port-forward | Forward local port to pod | kubectl port-forward pod/mypod 8080:80 |
Best Practices for Stability
- Use Aliases:
alias k=kubectlsaves time. - Enable Autocompletion:
source <(kubectl completion bash). - Dry-Run: Test changes with
--dry-run=clientbefore applying. - Resource Limits: Always define CPU/memory requests and limits to prevent cluster contention.
- Version Control: Keep all YAML manifests in Git.
English with a size of 7.76 KB