kubectl Cheat Sheet

kubectl commands and concepts for resources, contexts, logs, exec, labels, scaling, and rollouts.

Containers
kubectl
kubernetes
k8s

kubectl sends requests to the Kubernetes API selected by the current kubeconfig context. Always confirm the active cluster and namespace before a mutating command, especially when identical resource names exist across environments.

Inspect resources and output

Resource names can be singular, plural, or abbreviated. A namespace flag narrows namespaced resources, while cluster-scoped resources ignore it.

bash
kubectl get pods
kubectl get pods -o wide
kubectl get deployment web -o yaml
kubectl get pods -A
kubectl describe deployment web
kubectl describe pod POD_NAME
Table
Command or outputUse
get TYPEList resources of one type.
get TYPE NAMEFetch one named resource.
-o wideAdd commonly useful columns such as node and pod IP.
-o yamlPrint the complete API representation in YAML.
describe TYPE NAMEShow summarized fields and related events.
get events --sort-by=.metadata.creationTimestampReview events in chronological order.

kubectl get all covers a conventional subset of workload resources, not every resource type in the namespace. Use kubectl api-resources when the full discovery list matters.

Create, apply, and delete

create -f submits a new object and normally fails if it already exists. apply -f reconciles a declarative manifest, creating a missing object or updating an existing one.

bash
kubectl apply -f deployment.yaml
kubectl apply -f ./manifests/
kubectl create -f one-time-job.yaml
kubectl diff -f deployment.yaml
kubectl delete -f deployment.yaml
kubectl delete pods -l app=web
Table
PatternBest fit
apply -f FILERepeatable management from stored manifests.
create -f FILEA resource that must be new.
delete -f FILEDelete the objects identified by a manifest.
delete TYPE -l SELECTORDelete matching resources by label.

Standard input is useful when another command generates or transforms a manifest:

bash
kubectl create deployment web --image=nginx:stable \
  --dry-run=client -o yaml | kubectl apply -f -

--dry-run=client -o yaml renders locally without creating the object; apply -f - reads that YAML from standard input.

Contexts and namespaces

A context combines a cluster, user, and optional default namespace. use-context changes the persistent current context in the selected kubeconfig.

bash
kubectl config current-context
kubectl config get-contexts
kubectl config use-context staging
kubectl config set-context --current --namespace=team-a
kubectl get pods -n team-b
kubectl get pods --all-namespaces
Table
FlagScope
-n NAMEUse one namespace for this command.
--namespace=NAMELong form of -n.
-A, --all-namespacesQuery all namespaces where supported.

Before changing a production object, run kubectl config current-context and include -n explicitly when the namespace is operationally important.

Logs, exec, and port forwarding

Logs are container-specific. A pod with multiple containers needs -c; --previous reads the terminated instance of a restarted container when its prior log is still available.

bash
kubectl logs POD_NAME
kubectl logs -f POD_NAME -c CONTAINER_NAME
kubectl logs --previous POD_NAME -c CONTAINER_NAME
kubectl exec -it POD_NAME -- sh
kubectl exec -it POD_NAME -c CONTAINER_NAME -- sh
kubectl port-forward service/web 8080:80
Table
CommandBehavior
logs -fStream appended container logs.
logs --previousRead logs from the previous container instance.
exec -it ... -- shAttach an interactive terminal and run sh.
port-forward service/web 8080:80Forward local port 8080 to port 80 selected through the Service.

The -- separator ends kubectl option parsing; everything after it is the command and arguments executed in the container.

Labels, selectors, and generated manifests

Labels are indexed metadata used by Services, controllers, and kubectl selectors. Changing a label can alter which controller or Service selects an object, so inspect selectors first.

bash
kubectl get pods --show-labels
kubectl get pods -l app=web
kubectl get pods -l 'environment in (staging,production)'
kubectl label deployment web environment=staging
kubectl label deployment web environment=production --overwrite
Table
SelectorMeaning
app=webLabel equals a value.
app!=webLabel does not equal a value.
environment in (staging,production)Label value belongs to a set.
debugLabel key exists.
!debugLabel key does not exist.

Imperative generators can provide a starting manifest without changing the cluster:

bash
kubectl create service clusterip web --tcp=80:8080 --dry-run=client -o yaml
kubectl create configmap app-config --from-literal=mode=production --dry-run=client -o yaml

Review generated YAML before storing or applying it, because client version and command defaults influence the output.

Scaling and rollouts

Deployment rollout commands observe or revise the controller's revision history. A manual replica count can later be changed by a HorizontalPodAutoscaler if one manages the same workload.

bash
kubectl scale deployment/web --replicas=5
kubectl set image deployment/web app=example/web:2.0
kubectl rollout status deployment/web --timeout=2m
kubectl rollout history deployment/web
kubectl rollout undo deployment/web
kubectl rollout undo deployment/web --to-revision=2
Table
CommandPurpose
scaleSet the desired replica count.
rollout statusWait for and report rollout progress.
rollout historyList recorded revisions.
rollout undoRestore the previous or selected revision.
rollout restartTrigger a fresh rollout without changing the pod template's application fields.

Metrics and API help

top reads the resource metrics API, which is commonly supplied by Metrics Server. An unavailable metrics API makes top fail even when workloads are otherwise healthy.

bash
kubectl top nodes
kubectl top pods
kubectl top pods -A --containers
kubectl api-resources
kubectl api-versions
kubectl explain deployment
kubectl explain deployment.spec.template.spec.containers
kubectl auth can-i create deployments -n team-a
Table
CommandQuestion answered
api-resourcesWhich resource types and short names does this server expose?
explain PATHWhat does an API field mean according to the server schema?
auth can-iWould the current identity be authorized for an action?
topWhat recent CPU and memory usage does the metrics API report?

References