Overview
kubectl is the command-line tool for interacting with Kubernetes clusters. This cheat sheet covers the commands you will use most often for inspecting, creating, updating, and debugging resources.
Cluster and Context Commands
| Command |
Purpose |
kubectl config view |
Show the merged kubeconfig settings |
kubectl config get-contexts |
List all available contexts |
kubectl config use-context prod |
Switch to a different cluster context |
kubectl cluster-info |
Display cluster endpoint information |
kubectl version --short |
Show client and server versions |
Install kubectl from the Kubernetes official tools page.
Inspecting Resources
# List pods in the current namespace
kubectl get pods
# List pods across all namespaces
kubectl get pods -A
# List pods with node information
kubectl get pods -o wide
# Show detailed information about a pod
kubectl describe pod my-pod
# View logs
kubectl logs my-pod
# Follow logs in real time
kubectl logs -f my-pod
# View logs from the previous container instance
kubectl logs --previous my-pod
Creating and Applying Manifests
# Apply a YAML manifest (create or update)
kubectl apply -f deployment.yaml
# Apply all manifests in a directory
kubectl apply -f ./manifests/
# Delete resources defined in a manifest
kubectl delete -f deployment.yaml
Debugging and Executing Commands
| Command |
Purpose |
kubectl exec -it my-pod -- bash |
Open an interactive shell inside a pod |
kubectl exec my-pod -- env |
Run a single command inside a pod |
kubectl cp my-pod:/var/log/app.log ./app.log |
Copy files from a pod to local |
kubectl port-forward svc/my-service 8080:80 |
Forward a local port to a service |
kubectl rollout restart deployment/my-app |
Restart all pods in a deployment |
Resource Shortcuts Reference
| Shortcut |
Full resource name |
po |
pods |
svc |
services |
deploy |
deployments |
ns |
namespaces |
cm |
configmaps |
no |
nodes |
Common Output Formats
# JSON output
kubectl get pod my-pod -o json
# YAML output
kubectl get svc my-service -o yaml
# Custom columns
kubectl get pods -o custom-columns=NAME:.metadata.name,STATUS:.status.phase
# Sort by creation time
kubectl get pods --sort-by=.metadata.creationTimestamp
Quick Troubleshooting Flow
kubectl get pods — check if the pod is running or in CrashLoopBackOff.
kubectl describe pod my-pod — inspect events and resource limits.
kubectl logs my-pod — read application output.
kubectl logs --previous my-pod — read logs from the crashed instance.
kubectl exec -it my-pod -- sh — enter the container to check connectivity or files.