Sub topic 3
Anatomy of a manifest
This chapter assumes you can read basic YAML, know what a Pod and a Namespace are, and have kubectl pointed at a cluster. It covers the four top-level fields that every Kubernetes manifest uses, and the mistakes that most often break them.
The four top-level fields
A manifest is a YAML description of one object. Almost every manifest has the same four top-level fields.
| Field | Purpose |
|---|---|
| `apiVersion` | The API group and version that defines the object's schema |
| `kind` | The type of object, such as `Namespace` or `Deployment` |
| `metadata` | Identity: name, namespace, labels, annotations |
| `spec` | The desired state you want for the object |
Here is the smallest useful example:
apiVersion: v1
kind: Namespace
metadata:
name: demoThis manifest has no spec. A Namespace needs no configuration beyond its name, so the API server fills in defaults. Most other kinds do need a spec.
apiVersion and kind
kind tells the API server which type of object you are creating. apiVersion tells it which version of that type's schema to use. The two must match, because each kind belongs to exactly one API group.
- Core kinds such as
Namespace,Pod,Service, andConfigMapusev1, with no group name. - Kinds in named groups use
group/version. A Deployment usesapps/v1.
A wrong pairing, such as kind: Deployment with apiVersion: v1, fails with an error like no matches for kind "Deployment" in version "v1". To find the right value, run kubectl api-resources. The APIVERSION column shows the group and version for each kind.
metadata
metadata identifies the object. The fields you will use most often are:
name: required. It must be unique among objects of the same kind in the same namespace.namespace: the namespace the object lives in. If you leave it out,kubectluses the namespace from your current context, which is usuallydefault.labels: key-value pairs used to select and group objects.annotations: key-value pairs for non-identifying information, such as tool settings. You cannot select objects by annotation.
The next chapters cover namespaces and labels in detail. For now, remember that metadata is where an object's identity comes from.
spec and status
spec is your declaration of what you want. Its contents depend entirely on the kind. A Deployment's spec holds replicas and a Pod template. A Service's spec holds a selector and ports.
Objects read back from the cluster also carry a status field. Kubernetes writes status to report what is actually true now. Do not put status in a manifest you write. The control plane ignores it on create and update, and it makes the file misleading.
The cycle works like this: you declare spec, and controllers compare it with the observed status and act to close the gap.
Common errors
- Indentation mistakes. YAML nests by indentation, so a misplaced
specormetadataline moves a field to the wrong level. Use two spaces and never tabs. - Wrong
apiVersionfor the kind. Check it withkubectl api-resources. - Misspelled field names. Strict validation in recent
kubectlversions rejects unknown fields. Older tooling may silently drop them. - Missing
metadata.name. The API server rejects the object.
Exercise
A manifest has apiVersion: v1 and kind: Deployment. Why does it fail, and how do you find the correct apiVersion?
Show answer
Deployment belongs to the apps API group, so it needs apiVersion: apps/v1. Run kubectl api-resources and read the APIVERSION column for the Deployment row.