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.
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: justtype/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
- 0131 Kubernetes architecture: control plane, nodes, kubelet and etcd A Kubernetes cluster is a control plane that stores and decides the desired state, plus worker nodes that run your containers. This… Beginner · Concept · Linux Article Concepts 15 Oct 2026
- 0132 Pods, ReplicaSets and Deployments explained A Pod runs one or more containers together, a ReplicaSet keeps a fixed number of identical Pods alive, and a Deployment manages… Beginner · Concept · Linux Article Concepts · Workloads & services 15 Oct 2026
- 0138 Kubernetes RBAC basics: least-privilege access for a service account RBAC controls who may do what in a cluster with Roles, ClusterRoles and bindings. This guide grants a CI service account deploy… Advanced · How-to · Linux Article Concepts · Config, secrets & storage 15 Oct 2026
- 0139 Collect describe, events and logs of a failing Kubernetes pod A bash script that saves a pod's status, describe output, events, YAML, current and previous logs of every container and its node… Intermediate · How-to · Linux, macOS Script Troubleshooting 15 Oct 2026
- 0141 List Kubernetes pods that are not Running and Ready A bash script that lists every pod across namespaces that is Pending, failing or running but not ready, with the waiting reason,… Intermediate · Reference · Linux, macOS Script Troubleshooting 15 Oct 2026
- 0142 Fix: ImagePullBackOff and ErrImagePull in Kubernetes The pod cannot start because the node failed to pull the container image. Read the "Failed to pull image" event to see… Intermediate · Fix · Linux Article Troubleshooting 15 Oct 2026
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.
- commitpedia-admin +3,313 chars