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
Show kubectl version
kubectl version --client

# Check the installed kubectl client version.

Show client and server version
kubectl version

# Print the kubectl client and Kubernetes API server versions.

Display cluster endpoints
kubectl cluster-info

# Show control plane and core service endpoints.

View kubeconfig
kubectl config view

# Inspect the merged kubeconfig content.

Show current context
kubectl config current-context

# Print the active kubeconfig context.

List contexts
kubectl config get-contexts

# List configured contexts and their clusters/users.

Switch context
kubectl config use-context prod-cluster

# Change the active context in kubeconfig.

Set default namespace for context
kubectl config set-context --current --namespace=payments

# Persist a default namespace for the current context.

List API resources
kubectl api-resources

# Show resource types supported by the API server.

List API versions
kubectl api-versions

# Show supported API group versions.

## Namespaces and Basic Resource Listing
List namespaces
kubectl get namespaces

# Show all namespaces.

Create namespace
kubectl create namespace staging

# Create a namespace imperatively.

Delete namespace
kubectl delete namespace staging

# Delete a namespace and the resources in it.

List common resources in a namespace
kubectl get all -n payments

# List common workload and service objects.

List pods with extra columns
kubectl get pods -o wide -n payments

# Show node, IP, and extra details.

Get one pod
kubectl get pod web-abc123 -n payments

# Inspect a single pod quickly.

Describe pod
kubectl describe pod web-abc123 -n payments

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

Explain pod schema
kubectl explain pod

# Show resource documentation from the API schema.

Explain nested schema field
kubectl explain pod.spec.containers

# Inspect nested resource fields.

## Declarative Apply, Edit, and Replace
Apply manifest file
kubectl apply -f deployment.yaml

# Create or update a resource from a file.

Apply all manifests in directory
kubectl apply -f k8s/

# Recursively apply manifests from a directory.

Apply recursively
kubectl apply -R -f manifests/

# Apply manifests recursively from nested directories.

Apply manifest from URL
kubectl apply -f https://example.com/manifest.yaml

# Apply a manifest hosted remotely.

Preview apply changes
kubectl diff -f deployment.yaml

# See server-side differences before applying.

Edit resource live
kubectl edit deployment web -n payments

# Open a live editor against the API server.

Replace resource from file
kubectl replace -f deployment.yaml

# Replace an object using the manifest definition.

Force replace resource
kubectl replace --force -f deployment.yaml

# Delete and recreate the object.

Patch resource
kubectl patch deployment web -p '{"spec":{"replicas":5}}'

# Apply a strategic/merge patch to an object.

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

# Apply an RFC 6902 JSON patch.

Add annotation
kubectl annotate deployment web owner=platform-team --overwrite

# Set or replace an annotation.

Add label
kubectl label deployment web tier=frontend --overwrite

# Set or replace a label.

## Imperative Resource Creation
Create deployment
kubectl create deployment web --image=nginx:1.27

# Create a deployment from an image.

Scale deployment
kubectl scale deployment web --replicas=5

# Change replicas on a scalable workload.

Expose deployment
kubectl expose deployment web --port=80 --target-port=8080 --type=ClusterIP

# Create a Service for a workload.

Run ad hoc pod
kubectl run toolbox --image=busybox:1.36 -it --rm -- sh

# Create a one-off interactive pod.

Create configmap from literals
kubectl create configmap app-config --from-literal=APP_ENV=prod

# Create a ConfigMap imperatively.

Create configmap from file
kubectl create configmap nginx-conf --from-file=nginx.conf

# Build a ConfigMap from file content.

Create generic secret
kubectl create secret generic app-secret --from-literal=API_KEY=supersecret

# Create an opaque Secret.

Create TLS secret
kubectl create secret tls ingress-cert --cert=tls.crt --key=tls.key

# Create a TLS Secret from cert and key files.

Create job
kubectl create job db-migrate --image=alpine:3.20 -- echo migration

# Create a one-time Job.

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

# Create a CronJob from the CLI.

## Delete, Copy, and Exec
Delete resources from file
kubectl delete -f deployment.yaml

# Delete objects defined in a manifest file.

Delete pod
kubectl delete pod web-abc123 -n payments

# Delete a pod so the controller can recreate it.

Force delete pod
kubectl delete pod stuck-pod --grace-period=0 --force

# Force-delete a stuck pod.

Open shell in pod
kubectl exec -it web-abc123 -n payments -- /bin/sh

# Start an interactive shell inside a container.

Run command in pod
kubectl exec web-abc123 -n payments -- env

# Run a non-interactive command in a container.

Copy file to pod
kubectl cp ./app.conf payments/web-abc123:/etc/app/app.conf

# Copy a local file into a pod.

Copy file from pod
kubectl cp payments/web-abc123:/var/log/app.log ./app.log

# Copy a file from a pod to the local machine.

Port-forward pod
kubectl port-forward pod/web-abc123 8080:80 -n payments

# Forward a local port to a pod port.

Port-forward service
kubectl port-forward svc/web 8080:80 -n payments

# Forward a local port to a service.

Attach to running container
kubectl attach -it pod/debugger -n payments

# Attach STDIN/STDOUT to a running container.

## Logs, Events, and Watches
Get pod logs
kubectl logs web-abc123 -n payments

# Print the current container logs.

Follow pod logs
kubectl logs -f web-abc123 -n payments

# Stream logs in real time.

Get logs for named container
kubectl logs web-abc123 -c web -n payments

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

Get previous container logs
kubectl logs web-abc123 --previous -n payments

# Read logs from the previous crashed container instance.

Get logs for label selector
kubectl logs -l app=web --tail=100 -n payments

# Aggregate logs across matching pods.

List namespace events
kubectl get events -n payments --sort-by=.lastTimestamp

# Show events sorted by timestamp.

Watch events
kubectl events -n payments --watch

# Stream Kubernetes events.

Watch pods
kubectl get pods -n payments -w

# Watch resource changes live.

Show pod resource usage
kubectl top pods -n payments

# Requires Metrics Server; shows CPU and memory usage.

Show node resource usage
kubectl top nodes

# Requires Metrics Server; shows node CPU and memory usage.