Difficulty: Beginner
What are labels and selectors in Kubernetes, and how do Services, Deployments and kubectl use them?
Labels are key/value pairs attached to Kubernetes objects (for example app: checkout, env: prod) and selectors are queries that pick objects by those labels. They are the glue of Kubernetes: Services find their Pods, Deployments and ReplicaSets know which Pods they own, and you filter resources with kubectl get pods -l app=checkout, all through labels rather than hard-coded names.
Kubernetes objects rarely reference each other by name. Instead:
spec.selector and routes to every Ready Pod whose labels match.spec.selector.matchLabels, which must match the labels in its Pod template. The ReplicaSet counts matching Pods to decide whether to create or delete some.podSelector to decide which Pods a rule applies to.nodeSelector match labels on nodes, such as topology.kubernetes.io/zone or node.kubernetes.io/instance-type.This loose coupling is powerful. If you remove the app=checkout label from one misbehaving Pod, the Service stops sending it traffic and the ReplicaSet creates a replacement, while the old Pod stays alive for debugging.
| Kind | Syntax example | Supported by |
|---|---|---|
| Equality-based | app=checkout,env!=dev | Services, nodeSelector, kubectl |
| Set-based | env in (prod, staging), !canary | Deployments, ReplicaSets, Jobs, NetworkPolicy, kubectl |
In YAML, set-based selectors use matchExpressions with operators In, NotIn, Exists and DoesNotExist. Multiple conditions are ANDed; there is no OR across keys.
Both are key/value metadata, but only labels are indexed and selectable. Use labels for identifying, grouping and selecting (app, tier, version, team). Use annotations for non-identifying data that tools read: build commit SHA, owner contact, ingress controller options, Prometheus scrape hints. Label values are limited to 63 characters, while annotations can hold much larger values.
app.kubernetes.io/name, app.kubernetes.io/instance, app.kubernetes.io/version, app.kubernetes.io/component, app.kubernetes.io/part-of, app.kubernetes.io/managed-by. Helm and dashboards understand them.spec.selector is immutable after creation in apps/v1. Choose it carefully, and keep changing values like version out of the selector.apiVersion: apps/v1
kind: Deployment
metadata:
name: checkout
spec:
replicas: 3
selector:
matchLabels:
app.kubernetes.io/name: checkout # immutable after creation
matchExpressions:
- key: tier
operator: In
values: [backend]
template:
metadata:
labels:
app.kubernetes.io/name: checkout
tier: backend
version: v2.3.1 # not in selector, safe to change
annotations:
git-commit: 9f2c1ab # metadata for humans and tools
spec:
containers:
- name: app
image: ghcr.io/acme/checkout:2.3.1
The selector uses only stable labels. The version label and commit annotation can change on every release without breaking the Deployment.
kubectl get pods -l 'app.kubernetes.io/name=checkout,tier in (backend)'
kubectl get pods -L version # show the label as a column
kubectl label pod checkout-abc12 quarantine=true
# Remove the selector label to pull a Pod out of the Service for debugging
kubectl label pod checkout-abc12 app.kubernetes.io/name-
The trailing minus removes a label. The ReplicaSet will notice one fewer matching Pod and start a replacement.
Labels, Label Selectors, matchLabels, matchExpressions, Annotations