kubectl Cheat Sheet — Essential Commands for Kubernetes 2026

Sanjeev SharmaSanjeev Sharma
6 min read

Advertisement

Introduction

Why This Matters

kubectl is the primary CLI for interacting with any Kubernetes cluster — whether it's EKS, GKE, AKS, or a local kind cluster. Mastering kubectl commands dramatically speeds up debugging, deployment, and cluster administration. This cheat sheet covers the commands you'll use every day in 2026, organized by task so you can find exactly what you need in seconds.

Cluster and Context Management

# View current context
kubectl config current-context
 
# List all contexts
kubectl config get-contexts
 
# Switch context
kubectl config use-context my-cluster
 
# View cluster info
kubectl cluster-info
 
# Check cluster version
kubectl version --short
 
# Set default namespace for current context
kubectl config set-context --current --namespace=production

Working With Namespaces

# List all namespaces
kubectl get namespaces
 
# Create a namespace
kubectl create namespace staging
 
# Delete a namespace
kubectl delete namespace staging
 
# Get all resources in a namespace
kubectl get all -n staging
 
# Get resources across ALL namespaces
kubectl get pods -A

Pod Commands

# List pods in current namespace
kubectl get pods
 
# List pods with node and IP info
kubectl get pods -o wide
 
# Describe a pod (events, resource limits, image)
kubectl describe pod my-pod
 
# Get pod logs
kubectl logs my-pod
 
# Follow logs in real time
kubectl logs -f my-pod
 
# Logs from a specific container in a multi-container pod
kubectl logs my-pod -c my-container
 
# Previous container logs (after crash)
kubectl logs my-pod --previous
 
# Execute a command inside a pod
kubectl exec -it my-pod -- /bin/bash
 
# Execute in a specific container
kubectl exec -it my-pod -c my-container -- /bin/sh
 
# Copy files to/from a pod
kubectl cp my-pod:/app/logs/app.log ./app.log
kubectl cp ./config.yaml my-pod:/app/config.yaml
 
# Delete a pod (it will be recreated by its controller)
kubectl delete pod my-pod
 
# Force delete a stuck pod
kubectl delete pod my-pod --grace-period=0 --force

Deployments and ReplicaSets

# List deployments
kubectl get deployments
 
# Create a deployment
kubectl create deployment nginx --image=nginx:1.25
 
# Scale a deployment
kubectl scale deployment nginx --replicas=5
 
# Update a deployment image
kubectl set image deployment/nginx nginx=nginx:1.26
 
# View rollout status
kubectl rollout status deployment/nginx
 
# View rollout history
kubectl rollout history deployment/nginx
 
# Undo the last rollout
kubectl rollout undo deployment/nginx
 
# Undo to a specific revision
kubectl rollout undo deployment/nginx --to-revision=2
 
# Pause a rollout
kubectl rollout pause deployment/nginx
 
# Resume a paused rollout
kubectl rollout resume deployment/nginx
 
# Delete a deployment
kubectl delete deployment nginx

Services and Networking

# List services
kubectl get services
 
# Expose a deployment as a ClusterIP service
kubectl expose deployment nginx --port=80 --target-port=80
 
# Expose as a NodePort service
kubectl expose deployment nginx --type=NodePort --port=80
 
# Port-forward to access a pod locally
kubectl port-forward pod/my-pod 8080:80
 
# Port-forward a service
kubectl port-forward svc/my-service 8080:80
 
# Get endpoints for a service
kubectl get endpoints my-service
 
# View DNS name for a service
kubectl run -it --rm debug --image=busybox --restart=Never -- nslookup my-service

ConfigMaps and Secrets

# Create a ConfigMap from literal values
kubectl create configmap app-config \
  --from-literal=APP_ENV=production \
  --from-literal=LOG_LEVEL=info
 
# Create ConfigMap from a file
kubectl create configmap app-config --from-file=config.yaml
 
# Create a Secret from literals
kubectl create secret generic db-credentials \
  --from-literal=username=admin \
  --from-literal=password=s3cr3t
 
# Create a TLS secret
kubectl create secret tls my-tls \
  --cert=tls.crt --key=tls.key
 
# Decode a secret value
kubectl get secret db-credentials -o jsonpath='{.data.password}' | base64 --decode

Resource Inspection and Output Formats

# Get resource as YAML
kubectl get deployment nginx -o yaml
 
# Get resource as JSON
kubectl get pod my-pod -o json
 
# Use jsonpath to extract a field
kubectl get pod my-pod -o jsonpath='{.status.podIP}'
 
# List pod names only
kubectl get pods -o jsonpath='{.items[*].metadata.name}'
 
# Custom columns output
kubectl get pods -o custom-columns='NAME:.metadata.name,STATUS:.status.phase,NODE:.spec.nodeName'
 
# Watch resources for changes
kubectl get pods -w
 
# Sort by field
kubectl get pods --sort-by='.metadata.creationTimestamp'

Debugging and Troubleshooting

# Get events in a namespace (sorted by time)
kubectl get events --sort-by='.lastTimestamp'
 
# Describe a node
kubectl describe node my-node
 
# Check resource usage (requires metrics-server)
kubectl top pods
kubectl top nodes
 
# Run a temporary debug pod
kubectl run -it --rm debug --image=busybox --restart=Never -- sh
 
# Run a debug pod with the same namespace as target
kubectl debug -it my-pod --image=busybox --target=my-container
 
# Check pod resource requests vs limits
kubectl describe pod my-pod | grep -A 5 "Limits\|Requests"

Applying and Managing Manifests

# Apply a manifest file
kubectl apply -f deployment.yaml
 
# Apply all manifests in a directory
kubectl apply -f ./k8s/
 
# Apply from a URL
kubectl apply -f https://raw.githubusercontent.com/org/repo/main/deploy.yaml
 
# Diff current vs desired state
kubectl diff -f deployment.yaml
 
# Delete resources defined in a file
kubectl delete -f deployment.yaml
 
# Dry run (server-side)
kubectl apply -f deployment.yaml --dry-run=server
 
# Label a resource
kubectl label pod my-pod env=production
 
# Annotate a resource
kubectl annotate pod my-pod description="main web server"

Common Mistakes

  • Not specifying namespaces — resources in other namespaces are invisible without -n namespace or -A; set a default context namespace to avoid confusion.
  • Using kubectl delete pod to "redeploy" — deleting a pod just recreates it with the same image; update the Deployment to roll out new images.
  • Forgetting --previous on logs — after a CrashLoopBackOff, logs show the current (empty) container; add --previous to see the crashed container's output.
  • Port-forwarding to a pod instead of a service — if the pod restarts, the tunnel breaks; forward to the Service for stability.
  • Skipping kubectl diff — always diff before applying changes in production to avoid accidental deletions.

Best Practices

  • Use aliases — add alias k=kubectl and alias kns='kubectl config set-context --current --namespace' to your shell profile.
  • Install kubectl pluginskrew is the plugin manager; popular plugins include kubectl neat, kubectl tree, and kubectl ctx.
  • Use --dry-run=server — server-side dry run catches admission webhook rejections that client-side dry run misses.
  • Pipe to grep and jq — combine -o json | jq for complex field extraction instead of complex jsonpath expressions.
  • Use kubectl explainkubectl explain deployment.spec.strategy gives inline API documentation without leaving the terminal.

Key Takeaways

  • kubectl config use-context switches between clusters; kubectl config set-context --current --namespace sets the default namespace.
  • kubectl logs --previous retrieves the logs from a crashed container — essential for debugging CrashLoopBackOff errors.
  • kubectl rollout undo deployment/name reverts to the previous ReplicaSet, enabling instant rollback without redeploying.
  • kubectl port-forward svc/name 8080:80 creates a secure tunnel from your local machine to a Kubernetes Service.
  • kubectl get pods -o jsonpath and kubectl get pods -o custom-columns provide powerful, scriptable output formats.
  • kubectl diff -f manifest.yaml shows what will change before applying — a critical safety check for production changes.
  • kubectl top pods and kubectl top nodes show live CPU and memory usage when the metrics-server addon is installed.
  • The krew plugin manager extends kubectl with tools like ctx, ns, tree, and neat for faster cluster navigation.

Advertisement

Sanjeev Sharma

Written by

Sanjeev Sharma

Full Stack Engineer · E-mopro

Related reading