Kubernetes Cheatsheet

kubectl Commands

Use this Kubernetes reference while you build software engineering projects, review code for technical interview prep, or polish examples for a software engineer resume.

Installation and Config

# install (macOS)
brew install kubectl

# verify
kubectl version --client

# kubeconfig location (default)
~/.kube/config

# merge configs
KUBECONFIG=~/.kube/config:~/other-cluster.yaml kubectl config view --flatten > ~/.kube/config

Context Management

kubectl config get-contexts                          # list contexts
kubectl config current-context                       # show active context
kubectl config use-context my-cluster                # switch context
kubectl config set-context --current --namespace=dev # set default namespace
kubectl config delete-context old-cluster            # remove context
kubectl config rename-context old new                # rename

Getting Resources

kubectl get <resource>                   # list in current namespace
kubectl get <resource> -n staging        # specific namespace
kubectl get <resource> -A                # all namespaces
kubectl get <resource> -o wide           # extra columns (IP, node, etc.)
kubectl get <resource> -o yaml           # full YAML
kubectl get <resource> -o json           # full JSON
kubectl get <resource> -o name           # just resource/name pairs
kubectl get <resource> --watch           # stream updates
kubectl get <resource> --show-labels     # include labels column

Useful -o flags

# extract a specific field
kubectl get pod my-pod -o jsonpath='{.status.podIP}'
kubectl get nodes -o jsonpath='{.items[*].metadata.name}'
kubectl get svc -o jsonpath='{.items[*].spec.clusterIP}'

# custom columns
kubectl get pods -o custom-columns='NAME:.metadata.name,STATUS:.status.phase,NODE:.spec.nodeName'

# sort by field
kubectl get pods --sort-by='.metadata.creationTimestamp'
kubectl get pods --sort-by='.status.startTime'

Applying and Deleting

kubectl apply -f manifest.yaml           # create or update (idempotent)
kubectl apply -f ./dir/                  # apply all YAML files in a directory
kubectl apply -f https://url/file.yaml   # from URL
kubectl apply -k ./kustomize/            # apply Kustomize overlay

kubectl create -f manifest.yaml          # create only (errors if exists)
kubectl replace -f manifest.yaml         # replace (must exist)

kubectl delete -f manifest.yaml          # delete what the manifest describes
kubectl delete pod my-pod                # delete by name
kubectl delete pods --all -n staging     # delete all pods in namespace
kubectl delete all --all -n staging      # delete all common resources
kubectl delete pod my-pod --grace-period=0 --force   # immediate

Describe and Debug

kubectl describe pod my-pod              # events, state, probes
kubectl describe node my-node            # capacity, conditions, pods
kubectl describe deployment api          # replica history, events

kubectl logs my-pod                      # stdout of first container
kubectl logs my-pod -c sidecar           # specific container
kubectl logs my-pod --previous           # previous (crashed) container
kubectl logs my-pod --tail=100           # last 100 lines
kubectl logs my-pod --since=1h           # since duration
kubectl logs my-pod -f                   # follow (stream)
kubectl logs -l app=api --all-containers # logs for all matching pods

kubectl exec -it my-pod -- bash          # interactive shell
kubectl exec my-pod -- env               # run one-off command
kubectl exec -it my-pod -c sidecar -- sh # exec into specific container

kubectl cp my-pod:/app/log.txt ./log.txt  # copy from pod
kubectl cp ./config.yaml my-pod:/etc/app/ # copy to pod

kubectl port-forward pod/my-pod 8080:80
kubectl port-forward svc/api 8080:80
kubectl port-forward deployment/api 8080:80

Resource Management

kubectl scale deployment api --replicas=5
kubectl set image deployment/api api=myrepo/api:v2
kubectl set env deployment/api LOG_LEVEL=debug
kubectl set resources deployment/api -c api --limits=cpu=1,memory=512Mi
kubectl label pod my-pod version=v2
kubectl label pod my-pod version-          # remove label
kubectl annotate deployment api team=backend
kubectl patch deployment api -p '{"spec":{"replicas":3}}'
kubectl patch svc api --type=merge -p '{"spec":{"type":"LoadBalancer"}}'

Rollouts

kubectl rollout status deployment/api     # watch progress
kubectl rollout history deployment/api    # list revisions
kubectl rollout undo deployment/api       # roll back one revision
kubectl rollout undo deployment/api --to-revision=3
kubectl rollout pause deployment/api
kubectl rollout resume deployment/api
kubectl rollout restart deployment/api    # rolling restart

Node Operations

kubectl get nodes
kubectl get nodes -o wide                  # OS, kernel, container runtime
kubectl describe node my-node
kubectl cordon my-node                     # mark unschedulable (no new pods)
kubectl uncordon my-node                   # re-enable scheduling
kubectl drain my-node --ignore-daemonsets --delete-emptydir-data
kubectl top nodes                          # CPU/memory (needs metrics-server)
kubectl top pods                           # per-pod CPU/memory
kubectl top pods --containers              # per-container

Run a Throwaway Pod

# one-shot debug container
kubectl run debug --image=busybox --rm -it --restart=Never -- sh

# curl from inside the cluster
kubectl run curl --image=curlimages/curl --rm -it --restart=Never -- curl http://api/health

# dns lookup
kubectl run dnsutils --image=registry.k8s.io/e2e-test-images/jessie-dnsutils:1.3 \
  --rm -it --restart=Never -- nslookup api.production.svc.cluster.local

Explain (built-in API docs)

kubectl explain pod
kubectl explain pod.spec
kubectl explain pod.spec.containers.resources
kubectl explain deployment.spec.strategy

Common Shortcuts

AliasFull resource
popods
deploydeployments
svcservices
nsnamespaces
cmconfigmaps
pvcpersistentvolumeclaims
pvpersistentvolumes
saserviceaccounts
ingingresses
hpahorizontalpodautoscalers
rsreplicasets
stsstatefulsets
dsdaemonsets
cjcronjobs
ependpoints

Plugins (krew)

# install krew plugin manager
brew install krew

kubectl krew install ctx       # kubectx — fast context switching
kubectl krew install ns        # kubens — fast namespace switching
kubectl krew install neat       # clean up noisy YAML output
kubectl krew install tree       # show owner-reference tree
kubectl krew install stern      # multi-pod log tailing