Kubernetes label-based selection mechanisms
labels
selector
matchLabels
matchExpressions
nodeSelector
nodeName
- and more...
All explained with real examples and practical use cases for each.
🧠 Key Concepts & Definitions
Concept | Defined On | Purpose |
---|---|---|
labels | Pods, Nodes, etc. | Key-value tags used for grouping and selection |
selector | Services, ReplicaSets, Deployments | To select pods using labels |
matchLabels | Pod affinity, selectors | Exact match key-value pairs |
matchExpressions | More advanced logic for selectors | Use In , NotIn , Exists , DoesNotExist |
nodeSelector | Pod spec | Assign pods to specific node(s) by node labels |
nodeName | Pod spec | Hard assign pod to specific node name |
🔖 1. Labels
Use case: Add metadata to resources for grouping and filtering.
apiVersion: v1
kind: Pod
metadata:
name: myapp-pod
labels:
app: myapp
tier: frontend
environment: production
spec:
containers:
- name: nginx
image: nginx
🔎 2. Selector (Service selecting Pods)
Use case: Route traffic to pods with certain labels.
apiVersion: v1
kind: Service
metadata:
name: frontend-service
spec:
selector:
app: myapp
tier: frontend
ports:
- protocol: TCP
port: 80
targetPort: 80
✅ Explanation: Selects all pods with labels:
app=myapp
tier=frontend
🔍 3. matchLabels (inside selectors or affinity)
Use case: Used in deployments or affinity rules.
matchLabels:
app: myapp
tier: backend
Equivalent to saying:
matchExpressions:
- key: app
operator: In
values: [myapp]
- key: tier
operator: In
values: [backend]
🔬 4. matchExpressions (complex logic)
Use case: Match labels using logic.
matchExpressions:
- key: environment
operator: In
values: [dev, qa]
- key: tier
operator: NotIn
values: [frontend]
✅ Meaning: Matches pods where:
environment
isdev
orqa
tier
is notfrontend
📦 5. ReplicaSet / Deployment with selector
Use case: Control what pods a ReplicaSet manages
apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp-deploy
spec:
selector:
matchLabels:
app: myapp
tier: backend
template:
metadata:
labels:
app: myapp
tier: backend
spec:
containers:
- name: app
image: myapp:v1
✅ Deployment manages pods with app=myapp
and tier=backend
🧭 6. nodeSelector (simple node affinity)
Use case: Run pod only on nodes with certain labels.
apiVersion: v1
kind: Pod
metadata:
name: db-pod
spec:
nodeSelector:
disktype: ssd
zone: us-east-1a
containers:
- name: mysql
image: mysql
✅ Pod will only run on nodes with:
disktype=ssd
zone=us-east-1a
Set node labels like this:
kubectl label nodes <node-name> disktype=ssd zone=us-east-1a
🎯 7. nodeName (hard binding)
Use case: Force pod to a specific node (used rarely in production).
apiVersion: v1
kind: Pod
metadata:
name: node-specific-pod
spec:
nodeName: worker-node-1
containers:
- name: app
image: nginx
✅ Pod only runs on worker-node-1
, bypassing scheduling logic.
🔄 8. Affinity (with matchLabels)
Use case: Place pod near another pod with specific labels (or avoid).
affinity:
podAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
- labelSelector:
matchLabels:
app: redis
topologyKey: "kubernetes.io/hostname"
✅ Schedule pod on same node as pod with app=redis
.
🧪 Use Case Scenarios
Use Case | Tool Used | Example Key |
---|---|---|
Grouping resources | labels | app=myapp |
Targeting pods from service | selector | selector: app=myapp |
Complex selection logic | matchExpressions | operator: NotIn |
Affinity/Anti-affinity rules | matchLabels , matchExpressions | podAffinity , podAntiAffinity |
Scheduling on specific node types | nodeSelector | zone=us-east-1 |
Hard scheduling to exact node | nodeName | nodeName: node-1 |
Label-based replication management | selector in Deployment | matchLabels: app=myapp |
🛠 BONUS: Label Management Commands
-
Add label to node:
kubectl label node worker-1 disktype=ssd
-
List pods with label:
kubectl get pods -l app=myapp
-
Remove label:
kubectl label node worker-1 disktype-
✅ Final Tip
For dynamic or multi-criteria selection, use matchExpressions
. For simple exact matches, use matchLabels
. For basic node targeting, nodeSelector
is easiest. For tighter control, combine with affinity
or taints/tolerations
.