Sub topic 2
Anatomy of a Pod manifest
This chapter assumes you can read YAML, know what a container image is, and have kubectl access to a cluster. You will read a Pod manifest field by field and learn which fields matter first.
The four top-level fields
Every Kubernetes object manifest has the same skeleton. A Pod uses these four fields:
apiVersionnames the API group and version. Pods are in the core group, so the value isv1.kindnames the object type. Here it isPod.metadataidentifies the object: itsname, optionalnamespace, andlabels.specdescribes the desired state: which containers to run and how.
Kubernetes also adds a status field after creation. You never write it yourself. The cluster fills it in to report what is actually happening.
A minimal manifest
apiVersion: v1
kind: Pod
metadata:
name: web
labels:
app: web
spec:
containers:
- name: nginx
image: nginx:1.27
ports:
- containerPort: 80
env:
- name: GREETING
value: helloReading the metadata
metadata.name must be unique within a namespace. If you omit namespace, the Pod goes into the namespace from your current kubectl context, usually default.
labels are key-value pairs you choose. Here app: web lets other objects find this Pod later. Labels get their own chapter, so for now treat them as tags.
Reading the spec
spec.containers is a list, and it is the only required part of the spec. Each entry needs these fields:
| Field | Purpose |
|---|---|
| `name` | Identifies the container inside the Pod. Used by `kubectl logs -c`. |
| `image` | The image to run. Pin a tag such as `1.27` instead of relying on `latest`. |
| `ports` | Lists ports the container listens on. |
| `env` | Sets environment variables as name and value pairs. |
The list items begin with -. The name and image lines belong to the same item, so they must line up under the dash.
Apply and inspect it
Save the manifest as pod.yaml, then run:
kubectl apply -f pod.yaml
kubectl get pod web -o yamlThe second command prints your fields plus extra ones that Kubernetes added, such as status.phase and status.podIP. Comparing the output with your file shows the split between desired state (spec) and observed state (status).
Common errors
- Wrong indentation. A misplaced
imageline gives a validation error such asunknown fieldor a missing required field. - A missing
nameon a container. The API server rejects the Pod. - Most spec changes after creation. A running Pod's container image can be updated in place. Most other
specfields are immutable, so you delete and recreate the Pod. - Quoting values. Environment variable values must be strings. Write
value: "8080", notvalue: 8080.
Exercise
In the manifest above, which field would you change to run a different version of nginx, and which top-level field is filled in by the cluster instead of by you?
Show answer
Change spec.containers[0].image, for example to another tag. The status field is written by the cluster, not by you.
Exercise
Why does env fail validation when you write value: 8080?
Show answer
Environment variable values must be strings. YAML reads 8080 as an integer, so you must quote it as "8080".