Sub topic 11
Annotations: non-identifying metadata
This chapter assumes you can read Pod and Deployment manifests, know that every object has a metadata block, and have used basic kubectl commands. You should also know what labels and selectors are from the earlier chapters.
What annotations are
An annotation is a key-value pair in an object's metadata. Like a label, it attaches information to an object. Unlike a label, Kubernetes never uses it to select or group objects. No selector can query annotations.
Annotations hold information for people and tools. Examples are an owner contact, a link to a runbook, a build ID, or a setting that a controller reads.
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.27The labels and annotations sit side by side. The labels (app.kubernetes.io/name, environment, tier) identify the Pod and can be used in selectors. The annotations (example.com/owner, example.com/runbook) only describe it.
Annotation rules
Annotation keys follow the same format as label keys. A key has an optional prefix and a name, separated by /. The prefix is a DNS subdomain such as example.com. Use a prefix you control so your keys do not collide with keys from other tools.
The main difference is in values. A label value is limited to 63 characters and a restricted character set. An annotation value is any string, including JSON, URLs, or multi-line text. The total size of all annotations on one object cannot exceed 256 KiB.
Annotation values must be strings. In YAML, quote values that look like numbers or booleans, such as "true" or "8080". Without quotes, the API server rejects the manifest.
Keys with the prefixes kubernetes.io/ and k8s.io/ are reserved for Kubernetes core components.
Common uses
- Ownership and documentation. Record a team, a contact, or a link to a runbook or dashboard.
- Build and release data. Store a Git commit, a CI build URL, or an image digest.
- Controller configuration. Many tools read annotations to change behavior. An ingress controller can read an annotation to set a timeout. A monitoring system can read one to decide whether to scrape a Pod.
- Change tracking. Kubernetes itself writes annotations. For example,
deployment.kubernetes.io/revisionrecords a Deployment's revision number, andkubectl applystores the last applied configuration in an annotation. - Free-form notes. Record why a change was made, or that an object is being investigated.
Working with annotations in kubectl
Set or change an annotation with kubectl annotate. Without --overwrite, the command refuses to change an existing key.
kubectl annotate pod web-1 example.com/owner=team-checkout --overwrite
kubectl annotate pod web-1 example.com/ticket=PAY-1234
kubectl annotate pod web-1 example.com/ticket-
kubectl get pod web-1 -o jsonpath='{.metadata.annotations}'A trailing - after the key removes it, as it does with labels. The last command prints all annotations on the Pod. kubectl describe pod web-1 also shows them.
Annotations on workloads
On a Deployment, annotations in the Deployment's own metadata describe the Deployment. Annotations under spec.template.metadata are copied to each Pod it creates. Tools that read Pod annotations, such as a metrics scraper, need them in the template.
Changing a Pod template annotation changes the template, so it triggers a rolling update. Changing the Deployment's own annotations does not.
Exercise
A monitoring tool reads the annotation example.com/scrape: "true" from Pods. You add it to the metadata.annotations of your Deployment, but the Pods are not scraped. What is wrong, and what is the effect of fixing it?
Show answer
The annotation is on the Deployment object, not on its Pods. Move it to spec.template.metadata.annotations. Because this changes the Pod template, the Deployment starts a rolling update and the new Pods carry the annotation.
Exercise
Why does example.com/retries: 3 fail in a manifest, and how do you fix it?
Show answer
Annotation values must be strings, and YAML reads an unquoted 3 as a number. Quote it: example.com/retries: "3".