Overview

  • Chapter 3 of the Kubernetes Cookbook: the everyday kubectl verbs (get, delete, watch, edit, explain).
  • My basics are already in Kubectl (deploying images, get/edit/delete, port-forward, proxy). This note adds the power-user layer on top.
  • Back to the hub: Kubernetes Cookbook

The Mental Model

  • Verb + resource type + name: kubectl get deployment web, kubectl delete svc web.
  • Every command hits the API server. kubectl is just a fancy REST client (see API, Namespaces & Labels).
  • Context & namespace decide where you’re pointing:
kubectl config get-contexts
kubectl config use-context kind-dev
kubectl config set-context --current --namespace=my-app   # stop typing -n every time

Recipes

Recipe 3.1 — Listing Resources

  • Problem: You want to see what’s running.
  • Solution:
kubectl get pods                       # current namespace
kubectl get pods -A                    # all namespaces
kubectl get svc,deploy                 # several kinds at once
kubectl get deploy web -o wide         # extra columns
kubectl get pods -o yaml | less        # the full objects
kubectl api-resources                  # every kind + short name + namespaced?
  • Short names worth memorising:
ShortKindShortKind
popodsdeploydeployments
svcservicesrsreplicasets
cmconfigmapsdsdaemonsets
nsnamespacesstsstatefulsets
nonodesingingresses
pv / pvcpersistent volumes / claimshpahorizontal pod autoscalers
saserviceaccountsnetpolnetworkpolicies
  • Gotchas: kubectl get all is not all. It skips ConfigMaps, Secrets, Ingresses, PVCs, CRDs and more.

Source: K8s Cookbook, Recipe 3.1

Recipe 3.2 — Deleting Resources

  • Problem: You want to clean up.
  • Solution:
kubectl delete deploy web
kubectl delete svc,deploy -l app=niceone      # by label
kubectl delete -f app.yaml                     # whatever the file created
kubectl delete ns my-app                       # nukes everything inside it
kubectl delete pod stuck --grace-period=0 --force   # last resort
  • Discussion: Delete the owner, not the children. Deleting a Pod owned by a Deployment just makes a new one appear (that’s the self-healing from Pods). Owners cascade-delete their dependents via ownerReferences and garbage collection (--cascade=background|foreground|orphan).
  • Gotchas: Force-deleting a StatefulSet Pod can leave two Pods with the same identity if the node comes back. A namespace stuck in Terminating almost always means a finalizer is waiting on a controller that’s gone.

Source: K8s Cookbook, Recipe 3.2

Recipe 3.3 — Watching Changes

  • Problem: You want a live view while things roll out.
  • Solution: kubectl get pods -w (streams changes), or watch -n1 kubectl get pods (redraws the screen). kubectl rollout status deploy/web blocks until a rollout finishes. kubectl wait --for=condition=Ready pod -l app=web --timeout=60s is the scripting-friendly version.
  • Gotchas: -w prints a new line per change rather than redrawing. Use watch or k9s for a dashboard feel.

Source: K8s Cookbook, Recipe 3.3

Recipe 3.4 — Editing Resources

  • Problem: You need to change a live object.
  • Solution:
KUBE_EDITOR="code --wait" kubectl edit deploy web     # opens live YAML
kubectl set image deploy/web web=nginx:1.27            # targeted shortcuts
kubectl scale deploy/web --replicas=3
kubectl patch deploy web -p '{"spec":{"replicas":2}}'
  • Discussion: edit is great for experiments, but the change isn’t in Git. The next kubectl apply -f of your file will quietly undo it (“drift”). Prefer editing the file and re-applying. More on that in Managing Manifests.
  • Gotchas: Not every edit triggers a rollout. Only changes under .spec.template do (see Creating & Modifying Workloads).

Changed since the book:

The book’s example starts with kubectl run nginx and then edits deploy/nginx. Today run creates a Pod, so there is no deploy to edit. Use kubectl create deployment nginx --image=nginx.

Source: K8s Cookbook, Recipe 3.4

Recipe 3.5 — kubectl explain

  • Problem: What does this field mean, what type is it, and is it required?
  • Solution:
kubectl explain deployment.spec.strategy
kubectl explain pod.spec.containers.livenessProbe --recursive | less
kubectl explain ingress --api-version=networking.k8s.io/v1
  • Discussion: The docs come from the API server’s OpenAPI schema, so they match your cluster’s version and include CRDs.
  • Gotchas: It needs a reachable cluster, because the schema is served by the API server.

Source: K8s Cookbook, Recipe 3.5


Bonus: Modern kubectl Tricks (not in the book)

  • Generate YAML instead of writing it: kubectl create deploy web --image=nginx --dry-run=client -o yaml > web.yaml
  • Diff before apply: kubectl diff -f web.yaml
  • Server-side apply: kubectl apply --server-side -f web.yaml (see Managing Manifests)
  • Events, sorted: kubectl events --for pod/web-123 or kubectl get events --sort-by=.lastTimestamp
  • JSONPath output: kubectl get pods -o jsonpath='{.items[*].status.podIP}'
  • Custom columns: kubectl get pods -o custom-columns=NAME:.metadata.name,NODE:.spec.nodeName
  • Plugins via krew: kubectl krew install ctx ns neat tree. Then kubectl ctx / kubectl ns switch context and namespace fast.
  • Autocomplete: source <(kubectl completion zsh), and alias k=kubectl plus compdef k=kubectl (see Maintenance & Troubleshooting).

Summary

  • get / describe / explain to look, apply / edit / set / patch / scale to change, delete to remove.
  • Use short names and -A, -l, -o yaml|wide|jsonpath.
  • Delete owners, not Pods. Watch out for finalizers.
  • Imperative edits drift from Git, so prefer edit file → kubectl diff → kubectl apply.