Skip to content
Commitpedia
Log in Join & edit

kubectl cheat sheet: the commands you use every day

A compact reference of everyday kubectl commands: contexts and namespaces, listing and inspecting objects, logs and exec, applying changes, rollouts, scaling and debugging.

Not checked by a trusted editor yet Last edited Outdated?

Edit history of kubectl cheat sheet: the commands you use every day: 1 recorded edit
Edit history · 1 edit
Type
Article
Edits
1
Editors
1
First issued
15 Oct 2026
Last edit
15 Oct 2026
Reading
2 min

kubectl is the command-line client for the Kubernetes API. Most commands follow the pattern kubectl <verb> <type> [name] [flags]. Add -n NAMESPACE to work in a namespace, or -A (--all-namespaces) to see all of them.

Contexts and namespaces

kubectl config get-contexts
kubectl config current-context
kubectl config use-context lab-cluster
kubectl config set-context --current --namespace=shop
kubectl cluster-info

A context is a cluster + user + default namespace from your kubeconfig file (~/.kube/config by default). Always check the current context before running commands that change anything.

Listing and inspecting

kubectl get pods
kubectl get pods -o wide
kubectl get pods -A
kubectl get pods -l app=web
kubectl get pods --field-selector=status.phase=Running
kubectl get pods --sort-by='.status.containerStatuses[0].restartCount'
kubectl get deploy,svc,ingress -n shop
kubectl get pod web-0 -o yaml
kubectl describe pod web-0
kubectl explain deployment.spec.strategy
kubectl api-resources

describe shows a human-readable summary including recent events; -o yaml shows the full object as stored. kubectl explain documents any field without leaving the terminal.

Logs, exec and port-forward

kubectl logs web-0
kubectl logs -f web-0 -c app
kubectl logs web-0 --previous
kubectl logs -l app=web --all-containers --tail=100
kubectl exec -it web-0 -- /bin/sh
kubectl port-forward service/web 8080:80
kubectl cp shop/web-0:/tmp/report.txt ./report.txt

Creating and changing objects

kubectl apply -f app.yaml
kubectl apply -f k8s/
kubectl diff -f app.yaml
kubectl create deployment web --image=nginx:1.27 --dry-run=client -o yaml > web.yaml
kubectl edit deployment web
kubectl label pod web-0 tier=frontend
kubectl annotate deployment web owner=team-a

--dry-run=client -o yaml is the quickest way to get a correct starting manifest. kubectl diff shows what apply would change.

Deployments: scale, update, roll back

kubectl scale deployment/web --replicas=5
kubectl set image deployment/web web=nginx:1.28
kubectl rollout status deployment/web
kubectl rollout history deployment/web
kubectl rollout undo deployment/web
kubectl rollout undo deployment/web --to-revision=2
kubectl rollout restart deployment/web

Events, resources and debugging

kubectl get events --sort-by=.metadata.creationTimestamp
kubectl events --types=Warning
kubectl top nodes
kubectl top pod --containers
kubectl debug -it web-0 --image=busybox:1.36 --target=web
kubectl auth can-i create deployments -n shop

kubectl top needs the metrics-server add-on. kubectl debug adds an ephemeral debugging container to a running Pod, which helps with minimal images that have no shell.

Deleting

Warning: deletes take effect immediately and there is no undo. Run the same selector with kubectl get first to see exactly what matches.

kubectl get pods -l app=old-web
kubectl delete pods -l app=old-web
kubectl delete -f app.yaml

Output formats

  • -o wide: extra columns such as node and Pod IP.
  • -o yaml / -o json: the full object.
  • -o name: just type/name, handy in loops.
  • -o jsonpath='{.items[*].metadata.name}': extract specific fields.
  • -o custom-columns=NAME:.metadata.name,NODE:.spec.nodeName: your own table.

Official documentation

See also

Repos in the wild

  • kubernetes/kubernetes kubernetes/kubernetes The main source repository of Kubernetes: the API server, scheduler, controller manager, kubelet, kube-proxy and kubectl, plus release notes and issue tracking. GitHub
  • derailed/k9s derailed/k9s K9s is a terminal UI for Kubernetes that lets you browse resources, read logs, open shells and follow cluster changes from the keyboard. GitHub

History

Every saved edit is kept. The plot on the sleeve draws one line per edit. Open any edit to see exactly what changed.

  1. Commitpedia +3,313 chars

Report a problem

Something wrong, unsafe or copied? Tell the editors.
What is wrong?