← All cheatsheets

Cloud + DevOps

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

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

## Get / describe

```bash
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

```bash
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

```bash
kubectl apply -f deploy.yaml
kubectl rollout restart deployment/api
kubectl scale deployment/api --replicas=5
```

## Cluster + nodes

```bash
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

```bash
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%.