Labels & Selectors

Difficulty: Beginner

Question

Explain Labels and Selectors. How are they used in Kubernetes?

Answer

**Labels** are key-value pairs attached to objects (like Pods) to organize and select subsets of objects. They don't provide uniqueness but define attributes.

**Selectors** are used to filter objects based on their labels. They are the 'glue' that connects components. For example, a Service uses a Selector to find which Pods to send traffic to.

Usage: - **Equality-based**: `app=web`, `env!=prod` - **Set-based**: `env in (staging, qa)`, `tier notin (frontend)`

Concepts covered

Labels, Selectors, MetaData, Filtering