Sub topic 3
Valid label keys and values
This chapter assumes you can read a Pod manifest and run kubectl apply and kubectl get. Labels are plain strings, but the API server rejects any key or value that breaks its syntax rules. The rules are short, and knowing them saves you from confusing validation errors.
Anatomy of a label key
A label key has two parts separated by a slash. The prefix is optional.
example.com/tier
└─prefix──┘ └name┘The name is required:
- It is at most 63 characters.
- It begins and ends with a letter or digit.
- Between those characters it may contain dashes (
-), underscores (_), dots (.), letters, and digits.
The prefix, if present, must be a DNS subdomain. That means lowercase letters, digits, dashes, and dots, with dot-separated labels. It is at most 253 characters. If you omit the prefix, the key is treated as private to the user.
Rules for label values
A value follows the same rules as a name, with one difference. It may be empty.
- It is at most 63 characters.
- If it is not empty, it begins and ends with a letter or digit.
- Inside, it may contain dashes, underscores, and dots.
- It cannot contain spaces, slashes, colons, or commas.
An empty value is useful together with the exists selector, which you will meet later. The label canary: "" marks an object without saying anything more about it.
Reserved prefixes
The prefixes kubernetes.io/ and k8s.io/ are reserved for Kubernetes core components. Do not invent keys such as kubernetes.io/team. Use a domain you control, such as example.com/team. The recommended app.kubernetes.io/ labels are the documented exception.
Examples
apiVersion: v1
kind: Pod
metadata:
name: label-demo
labels:
app.kubernetes.io/name: web # valid: prefix plus name
example.com/release-track: "2024.11_a"
team: payments
canary: "" # valid: empty value
version: "1" # quoted so YAML keeps it a string
enabled: "true" # quoted for the same reason
spec:
containers:
- name: nginx
image: nginx:1.27These would be rejected:
| Label | Why it fails |
|---|---|
| `-team: a` | The key name starts with a dash. |
| `team: payments team` | The value contains a space. |
| `team: payments-` | The value ends with a dash. |
| `Example.com/team: a` | The prefix has an uppercase letter. |
| `owner: [email protected]` | The value contains `@`. |
The YAML quoting trap
Label values are always strings. YAML does not know that. An unquoted version: 1 is an integer, and enabled: true is a boolean. The API server refuses both, and the error mentions a cannot-unmarshal problem instead of label syntax.
You can check a value before you apply a manifest by trying it on a throwaway object:
kubectl run label-test --image=nginx:1.27 --labels="team=payments,version=1"
kubectl label pod label-test '[email protected]'
kubectl delete pod label-testThe first command succeeds, because kubectl sends 1 as a string. The second fails validation because of the @. When a value does not fit, the usual fix is to encode the data differently. For example, use alice instead of an email address. Alternatively, move the data to an annotation, which accepts nearly any text.
Exercise
Which of these labels is valid, and why do the others fail? (a) Team/name: ops (b) example.com/tier: front end (c) example.com/tier: frontend (d) _tier: web
Show answer
Only (c) is valid. (a) fails because the prefix Team has an uppercase letter and is not a DNS subdomain. (b) fails because the value contains a space. (d) fails because the key name must start with a letter or digit, and _ is not one.
Exercise
A manifest contains labels: { release: 2 } and the API server rejects it. What is wrong, and how do you fix it?
Show answer
YAML parses 2 as an integer, but label values must be strings. Quote it: release: "2".