Kubernetes Cheatsheet - Essential kubectl Commands for DevOps

Master Kubernetes with this essential kubectl cheatsheet. Quickly find commands for managing deployments, pods, logs, contexts, and more. Ideal for DevOps engineers.

Kubernetes Cheatsheet

Kubernetes kubectl Command Reference

This Kubernetes cheatsheet provides essential kubectl commands for DevOps engineers to efficiently manage containerized applications. It covers common tasks like setting contexts, retrieving resource information, inspecting pods, and viewing logs.

Managing Kubernetes Contexts

Easily switch between Kubernetes clusters and namespaces using kubectl config commands. While tools like kubectx are popular, understanding the native kubectl commands is fundamental.

kubectl config set-context --current --namespace applications
kubectl config use-context $EKS_CLUSTER_ARN

Retrieving Deployments

Get detailed information about your Kubernetes deployments, including their status and configuration, within a specific namespace.

kubectl get deployments.v1.apps -o json -n test

Inspecting Pods

View a list of pods, including their status and network details. Filter for pods that are currently in a running state.

kubectl get pods -o wide -n test

Pods that are running.

kubectl get pods -n test --field-selector=status.phase==Running

Describing Pods

Get in-depth details about a specific pod, including its events, status, and container information. This is crucial for debugging.

kubectl describe pods/bridge-client-876b58b4d-tsbhc -n test

Executing Commands within Pods

Access a pod's shell or run specific commands directly inside a container. The double dash (--) separates kubectl arguments from the command to be executed within the pod.

kubectl exec -it --namespace=tools mongo-pod -- bash -c "mongo"

Checking Component Status

Monitor the health and status of Kubernetes control plane components.

kubectl get componentstatuses -n test

Viewing Kubernetes Events

Retrieve a list of events occurring within your Kubernetes cluster, which is vital for understanding cluster activity and troubleshooting issues.

kubectl -n test get events

Accessing Pod Logs

View the logs generated by your application running inside a pod. You can also follow the logs in real-time.

kubectl logs bridge-client-6df4b7bfdc-2trr5 -n test
kubectl logs bridge-client-6df4b7bfdc-2trr5 -n test -f

Managing Pod Disruption Budgets

Check the configured Pod Disruption Budgets (PDBs) to ensure application availability during voluntary disruptions.

kubectl get poddisruptionbudget

Key Kubernetes Resources

Understanding these core Kubernetes concepts is essential for effective cluster management.

External Resources

  1. Official kubectl Cheat Sheet
  2. Kubernetes (kubectl) get running pods - Stack Overflow
  3. Kubernetes Pods Documentation