10.31 - kubectl Cheatsheet

The only commands you need this week, grouped. kubectl talks to the API server; everything is a REST call against desired state.

Get

kubectl get nodes -o wide                # nodes + internal IP + roles
kubectl get pods                         # default namespace
kubectl get pods -A                      # all namespaces
kubectl get pods -o wide                 # + pod IP + node
kubectl get pods -o yaml                 # full manifest (what the API has)
kubectl get pods -o jsonpath='{.items[0].metadata.name}'
kubectl get deploy,rs,pods,svc,cm,secret,pvc,ing,job,cronjob,ds,sts,hpa
kubectl get all -n <ns>                  # NOT everything: no PVC/Ingress
kubectl get events --sort-by=.lastTimestamp | tail -20
kubectl api-resources                    # every resource kind + apiVersion
kubectl explain deployment.spec.replicas # offline API docs

Labels:

kubectl get pods --show-labels
kubectl get pods -l app=web
kubectl get pods -l 'app in (web,api),tier!=db'
kubectl label pod <name> env=prod
kubectl label pod <name> env-            # remove label

Create / apply / delete

kubectl apply -f manifest.yaml           # create or update (declarative)
kubectl apply -f dir/                    # apply a directory
kubectl apply --dry-run=client -f x.yaml # YAML sanity check
kubectl apply --dry-run=server -f x.yaml # schema check vs real API
kubectl delete -f manifest.yaml          # delete what the file created
kubectl delete pod <name>                # deletes; controller replaces it
kubectl delete namespace lab             # nuke a whole namespace + contents
kubectl create deployment web --image=nginx:alpine   # imperative, quick tests
kubectl run hello --image=busybox -- sh -c "sleep 3600"
kubectl create configmap app --from-literal=K=V --from-file=./f.conf
kubectl create secret generic db --from-literal=password=x
kubectl create namespace lab

Workloads

kubectl scale deployment hello --replicas=5
kubectl rollout status deployment/hello
kubectl rollout history deployment/hello
kubectl rollout undo deployment/hello          # rollback one revision
kubectl rollout undo deployment/hello --to-revision=1
kubectl rollout restart deployment/hello       # force new pods (config change)
kubectl autoscale deployment hello --cpu-percent=50 --min=1 --max=5
kubectl top nodes && kubectl top pods

Interact

kubectl logs <pod> --tail=50
kubectl logs <pod> --previous         # crashed container's last output
kubectl logs deploy/hello --follow
kubectl exec -it <pod> -- bash        # sh if no bash
kubectl exec <pod> -- env             # inspect env inside
kubectl port-forward svc/web 8080:80  # localhost:8080 -> service
kubectl port-forward pod/db-0 3306:3306
kubectl cp <pod>:/path/file ./file    # pull a file out of a pod

Contexts and namespaces

kubectl config current-context
kubectl config get-contexts
kubectl config set-context --current --namespace=lab   # default ns for a while
kubectl config use-context <name>     # switch clusters/profiles
kubectl config view                   # everything (careful: contains certs)

Describe (debug)

kubectl describe pod <name>
kubectl describe node <node>
kubectl describe svc <name>           # endpoints tell the selector story
kubectl describe pvc <name>
kubectl describe deploy <name>

Edit / patch (when you must)

kubectl edit deployment hello         # opens $EDITOR, applies on save
kubectl patch deployment hello --type=merge \
  -p '{"spec":{"template":{"spec":{"containers":[{"name":"hello","image":"nginx:1.27"}]}}}}'

Prefer edit/apply over patch; patch is for scripts and quick fixes.

Short aliases (k9s even better)

alias k='kubectl'
# fish: kubectl completion fish | source

k9s (nix shell nixpkgs#k9s) is a TUI over all of this: scroll pods, hit l for logs, d for describe, s for shell. Great for day 7 review, and it makes you look like a wizard on a shared screen. Use kubectl first until you can do everything it does with kubectl, then switch for speed.


This site uses Just the Docs, a documentation theme for Jekyll.