Kubernetes & kubectl Cheat Sheet

Comprehensive kubectl commands for contexts, resources, apply flows, creation, deletion, logs, and cluster introspection.

View
StandardDetailedCompact
Export
Copy the compact sheet, download it, or print it.
Download
`D` dense toggle · `C` copy all

Setup, Contexts, and Cluster Info

Install validation, context switching, and cluster metadata.

Show kubectl version

Check the installed kubectl client version.

bashANYsetupversion
bash
kubectl version --client
Notes

Check the installed kubectl client version.

Show client and server version

Print the kubectl client and Kubernetes API server versions.

bashANYsetupversioncluster
bash
kubectl version
Notes

Print the kubectl client and Kubernetes API server versions.

Display cluster endpoints

Show control plane and core service endpoints.

bashANYclusterinfo
bash
kubectl cluster-info
Notes

Show control plane and core service endpoints.

View kubeconfig

Inspect the merged kubeconfig content.

bashANYconfigcontext
bash
kubectl config view
Notes

Inspect the merged kubeconfig content.

Show current context

Print the active kubeconfig context.

bashANYconfigcontext
bash
kubectl config current-context
Notes

Print the active kubeconfig context.

List contexts

List configured contexts and their clusters/users.

bashANYconfigcontext
bash
kubectl config get-contexts
Notes

List configured contexts and their clusters/users.

Switch context

Change the active context in kubeconfig.

bashANYconfigcontextswitch
bash
kubectl config use-context prod-cluster
Notes

Change the active context in kubeconfig.

Set default namespace for context

Persist a default namespace for the current context.

bashANYconfignamespacecontext
bash
kubectl config set-context --current --namespace=payments
Notes

Persist a default namespace for the current context.

List API resources

Show resource types supported by the API server.

bashANYapiresources
bash
kubectl api-resources
Notes

Show resource types supported by the API server.

List API versions

Show supported API group versions.

bashANYapiversions
bash
kubectl api-versions
Notes

Show supported API group versions.

Namespaces and Basic Resource Listing

Get comfortable listing and scoping objects.

List namespaces

Show all namespaces.

bashANYnamespaceget
bash
kubectl get namespaces
Notes

Show all namespaces.

Create namespace

Create a namespace imperatively.

bashANYnamespacecreate
bash
kubectl create namespace staging
Notes

Create a namespace imperatively.

Delete namespace

Delete a namespace and the resources in it.

bashANYnamespacedelete
bash
kubectl delete namespace staging
Notes

Delete a namespace and the resources in it.

List common resources in a namespace

List common workload and service objects.

bashANYgetnamespace
bash
kubectl get all -n payments
Notes

List common workload and service objects.

List pods with extra columns

Show node, IP, and extra details.

bashANYpodget
bash
kubectl get pods -o wide -n payments
Notes

Show node, IP, and extra details.

Get one pod

Inspect a single pod quickly.

bashANYpodget
bash
kubectl get pod web-abc123 -n payments
Notes

Inspect a single pod quickly.

Describe pod

View events, status, probes, mounts, and conditions.

bashANYpoddescribe
bash
kubectl describe pod web-abc123 -n payments
Notes

View events, status, probes, mounts, and conditions.

Explain pod schema

Show resource documentation from the API schema.

bashANYapiexplain
bash
kubectl explain pod
Notes

Show resource documentation from the API schema.

Explain nested schema field

Inspect nested resource fields.

bashANYapiexplainschema
bash
kubectl explain pod.spec.containers
Notes

Inspect nested resource fields.

Declarative Apply, Edit, and Replace

Core workflows for changing objects from YAML.

Apply manifest file

Create or update a resource from a file.

bashANYapplyyaml
bash
kubectl apply -f deployment.yaml
Notes

Create or update a resource from a file.

Apply all manifests in directory

Recursively apply manifests from a directory.

bashANYapplyyaml
bash
kubectl apply -f k8s/
Notes

Recursively apply manifests from a directory.

Apply recursively

Apply manifests recursively from nested directories.

bashANYapplyyaml
bash
kubectl apply -R -f manifests/
Notes

Apply manifests recursively from nested directories.

Apply manifest from URL

Apply a manifest hosted remotely.

bashANYapplyremote
bash
kubectl apply -f https://example.com/manifest.yaml
Notes

Apply a manifest hosted remotely.

Preview apply changes

See server-side differences before applying.

bashANYapplydiff
bash
kubectl diff -f deployment.yaml
Notes

See server-side differences before applying.

Edit resource live

Open a live editor against the API server.

bashANYeditdeployment
bash
kubectl edit deployment web -n payments
Notes

Open a live editor against the API server.

Replace resource from file

Replace an object using the manifest definition.

bashANYreplaceyaml
bash
kubectl replace -f deployment.yaml
Notes

Replace an object using the manifest definition.

Force replace resource

Delete and recreate the object.

bashANYreplaceforce
bash
kubectl replace --force -f deployment.yaml
Notes

Delete and recreate the object.

Patch resource

Apply a strategic/merge patch to an object.

bashANYpatchdeployment
bash
kubectl patch deployment web -p '{"spec":{"replicas":5}}'
Notes

Apply a strategic/merge patch to an object.

JSON patch resource

Apply an RFC 6902 JSON patch.

bashANYpatchjson
bash
kubectl patch deployment web --type='json' -p='[{"op":"replace","path":"/spec/replicas","value":5}]'
Notes

Apply an RFC 6902 JSON patch.

Add annotation

Set or replace an annotation.

bashANYannotatemetadata
bash
kubectl annotate deployment web owner=platform-team --overwrite
Notes

Set or replace an annotation.

Add label

Set or replace a label.

bashANYlabelmetadata
bash
kubectl label deployment web tier=frontend --overwrite
Notes

Set or replace a label.

Imperative Resource Creation

Fast object creation for experiments and demos.

Create deployment

Create a deployment from an image.

bashANYcreatedeployment
bash
kubectl create deployment web --image=nginx:1.27
Notes

Create a deployment from an image.

Scale deployment

Change replicas on a scalable workload.

bashANYscaledeployment
bash
kubectl scale deployment web --replicas=5
Notes

Change replicas on a scalable workload.

Expose deployment

Create a Service for a workload.

bashANYserviceexpose
bash
kubectl expose deployment web --port=80 --target-port=8080 --type=ClusterIP
Notes

Create a Service for a workload.

Run ad hoc pod

Create a one-off interactive pod.

bashANYrunpod
bash
kubectl run toolbox --image=busybox:1.36 -it --rm -- sh
Notes

Create a one-off interactive pod.

Create configmap from literals

Create a ConfigMap imperatively.

bashANYconfigmapcreate
bash
kubectl create configmap app-config --from-literal=APP_ENV=prod
Notes

Create a ConfigMap imperatively.

Create configmap from file

Build a ConfigMap from file content.

bashANYconfigmapcreate
bash
kubectl create configmap nginx-conf --from-file=nginx.conf
Notes

Build a ConfigMap from file content.

Create generic secret

Create an opaque Secret.

bashANYsecretcreate
bash
kubectl create secret generic app-secret --from-literal=API_KEY=supersecret
Notes

Create an opaque Secret.

Create TLS secret

Create a TLS Secret from cert and key files.

bashANYsecrettls
bash
kubectl create secret tls ingress-cert --cert=tls.crt --key=tls.key
Notes

Create a TLS Secret from cert and key files.

Create job

Create a one-time Job.

bashANYjobcreate
bash
kubectl create job db-migrate --image=alpine:3.20 -- echo migration
Notes

Create a one-time Job.

Create cronjob

Create a CronJob from the CLI.

bashANYcronjobcreate
bash
kubectl create cronjob db-backup --image=alpine:3.20 --schedule='0 2 * * *' -- sh -c 'echo backup'
Notes

Create a CronJob from the CLI.

Delete, Copy, and Exec

Safe cleanup and shell access workflows.

Delete resources from file

Delete objects defined in a manifest file.

bashANYdeleteyaml
bash
kubectl delete -f deployment.yaml
Notes

Delete objects defined in a manifest file.

Delete pod

Delete a pod so the controller can recreate it.

bashANYdeletepod
bash
kubectl delete pod web-abc123 -n payments
Notes

Delete a pod so the controller can recreate it.

Force delete pod

Force-delete a stuck pod.

bashANYdeletepodforce
bash
kubectl delete pod stuck-pod --grace-period=0 --force
Notes

Force-delete a stuck pod.

Open shell in pod

Start an interactive shell inside a container.

bashANYexecshell
bash
kubectl exec -it web-abc123 -n payments -- /bin/sh
Notes

Start an interactive shell inside a container.

Run command in pod

Run a non-interactive command in a container.

bashANYexec
bash
kubectl exec web-abc123 -n payments -- env
Notes

Run a non-interactive command in a container.

Copy file to pod

Copy a local file into a pod.

bashANYcpfiles
bash
kubectl cp ./app.conf payments/web-abc123:/etc/app/app.conf
Notes

Copy a local file into a pod.

Copy file from pod

Copy a file from a pod to the local machine.

bashANYcpfiles
bash
kubectl cp payments/web-abc123:/var/log/app.log ./app.log
Notes

Copy a file from a pod to the local machine.

Port-forward pod

Forward a local port to a pod port.

bashANYport-forwardpod
bash
kubectl port-forward pod/web-abc123 8080:80 -n payments
Notes

Forward a local port to a pod port.

Port-forward service

Forward a local port to a service.

bashANYport-forwardservice
bash
kubectl port-forward svc/web 8080:80 -n payments
Notes

Forward a local port to a service.

Attach to running container

Attach STDIN/STDOUT to a running container.

bashANYattachdebug
bash
kubectl attach -it pod/debugger -n payments
Notes

Attach STDIN/STDOUT to a running container.

Logs, Events, and Watches

Observe runtime behavior quickly.

Get pod logs

Print the current container logs.

bashANYlogs
bash
kubectl logs web-abc123 -n payments
Notes

Print the current container logs.

Follow pod logs

Stream logs in real time.

bashANYlogsfollow
bash
kubectl logs -f web-abc123 -n payments
Notes

Stream logs in real time.

Get logs for named container

Read logs from a specific container in a multi-container pod.

bashANYlogscontainer
bash
kubectl logs web-abc123 -c web -n payments
Notes

Read logs from a specific container in a multi-container pod.

Get previous container logs

Read logs from the previous crashed container instance.

bashANYlogscrashloop
bash
kubectl logs web-abc123 --previous -n payments
Notes

Read logs from the previous crashed container instance.

Get logs for label selector

Aggregate logs across matching pods.

bashANYlogsselector
bash
kubectl logs -l app=web --tail=100 -n payments
Notes

Aggregate logs across matching pods.

List namespace events

Show events sorted by timestamp.

bashANYevents
bash
kubectl get events -n payments --sort-by=.lastTimestamp
Notes

Show events sorted by timestamp.

Watch events

Stream Kubernetes events.

bashANYeventswatch
bash
kubectl events -n payments --watch
Notes

Stream Kubernetes events.

Watch pods

Watch resource changes live.

bashANYwatchget
bash
kubectl get pods -n payments -w
Notes

Watch resource changes live.

Show pod resource usage

Requires Metrics Server; shows CPU and memory usage.

bashANYmetricstoppods
bash
kubectl top pods -n payments
Notes

Requires Metrics Server; shows CPU and memory usage.

Show node resource usage

Requires Metrics Server; shows node CPU and memory usage.

bashANYmetricstopnodes
bash
kubectl top nodes
Notes

Requires Metrics Server; shows node CPU and memory usage.