kubectl get resources by label with OR operator
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 AND logic natively (comma-separated requirements), but do not support an OR operator directly. To select resources matching label A or label B, you use the set-based in operator: -l 'key in (value1, value2)'. For OR across different keys, you must run multiple kubectl commands or use field selectors in combination. Understanding the distinction between equality-based and set-based selectors is key to effective label querying.
AND Logic (Native — Comma Separated)
Comma-separated label selectors are always AND. Every condition must match.
OR Logic with in Operator (Same Key, Different Values)
The in operator matches resources where a key has any of the listed values:
The in operator provides OR semantics for a single key.
OR Across Different Keys
There is no native way to express "label A=x OR label B=y" in a single selector. You need multiple queries:
Negation with notin and !=
Existence and Non-Existence Selectors
Practical Examples
Select All Non-System Pods
Select Pods by Multiple Environments
Select by Version Range
Delete by Label
Apply to Other Resource Types
Using Labels in YAML Manifests
Label selectors in Kubernetes resources use the same syntax:
In Deployments and ReplicaSets
Label Selector Summary Table
| Selector | Syntax | Example |
| Equality | key=value | app=web |
| Inequality | key!=value | env!=test |
| Set membership (OR) | key in (v1,v2) | env in (dev,staging) |
| Set exclusion | key notin (v1,v2) | env notin (prod) |
| Exists | key | app |
| Does not exist | !key | !temporary |
| AND | , (comma) | app=web,env=prod |
Common Pitfalls
- Expecting OR with comma:
kubectl get pods -l app=web,app=apidoes NOT mean OR. It means app=web AND app=api, which matches nothing (a label cannot have two values). Useapp in (web, api)for OR. - Quoting
inexpressions in bash: The parentheses inin (val1, val2)must be quoted to prevent shell interpretation. Always wrap in single quotes:-l 'app in (web, api)'. Without quotes, bash tries to expand the parentheses as a subshell. - Service selectors only support equality: Kubernetes
Serviceobjects only acceptmatchLabels(equality-based), notmatchExpressions(set-based). You cannot usein/notinin a Service selector. Use a separate Service per label value or restructure labels. - Spaces in
inexpressions:in(val1,val2)without a space afterinmay fail. Usein (val1, val2)with spaces for reliability. - Label key naming: Label keys follow
[prefix/]nameformat. The name segment must be 63 characters or fewer. Keys with slashes likeapp.kubernetes.io/namemust be quoted in selectors:-l 'app.kubernetes.io/name=myapp'.
Summary
- Use
in (value1, value2)for OR logic on the same label key - Comma-separated selectors are always AND, not OR
- OR across different keys requires multiple
kubectlcommands - Use
notinfor exclusion and!keyfor non-existence checks - Quote selectors containing parentheses to prevent shell expansion
- In YAML manifests, use
matchExpressionswithoperator: Infor set-based OR selectors

