← All cheatsheets

Cloud + DevOps

kubectl — the 30 commands that cover 95% of kubernetes daily ops

The 30 kubectl commands for daily Kubernetes work: switch contexts, get and describe, logs and exec, apply, scale, drain nodes and debug.

kubectl is verbose by default but predictable. Memorize the verbs (get/describe/logs/exec) + scopes (-n, -A) and you can navigate any cluster.

Context

kubectl config get-contexts                         # list contexts
kubectl config use-context name                     # switch cluster
kubectl config current-context                      # show active context

Get / describe

kubectl get pods                                    # current namespace
kubectl get pods -A                                 # all namespaces
kubectl get pods -n monitoring                      # specific namespace
kubectl get pods -o wide                            # extra columns
kubectl get pods -l app=api                         # label selector
kubectl describe pod foo                            # full details

Logs + exec

kubectl logs foo --tail=50 --follow
kubectl logs foo -c sidecar                         # specific container
kubectl logs foo --previous                         # crashed container
kubectl exec -it foo -- bash
kubectl port-forward foo 8080:80                    # local port forward

Apply / delete

kubectl apply -f deploy.yaml
kubectl delete -f deploy.yaml
kubectl get deployment,svc                          # workloads + services
kubectl port-forward svc/api 8080:80                # tunnel to a service
kubectl rollout restart deployment/api
kubectl scale deployment/api --replicas=5

Cluster + nodes

kubectl get nodes
kubectl top pods                                    # resource usage
kubectl top nodes
kubectl cordon node-x                               # don't schedule new pods
kubectl drain node-x --ignore-daemonsets            # evict, prep for maintenance

Debugging

kubectl get events --sort-by='.lastTimestamp'       # what just happened
kubectl describe pod foo | grep -A 20 Events
kubectl debug pod/foo --image=busybox -it           # ephemeral container

Tip

Run alias k=kubectl and source <(kubectl completion bash). Cuts typing 70%.