πŸ“šBook Signing at KubeCon EU 2026Meet us at Booking.com HQ (Mon 18:30-21:00) & vCluster booth #521 (Tue 24 Mar, 12:30-1:30pm) β€” free book giveaway!RSVP Booking.com Event
Configuration beginner ⏱ 15 minutes K8s 1.28+

kubectl Cheat Sheet: Essential Commands

Complete kubectl cheat sheet with essential commands for pods, deployments, services, logs, debugging, and cluster management. Copy-paste ready examples.

By Luca Berton β€’ β€’ πŸ“– 5 min read

πŸ’‘ Quick Answer: Complete kubectl cheat sheet with essential commands for pods, deployments, services, logs, debugging, and cluster management. Copy-paste ready examples.

The Problem

This is one of the most searched Kubernetes topics. Having a comprehensive, well-structured guide helps both beginners and experienced users quickly find what they need.

The Solution

Pod Operations

# List pods
kubectl get pods
kubectl get pods -A                    # All namespaces
kubectl get pods -o wide               # Show node + IP
kubectl get pods -l app=nginx          # Filter by label
kubectl get pods --field-selector status.phase=Running

# Describe pod (events + config)
kubectl describe pod <name>

# Create pod from YAML
kubectl apply -f pod.yaml

# Delete pod
kubectl delete pod <name>
kubectl delete pod <name> --grace-period=0 --force  # Force delete

# Exec into pod
kubectl exec -it <pod> -- /bin/bash
kubectl exec -it <pod> -c <container> -- sh

# Port forward
kubectl port-forward pod/<name> 8080:80
kubectl port-forward svc/<name> 8080:80

# Copy files
kubectl cp <pod>:/path/file ./local
kubectl cp ./local <pod>:/path/file

Deployment Operations

# Create/update deployment
kubectl apply -f deployment.yaml
kubectl create deployment nginx --image=nginx --replicas=3

# Scale
kubectl scale deployment <name> --replicas=5

# Rolling update
kubectl set image deployment/<name> container=image:tag
kubectl rollout status deployment/<name>
kubectl rollout history deployment/<name>
kubectl rollout undo deployment/<name>
kubectl rollout undo deployment/<name> --to-revision=2

# Restart deployment (rolling restart)
kubectl rollout restart deployment/<name>

Service & Networking

# Expose deployment
kubectl expose deployment <name> --port=80 --target-port=8080 --type=ClusterIP
kubectl expose deployment <name> --type=NodePort
kubectl expose deployment <name> --type=LoadBalancer

# Get services
kubectl get svc
kubectl get endpoints <svc>

# DNS test
kubectl run dns-test --rm -it --image=busybox -- nslookup <svc>.<ns>.svc.cluster.local

Logs & Debugging

# Logs
kubectl logs <pod>
kubectl logs <pod> --previous          # Previous crash
kubectl logs <pod> -c <container>      # Specific container
kubectl logs <pod> -f                  # Follow/stream
kubectl logs <pod> --tail=100          # Last 100 lines
kubectl logs -l app=nginx --all-containers

# Debug
kubectl debug <pod> -it --image=nicolaka/netshoot
kubectl debug node/<node> -it --image=ubuntu

# Events
kubectl get events --sort-by='.lastTimestamp'
kubectl get events --field-selector type=Warning

# Resource usage
kubectl top pods
kubectl top nodes
kubectl top pods --sort-by=memory

Namespace & Context

# Namespaces
kubectl get ns
kubectl create ns <name>
kubectl config set-context --current --namespace=<name>

# Context
kubectl config get-contexts
kubectl config use-context <name>
kubectl config current-context

Resource Management

# Get any resource
kubectl get <resource>                 # pods, svc, deploy, pvc, cm, secret...
kubectl get all                        # Common resources
kubectl api-resources                  # All resource types

# Output formats
kubectl get pods -o yaml
kubectl get pods -o json
kubectl get pods -o jsonpath='{.items[*].metadata.name}'
kubectl get pods -o custom-columns=NAME:.metadata.name,STATUS:.status.phase

# Labels & annotations
kubectl label pod <name> env=prod
kubectl annotate pod <name> description="my app"
kubectl get pods --show-labels

# Dry run (preview)
kubectl apply -f file.yaml --dry-run=client
kubectl apply -f file.yaml --dry-run=server

# Diff before applying
kubectl diff -f file.yaml

Quick Reference Table

ActionCommand
List podskubectl get pods -A
Describekubectl describe pod <name>
Logskubectl logs <pod> -f
Execkubectl exec -it <pod> -- bash
Scalekubectl scale deploy <name> --replicas=N
Rolloutkubectl rollout restart deploy/<name>
Debugkubectl debug <pod> -it --image=netshoot
Port forwardkubectl port-forward svc/<name> 8080:80
graph TD
    A[kubectl] --> B[get - List resources]
    A --> C[describe - Show details]
    A --> D[apply - Create/update]
    A --> E[delete - Remove]
    A --> F[logs - View output]
    A --> G[exec - Run commands]
    A --> H[debug - Troubleshoot]
    A --> I[port-forward - Local access]

Frequently Asked Questions

What is kubectl?

kubectl is the command-line tool for interacting with Kubernetes clusters. It communicates with the API server to create, inspect, update, and delete resources.

How do I install kubectl?

# Linux
curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"
chmod +x kubectl && sudo mv kubectl /usr/local/bin/

# macOS
brew install kubectl

# Verify
kubectl version --client

How do I enable kubectl auto-completion?

# bash
echo 'source <(kubectl completion bash)' >> ~/.bashrc
echo 'alias k=kubectl' >> ~/.bashrc
echo 'complete -o default -F __start_kubectl k' >> ~/.bashrc
source ~/.bashrc

Best Practices

  • Start simple β€” use the basic form first, add complexity as needed
  • Be consistent β€” follow naming conventions across your cluster
  • Document your choices β€” add annotations explaining why, not just what
  • Monitor and iterate β€” review configurations regularly

Key Takeaways

  • This is fundamental Kubernetes knowledge every engineer needs
  • Start with the simplest approach that solves your problem
  • Use kubectl explain and kubectl describe when unsure
  • Practice in a test cluster before applying to production
#kubectl #cheat-sheet #commands #cli #kubernetes
Luca Berton
Written by Luca Berton

Principal Solutions Architect specializing in Kubernetes, AI/GPU infrastructure, and cloud-native platforms. Author of Kubernetes Recipes and creator of CopyPasteLearn courses.

Kubernetes Recipes book cover

Want More Kubernetes Recipes?

This recipe is from Kubernetes Recipes, our 750-page practical guide with hundreds of production-ready patterns.

Luca Berton Ansible Pilot Ansible by Example Open Empower K8s Recipes Terraform Pilot CopyPasteLearn ProteinLens