Sub topic 1
Where labels and annotations live: object metadata
Prerequisites
This chapter assumes you can read simple YAML manifests and run basic kubectl commands. You should also know what a Pod, a Deployment, and a Service are. You do not need to know anything about labels or selectors yet. This chapter introduces where they are stored. Later chapters cover how to use them.
The metadata block
Every Kubernetes object has a metadata field. It holds the data that identifies the object and describes it to the cluster and to people. The spec field says what you want the object to do. The metadata field says what the object is.
Labels and annotations are two maps inside metadata. Each map holds string keys and string values.
| Field | Purpose | Used by selectors |
|---|---|---|
| `name` | Identifies the object within its namespace and kind | No |
| `namespace` | Scopes the object to a group inside the cluster | No |
| `uid` | Unique ID assigned by the cluster | No |
| `labels` | Identifying key-value pairs | Yes |
| `annotations` | Non-identifying key-value pairs | No |
Some metadata fields are set by you, such as name, labels, and annotations. Others are filled in by the API server, such as uid, creationTimestamp, and resourceVersion. You do not write the server-set fields in a manifest.
A Pod with both maps
This Pod carries three labels and two annotations.
apiVersion: v1
kind: Pod
metadata:
name: web-1
labels:
app.kubernetes.io/name: web
environment: production
tier: frontend
annotations:
example.com/owner: "team-payments"
example.com/runbook: "https://wiki.example.com/runbooks/web"
spec:
containers:
- name: nginx
image: nginx:1.27Both maps sit under metadata, at the same level as name. A common mistake is to put labels under spec. The API server rejects an unknown field there when validation is on.
Quote values that YAML could read as another type. Label and annotation values must be strings, so "true" and "3" need quotes. An unquoted true or 3 is parsed as a boolean or a number, and the API server rejects it.
Reading metadata from a live object
Use kubectl get with an output format to see the stored metadata.
kubectl get pod web-1 -o jsonpath='{.metadata.labels}'
kubectl get pod web-1 -o jsonpath='{.metadata.annotations}'
kubectl get pod web-1 -o yamlThe YAML output also shows the fields the server added. You will see uid, creationTimestamp, and often ownerReferences and managedFields. Ignore these when you write your own manifests.
Metadata on Pod templates
Controllers such as Deployments create Pods from a template. The template has its own metadata, separate from the Deployment's.
Instead of a picture, think of two levels. The Deployment has metadata.labels. Its Pod template has spec.template.metadata.labels. The Pods created from the template get the template's labels, not the Deployment's.
This distinction causes many selector bugs, so keep it in mind. Labels on the Deployment describe the Deployment. Labels in the template describe the Pods.
Why two maps
Labels exist so that the system can select groups of objects. Annotations exist to attach extra information that nothing selects on. Keeping them apart lets Kubernetes index and filter labels efficiently. It also lets annotations hold larger values, such as URLs or JSON, without affecting selection. Later chapters cover the exact rules.
Exercise
A teammate adds labels: under spec: in a Pod manifest, and the labels do not appear on the Pod. Where should the field go, and what is the difference between the Deployment's metadata.labels and spec.template.metadata.labels?
Show answer
labels belongs under metadata, at the same level as name. In a Deployment, metadata.labels describes the Deployment object itself. spec.template.metadata.labels is copied onto each Pod the Deployment creates, so it is the one that Pods are selected by.
Exercise
Which command prints only the annotations of the Pod web-1 as stored in the cluster?
Show answer
kubectl get pod web-1 -o jsonpath='{.metadata.annotations}'