Cloud & DevOps

Kubernetes Commands Cheat Sheet

A kubectl and Kubernetes commands cheat sheet for contexts, workloads, logs, debugging, scaling, rollout and safe resource changes.

Kubernetes operations console with an orchestrated cluster of nodes and pods
Original concept artwork created for this Circuit Compass guide.

This Kubernetes cheat sheet covers common kubectl tasks. Always confirm the active context and namespace before changing a cluster.

Context and discovery

kubectl config current-contextShow the active cluster context.
kubectl config get-contextsList available contexts.
kubectl get namespacesList namespaces.
kubectl api-resourcesList resource types supported by the server.
kubectl versionShow client and server version information.

Inspect workloads

kubectl get pods -n NAMEList pods in a namespace.
kubectl get deploy,svc -n NAMEList deployments and services.
kubectl describe pod POD -n NAMEShow status, events and configuration.
kubectl logs POD -n NAMERead container logs.
kubectl logs POD --previous -n NAMERead logs from the previous crashed container.

Apply, scale and roll out

kubectl diff -f FILEPreview declarative changes.
kubectl apply -f FILEApply configuration.
kubectl scale deploy/APP --replicas=3 -n NAMEChange replica count.
kubectl rollout status deploy/APP -n NAMEWatch rollout progress.
kubectl rollout undo deploy/APP -n NAMERoll back to the previous revision.

Debug safely

Start with events, status and logs. Use kubectl exec only when the image contains the required tool, and prefer an approved debug-container workflow for minimal images. Avoid broad delete commands and never assume the default namespace or current context is correct.

Worked example: inspect one namespace

development-cluster, demo and web are placeholders. Substitute an authorized existing context, namespace and deployment.

kubectl config get-contexts
kubectl --context=development-cluster -n demo get deployment web
kubectl --context=development-cluster -n demo get pods -o wide
kubectl --context=development-cluster -n demo describe deployment web

Explicit context and namespace show the target on each command. Compare desired and actual readiness and look for restarts. Running does not necessarily mean ready to serve requests.

Choose the affected pod and container from the output, then inspect logs:

kubectl --context=development-cluster -n demo logs POD -c CONTAINER --tail=100
kubectl --context=development-cluster -n demo logs POD -c CONTAINER --previous --tail=100

The previous-log request can fail if no earlier container instance exists. That describes available logs, not application health. The pod debugging guide covers deeper inspection.

Preview a change and verify its outcome

Review the manifest, including its namespaces, and confirm authorization for every resource. A command-line namespace does not make an arbitrary manifest safe.

kubectl --context=development-cluster -n demo diff -f deployment.yaml
kubectl --context=development-cluster -n demo apply -f deployment.yaml
kubectl --context=development-cluster -n demo rollout status deployment/web --timeout=120s

kubectl diff returns 0 for no differences, 1 for differences and more than 1 for an error. Inspect its output instead of treating every nonzero result as a failed preview. See the diff reference.

A successful rollout is not an end-to-end test. Check readiness, routing and a representative request. Inspect history and the desired revision before rolling back. A deployment rollback does not restore database contents, all configuration or external services.

These examples were checked against documentation, not run against a cluster. Consult the kubectl reference for further context and resource commands.

Frequently asked questions

What is kubectl?

kubectl is the command-line tool that communicates with a Kubernetes cluster control plane through the Kubernetes API.

How do I check my current Kubernetes cluster?

Run kubectl config current-context, then inspect the context details before making changes.

Sources and further reading

Use these official references for version-specific command behavior and options.

Found something that needs updating? Send a correction with the page URL and a supporting source.