Containers
Kubernetes & kubectl — inspect, deploy, and roll out
Confirm which context and namespace you are pointed at, inspect what is running, apply a change, watch the rollout, and roll it back if it goes bad.
Docs-checkedkuberneteskubectldeployrolloutcontext
Quick reference — reach for these first
context
kubectl config current-contextALWAYS run this first — it names the cluster you are about to change. kubectl config get-contexts lists all; use-context NAME switches.namespace
kubectl config set-context --current --namespace=NSPin the namespace so later commands need no -n. Without it, commands hit 'default' and silently show nothing.inspect
kubectl get pods -n NS -o wideWhat is running, on which node, and its READY/STATUS/RESTARTS. Read this before and after any change.apply
kubectl apply -f app.yaml -n NSDeclarative create-or-update from a manifest. Run kubectl diff -f app.yaml first to see what changes.rollout
kubectl rollout status deployment/DEPLOY -n NSBlocks until the new pods are Available or it reports failure. rollout undo reverts to the prior ReplicaSet.Deploy a change, then verify it before trusting it
Text equivalent: A left-to-right flow: step 1 confirm the context and namespace (accent), step 2 apply -f after a diff, step 3 rollout status, step 4 get pods and endpoints, step 5 port-forward to test. A good response ends at 'done'; a bad response or CrashLoop loops back to step 6, rollout undo, which reverts to the previous ReplicaSet.
Common tasks
Task · Command · Check
| Task | Command | Check |
|---|---|---|
| List pods wide | kubectl get pods -n NS -o wide | READY n/n, STATUS Running, low RESTARTS, and which node each is on. |
| Describe a resource | kubectl describe pod POD -n NS | Read the Events at the bottom — scheduling, image pull, probe failures. |
| Follow logs live | kubectl logs -f deploy/DEPLOY -n NS | Streams all matching pods. Add --previous to read a crashed container's last run. |
| Exec into a container | kubectl exec -it POD -n NS -- sh | A shell opens; use for curl localhost, env, or ls inside the pod. |
| Port-forward to test locally | kubectl port-forward svc/SVC 8080:80 -n NS | curl localhost:8080 reaches the service without exposing it publicly. |
| Preview a change | kubectl diff -f app.yaml -n NS | Shows the exact fields apply would add/change/remove. Empty = no-op. |
| Apply a manifest [CHANGES CLUSTER — prod: confirm context first] | kubectl apply -f app.yaml -n NS | 'configured'/'created' lines. On a prod context, re-run current-context before this. |
| Watch the rollout | kubectl rollout status deploy/DEPLOY -n NS | 'successfully rolled out'. If it hangs, describe the new pods for the cause. |
| Roll back [REVERTS PROD] | kubectl rollout undo deploy/DEPLOY -n NS | Reverts to the prior ReplicaSet; confirm with rollout status again. |
| Scale replicas | kubectl scale deploy/DEPLOY --replicas=N -n NS | get pods shows N pods; Deployment AVAILABLE matches N. |
| Recent events | kubectl get events -n NS --sort-by=.lastTimestamp | Newest last — Warning rows name pull, probe, or scheduling problems. |
| Resource usage | kubectl top pods -n NS | CPU/memory per pod (needs metrics-server). Spot a pod near its limit. |
| Discover a field | kubectl explain deployment.spec.strategy | Authoritative field docs from the live API — no guessing manifest keys. |
| Delete resources [DELETES DATA / DISRUPTS SERVICE] | kubectl delete -f app.yaml -n NS | Prefer delete -f over delete pod. Confirm the namespace; this does not prompt. |
Worked example — deploy web, verify, and roll back a bad image
DEPLOY & VERIFY
1
Confirm the target before touching anything.
kubectl config current-context
kubectl get pods -n NS
# staging-cluster
# web-7c9f 1/1 Running 0 5d2
Preview, then apply the new manifest.
kubectl diff -f web.yaml -n NS
kubectl apply -f web.yaml -n NS
# deployment.apps/web configured3
Watch the rollout — it stalls.
kubectl rollout status deploy/web -n NS
# Waiting for deployment "web" rollout to finish: 1 old replicas are pending termination...4
Find why the new pod is unhealthy.
kubectl get pods -n NS
kubectl describe pod web-NEW -n NS | tail -n 5
# web-NEW 0/1 ImagePullBackOff 0 40s
# Failed to pull image "web:v2-typo": not found5
Roll back to the last good ReplicaSet and confirm.
kubectl rollout undo deploy/web -n NS
kubectl rollout status deploy/web -n NS
# deployment.apps/web rolled back
# deployment "web" successfully rolled out✓
Endpoints repopulate and a port-forward smoke test returns 200. The stall was a bad image tag, not the cluster — fix the tag in web.yaml and re-apply.
Troubleshooting — symptom → first check → next check
| Symptom | First check | Establishes | Next check |
|---|---|---|---|
| Changes hit the wrong cluster | kubectl config current-context | Which cluster kubectl points at | config use-context NAME before any apply/delete |
| 'No resources found' | kubectl get pods -A | grep NAME | Right object, wrong namespace | Add -n NS or set the namespace on the context |
| ImagePullBackOff / ErrImagePull | kubectl describe pod POD -n NS | Bad tag vs missing registry auth | Fix image tag or add an imagePullSecret |
| Pod stuck Pending | kubectl describe pod POD -n NS | No node fits (CPU/mem, taints, PVC) | Lower requests, add nodes, or check the PVC |
| rollout status hangs | kubectl get pods -n NS -w | New pods crash or fail probes | Logs --previous + describe; then rollout undo |
| Deeper debugging needed | See the kubernetes-troubleshooting sheet | CrashLoop, OOMKill, DNS, probes in depth | Follow that sheet's decision tree |
References
- kubectl Quick Reference (Cheat Sheet)kubernetes.io
- kubectl Commands referencekubernetes.io
- Deployments — rolling updates & rollbackkubernetes.io
- Debug Running Podskubernetes.io
Assumes kubectl ≥ 1.29 against a reachable cluster on Linux/macOS; top needs metrics-server installed. DEPLOY, SVC, POD, NS, CONTEXT are CAPS placeholders — set your own.