Sub topic 12
Common uses of annotations
This chapter assumes you know how to write a Pod manifest, how kubectl apply works, and what labels and selectors do. Annotations are key-value pairs in metadata.annotations. Kubernetes never uses them to select objects. Instead, tools, controllers, and people read them.
Three kinds of annotation
Most annotations you meet fall into one of three groups.
| Group | Who reads it | Example |
|---|---|---|
| Descriptive notes | People and dashboards | `example.com/owner: "team-payments"` |
| Tool configuration | A controller or add-on | `nginx.ingress.kubernetes.io/rewrite-target: /` |
| Tool bookkeeping | Kubernetes or `kubectl` | `kubectl.kubernetes.io/last-applied-configuration` |
Descriptive notes
Use these for facts that help an engineer who finds the object at 3 a.m. Good examples are an owner, a runbook link, a build ID, or a Git commit. Values can be long and can contain characters that labels reject, such as URLs and spaces.
apiVersion: v1
kind: Pod
metadata:
name: web-1
annotations:
example.com/owner: "team-payments"
example.com/runbook: "https://wiki.example.com/runbooks/web"
example.com/git-commit: "9f3c2ab"
spec:
containers:
- name: nginx
image: nginx:1.27Tool configuration
Many add-ons read annotations on the objects they manage. An ingress controller reads annotations on an Ingress. A cloud controller reads annotations on a Service to decide which kind of load balancer to create. Prometheus setups often read annotations to find scrape targets. The keys and values are defined by the tool, so check its documentation. A misspelled key is not an error. The tool ignores it, and the object looks fine.
Tool bookkeeping
Kubernetes and kubectl write some annotations themselves. For example, kubectl apply stores the last applied configuration in an annotation, and Deployments record deployment.kubernetes.io/revision. Treat these as read-only unless you know what you are doing. Keys with the kubernetes.io/ or k8s.io/ prefix are reserved for Kubernetes components.
Working with annotations
Add or change an annotation with kubectl annotate. Use --overwrite to replace an existing value, and a trailing dash to remove one.
kubectl annotate pod web-1 example.com/owner=team-checkout --overwrite
kubectl annotate pod web-1 example.com/build-id=1842
kubectl annotate pod web-1 example.com/build-id-
kubectl get pod web-1 -o jsonpath='{.metadata.annotations}'To read a single key whose name contains dots, escape each dot with a backslash:
kubectl get pod web-1 -o jsonpath='{.metadata.annotations.example\.com/owner}'You cannot filter with -l on annotations. If you need to find objects by a value, it should probably be a label instead. The next chapter covers that decision.
Annotations and rollouts
Changing an annotation on a Deployment's own metadata does not restart its Pods. Changing one under spec.template.metadata.annotations does, because that edits the Pod template. Teams use this on purpose to force a rolling restart, which is what kubectl rollout restart does by adding a kubectl.kubernetes.io/restartedAt annotation to the template.
Common errors
- Non-string values. Annotation values must be strings. Write
"true"and"30", nottrueand30, or the API server rejects the manifest. - Missing prefix. Unprefixed keys look like they belong to the user. Use a domain prefix such as
example.com/to avoid clashes with tools. - Wrong object. An ingress annotation placed on the Service does nothing. Check which object the tool reads.
Exercise
You add example.com/release: "2024-06" to the metadata of a Deployment, not its Pod template. Do the Pods restart, and can you find the Deployment with kubectl get deploy -l example.com/release=2024-06?
Show answer
The Pods do not restart, because the Pod template is unchanged. The -l query returns nothing, because label selectors only match labels and this is an annotation.
Exercise
Why does kubectl annotate pod web-1 example.com/ready=true fail, and how do you fix it?
Show answer
The value true is parsed as a boolean, but annotation values must be strings. Quote it so the value is a string, for example example.com/ready="true" in a manifest. On the command line, shell quoting can leave true as the literal text, which the CLI sends as a string.