Room 31
LABELS & SELECTORS
The Sorting Hat
- Labels are key-value pairs attached to objects. Selectors filter by labels.
- Multiple resources have been scattered with label fragments. Collect them.
📜 Mission Briefing
- Deploy the resources in
labeled-resources.yaml. - Use label selectors to find pods with specific labels.
- Find all pods with
team=escapeANDtier=backend. - Those pods have a label
fragment- concatenate the values (sorted by name). - The result spells
label3rs.
Key Commands
| Command | Purpose |
|---|---|
kubectl get pods --show-labels |
Show all labels |
kubectl get pods -l key=value |
Filter by label |
kubectl get pods -l 'key in (v1,v2)' |
Set-based selector |
kubectl label pod NAME key=value |
Add label |
kubectl label pod NAME key- |
Remove label |
How Labels & Selectors Work
# Labels: key=value pairs for organizing resources
# Selectors: queries to filter by labels
# Equality-based selectors
kubectl get pods -l environment=production
kubectl get pods -l environment!=staging
kubectl get pods -l 'environment=production,tier=frontend'
# Set-based selectors
kubectl get pods -l 'environment in (production, staging)'
kubectl get pods -l 'tier notin (frontend)'
kubectl get pods -l '!canary' # Pods WITHOUT the "canary" label
# Add/modify/remove labels
kubectl label pod my-pod version=v2
kubectl label pod my-pod version=v3 --overwrite
kubectl label pod my-pod version- # Remove label
# Label naming rules:
# Key: [prefix/]name (prefix optional, max 253 chars)
# Value: max 63 chars, alphanumeric, -, _, .
# Examples: app.kubernetes.io/name=nginx, team=backend
Hints
Hint 2
Hint 3
The password is label3rs.