cheatsheet
Kubernetes Commands Cheatsheet
Essential kubectl commands for managing pods, deployments, services, secrets, and more.
Published: December 8, 2024
Kubernetes Commands Cheatsheet
Setup Alias
alias k=kubectl
Get Objects
# Get pods in a namespace
k get pods -n <namespace>
# Get deployments
k get deployments -n <namespace>
# Get services
k get services -n <namespace>
# Generic syntax
k get <object> -n <namespace>
# Get in ALL namespaces
k get <object> --all-namespaces
Edit Objects
k edit <object> <name> -n <namespace>
Describe Objects
k describe <object> <name> -n <namespace>
Delete Objects
k delete <object> <name> -n <namespace>
Execute Inside a Container
k exec -it <pod_name> -n <namespace> /bin/sh
Scale Deployments
# Scale a single deployment
kubectl scale deployment <deployment_name> --replicas=<number>
# Scale ALL deployments in a namespace
kubectl get deploy -n <namespace> -o name | xargs -I % kubectl scale % --replicas=1 -n <namespace>
Get Pods on a Specific Node
kubectl get pods --all-namespaces -o wide --field-selector spec.nodeName=<node-name>
Secrets Management
# List secrets in namespace
k get secrets -n <namespace>
# Get a particular secret as YAML
k get secret <secret-name> -n <namespace> -o yaml
# Decode secret value
echo "encoded-value" | base64 -D # macOS
echo "encoded-value" | base64 -d # Linux
Quick Reference
| Task | Command |
|---|---|
| Get pods | k get pods -n <ns> |
| Get all pods | k get pods --all-namespaces |
| Describe pod | k describe pod <name> -n <ns> |
| Exec into pod | k exec -it <pod> -n <ns> /bin/sh |
| View logs | k logs <pod> -n <ns> |
| Follow logs | k logs -f <pod> -n <ns> |
| Scale deployment | k scale deploy <name> --replicas=3 |
| Get secrets | k get secrets -n <ns> |
| Delete pod | k delete pod <name> -n <ns> |
Useful Tools
# Install with Homebrew (macOS)
brew install kubectx # Switch contexts/namespaces easily
brew install stern # Multi-pod log tailing
Tags
kubernetesk8sdevopscontainerscommands