Kubernetes and kubectl cheat sheet
The kubectl commands and YAML for pods, deployments, services, scaling, rollouts, rollbacks, ConfigMaps and Secrets from the Kubernetes course.
Looking around
| Command | What it does |
|---|---|
kubectl get pods -o wide | pods, with node and IP |
kubectl get deploy,svc,cm,secret | several kinds at once |
kubectl describe pod NAME | details and Events (start here when a pod fails) |
kubectl logs NAME [-c container] [-f] [--previous] | output, or the crashed run's |
kubectl exec -it NAME -- sh | a shell in a pod |
kubectl get events --sort-by=.lastTimestamp | what happened, in order |
kubectl explain deployment.spec.strategy | the docs for a field |
Add -n namespace or -A (all namespaces).
Creating and changing
| Command | What it does |
|---|---|
kubectl apply -f app.yaml | create or update from a file |
kubectl create deployment web --image=nginx:1.27 --replicas=3 | quick deployment |
kubectl expose deployment web --port=80 --type=NodePort | a Service for it |
kubectl scale deployment web --replicas=5 | change the count |
kubectl delete -f app.yaml | remove what the file made |
kubectl port-forward svc/web 8080:80 | reach it from your machine |
Rollouts
| Command | What it does |
|---|---|
kubectl set image deployment/web web=nginx:1.28 | new version |
kubectl rollout status deployment/web | wait for it |
kubectl rollout history deployment/web | revisions |
kubectl rollout undo deployment/web [--to-revision=N] | go back |
kubectl rollout restart deployment/web | restart every pod |
A deployment and its service
apiVersion: apps/v1
kind: Deployment
metadata: { name: web }
spec:
replicas: 3
selector: { matchLabels: { app: web } }
template:
metadata: { labels: { app: web } } # must match the selector
spec:
containers:
- name: web
image: nginx:1.27
ports: [{ containerPort: 80 }]
resources:
requests: { cpu: 100m, memory: 64Mi }
limits: { memory: 128Mi }
readinessProbe: { httpGet: { path: /, port: 80 } }
envFrom: [{ configMapRef: { name: web-config } }]
---
apiVersion: v1
kind: Service
metadata: { name: web }
spec:
selector: { app: web }
ports: [{ port: 80, targetPort: 80 }]
Paste a manifest into the Kubernetes YAML explainer to check it.
Configuration
kubectl create configmap web-config --from-literal=MODE=prod --from-file=app.conf
kubectl create secret generic db --from-literal=password='s3cret'
kubectl get secret db -o jsonpath='{.data.password}' | base64 -d
A Secret is base64, not encryption. A changed ConfigMap reaches env vars only after the pods restart.
Common pod states
Pending (no node fits: requests, taints) · ImagePullBackOff (image name or registry) · CrashLoopBackOff (the app exits: read logs --previous) · Running but not READY (readiness probe fails).