Kubernetes
Cluster control without the typing
Managing Kubernetes shouldn't feel like you're programming in kubectl. These shortcuts make it tolerable.
Core Aliases
| Alias | Command | Description |
|---|---|---|
k | kubectl | Save 6 keystrokes every time |
kx | kubectx | Context switcher |
kns | kubens | Namespace switcher |
You'll type k get pods so much it'll become muscle memory.
Common Operations
| Alias | Command | Description |
|---|---|---|
kgp | kubectl get pods | List pods |
kaf | kubectl apply -f | Apply manifest |
keti | kubectl exec -ti | Interactive exec into pod |
These cover 80% of daily kubectl usage.
Functions
kconfig — Switch Kubeconfig
Manage multiple kubeconfig files:
kconfig dev
# Switches to ~/.kube/configs/dev
kconfig prod
# Switches to ~/.kube/configs/prodStore different cluster configs in ~/.kube/configs/ and switch between them easily.
klogs — Quick Pod Logs
Tail logs from a pod:
klogs my-pod
# Shows last 100 lines with follow (-f)Add -c container-name if the pod has multiple containers.
khelp — Quick Reference
Forgot that kubectl command?
khelp
# Shows common kubectl commands
# Organized by categoryYour personal kubectl cheatsheet.
Context & Namespace Management
kubectx
Switch between clusters:
kx # List contexts (with fzf if available)
kx production # Switch to production
kx staging # Switch to staging
kx - # Switch back to previous contextCritical for multi-cluster workflows. One command to switch environments.
kubens
Switch between namespaces:
kns # List namespaces (with fzf if available)
kns kube-system # Switch to kube-system
kns default # Back to default
kns - # Switch to previous namespaceStop typing -n namespace on every command.
Krew Plugin Manager
Krew extends kubectl with plugins. The path is already configured.
# Install plugins
kubectl krew install ctx ns
# Update krew
kubectl krew upgrade
# List installed
kubectl krew listRecommended plugins:
ctx/ns— Context/namespace managementtree— Show resources in tree formatview-secret— Decode secrets easilytail— Tail logs from multiple pods
k9s Integration
k9s is the ultimate Kubernetes TUI. Works seamlessly with these configs.
k9s # Open k9s in current context/namespace
k9s -n default # Open in specific namespace
k9s --context prod # Open with specific contextLearn k9s keyboard shortcuts:
0— Show all namespaces:pod— Jump to pods:svc— Jump to services:deploy— Jump to deploymentsl— View logsd— Describe resourcee— Edit resources— Shell into container
Common Workflows
Debug a Pod
# Find the pod
kgp
# Check logs
klogs my-pod-abc123
# Or shell into it
keti my-pod-abc123 -- bash
# or: keti my-pod-abc123 -- sh
# Describe it
k describe pod my-pod-abc123Apply Configuration Changes
# Apply single file
kaf deployment.yaml
# Apply directory
k apply -f ./manifests/
# Apply with kustomize
k apply -k ./overlays/production/Switch Environments
Multi-environment workflow:
# Check current context
kx
# Switch to staging
kx staging
# Switch namespace
kns my-app
# Verify you're in the right place
kgp
# Make changes
kaf deployment.yaml
# Switch back to dev
kx dev
kns defaultPort Forward for Local Access
# Forward pod port
k port-forward my-pod 8080:80
# Forward service port
k port-forward svc/my-service 8080:80
# Run in background
k port-forward my-pod 8080:80 &Access the service at localhost:8080.
Scale Deployments
# Scale up
k scale deployment/my-app --replicas=5
# Scale down
k scale deployment/my-app --replicas=1
# Scale to zero (careful!)
k scale deployment/my-app --replicas=0Check Resource Usage
# Node resources
k top nodes
# Pod resources
k top pods
# Specific namespace
k top pods -n productionRequires metrics-server installed in the cluster.
Watch Resources
# Watch pods
k get pods -w
# Watch with timestamp
k get pods -w --show-labels
# Watch events
k get events -wThe -w flag watches for changes. Useful during deployments.
Quick Debugging
Pod Won't Start
kgp # Check status
k describe pod <name> # Look for events
klogs <name> # Check logsUsually: ImagePullBackOff (image doesn't exist) or CrashLoopBackOff (app crashes on start).
Service Not Accessible
k get svc # List services
k get ep # List endpoints
k describe svc <name> # Check selector
kgp -l app=myapp # Verify pods match selectorOften the service selector doesn't match pod labels.
Config Not Applying
# Dry-run first
k apply -f config.yaml --dry-run=client
# Check diff
k diff -f config.yaml
# Then apply
kaf config.yamlCheck Cluster Health
k get nodes # Node status
k get pods -A # All pods, all namespaces
k get events -A --sort-by='.lastTimestamp' | tail -20Pro Tips
Always set context and namespace first: Use kx and kns before running commands. Prevents accidents in production.
Use k9s for exploration: Typing kubectl commands is fine for scripts, but k9s is faster for ad-hoc exploration.
Label everything: Use consistent labels on resources. Makes selectors and debugging easier.
Dry-run before apply: Always run --dry-run=client on changes to critical resources.
Keep kube configs separate: Use kconfig to manage different cluster credentials. Never accidentally apply dev config to prod.
Learn one good TUI: Either k9s, or another like kdash/lens. GUIs are faster than kubectl for many tasks.
Watch during deployments: Use -w flag to watch rollout status in real-time.