Kubernetes Cheat Sheet: Essential kubectl Commands for Developers
The practical reference guide to managing containers, pods, and clusters
Posted on February 2, 2026
· 18 minute read
This cheat sheet is everything I wish I had when I started with Kubernetes. It covers the commands you will actually use, organized by what you are trying to do. No theory dumps. Just practical commands that work.
You should see output showing the client version. If you get “command not found”, ensure kubectl is in your PATH.
Check Your Setup
1
2
3
4
5
6
7
8
9
10
11
# Check kubectl version
kubectl version --client# Check cluster connection
kubectl cluster-info
# Check current context (which cluster you are talking to)
kubectl config current-context
# List all nodes in cluster
kubectl get nodes
Understanding Kubernetes Objects
Everything in Kubernetes is an object. Here is how they relate:
flowchart TB
subgraph Control["Control Plane"]
API[API Server]
end
subgraph Objects["Core Objects"]
D[Deployment]
RS[ReplicaSet]
P[Pod]
S[Service]
CM[ConfigMap]
SEC[Secret]
end
D --> RS
RS --> P
S --> P
CM --> P
SEC --> P
API --> D
API --> S
API --> CM
API --> SEC
style API fill:#2d3748,stroke:#4a5568,color:#e2e8f0
style D fill:#1a365d,stroke:#2b6cb0,color:#bee3f8
style RS fill:#1a365d,stroke:#2b6cb0,color:#bee3f8
style P fill:#234e52,stroke:#319795,color:#b2f5ea
style S fill:#744210,stroke:#d69e2e,color:#fefcbf
style CM fill:#2d3748,stroke:#4a5568,color:#e2e8f0
style SEC fill:#2d3748,stroke:#4a5568,color:#e2e8f0
Object
What It Does
Pod
Smallest deployable unit. Runs one or more containers
When you work with multiple clusters (dev, staging, prod), context management becomes essential.
Viewing Contexts
1
2
3
4
5
6
7
8
# List all contexts
kubectl config get-contexts
# Show current context
kubectl config current-context
# Show full config
kubectl config view
Switching Contexts
1
2
3
4
5
6
7
8
9
10
11
# Switch to a different cluster
kubectl config use-context production-cluster
# Set default namespace for current context
kubectl config set-context --current--namespace=my-namespace
# Create a new context
kubectl config set-context dev-context \--cluster=dev-cluster \--user=dev-user \--namespace=development
Quick Tip: kubectx and kubens
If you switch contexts frequently, install kubectx. It provides:
kubectx to switch clusters quickly
kubens to switch namespaces quickly
1
2
3
4
5
# Instead of: kubectl config use-context production
kubectx production
# Instead of: kubectl config set-context --current --namespace=my-app
kubens my-app
Pods
Pods are the atomic unit of Kubernetes. A pod runs one or more containers that share network and storage.
# List pods in current namespace
kubectl get pods
# List pods in all namespaces
kubectl get pods -A# List pods with more details
kubectl get pods -o wide
# List pods with labels
kubectl get pods --show-labels# Watch pods in real time
kubectl get pods -w# Get pod details
kubectl describe pod my-pod
# Get pod YAML
kubectl get pod my-pod -o yaml
Creating Pods
For production, always use Deployments instead of creating pods directly. Direct pods are not recreated when they die.
1
2
3
4
5
6
7
8
9
10
11
# Run a quick test pod
kubectl run nginx --image=nginx
# Run and expose a port
kubectl run nginx --image=nginx --port=80
# Run with environment variables
kubectl run myapp --image=myapp:v1 --env="DB_HOST=localhost"# Generate YAML without creating (dry run)
kubectl run nginx --image=nginx --dry-run=client -o yaml > pod.yaml
Deleting Pods
1
2
3
4
5
6
7
8
9
10
11
# Delete a pod
kubectl delete pod my-pod
# Delete pod immediately (no grace period)
kubectl delete pod my-pod --grace-period=0 --force# Delete all pods in namespace
kubectl delete pods --all# Delete pods by label
kubectl delete pods -lapp=myapp
Interacting with Pods
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# Execute command in pod
kubectl exec my-pod --ls /app
# Get interactive shell
kubectl exec-it my-pod -- /bin/bash
# For pods without bash
kubectl exec-it my-pod -- /bin/sh
# Execute in specific container (multi-container pod)
kubectl exec-it my-pod -c sidecar -- /bin/bash
# Copy file to pod
kubectl cp local-file.txt my-pod:/path/in/pod/
# Copy file from pod
kubectl cp my-pod:/path/in/pod/file.txt ./local-copy.txt
Deployments
Deployments manage pods and provide rolling updates, rollbacks, and scaling.
Viewing Deployments
1
2
3
4
5
6
7
8
9
10
11
# List deployments
kubectl get deployments
# List with details
kubectl get deployments -o wide
# Describe deployment
kubectl describe deployment my-deployment
# Get deployment YAML
kubectl get deployment my-deployment -o yaml
Services provide stable network endpoints for accessing pods. Since pods are ephemeral and get new IPs when recreated, Services give you a consistent way to reach them.
# List services
kubectl get services
# List with shorthand
kubectl get svc
# Describe service
kubectl describe service my-service
# Get service YAML
kubectl get service my-service -o yaml
Creating Services
1
2
3
4
5
6
7
8
9
10
11
# Expose deployment as ClusterIP (internal only)
kubectl expose deployment my-deployment --port=80
# Expose as NodePort (accessible on node IP)
kubectl expose deployment my-deployment --port=80 --type=NodePort
# Expose as LoadBalancer (cloud only)
kubectl expose deployment my-deployment --port=80 --type=LoadBalancer
# Create from YAML
kubectl apply -f service.yaml
Service YAML example:
1
2
3
4
5
6
7
8
9
10
11
apiVersion:v1kind:Servicemetadata:name:web-servicespec:selector:app:web-app# Finds pods with this labelports:-port:80# Service porttargetPort:80# Pod porttype:ClusterIP
Important: The selector must match labels on your pods. If they do not match, the service will not route traffic to your pods. This is the most common service configuration mistake.
Testing Services
1
2
3
4
5
6
7
8
9
10
# Port forward to access locally
kubectl port-forward service/my-service 8080:80
# Access at http://localhost:8080# Get service endpoints (which pods it routes to)
kubectl get endpoints my-service
# Test from within cluster
kubectl run test--image=busybox --rm-it-- wget -qO- my-service:80
Deleting Services
1
2
3
4
5
# Delete service
kubectl delete service my-service
# Delete by label
kubectl delete service -lapp=myapp
Namespaces
Namespaces provide logical isolation within a cluster. Use them to separate environments, teams, or applications.
Viewing Namespaces
1
2
3
4
5
6
7
8
# List namespaces
kubectl get namespaces
# Short form
kubectl get ns
# Describe namespace
kubectl describe namespace my-namespace
Working with Namespaces
1
2
3
4
5
6
7
8
9
10
11
12
13
14
# Create namespace
kubectl create namespace my-namespace
# Run commands in specific namespace
kubectl get pods -n my-namespace
# Run commands in all namespaces
kubectl get pods -A# Set default namespace for current context
kubectl config set-context --current--namespace=my-namespace
# Delete namespace (deletes ALL resources inside)
kubectl delete namespace my-namespace
Default Namespaces
Namespace
Purpose
default
Where resources go if you don’t specify
kube-system
System components (DNS, scheduler)
kube-public
Publicly accessible resources
kube-node-lease
Node heartbeats
ConfigMaps and Secrets
ConfigMaps
ConfigMaps store non-sensitive configuration data.
spec:containers:-name:appimage:myappenvFrom:-configMapRef:name:my-config# Or mount as volumevolumeMounts:-name:config-volumemountPath:/etc/configvolumes:-name:config-volumeconfigMap:name:my-config
Secrets
Secrets store sensitive data like passwords, tokens, and keys. Values are base64 encoded (not encrypted by default).
Security Note: Base64 is encoding, not encryption. Anyone with cluster access can decode secrets using a base64 decoder. For production, consider using a secrets management tool like HashiCorp Vault or AWS Secrets Manager.
Logs and Debugging
This is where you spend most of your time when things go wrong.
# View pod logs
kubectl logs my-pod
# Follow logs in real time
kubectl logs -f my-pod
# View last 100 lines
kubectl logs --tail=100 my-pod
# View logs from last hour
kubectl logs --since=1h my-pod
# View logs from specific container
kubectl logs my-pod -c sidecar
# View logs from previous container (after crash)
kubectl logs my-pod --previous# View logs from all pods with label
kubectl logs -lapp=myapp
# View logs from all containers in pod
kubectl logs my-pod --all-containers
# Get detailed pod info
kubectl describe pod my-pod
# Check events in namespace
kubectl get events --sort-by='.lastTimestamp'# Check events for specific pod
kubectl get events --field-selector involvedObject.name=my-pod
# Get shell in running pod
kubectl exec-it my-pod -- /bin/bash
# Run debug container (for distroless images)
kubectl debug my-pod -it--image=busybox
# Check pod resource usage
kubectl top pods
# Check node resource usage
kubectl top nodes
The Debugging Workflow
When a pod is not working, follow this sequence:
flowchart TB
A[Pod Issue] --> B{kubectl get pods}
B -->|CrashLoopBackOff| C[kubectl logs pod --previous]
B -->|Pending| D[kubectl describe pod]
B -->|Running but broken| E[kubectl logs pod]
C --> F{Check application error}
D --> G{Check Events section}
E --> H{Check for errors}
F --> I[Fix code or config]
G --> J[Fix scheduling or resources]
H --> K[Fix application logic]
style A fill:#e53e3e,stroke:#c53030,color:#fff
style B fill:#2d3748,stroke:#4a5568,color:#e2e8f0
style C fill:#1a365d,stroke:#2b6cb0,color:#bee3f8
style D fill:#1a365d,stroke:#2b6cb0,color:#bee3f8
style E fill:#1a365d,stroke:#2b6cb0,color:#bee3f8
style I fill:#234e52,stroke:#319795,color:#b2f5ea
style J fill:#234e52,stroke:#319795,color:#b2f5ea
style K fill:#234e52,stroke:#319795,color:#b2f5ea
Common Pod States
State
Meaning
What to Check
Pending
Not scheduled yet
Events, node resources, taints
Running
At least one container running
Logs if misbehaving
Succeeded
All containers completed
Expected for Jobs
Failed
All containers stopped, at least one failed
Logs, previous logs
CrashLoopBackOff
Container keeps crashing
Previous logs, resource limits
ImagePullBackOff
Cannot pull image
Image name, registry auth
Resource Management
Setting Resource Limits
Always set resource requests and limits. Without them, a single pod can consume all node resources.
1
2
3
4
5
6
7
resources:requests:cpu:100m# Guaranteed CPUmemory:128Mi# Guaranteed memorylimits:cpu:500m# Maximum CPUmemory:512Mi# Maximum memory (OOMKill if exceeded)
Beyond Deployments, Kubernetes has specialized controllers for different workload patterns.
StatefulSets
For stateful applications that need stable network identity and persistent storage.
1
2
3
4
5
6
7
8
# List statefulsets
kubectl get statefulsets
# Scale statefulset
kubectl scale statefulset my-statefulset --replicas=5
# Delete pod (will be recreated with same identity)
kubectl delete pod my-statefulset-0
Use for: databases, message queues, anything needing stable storage and network identity.
DaemonSets
Run one pod per node. Great for node agents.
1
2
3
4
5
# List daemonsets
kubectl get daemonsets
# Describe daemonset
kubectl describe daemonset my-daemonset
Use for: log collectors, monitoring agents, network plugins.
Jobs and CronJobs
Run tasks to completion or on a schedule.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
# Create job
kubectl create job my-job --image=busybox --echo"Hello"# List jobs
kubectl get jobs# Create cronjob
kubectl create cronjob my-cronjob --image=busybox --schedule="0 * * * *"--echo"Hourly"# List cronjobs
kubectl get cronjobs
# View job logs
kubectl logs job/my-job
# 1. Check service exists
kubectl get service my-service
# 2. Check endpoints (pods the service routes to)
kubectl get endpoints my-service
# 3. If no endpoints, check selector matches pod labels
kubectl describe service my-service
kubectl get pods --show-labels# 4. Test from within cluster
kubectl run test--image=busybox --rm-it-- wget -qO- my-service:80
# 5. Check pod is ready
kubectl get pods -lapp=myapp
Common causes:
Selector does not match pod labels
Pod is not ready (readiness probe failing)
Wrong port in service definition
Pod crashed
Workflow 3: Pod Stuck in Pending
1
2
3
4
5
6
7
8
9
10
11
# 1. Check pod events
kubectl describe pod my-pod | grep-A 10 Events
# 2. Check node resources
kubectl describe nodes | grep-A 5 "Allocated resources"# 3. Check for taints
kubectl describe nodes | grep Taints
# 4. Check pod resource requests
kubectl get pod my-pod -o yaml | grep-A 10 resources
Common causes:
Insufficient CPU or memory on nodes
Node selector or affinity not matching any nodes
Taints without matching tolerations
Volume cannot be mounted
Productivity Tips
Essential Aliases
Add these to your shell profile (.bashrc or .zshrc):
# Basic aliasalias k=kubectl
# Get commandsalias kg='kubectl get'alias kgp='kubectl get pods'alias kgd='kubectl get deployments'alias kgs='kubectl get services'alias kgn='kubectl get nodes'# Describe and logsalias kd='kubectl describe'alias kl='kubectl logs'alias klf='kubectl logs -f'# Apply and deletealias ka='kubectl apply -f'alias kdel='kubectl delete'# Contextalias kctx='kubectl config use-context'alias kns='kubectl config set-context --current --namespace'
Shell Completion
Enable tab completion for kubectl:
1
2
3
4
5
6
7
8
# Bashecho'source <(kubectl completion bash)'>> ~/.bashrc
# Zshecho'source <(kubectl completion zsh)'>> ~/.zshrc
# Make alias work with completioncomplete-F __start_kubectl k
Output Formats
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# Wide output (more columns)
kubectl get pods -o wide
# YAML output
kubectl get pod my-pod -o yaml
# JSON output
kubectl get pod my-pod -o json
# Custom columns
kubectl get pods -o custom-columns=NAME:.metadata.name,STATUS:.status.phase
# JSONPath (extract specific fields)
kubectl get pods -ojsonpath='{.items[*].metadata.name}'# Get just names
kubectl get pods -o name
Dry Run and Diff
1
2
3
4
5
6
7
8
# Preview what would be created
kubectl apply -f deployment.yaml --dry-run=client
# Server-side dry run (validates against API)
kubectl apply -f deployment.yaml --dry-run=server
# Show diff before applying
kubectl diff -f deployment.yaml
The best way to learn kubectl is to use it. Start with kubectl get and kubectl describe. When something breaks, follow the debugging workflow. The commands will become muscle memory faster than you expect.
Have a command or tip that should be here? Drop it in the comments below.