kubectl Core Commands Cheat Sheet
This is your quick reference for the most commonly used kubectl commands. Bookmark this page and come back whenever you need a refresher.
Create and Apply
Use apply for declarative management and create for imperative one-off resources.
# Apply a manifest file (create or update)
kubectl apply -f deployment.yaml
# Apply all manifests in a directory
kubectl apply -f ./manifests/
# Create a resource imperatively
kubectl create deployment nginx --image=nginx
# Create a namespace
kubectl create namespace staging
# Create a configmap from literal values
kubectl create configmap app-config --from-literal=ENV=production --from-literal=LOG_LEVEL=info
# Create a secret
kubectl create secret generic db-creds --from-literal=password=s3cret
Get and Describe
Use get to list resources and describe for detailed information.
# List resources
kubectl get pods
kubectl get services
kubectl get deployments
kubectl get nodes
# Wide output with extra columns
kubectl get pods -o wide
# All namespaces
kubectl get pods -A
# YAML output for a specific resource
kubectl get deployment my-app -o yaml
# Filter by label
kubectl get pods -l app=nginx
# Detailed information about a specific resource
kubectl describe pod my-app-abc123
kubectl describe node worker-1
kubectl describe service my-service
Edit and Patch
Modify resources that are already running in the cluster.
# Open a resource in your default editor
kubectl edit deployment my-app
# Patch a specific field
kubectl patch deployment my-app -p '{"spec":{"replicas":5}}'
# Update the container image
kubectl set image deployment/my-app nginx=nginx:1.28
# Add or update labels
kubectl label pod my-pod version=v2
# Add or update annotations
kubectl annotate pod my-pod description="production workload"
Delete
Remove resources from the cluster.
# Delete a specific resource
kubectl delete pod my-pod
kubectl delete deployment my-app
# Delete using a manifest file
kubectl delete -f deployment.yaml
# Delete all pods in a namespace
kubectl delete pods --all -n staging
# Delete with grace period
kubectl delete pod my-pod --grace-period=0 --force
# Delete resources by label
kubectl delete pods -l app=old-app
Debug and Troubleshoot
Commands for investigating problems and understanding what is happening.
# View pod logs
kubectl logs my-pod
kubectl logs my-pod -c sidecar-container # specific container
kubectl logs my-pod --previous # previous instance
kubectl logs my-pod -f # follow/stream logs
kubectl logs -l app=nginx --all-containers # logs by label
# Execute a command inside a running pod
kubectl exec my-pod -- ls /app
kubectl exec -it my-pod -- /bin/sh # interactive shell
# Port forward to access a pod locally
kubectl port-forward pod/my-pod 8080:80
kubectl port-forward svc/my-service 8080:80
# Copy files to/from a pod
kubectl cp my-pod:/app/logs.txt ./logs.txt
kubectl cp ./config.json my-pod:/app/config.json
# View resource usage
kubectl top pods
kubectl top nodes
# Run a temporary debug pod
kubectl run debug --image=busybox -it --rm -- /bin/sh
Quick Reference Table
| Action | Command |
|---|---|
| List pods | kubectl get pods |
| Pod details | kubectl describe pod <name> |
| Pod logs | kubectl logs <name> |
| Shell into pod | kubectl exec -it <name> -- /bin/sh |
| Create deployment | kubectl create deployment <name> --image=<image> |
| Scale deployment | kubectl scale deployment <name> --replicas=<n> |
| Update image | kubectl set image deployment/<name> <container>=<image> |
| Rollback | kubectl rollout undo deployment/<name> |
| Apply manifest | kubectl apply -f <file> |
| Delete resource | kubectl delete <type> <name> |
| Switch namespace | kubectl config set-context --current --namespace=<ns> |
| All namespaces | kubectl get <resource> -A |
| Port forward | kubectl port-forward <pod> <local>:<remote> |
| Current context | kubectl config current-context |
| Cluster info | kubectl cluster-info |
Key Takeaways
applyis for declarative management;createis for imperative one-offsgetlists resources;describegives detailed informationlogs,exec, andport-forwardare your primary debugging tools- Use
-Ato search across all namespaces when you cannot find a resource - Keep this cheat sheet handy as you work through the rest of the course