Sub topic 5
Set-based selectors: in, notin, exists
This chapter assumes you can read Pod manifests, know that labels are key-value pairs on objects, and have used kubectl get -l with an equality selector such as tier=frontend.
Why equality is not enough
An equality selector compares one key to one value. It cannot say "environment is production or staging" without running two queries. It also cannot test whether a key exists at all. Set-based selectors cover those cases.
The four forms
| Form | Meaning | Matches an object with no such key? |
|---|---|---|
| `key in (a,b)` | The key exists and its value is `a` or `b` | No |
| `key notin (a,b)` | The key is absent, or its value is not `a` or `b` | Yes |
| `key` | The key exists, with any value | No |
| `!key` | The key does not exist | Yes |
The notin row catches people out. tier notin (backend) returns Pods that have no tier label, because "not in the set" is true when there is no value. If you want only Pods that have a tier label with another value, combine the two: tier,tier notin (backend).
Using set-based selectors in kubectl
Quote the whole expression. Shells treat parentheses and ! specially.
kubectl get pods -l 'environment in (production,staging)'
kubectl get pods -l 'tier notin (backend)'
kubectl get pods -l 'tier'
kubectl get pods -l '!canary'
kubectl get pods -l 'environment in (production,staging),tier notin (backend),!canary'A comma between requirements means AND. There is no OR operator between requirements. Use a value list inside in when you need OR on a single key. You can mix both styles in one selector, as in app=web,tier in (frontend,edge).
Using them in manifests
Manifests do not accept the text syntax. Workload selectors use matchExpressions, a list of objects with key, operator, and values. The operators are In, NotIn, Exists, and DoesNotExist.
apiVersion: apps/v1
kind: Deployment
metadata:
name: web
spec:
replicas: 3
selector:
matchLabels:
app: web
matchExpressions:
- key: environment
operator: In
values: [production, staging]
- key: canary
operator: DoesNotExist
template:
metadata:
labels:
app: web
environment: production
spec:
containers:
- name: nginx
image: nginx:1.27Every entry in matchLabels and matchExpressions must be true at once, so they are ANDed. The Pod template labels must satisfy the whole selector. Here the template has app: web, has environment: production, and has no canary key. If any of these were wrong, the API server would reject the Deployment.
Where set-based selectors are not available
A Service's spec.selector is a plain key-value map. It supports only equality, ANDed together. Set-based matching works in kubectl -l and in the selector field of Deployments, ReplicaSets, StatefulSets, DaemonSets, and Jobs. A nodeSelector is also a plain map.
Common mistakes
- Forgetting quotes, so the shell reports a syntax error or expands
!. - Expecting
notinor!=to skip objects that lack the key. - Writing
valuesfor anExistsoperator. - Putting a space-separated list inside the parentheses instead of a comma-separated one.
Exercise
You run kubectl get pods -l 'tier notin (backend)' and the output includes a Pod with no tier label. Why, and what selector returns only Pods that have a tier label whose value is not backend?
Show answer
notin is true when the key is absent as well as when the value is outside the set, so unlabeled Pods match. Add an existence requirement: kubectl get pods -l 'tier,tier notin (backend)'.
Exercise
Write a matchExpressions entry that selects objects whose environment label is production or staging.
Show answer
- key: environment, operator: In, values: [production, staging]. In full, the entry is a list item with the fields key: environment, operator: In, and values: [production, staging].