Read a pod.yaml and follow a pod's lifecycle
In this task you'll read any pod definition and follow a pod from creation to finish.
A pod.yaml has four parts: apiVersion and kind (what it is), metadata (name, namespace and labels that services and deployments select on), and spec (what to run). In the spec, each container has an image, ports, env, resources (requests and limits), probes and volumeMounts.
A pod moves through phases: Pending (accepted, but not all containers are running; often still scheduling or pulling images), Running, then Succeeded or Failed, or Unknown if its node can't be reached. Each container also has a state (Waiting, Running or Terminated) with a reason, and that reason (such as CrashLoopBackOff) is usually the clue you need.
Example. Quayhurst Health's appointment API stayed Pending after a release. Reading the pod.yaml showed a memory request of 64Gi (a typo for 64Mi); no node had that much free, so the scheduler couldn't place it.
Before you start
You need: Docker Desktop and kind on your computer (both free). Time: 25 minutes.
Where to look
- Terminal
- kubectl
Steps
- Run
kubectl run demo --image=nginx --dry-run=client -o yaml > pod.yamland read each section of the file. - Add
resources(requests and limits) and areadinessProbeon port 80, thenkubectl apply -f pod.yaml. - Watch it with
kubectl get pod demo -wand note the phase changes. - Run
kubectl describe pod demoand read the Conditions and Events at the bottom. - Use
kubectl explain pod.spec.containers.resourceswhenever you don't know what a field means.
Commands
kubectl run demo --image=nginx --dry-run=client -o yaml > pod.yaml
kubectl apply -f pod.yaml
kubectl get pod demo -o jsonpath='{.status.phase}{"\n"}'
kubectl describe pod demo
kubectl explain pod.spec.containers.resources
Watch out for
- Cost
- Free on kind.
- Security
- Read every pod.yaml for
privileged,hostPathandrunAsUser: 0before you apply it. - Common mistakes
- Running bare pods in production: use a Deployment so a controller replaces pods that die.
Clean up
kubectl delete pod demo.
Words to know
- Pod phase
- The pod's overall stage: Pending, Running, Succeeded, Failed or Unknown.
- Container state
- Each container's status (Waiting, Running or Terminated) with a reason such as CrashLoopBackOff.
- Probe
- A check the kubelet runs on a container: startup, readiness or liveness.
- Label selector
- How services and deployments find their pods, by matching labels.
Tick off each step and track your progress in the guide.
Open this task in DevOps, Hands On