Sub topic 5
Labels and selectors
This chapter assumes you can read a Pod manifest and run basic kubectl commands. Both were covered in earlier chapters.
What labels are
A label is a key/value pair attached to an object's metadata. Kubernetes does not interpret labels. You use them to group objects, and other objects use them to find the group.
Names are unique within a namespace, but labels are not. Many Pods can carry app: web, and one Pod can carry many labels.
Label rules:
- A key has an optional DNS-style prefix and a name, such as
example.com/teamorapp. - The name and the value are at most 63 characters.
- They must start and end with an alphanumeric character. In between you can use
-,_, and.. - A value can be empty.
Use annotations instead for data that is not used for selection, such as build URLs or long descriptions.
apiVersion: v1
kind: Pod
metadata:
name: web-blue
labels:
app: web
tier: frontend
env: prod
spec:
containers:
- name: nginx
image: nginx:1.27Selecting with kubectl
A selector is a query over labels. Equality-based selectors compare a key to a value. Set-based selectors test membership.
kubectl apply -f pod.yaml
kubectl get pods --show-labels
kubectl get pods -l app=web
kubectl get pods -l 'app=web,env!=dev'
kubectl get pods -l 'env in (prod,staging)'
kubectl get pods -l '!tier'
kubectl label pod web-blue env=staging --overwrite
kubectl label pod web-blue tier-Commas combine requirements with AND. There is no OR operator, so use in for alternatives. A bare key such as tier matches objects that have the label, whatever its value. A leading ! matches objects that lack it. The last two commands show that you can change labels on a live Pod. --overwrite replaces an existing value, and a trailing - removes a label.
Where selectors are used
Other objects store selectors and re-evaluate them continuously. A Service sends traffic to the Pods that match its selector. A Deployment or ReplicaSet counts the Pods that match its selector. A nodeSelector in a Pod spec chooses nodes by node labels.
apiVersion: v1
kind: Service
metadata:
name: web
spec:
selector:
app: web
ports:
- port: 80
targetPort: 80Common errors
- A Service has no endpoints. The selector does not match the Pod labels. Check for typos and case differences, since keys and values are case-sensitive. Compare
kubectl get pods --show-labelswith the Service'sspec.selector. - Quoting problems. Quote selectors that contain
!or parentheses, or your shell will interpret them. - Overlapping selectors. Two controllers that select the same Pods can fight over them. Give each workload a unique label, such as
app.kubernetes.io/name. - Missing labels in a manifest. A Pod without labels cannot be selected by any label query.
Exercise
A Pod has labels app: web and env: prod. Which of these selectors match it: app=web,env=dev, env in (prod,staging), !env, app?
Show answer
Two match: env in (prod,staging) and app. app=web,env=dev fails because env is prod. !env fails because the Pod has an env label.