Sub topic 13
Labels or annotations: how to choose
This chapter assumes you can read Pod, Deployment, and Service manifests, and that you have used kubectl get and kubectl label. It gives you a short test for deciding where a piece of metadata belongs.
The deciding question
Labels and annotations are both key-value maps under metadata. The difference is what reads them. Ask one question: will anything need to select objects by this value?
- If yes, use a label. Selectors in kubectl, Services, Deployments, and
nodeSelectorread only labels. - If no, use an annotation. Annotations cannot be selected on, so they can hold values that labels do not allow.
Practical differences
| Property | Labels | Annotations |
|---|---|---|
| Used for selection | Yes | No |
| Value content | At most 63 characters, restricted character set | Any string, such as JSON or a URL |
| Size limit | Small per value | Total of all annotations on an object limited to 256 KiB |
| Typical readers | Controllers, Services, kubectl | Tools, operators, people |
| Changing the value | Can change which objects are selected | No effect on selection |
The last row matters most in practice. Editing a label can detach a Pod from a Service or from its ReplicaSet. Editing an annotation never changes which objects a selector matches.
Worked example
Take the Pod from the earlier chapter. Consider each field in turn.
apiVersion: v1
kind: Pod
metadata:
name: web-1
labels:
app.kubernetes.io/name: web
environment: production
annotations:
example.com/owner: "team-payments"
example.com/runbook: "https://wiki.example.com/runbooks/web"
example.com/last-deploy: '{"commit":"a1b2c3d","by":"ci"}'
spec:
containers:
- name: nginx
image: nginx:1.27app.kubernetes.io/nameis a label because a Service selector uses it.environmentis a label because you will runkubectl get pods -l environment=production.example.com/ownercould be either. If you never query by team, an annotation is enough. If you wantkubectl get pods -l team=payments, make it a label.- The runbook URL contains characters that labels forbid, such as
:and/in the value. It must be an annotation. - The deploy record is JSON. It exceeds what a label value can express, so it is an annotation.
Common mistakes
- Putting a long or structured value in a label. The API server rejects values over 63 characters or with invalid characters.
- Putting a selection key in an annotation. A Service with
selector: {tier: frontend}ignorestierif you set it as an annotation, and the Service gets no endpoints. - Labeling everything "just in case." Each label is part of the object's identity. Labels you never select on add noise, and changing them later can have side effects.
- Using annotations as a small database. They are for hints and records, not for data that applications must read at high volume.
A rule of thumb
Start with an annotation. Promote the value to a label when you find yourself wanting to filter or group by it. Promoting is cheap on a new object. On a running workload, remember that a Deployment's spec.selector cannot be changed after creation, so labels that the selector depends on need to be chosen with care from the start.
Exercise
A team wants to record the URL of a Grafana dashboard on each Pod, and also wants to list all Pods owned by team "checkout" with one kubectl command. Which piece of metadata should be a label, which an annotation, and why?
Show answer
The team owner should be a label, for example team: checkout, because kubectl get pods -l team=checkout selects on labels. The dashboard URL should be an annotation, because it is not used for selection and its value contains characters (: and /) that label values do not allow.