Kubernetes Cheatsheet
Every kubectl command for managing Kubernetes clusters: from contexts to deployments, services, configs, and debugging, with syntax and real use cases.105 commands · 7 sections
Kubernetes orchestrates containerized workloads across a cluster. This cheatsheet covers the commands you run daily: cluster and context management, pods and deployments, scaling and rollouts, services and networking, ConfigMaps and Secrets, storage, and debugging workloads.
Every command shows its real syntax followed by the use case: when and why you reach for it.
Cluster Info & Context17
kubectl version --shortkubectl cluster-infokubectl config get-contextskubectl config current-contextkubectl config use-context <name>kubectl config set-context --current --namespace=devkubectl get nodeskubectl get nodes -o widekubectl describe node <name>kubectl top nodekubectl api-resourceskubectl explain deploymentkubectl get nskubectl create ns <name>kubectl delete ns <name>kubectl auth can-i create deploymentskubectl whoamiPod Management16
kubectl get podskubectl get pods -n <ns>kubectl get pods -Akubectl get pods -o widekubectl get pods -l app=webkubectl run nginx --image=nginxkubectl run test --image=busybox --rm -it -- shkubectl delete pod <name>kubectl delete pod --force --grace-period=0 <name>kubectl get pod <name> -o yamlkubectl get pod <name> -o jsonpath="{.status.podIP}"kubectl label pod <name> env=prodkubectl annotate pod <name> description="main web pod"kubectl get pods --sort-by=.metadata.creationTimestampkubectl get pod --watchkubectl get pods --field-selector=status.phase=RunningDeployments & Scaling22
kubectl create deployment web --image=nginxkubectl get deploymentskubectl scale deployment web --replicas=5kubectl scale deployment web --replicas=0kubectl set image deployment/web nginx=nginx:1.27kubectl edit deployment webkubectl rollout status deployment/webkubectl rollout history deployment/webkubectl rollout undo deployment/webkubectl rollout undo deployment/web --to-revision=2kubectl rollout restart deployment/webkubectl rollout pause deployment/webkubectl rollout resume deployment/webkubectl autoscale deployment web --min=2 --max=10 --cpu-percent=70kubectl get hpakubectl get rskubectl get statefulsetskubectl get daemonsets -Akubectl get jobskubectl create job backup --image=myapp --from=cronjob/daily-backupkubectl get cronjobskubectl get deployment web -o yaml > deployment.yamlServices & Networking12
kubectl get serviceskubectl get svc -o widekubectl expose deployment web --port=80 --target-port=8080 --type=ClusterIPkubectl expose deployment web --type=LoadBalancer --port=80kubectl expose deployment web --type=NodePort --port=80kubectl get endpointskubectl port-forward svc/web 8080:80kubectl port-forward pod/web-7d8f9 3000:3000kubectl get ingresskubectl get networkpolicykubectl get endpointslices -l kubernetes.io/service-name=webkubectl get svc --field-selector type=LoadBalancerConfigMaps & Secrets13
kubectl create configmap app-config --from-literal=DEBUG=truekubectl create configmap app-config --from-file=config.jsonkubectl create configmap app-config --from-env-file=.envkubectl get configmapskubectl get cm app-config -o yamlkubectl create secret generic db-secret --from-literal=password=supersecretkubectl create secret generic db-secret --from-file=./credentials.jsonkubectl create secret tls tls-secret --cert=cert.pem --key=key.pemkubectl create secret docker-registry regcred --docker-username=user --docker-password=passkubectl get secretskubectl get secret db-secret -o jsonpath="{.data.password}" | base64 -dkubectl delete secret db-secretkubectl create secret generic sops-age --from-file=age.agekey -n flux-systemStorage & Volumes7
kubectl get pvkubectl get pvckubectl get storageclasskubectl describe pvc data-pvckubectl delete pvc data-pvckubectl patch pvc data-pvc -p '{"spec":{"resources":{"requests":{"storage":"20Gi"}}}}'kubectl get pvc -o jsonpath="{.items[*].status.capacity.storage}"Debugging & Logs18
kubectl logs <pod>kubectl logs -f <pod>kubectl logs --tail=100 <pod>kubectl logs deployment/webkubectl logs <pod> -c <container>kubectl logs -l app=web --tail=50kubectl logs --since=10m <pod>kubectl logs -p <pod>kubectl exec -it <pod> -- /bin/shkubectl exec <pod> -- <cmd>kubectl describe pod <name>kubectl get events --sort-by=.lastTimestampkubectl get events --field-selector involvedObject.name=web-7d8f9kubectl cp <pod>:/app/report.log ./report.logkubectl top podkubectl debug node/node-1 -it --image=ubuntukubectl debug pod/web-7d8f9 -it --image=nicolaka/netshootkubectl get pods -o custom-columns=NAME:.metadata.name,STATUS:.status.phase,RESTARTS:.status.containerStatuses[0].restartCountKubernetes Cheatsheet
Every kubectl command for managing Kubernetes clusters: from contexts to deployments, services, configs, and debugging, with syntax and real use cases.
Kubernetes orchestrates containerized workloads across a cluster. This cheatsheet covers the commands you run daily: cluster and context management, pods and deployments, scaling and rollouts, services and networking, ConfigMaps and Secrets, storage, and debugging workloads.
Every command shows its real syntax followed by the use case: when and why you reach for it.
Cluster Info & Context
kubectl version --short: Show client and server versions: verify kubectl matches the cluster.kubectl cluster-info: Show cluster endpoints and services: is my cluster reachable?kubectl config get-contexts: List all contexts: every cluster you have configured.kubectl config current-context: Print the active context: which cluster am I about to touch?kubectl config use-context <name>: Switch to another cluster context.kubectl config set-context --current --namespace=dev: Set the default namespace for the current context: stop typing -n everywhere.kubectl get nodes: List cluster nodes and their status.kubectl get nodes -o wide: Show node IPs, roles, and Kubernetes version: for networking and upgrades.kubectl describe node <name>: Show node details: resources, taints, conditions, running pods.kubectl top node: Show live CPU/memory usage per node: spot the overloaded one.kubectl api-resources: List all resource types the cluster supports: discover CRDs.kubectl explain deployment: Show the schema of a resource type: self-documenting kubectl.kubectl get ns: List all namespaces.kubectl create ns <name>: Create a namespace: isolate environments or teams.kubectl delete ns <name>: Delete a namespace and EVERYTHING in it: containers, services, configs.kubectl auth can-i create deployments: Check whether your current user can perform an action: RBAC debugging.kubectl whoami: Print the current user identity (plugin): know who you are in the cluster.Pod Management
kubectl get pods: List pods in the current namespace.kubectl get pods -n <ns>: List pods in a specific namespace.kubectl get pods -A: List pods in ALL namespaces: find where things run.kubectl get pods -o wide: Show pod node, IP, and ready state: map pods to nodes.kubectl get pods -l app=web: Filter pods by label selector.kubectl run nginx --image=nginx: Create a standalone pod from an image: quick smoke tests.kubectl run test --image=busybox --rm -it -- sh: Run an interactive ephemeral pod and delete it on exit: perfect for probing.kubectl delete pod <name>: Delete a pod: the Deployment will recreate it.kubectl delete pod --force --grace-period=0 <name>: Force-delete a stuck pod stuck in Terminating.kubectl get pod <name> -o yaml: Dump a pod's full YAML: inspect labels, tolerations, and status.kubectl get pod <name> -o jsonpath="{.status.podIP}": Extract a single field: script-friendly queries.kubectl label pod <name> env=prod: Add or update a label: attach metadata without recreating.kubectl annotate pod <name> description="main web pod": Add a non-selectable annotation: human notes and tooling metadata.kubectl get pods --sort-by=.metadata.creationTimestamp: Sort pods by creation time: newest first for review.kubectl get pod --watch: Watch pod changes live: see restart loops in real time.kubectl get pods --field-selector=status.phase=Running: Filter pods by field: e.g. only running ones.Deployments & Scaling
kubectl create deployment web --image=nginx: Create a Deployment with one replica: the standard way to run stateless apps.kubectl get deployments: List deployments and their replica status.kubectl scale deployment web --replicas=5: Scale a deployment up or down.kubectl scale deployment web --replicas=0: Scale to zero: stop a service without deleting its definition.kubectl set image deployment/web nginx=nginx:1.27: Update a container image: triggers a rolling update.kubectl edit deployment web: Open the deployment spec in your editor and apply changes live.kubectl rollout status deployment/web: Wait until a rollout completes: script it in CI.kubectl rollout history deployment/web: Show rollout revisions: see every change you made.kubectl rollout undo deployment/web: Roll back to the previous revision: instant recovery.kubectl rollout undo deployment/web --to-revision=2: Roll back to a specific revision.kubectl rollout restart deployment/web: Restart all pods without changing the image: pick up new ConfigMaps/Secrets.kubectl rollout pause deployment/web: Pause a rollout: freeze replicas during multi-step changes.kubectl rollout resume deployment/web: Resume a paused rollout.kubectl autoscale deployment web --min=2 --max=10 --cpu-percent=70: Enable HPA: autoscale by CPU utilization.kubectl get hpa: List horizontal pod autoscalers and their targets.kubectl get rs: List ReplicaSets: each rollout creates one, useful for rollback history.kubectl get statefulsets: List StatefulSets: for databases and stateful workloads with stable identities.kubectl get daemonsets -A: List DaemonSets: pods that must run on every node (log agents, metrics).kubectl get jobs: List Jobs: one-off batch workloads.kubectl create job backup --image=myapp --from=cronjob/daily-backup: Manually trigger a CronJob now: run it on demand.kubectl get cronjobs: List scheduled CronJobs.kubectl get deployment web -o yaml > deployment.yaml: Export a live deployment's spec to a file: backup or migrate it.Services & Networking
kubectl get services: List services and their cluster IPs.kubectl get svc -o wide: Show service type, cluster IP, and selector: verify routing.kubectl expose deployment web --port=80 --target-port=8080 --type=ClusterIP: Create a Service from a deployment: internal load balancing.kubectl expose deployment web --type=LoadBalancer --port=80: Expose a deployment externally: cloud LB provisions a public IP.kubectl expose deployment web --type=NodePort --port=80: Expose on a node port: for clusters without a load balancer.kubectl get endpoints: Show service endpoints: which pod IPs are currently behind each service.kubectl port-forward svc/web 8080:80: Forward a local port to a service: access internal services from your laptop.kubectl port-forward pod/web-7d8f9 3000:3000: Forward to a specific pod: debug a single instance.kubectl get ingress: List ingress rules: external HTTP routing.kubectl get networkpolicy: List network policies: which pods can talk to which.kubectl get endpointslices -l kubernetes.io/service-name=web: Inspect endpoint slices for a service: newer API for endpoint discovery.kubectl get svc --field-selector type=LoadBalancer: Find all load-balanced services: audit public exposure.ConfigMaps & Secrets
kubectl create configmap app-config --from-literal=DEBUG=true: Create a ConfigMap from a literal key-value.kubectl create configmap app-config --from-file=config.json: Create a ConfigMap from a file's contents.kubectl create configmap app-config --from-env-file=.env: Import a whole .env file as a ConfigMap.kubectl get configmaps: List ConfigMaps.kubectl get cm app-config -o yaml: View a ConfigMap's data.kubectl create secret generic db-secret --from-literal=password=supersecret: Create a Secret: base64-encoded by default.kubectl create secret generic db-secret --from-file=./credentials.json: Create a Secret from a file: better than pasting literals in shell history.kubectl create secret tls tls-secret --cert=cert.pem --key=key.pem: Create a TLS Secret for HTTPS ingresses.kubectl create secret docker-registry regcred --docker-username=user --docker-password=pass: Create an image pull secret for private registries.kubectl get secrets: List secrets.kubectl get secret db-secret -o jsonpath="{.data.password}" | base64 -d: Decode a secret value: view what is actually stored.kubectl delete secret db-secret: Delete a secret.kubectl create secret generic sops-age --from-file=age.agekey -n flux-system: Store a tool-specific secret (SOPS key) in a namespace: GitOps workflows.Storage & Volumes
kubectl get pv: List persistent volumes (cluster-level storage).kubectl get pvc: List persistent volume claims (namespace-level requests).kubectl get storageclass: List storage classes: the dynamic provisioning options available.kubectl describe pvc data-pvc: Inspect a PVC: status, capacity, and the PV it bound to.kubectl delete pvc data-pvc: Delete a PVC: depending on reclaim policy, this may destroy the data.kubectl patch pvc data-pvc -p '{"spec":{"resources":{"requests":{"storage":"20Gi"}}}}': Expand a PVC's size (if the storage class allows volume expansion).kubectl get pvc -o jsonpath="{.items[*].status.capacity.storage}": List all PVC capacities: audit your storage usage.Debugging & Logs
kubectl logs <pod>: Show a pod's logs.kubectl logs -f <pod>: Follow a pod's logs live.kubectl logs --tail=100 <pod>: Show the last 100 log lines.kubectl logs deployment/web: Stream logs from all pods of a deployment.kubectl logs <pod> -c <container>: Logs of a specific container in a multi-container pod.kubectl logs -l app=web --tail=50: Logs from all pods matching a label: follow a service's logs.kubectl logs --since=10m <pod>: Logs from the last 10 minutes: post-incident review.kubectl logs -p <pod>: Show logs of the previous (crashed) container instance: why did it die?kubectl exec -it <pod> -- /bin/sh: Open a shell inside a running container: the classic debugging move.kubectl exec <pod> -- <cmd>: Run a single command in a container.kubectl describe pod <name>: Show pod events, conditions, and container status: the first place to look at failures.kubectl get events --sort-by=.lastTimestamp: Show cluster events sorted by time: what happened recently.kubectl get events --field-selector involvedObject.name=web-7d8f9: Events for one object: trace a pod's lifecycle.kubectl cp <pod>:/app/report.log ./report.log: Copy a file out of a container: grab logs or artifacts.kubectl top pod: Show CPU/memory usage per pod: find the memory hog.kubectl debug node/node-1 -it --image=ubuntu: Run a debug container on a node: inspect node-level issues.kubectl debug pod/web-7d8f9 -it --image=nicolaka/netshoot: Run an ephemeral debug container with networking tools next to a pod.kubectl get pods -o custom-columns=NAME:.metadata.name,STATUS:.status.phase,RESTARTS:.status.containerStatuses[0].restartCount: Custom table output: restart counts in one glance.Frequently asked questions
How do I view and switch Kubernetes contexts?
Run kubectl config get-contexts to list clusters, kubectl config use-context <name> to switch, and kubectl config current-context to see the active one. Contexts bundle cluster, user, and namespace.
How do I roll back a deployment?
Run kubectl rollout undo deployment/<name> to revert to the previous revision, or kubectl rollout undo deployment/<name> --to-revision=<n> for a specific one. Check history with kubectl rollout history deployment/<name>.
What is the difference between a pod and a deployment?
A pod is the smallest deployable unit: one or more containers sharing networking and storage. A deployment manages a set of replica pods, providing rolling updates, scaling, and self-healing.
How do I get logs from all pods of a deployment?
Use kubectl logs deployment/<name> to stream logs from all pods. Add -f to follow, --tail=50 to limit lines, or kubectl logs -l app=<label> to select pods by label.