Labels, Selectors, and Annotations
What you'll learn: How Kubernetes uses labels to organize resources, selectors to query them, and annotations to store metadata.
The Organizational System
In Kubernetes, you'll manage potentially hundreds or thousands of Pods and other resources. Labels, selectors, and annotations form a flexible organizational system—like a library cataloging system that lets you group books by genre, author, or reading level, then quickly find exactly what you need.
Labels: The Tags That Group Resources
Labels are key-value pairs you attach to Kubernetes resources like Pods. They're meant for identifying and selecting objects. Think of them as sticky notes you can filter by.
Common label patterns:
app: frontend— identifies the application componentenvironment: production— distinguishes prod from stagingversion: v2.1.0— tracks releases
Labels let you organize resources however makes sense for your system.
Selectors: Querying by Labels
Selectors let you find resources based on their labels. They're like search queries that match label criteria.
Example: "Find all Pods where app=frontend AND environment=production"
Kubernetes uses selectors internally too—Services use them to decide which Pods receive traffic, and Deployments use them to manage which Pods they control.
Annotations: The Freeform Metadata
Annotations are also key-value pairs, but they're for storing non-identifying metadata that you don't query on. Use them for:
- Build timestamps or git commit hashes
- Tool configuration or debugging information
- Links to external documentation
Annotations can contain larger, more structured data than labels.
The Key Difference
Labels are for organizing and selecting resources. Annotations are for attaching information to resources. If you need to query by it, use a label. If it's just helpful context, use an annotation.
Key Takeaway: Labels and selectors provide a powerful, flexible way to organize and query Kubernetes resources, while annotations let you attach arbitrary metadata without affecting selection logic.