Sub topic 8
Selectors in Deployments and other workloads
This chapter assumes you know what Pods, Deployments, and Services are, can read YAML manifests, and have met label selectors in kubectl -l. Here you will see how workload controllers use selectors to decide which Pods they own.
Why workloads need a selector
A Deployment does not keep a list of its Pods. It manages a ReplicaSet, and the ReplicaSet counts the Pods whose labels match its selector. If the count is too low, it creates Pods. If the count is too high, it deletes Pods. The selector is the only link between a controller and its Pods.
ReplicaSet, StatefulSet, and DaemonSet work the same way. Jobs also use a selector, but Kubernetes generates it for you.
Writing the selector
In the apps/v1 API, the spec.selector field is required. It has two parts, and you can use either or both:
matchLabelsis a map of exact key-value pairs.matchExpressionsis a list of rules. Each rule has akey, anoperator, and usuallyvalues.
The operators are In, NotIn, Exists, and DoesNotExist. They are the set-based selectors from earlier, written as YAML. Exists and DoesNotExist must not have values. When you use several entries, all of them must match. The conditions are combined with AND.
apiVersion: apps/v1
kind: Deployment
metadata:
name: web
spec:
replicas: 3
selector:
matchLabels:
app: web
matchExpressions:
- key: tier
operator: In
values: [frontend]
template:
metadata:
labels:
app: web
tier: frontend
spec:
containers:
- name: nginx
image: nginx:1.27
---
apiVersion: v1
kind: Service
metadata:
name: web
spec:
selector:
app: web
ports:
- port: 80
targetPort: 80The Deployment selects Pods with app=web and tier equal to frontend. The Pod template carries both labels, so the match succeeds. The Service below it uses a plain map, because Service selectors support equality only.
Rules and common errors
- The selector must match the template labels. If it does not, the API server rejects the Deployment with a validation error. The template's labels may include extra labels, as
tiercould be here, but every selector condition must be satisfied. - The selector cannot be changed after creation. In
apps/v1,spec.selectoris immutable. To change it, delete and recreate the Deployment. - Selectors are not scoped to one controller. Two Deployments in the same namespace with overlapping selectors can compete for the same Pods, and the result is unpredictable. Give each workload a unique label, such as
app.kubernetes.io/name. - Relabeling a Pod detaches it. Remove or change a label that the selector needs, and the ReplicaSet stops counting that Pod. It creates a replacement. The old Pod keeps running, which makes this a handy way to take a Pod out for debugging.
- A matching bare Pod can be adopted. A Pod with no owner that matches a ReplicaSet's selector can become part of that ReplicaSet.
To see which Pods a controller selects, run kubectl get deployment web -o jsonpath='{.spec.selector}'. Then pass the same labels to kubectl get pods -l.
Exercise
A Deployment has selector.matchLabels: {app: api}, but its Pod template has the label app: backend. What happens when you apply it, and how do you fix it?
Show answer
The API server rejects it, because the selector does not match the template labels. Make the two agree. For example, change the template label to app: api (or change the selector to app: backend). Both must also match what any Service selector expects.