Overview
kubectl is your interface to Kubernetes clusters. This cheat sheet covers the commands you reach for 90% of the time: checking cluster state, viewing resources, viewing and following logs, running commands inside containers, port-forwarding, applying manifests, and debugging. Every command below is tested against Kubernetes 1.30.
TL;DR
# Switch cluster and check access
kubectl config use-context <context>
kubectl cluster-info
# View resources (the 80/20 answer)
kubectl get pods
kubectl get nodes
kubectl get all -n <namespace>
kubectl describe pod <name>
# Logs and exec
kubectl logs <pod>
kubectl logs -f <pod>
kubectl exec -it <pod> -- bash
# Deploy and update
kubectl apply -f deployment.yaml
kubectl rollout status deployment/<name>
kubectl rollout undo deployment/<name>
# Scale and delete
kubectl scale deployment/<name> --replicas=3
kubectl delete pod <name>
On this page Overview Cluster and config Get and describe: viewing resources Logs Exec and interactive commands Port forwarding Apply, delete, and manifest management Rollout and updates Scale and resource management Labels and selectors Debugging and troubleshooting Output formats Namespaces Patterns you’ll reuse FAQ Cluster and config
Command
What it does
Example
kubectl config view
Show current kubeconfig file
kubectl config view
kubectl config use-context <ctx>
Switch to a cluster context
kubectl config use-context prod
kubectl config current-context
Print active context
kubectl config current-context
kubectl config get-contexts
List all configured contexts
kubectl config get-contexts
kubectl config set-context <name> --cluster=<cluster> --user=<user>
Create a new context
kubectl config set-context dev --cluster=kind-dev --user=admin@kind-dev
kubectl cluster-info
Display cluster API server address
kubectl cluster-info
kubectl auth can-i <verb> <resource>
Check if you can perform an action
kubectl auth can-i create deployments --namespace prod
Get and describe: viewing resources
Command
What it does
Example
kubectl get pods
List pods in the current namespace
kubectl get pods
kubectl get pods -A
List pods in all namespaces
kubectl get pods -A
kubectl get pods -n <ns>
List pods in a specific namespace
kubectl get pods -n prod
kubectl get nodes
List cluster nodes
kubectl get nodes
kubectl get services
List services
kubectl get services or kubectl get svc
kubectl get deployments
List deployments
kubectl get deploy
kubectl get statefulsets
List stateful sets
kubectl get statefulsets or kubectl get sts
kubectl get daemonsets
List daemon sets
kubectl get ds
kubectl get jobs
List jobs
kubectl get jobs
kubectl get cronjobs
List cron jobs
kubectl get cronjobs
kubectl get pvc
List persistent volume claims
kubectl get pvc
kubectl get ingress
List ingress resources
kubectl get ingress
kubectl get all
List all common resources in namespace
kubectl get all
kubectl get all -A
List all resources across all namespaces
kubectl get all -A
kubectl describe pod <name>
Show full details of a pod
kubectl describe pod web-abc123
kubectl describe node <name>
Show node details and conditions
kubectl describe node node-1
kubectl describe svc <name>
Show service configuration and endpoints
kubectl describe svc api
Logs
Command
What it does
Example
kubectl logs <pod>
Print pod logs to stdout
kubectl logs web-abc123
kubectl logs -f <pod>
Stream logs continuously (follow)
kubectl logs -f web-abc123
kubectl logs <pod> -c <container>
View logs from a specific container in a pod
kubectl logs web-abc123 -c app
kubectl logs <pod> --previous
Show logs from the previous (crashed) container
kubectl logs web-abc123 --previous
kubectl logs -l <label>
Show logs from pods matching a label
kubectl logs -l app=web
kubectl logs <pod> --tail=50
Show only the last 50 lines
kubectl logs web-abc123 --tail=50
kubectl logs <pod> --since=1h
Show logs from the last 1 hour
kubectl logs web-abc123 --since=1h
kubectl logs <pod> -f -l <label>
Stream logs from all pods with a label
kubectl logs -f -l app=web
Exec and interactive commands
Command
What it does
Example
kubectl exec -it <pod> -- bash
Open a bash shell in a pod
kubectl exec -it web-abc123 -- bash
kubectl exec -it <pod> -- sh
Open a shell (works in minimal images)
kubectl exec -it web-abc123 -- sh
kubectl exec -it <pod> -c <container> -- bash
Exec into a specific container in a multi-container pod
kubectl exec -it web-abc123 -c app -- bash
kubectl exec <pod> -- <command>
Run a command without interactive mode
kubectl exec web-abc123 -- ps aux
kubectl exec -it <pod> -- /bin/bash --user=1000
Exec as a different user (UID)
kubectl exec -it web-abc123 -- bash --user=1000
Port forwarding
Command
What it does
Example
kubectl port-forward pod/<name> 8080:8080
Forward local port 8080 to pod port 8080
kubectl port-forward pod/web-abc123 8080:8080
kubectl port-forward svc/<name> 5432:5432
Forward to a service (connects to any pod backing it)
kubectl port-forward svc/database 5432:5432
kubectl port-forward pod/<name> 9000:8080
Forward local 9000 to pod 8080
kubectl port-forward pod/app 9000:8080
kubectl port-forward <pod> 0:8080
Assign a random local port
kubectl port-forward app 0:8080
kubectl port-forward pod/<name> 8080:8080 --address 0.0.0.0
Allow external clients to connect (not just localhost)
kubectl port-forward pod/api 8080:8080 --address 0.0.0.0
Apply, delete, and manifest management
Command
What it does
Example
kubectl apply -f <file.yaml>
Create or update a resource from a manifest
kubectl apply -f deployment.yaml
kubectl apply -f <dir>/
Apply all YAML files in a directory
kubectl apply -f manifests/
kubectl apply -f <url>
Apply a manifest from a URL
kubectl apply -f https://example.com/config.yaml
kubectl apply --dry-run=client -f <file>
Preview what would be applied without making changes
kubectl apply --dry-run=client -f deployment.yaml
kubectl apply --dry-run=server -f <file>
Server-side dry run (webhook validation)
kubectl apply --dry-run=server -f deployment.yaml
kubectl delete pod <name>
Delete a pod
kubectl delete pod web-abc123
kubectl delete -f <file.yaml>
Delete all resources in a manifest
kubectl delete -f deployment.yaml
kubectl delete <resource-type> -l <label>
Delete resources matching a label
kubectl delete pods -l app=web
kubectl delete <resource-type> --all
Delete all resources of a type in current namespace
kubectl delete pods --all
kubectl delete <resource-type> --all -A
Delete all resources of a type across all namespaces
kubectl delete pods --all -A
Rollout and updates
Command
What it does
Example
kubectl rollout status deployment/<name>
Watch rollout progress
kubectl rollout status deployment/web
kubectl rollout history deployment/<name>
Show revision history
kubectl rollout history deployment/web
kubectl rollout undo deployment/<name>
Revert to the previous revision
kubectl rollout undo deployment/web
kubectl rollout undo deployment/<name> --to-revision=3
Revert to a specific revision
kubectl rollout undo deployment/web --to-revision=3
kubectl set image deployment/<name> <container>=<new-image>
Update the container image
kubectl set image deployment/web web=nginx:1.26 && kubectl annotate deployment/web kubernetes.io/change-cause="nginx 1.26"
kubectl patch deployment/<name> -p '{"spec":{"replicas":5}}'
Patch a resource inline
kubectl patch deployment/web -p '{"spec":{"replicas":5}}'
Scale and resource management
Command
What it does
Example
kubectl scale deployment/<name> --replicas=3
Scale a deployment to a number of replicas
kubectl scale deployment/web --replicas=3
kubectl scale statefulset/<name> --replicas=5
Scale a stateful set
kubectl scale statefulset/db --replicas=5
kubectl top nodes
Show CPU and memory usage by node (requires metrics-server)
kubectl top nodes
kubectl top pod
Show CPU and memory usage by pod
kubectl top pod
kubectl top pod -l <label>
Show usage for pods with a label
kubectl top pod -l app=web
Labels and selectors
Command
What it does
Example
kubectl label pod <name> <key>=<value>
Add a label to a pod
kubectl label pod web-abc tier=frontend
kubectl label pod <name> <key>=<value> --overwrite
Update an existing label
kubectl label pod web-abc tier=backend --overwrite
kubectl label pod <name> <key>-
Remove a label from a pod
kubectl label pod web-abc tier-
kubectl label nodes <node> <key>=<value>
Label a node
kubectl label nodes node-1 disktype=ssd
kubectl get pods -l <key>=<value>
Get pods matching a label
kubectl get pods -l app=web
kubectl get pods -l <key>!=<value>
Get pods where label does not match
kubectl get pods -l app!=db
kubectl get pods -l '<key> in (val1, val2)'
Get pods where label is in a list
kubectl get pods -l 'tier in (frontend, backend)'
Debugging and troubleshooting
Command
What it does
Example
kubectl describe pod <name>
Show events and state of a pod
kubectl describe pod web-abc123
kubectl get events -n <namespace>
List recent cluster events
kubectl get events -n prod
kubectl get events -n <namespace> --sort-by='.lastTimestamp'
Sort events by time
kubectl get events -n prod --sort-by='.lastTimestamp'
kubectl debug pod/<name>
Start a debug container in the pod namespace (Kubernetes 1.16+)
kubectl debug pod/web-abc123
kubectl debug pod/<name> -it --image=busybox
Debug pod with a specific image
kubectl debug pod/app -it --image=busybox
kubectl logs <pod> --previous
Show logs from a crashed container
kubectl logs web-abc123 --previous
kubectl top pod <name>
Show CPU and memory of a specific pod
kubectl top pod web-abc123
kubectl api-resources
List all resource types available in the cluster
kubectl api-resources
kubectl get pod/<name> -o yaml
Export a pod’s definition as YAML
kubectl get pod/web-abc123 -o yaml
Command
What it does
Example
kubectl get pods -o yaml
Output as YAML
kubectl get pods -o yaml
kubectl get pods -o json
Output as JSON
kubectl get pods -o json
kubectl get pods -o wide
Include additional columns (node, IP, etc.)
kubectl get pods -o wide
kubectl get pods -o name
Output only resource names
kubectl get pods -o name
kubectl get pods -o jsonpath='{.items[*].metadata.name}'
Extract specific fields with JSONPath
kubectl get pods -o jsonpath='{.items[*].metadata.name}'
kubectl get pods -o custom-columns=NAME:.metadata.name,STATUS:.status.phase
Define custom columns
kubectl get pods -o custom-columns=NAME:.metadata.name,STATUS:.status.phase,IMAGE:.spec.containers[0].image
kubectl get pods --no-headers
Hide the header row
kubectl get pods --no-headers
kubectl get pods -L tier,app
Show values of specific labels as columns
kubectl get pods -L tier,app
Namespaces
Command
What it does
Example
kubectl get ns
List all namespaces
kubectl get ns
kubectl create namespace <name>
Create a namespace
kubectl create namespace prod
kubectl delete namespace <name>
Delete a namespace (and all resources in it)
kubectl delete namespace staging
kubectl config set-context --current --namespace=<namespace>
Set default namespace for current context
kubectl config set-context --current --namespace=prod
kubectl get pods -n <namespace>
Query a specific namespace
kubectl get pods -n prod
kubectl get pods -A
Query all namespaces at once
kubectl get pods -A
Patterns you’ll reuse
# Watch a deployment rollout in real time
kubectl rollout status deployment/<name> --watch
# Get YAML of running resource for editing
kubectl get deployment/<name> -o yaml | vim - | kubectl apply -f -
# Find pods in a failed state
kubectl get pods --field-selector=status.phase!=Running
# Get all events in the cluster sorted by time
kubectl get events -A --sort-by='.lastTimestamp'
# Stream logs from multiple pods with a label
kubectl logs -f -l app=<label> --all-containers=true --max-log-requests=10
# View and modify resource limits
kubectl set resources deployment/<name> --limits=cpu=500m,memory=512Mi --requests=cpu=250m,memory=256Mi
# Get the IP address of a service
kubectl get svc <name> -o jsonpath='{.spec.clusterIP}'
# Export pod manifest to a file for backup
kubectl get pod/<name> -o yaml > backup.yaml
FAQ
How do I switch Kubernetes contexts?
Run kubectl config use-context <context-name> to switch. List available contexts with kubectl config get-contexts. View the current context via kubectl config current-context.
What’s the difference between apply and create?
create fails if the resource exists; apply creates or updates. Use apply in production so you can safely re-run your manifests. To track why a rollout happened, set the kubernetes.io/change-cause annotation (the old --record flag is removed).
How do I stream logs from a pod?
Use kubectl logs -f <pod> for live logs from one pod, or kubectl logs -f -l app=myapp to tail logs from all pods matching a label. Add --previous to see logs from a crashed container.
Can I run a command inside a running container?
Yes: kubectl exec -it <pod> -- /bin/bash (or sh for minimal images). For multi-container pods, specify the container: kubectl exec -it <pod> -c <container> -- bash. Use --user to run as a different user.
How do I port-forward from a pod?
Run kubectl port-forward <pod> 8080:8080 to forward local 8080 to pod 8080. Use --address 0.0.0.0 to allow remote access. For services: kubectl port-forward svc/<service> 5432:5432.
How this guide was verified Written by hand from the official documentation, every command re-checked, and reviewed a second time before publishing. Last reviewed on October 27, 2026 .
Official sources read for this article