kubectl is the primary interface to a Kubernetes cluster. This cheatheet covers routine operations — from context setup to pod debugging and namespace switching. All commands are verified against current kubectl versions (1.28+).
Installation and Context Setup
Installation depends on your OS. On Linux, use the package manager or the binary directly:
Configuration lives in ~/.kube/config. A context defines the cluster, user, and default namespace.
kubectl config current-context
kubectl config use-context production-cluster
kubectl config view --minify # show current contextkubectl config get-contexts # list all contexts
Tip
If you work with multiple clusters, store configs in the KUBECONFIG variable separated by colons: export KUBECONFIG=~/.kube/config:~/.kube/prod-config.
Working with Pods
Basic operations:
kubectl get pods # all pods in the current namespacekubectl get pods -A # all namespaceskubectl describe pod <pod-name> # details and eventskubectl logs <pod-name> # container logskubectl logs <pod-name> -c <container> # logs for a specific container in a multi-container podkubectl exec -it <pod-name> -- /bin/sh # enter a podkubectl delete pod <pod-name> # delete a pod
Deployments, StatefulSets, and DaemonSets
kubectl get deployments,statefulsets,daemonsets
kubectl rollout status deployment/<name> # update statuskubectl rollout history deployment/<name> # revision historykubectl rollout undo deployment/<name> # rollback to previous revisionkubectl rollout undo deployment/<name> --to-revision=2# rollback to a specific revisionkubectl scale deployment/<name> --replicas=5# scale replicas
For StatefulSets, startup order and stable identities are critical. For DaemonSets, a guaranteed instance runs on every node.
Warning
Do not run kubectl delete on a Deployment without checking kubectl get deployment first. Deleting the controller does not automatically remove pods — they will be recreated unless you use --cascade=orphan (in older versions) or delete through kubectl delete deployment.
Services, Ingress, and ConfigMap
kubectl get svc # serviceskubectl expose deployment/<name> --port=80 --type=NodePort # create svc from a deploymentkubectl get ingress # ingress resourceskubectl describe ingress <name> # rules and eventskubectl apply -f ingress.yaml # apply ingress from a file
kubectl get pods -o wide # status and nodekubectl describe pod <pod-name> # events, reason for CrashLoopBackOffkubectl logs <pod-name> --previous # logs from a crashed containerkubectl top pod <pod-name> # CPU/memory consumption (requires metrics-server)kubectl attach -it <pod-name> -- /bin/sh # alternative to exec
To test network connectivity from inside the cluster:
If kubectl top returns an error, metrics-server is not installed. Installation depends on your provider: kubectl apply -f https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yaml.
Labels, Selectors, and Output Formatting
Labels let you group resources and select them in bulk:
kubectl get pods --show-labels
kubectl label pods <pod-name> app=frontend tier=web
kubectl get pods -l app=frontend # label selectorkubectl get pods -l 'app in (frontend,backend)'kubectl get pods -l '!tier=web'# negative selector
Output formatting:
kubectl get pods -o wide
kubectl get pods -o json # full JSONkubectl get pods -o jsonpath='{.items[*].metadata.name}'kubectl get pods -o custom-columns=NAME:.metadata.name,STATUS:.status.phase
Flag
Description
-n, --namespace
Specify namespace
-o, --output
Format: json, yaml, wide, custom-columns
-l, --selector
Label selector
-f, --filename
Configuration file (YAML/JSON)
--show-labels
Show labels column
-w, --watch
Streaming update mode
--all-namespaces, -A
All namespaces
Managing Namespaces and Switching Contexts
kubectl get namespace
kubectl create namespace staging
kubectl delete namespace staging # deletion is asynchronouskubectl config set-context --current --namespace=staging # default namespace in context
For convenience, create a shell alias or function:
kubectl config rename-context, kubectl config unset, and kubectl config set let you edit the config without manually editing YAML. Verify the result with kubectl config view.
kubectl is more than a CLI — it is an abstraction layer over the Kubernetes API. Knowing formatting flags and selectors cuts diagnostic time significantly. Keep this cheatheet handy and update it as new versions ship.