kubectl Cheat Sheet
kubectl commands and concepts for resources, contexts, logs, exec, labels, scaling, and rollouts.
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.
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
| Command or output | Use |
|---|---|
get TYPE | List resources of one type. |
get TYPE NAME | Fetch one named resource. |
-o wide | Add commonly useful columns such as node and pod IP. |
-o yaml | Print the complete API representation in YAML. |
describe TYPE NAME | Show summarized fields and related events. |
get events --sort-by=.metadata.creationTimestamp | Review 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.
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
| Pattern | Best fit |
|---|---|
apply -f FILE | Repeatable management from stored manifests. |
create -f FILE | A resource that must be new. |
delete -f FILE | Delete the objects identified by a manifest. |
delete TYPE -l SELECTOR | Delete matching resources by label. |
Standard input is useful when another command generates or transforms a manifest:
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.
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
| Flag | Scope |
|---|---|
-n NAME | Use one namespace for this command. |
--namespace=NAME | Long form of -n. |
-A, --all-namespaces | Query 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.
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
| Command | Behavior |
|---|---|
logs -f | Stream appended container logs. |
logs --previous | Read logs from the previous container instance. |
exec -it ... -- sh | Attach an interactive terminal and run sh. |
port-forward service/web 8080:80 | Forward 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.
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
| Selector | Meaning |
|---|---|
app=web | Label equals a value. |
app!=web | Label does not equal a value. |
environment in (staging,production) | Label value belongs to a set. |
debug | Label key exists. |
!debug | Label key does not exist. |
Imperative generators can provide a starting manifest without changing the cluster:
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.
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
| Command | Purpose |
|---|---|
scale | Set the desired replica count. |
rollout status | Wait for and report rollout progress. |
rollout history | List recorded revisions. |
rollout undo | Restore the previous or selected revision. |
rollout restart | Trigger 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.
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
| Command | Question answered |
|---|---|
api-resources | Which resource types and short names does this server expose? |
explain PATH | What does an API field mean according to the server schema? |
auth can-i | Would the current identity be authorized for an action? |
top | What recent CPU and memory usage does the metrics API report? |