Sub topic 4
Equality-based selectors
This chapter assumes you can read Pod manifests, know what a Pod, Deployment, and Service are, and can run basic kubectl get commands. It also assumes you know that labels are key-value pairs in metadata.labels.
What an equality-based selector does
A label selector is a query over labels. An equality-based selector compares a label's value to a fixed value. It supports three operators:
| Operator | Meaning | Example |
|---|---|---|
| `=` | The label equals the value | `environment=production` |
| `==` | Same as `=` | `environment==production` |
| `!=` | The label is not equal to the value, or is missing | `tier!=backend` |
Several requirements can be joined with commas. A comma acts as a logical AND, so an object must satisfy every requirement. There is no OR operator in this syntax. For OR logic you need set-based selectors, which the next chapter covers.
Using selectors with kubectl
The -l (or --selector) flag filters kubectl get output.
kubectl get pods -l environment=production
kubectl get pods -l environment=production,tier=frontend
kubectl get pods -l 'tier!=backend'
kubectl get pods -l environment=production,tier!=frontend --show-labelsQuote the selector when it contains !. Many shells treat ! as a history character, and quoting avoids that.
The last command returns Pods labeled environment=production whose tier label is anything other than frontend. That includes Pods with no tier label at all.
How matching works
The selector is evaluated against each object's labels.
Take three Pods:
| Pod | Labels |
|---|---|
| web-1 | `environment=production`, `tier=frontend` |
| api-1 | `environment=production`, `tier=backend` |
| job-1 | `environment=production` |
The selector environment=production,tier!=backend selects web-1 and job-1. It rejects api-1.
Where equality-based selectors appear
kubectl is one place. The same idea is used inside manifests:
- A Service's
spec.selectoris a plain map of keys to values. Every entry must match, so it behaves like=requirements joined by commas. - A Deployment's
spec.selector.matchLabelsis also a map of exact matches. - A Pod's
spec.nodeSelectoris a map that must match node labels.
Here is a Service that selects Pods with both app=web and tier=frontend:
apiVersion: v1
kind: Service
metadata:
name: web
spec:
selector:
app: web
tier: frontend
ports:
- port: 80
targetPort: 80A Pod needs both labels to receive traffic. A Pod with only app: web is ignored. Extra labels on the Pod are fine, because a selector only constrains the keys it names.
Common errors
- Typos in keys or values. Labels are case-sensitive, so
App=webdoes not matchapp=web. - Wrong operator for the goal. Using
!=and expecting it to exclude unlabeled objects. - Unquoted special characters. The shell may rewrite the selector before kubectl sees it.
- Selecting nothing silently. A selector that matches no objects is valid. kubectl prints
No resources foundand does not report an error. - Spaces around operators in manifests. In kubectl, write
-l environment=production,tier=frontendwithout spaces after commas.
Exercise
Pods a (labels app=web, env=prod), b (labels app=web), and c (labels app=api, env=prod) exist. Which Pods does kubectl get pods -l app=web,env!=staging return?
Show answer
Pods a and b. Both have app=web. Pod a has env=prod, which is not staging. Pod b has no env label, and != matches missing keys. Pod c fails because app=api.