Essential Kubectl Commands: The Ultimate Kubernetes Cheat Sheet

Posted by Anonymous and classified in Computers

Written on in English with a size of 7.76 KB

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-run is essential.
  • Use a consistent workflow for debugging: getdescribelogs. This structured approach surfaces issues faster and reduces downtime.
  • Favor declarative over imperative commands by using YAML manifests with kubectl apply to 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.

CommandDescriptionExample
kubectl get podsList pods in current namespacekubectl get pods
kubectl get pods -nList pods in specific namespacekubectl get pods -n kube-system
kubectl describe podDetailed pod informationkubectl describe pod nginx-pod
kubectl get pods -o wideShow additional info (IPs, nodes)kubectl get pods -o wide

Deploying and Managing Applications

CommandDescriptionExample
kubectl apply -fCreate/update from YAMLkubectl apply -f app.yaml
kubectl scaleScale deployment replicaskubectl scale deployment web --replicas=5
kubectl rollout undoRoll back a deploymentkubectl rollout undo deployment/web

Debugging and Troubleshooting

Follow the getdescribelogs workflow. If pods fail, check for ImagePullBackOff or CrashLoopBackOff states.

Logs and Exec

CommandDescriptionExample
kubectl logs -fStream pod logskubectl logs -f api-pod
kubectl exec -itOpen shell inside containerkubectl exec -it mypod -- /bin/bash
kubectl port-forwardForward local port to podkubectl port-forward pod/mypod 8080:80

Best Practices for Stability

  • Use Aliases: alias k=kubectl saves time.
  • Enable Autocompletion: source <(kubectl completion bash).
  • Dry-Run: Test changes with --dry-run=client before applying.
  • Resource Limits: Always define CPU/memory requests and limits to prevent cluster contention.
  • Version Control: Keep all YAML manifests in Git.

Related entries: