kubectl
Kubernetes
resource management
querying labels
OR operator

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)

bash
1# AND: resources with BOTH app=web AND env=prod
2kubectl get pods -l app=web,env=prod
3
4# This is equivalent to:
5# app=web AND env=prod

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:

bash
1# OR: pods where env is "staging" OR "production"
2kubectl get pods -l 'env in (staging, production)'
3
4# OR: pods where app is "web" OR "api" OR "worker"
5kubectl get pods -l 'app in (web, api, worker)'
6
7# Combine with AND: app is web or api, AND tier=frontend
8kubectl get pods -l 'app in (web, api),tier=frontend'

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:

bash
1# Get pods with app=web OR tier=frontend (different keys)
2# Option 1: Two separate commands
3kubectl get pods -l app=web
4kubectl get pods -l tier=frontend
5
6# Option 2: Combine output and deduplicate
7kubectl get pods -l app=web -o name; kubectl get pods -l tier=frontend -o name | sort -u
8
9# Option 3: Use --output with jq for clean results
10{ kubectl get pods -l app=web -o json; kubectl get pods -l tier=frontend -o json; } | \
11  jq -s '.[0].items + .[1].items | unique_by(.metadata.name) | .[].metadata.name'

Negation with notin and !=

bash
1# Pods where env is NOT production
2kubectl get pods -l 'env notin (production)'
3
4# Equivalent
5kubectl get pods -l 'env!=production'
6
7# Pods where env is NOT staging AND NOT production
8kubectl get pods -l 'env notin (staging, production)'

Existence and Non-Existence Selectors

bash
1# Pods that HAVE the "app" label (any value)
2kubectl get pods -l app
3
4# Pods that DO NOT have the "app" label
5kubectl get pods -l '!app'
6
7# Pods with "app" label AND env=prod
8kubectl get pods -l 'app,env=prod'

Practical Examples

Select All Non-System Pods

bash
# Pods NOT in kube-system related apps
kubectl get pods --all-namespaces -l 'app notin (kube-dns, coredns, kube-proxy)'

Select Pods by Multiple Environments

bash
# All pods in dev, staging, or canary
kubectl get pods -l 'env in (dev, staging, canary)'

Select by Version Range

bash
# Pods running version v1 or v2
kubectl get pods -l 'version in (v1, v2)'

Delete by Label

bash
1# Delete all pods matching a label selector
2kubectl delete pods -l 'env in (dev, test)'
3
4# Delete pods with specific app label
5kubectl delete pods -l app=temp-worker

Apply to Other Resource Types

bash
1# Services with specific labels
2kubectl get services -l 'tier in (frontend, backend)'
3
4# Nodes with specific roles
5kubectl get nodes -l 'node-role.kubernetes.io/worker'
6
7# Deployments matching app label
8kubectl get deployments -l 'app in (web, api)'

Using Labels in YAML Manifests

Label selectors in Kubernetes resources use the same syntax:

yaml
1# Service selecting pods with OR on version
2apiVersion: v1
3kind: Service
4metadata:
5  name: my-service
6spec:
7  selector:
8    app: web        # Only equality-based selectors allowed in Service
9  ports:
10    - port: 80
11---
12# NetworkPolicy with set-based selectors
13apiVersion: networking.k8s.io/v1
14kind: NetworkPolicy
15metadata:
16  name: allow-web-and-api
17spec:
18  podSelector:
19    matchExpressions:
20      - key: app
21        operator: In
22        values: ["web", "api"]   # OR: app=web OR app=api
23      - key: env
24        operator: NotIn
25        values: ["test"]         # AND: env != test

In Deployments and ReplicaSets

yaml
1apiVersion: apps/v1
2kind: Deployment
3metadata:
4  name: web-deployment
5spec:
6  selector:
7    matchLabels:
8      app: web                    # Equality-based
9    matchExpressions:
10      - key: version
11        operator: In
12        values: ["v1", "v2"]     # Set-based OR
13  template:
14    metadata:
15      labels:
16        app: web
17        version: v1

Label Selector Summary Table

SelectorSyntaxExample
Equalitykey=valueapp=web
Inequalitykey!=valueenv!=test
Set membership (OR)key in (v1,v2)env in (dev,staging)
Set exclusionkey notin (v1,v2)env notin (prod)
Existskeyapp
Does not exist!key!temporary
AND, (comma)app=web,env=prod

Common Pitfalls

  • Expecting OR with comma: kubectl get pods -l app=web,app=api does NOT mean OR. It means app=web AND app=api, which matches nothing (a label cannot have two values). Use app in (web, api) for OR.
  • Quoting in expressions in bash: The parentheses in in (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 Service objects only accept matchLabels (equality-based), not matchExpressions (set-based). You cannot use in/notin in a Service selector. Use a separate Service per label value or restructure labels.
  • Spaces in in expressions: in(val1,val2) without a space after in may fail. Use in (val1, val2) with spaces for reliability.
  • Label key naming: Label keys follow [prefix/]name format. The name segment must be 63 characters or fewer. Keys with slashes like app.kubernetes.io/name must 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 kubectl commands
  • Use notin for exclusion and !key for non-existence checks
  • Quote selectors containing parentheses to prevent shell expansion
  • In YAML manifests, use matchExpressions with operator: In for set-based OR selectors

Course illustration
Course illustration

All Rights Reserved.