Skip to content

Labels & Selectors

Labels are key-value pairs attached to objects, and selectors are used to identify objects with matching labels.

Labels
app=web
env=prod
tier=frontend
Selector
app=web

Mental Model

Labels are like name tags on luggage. Selectors are like "All bags with red tags go to carousel 1". They help organize and group Kubernetes objects without changing their core structure.

YAML Example

apiVersion: v1
kind: Pod
metadata:
  labels:
    app: web
    tier: frontend
---
apiVersion: v1
kind: Service
spec:
  selector:
    app: web    # Selects pods with this label
  ports:
  - port: 80

Common Misconceptions

  • ✗Labels are for identification only - FALSE: Used for scheduling too
  • ✗Labels must be unique - FALSE: Multiple objects can have same labels
  • ✗Labels are required - FALSE: Optional but highly recommended

Key Takeaways

  • • Labels are key-value pairs attached to objects, and selectors are used to identify objects with matching labels.
  • • Labels are like name tags on luggage. Selectors are like "All bags with red tags go to carousel 1". ...
  • • Check the YAML example for implementation details
  • • Be aware of the common misconceptions listed above
Kubernetes Cosmos•An interactive learning experience
Built by GLM 4.7