How can the OR selector be used with labels in Kubernetes?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Kubernetes label selectors support OR logic within a single label key using set-based selectors with the In operator. For example, app in (frontend, backend) matches resources where the app label is either frontend or backend. However, there is no native OR across different label keys — multiple selector conditions are always AND-ed together. To achieve cross-key OR logic, you must use multiple queries and merge the results, or use a workaround like adding a grouping label. This article covers how to use set-based selectors for OR behavior and the workarounds for cases the native syntax cannot handle.
Label Selector Types
Kubernetes has two types of selectors:
Equality-Based Selectors
Set-Based Selectors
The In operator is how you achieve OR logic for a single label key.
OR Within a Single Key
kubectl Examples
In YAML Manifests (set-based)
NetworkPolicy Example
Combining AND and OR
Multiple selector expressions are AND-ed together. Each expression can have OR logic within it using In:
This selects pods where env is (staging OR production) AND tier is (frontend OR api) AND version is NOT deprecated.
Cross-Key OR (Workarounds)
Kubernetes does not support OR across different keys natively. For example, you cannot directly say "app=frontend OR tier=web" in a single selector.
Workaround 1: Multiple Queries
Or using a single command:
Workaround 2: Add a Grouping Label
Add a common label to all resources that should match your OR condition:
Workaround 3: Use the Kubernetes API Directly
Operator Reference
| Operator | Meaning | Example |
In | Value is in set (OR) | app in (a, b, c) |
NotIn | Value is not in set | env notin (test) |
Exists | Key exists (any value) | key: app, operator: Exists |
DoesNotExist | Key does not exist | key: legacy, operator: DoesNotExist |
Common Pitfalls
- Expecting OR across different label keys: Multiple selector conditions are always AND-ed.
kubectl get pods -l 'app=frontend,tier=web'means app=frontend AND tier=web, not OR. UseInwithin a single key for OR logic, or run separate queries. - Using set-based selectors in Service
spec.selector: The Servicespec.selectorfield only supports equality-based matching (matchLabels). Set-based selectors (matchExpressionswithIn) are supported in Deployments, ReplicaSets, Jobs, DaemonSets, and NetworkPolicies, but not in Services. - Forgetting quotes around set-based selectors in kubectl:
kubectl get pods -l app in (frontend, backend)fails because the shell interprets parentheses. Always quote the selector:kubectl get pods -l 'app in (frontend, backend)'. - Mismatching selector and template labels: In a Deployment,
spec.selector.matchExpressionsmust match the labels inspec.template.metadata.labels. If the template labels do not satisfy the selector, the Deployment fails to create pods. - Assuming label changes propagate to selectors: Changing a pod's labels after creation causes it to fall out of any Deployment or Service selector that no longer matches. The controller may create a new pod to replace it, leaving the relabeled pod orphaned.
Summary
- Use
Inoperator for OR logic within a single label key:app in (frontend, backend) - Multiple selector conditions are always AND-ed — there is no native cross-key OR
- Set-based selectors (
matchExpressions) supportIn,NotIn,Exists, andDoesNotExist - For cross-key OR logic, use multiple queries and merge results, or add a common grouping label
- Quote set-based selectors in kubectl commands to prevent shell interpretation of parentheses

