Sub topic 5
Labels and selectors
This chapter assumes you can read a basic manifest (apiVersion, kind, metadata, spec) and know that a Deployment creates Pods. Labels are how Kubernetes connects those objects to each other.
What labels are
A label is a key-value pair in metadata.labels. Kubernetes does not interpret most labels. They are tags that you and other objects can query.
metadata:
labels:
app: web
tier: frontend
environment: stagingKeys and values have format rules. Values are at most 63 characters, must start and end with a letter or digit, and may contain dashes, underscores, and dots. Keys can have an optional DNS prefix, such as app.kubernetes.io/name. The app.kubernetes.io/ family is a shared convention, and tools like Helm and dashboards recognize it.
What selectors do
A selector is a query over labels. Kubernetes has no built-in link between a Service and its Pods. The Service holds a selector, and the cluster routes traffic to every Pod whose labels match.
The dotted line to the app=db Pod means no match, so that Pod receives no traffic.
Matching in a Deployment and a Service
A Deployment uses matchLabels under spec.selector. This must match the labels in the Pod template.
apiVersion: apps/v1
kind: Deployment
metadata:
name: web
namespace: demo
spec:
replicas: 2
selector:
matchLabels:
app: web
template:
metadata:
labels:
app: web
spec:
containers:
- name: web
image: nginx:1.27
---
apiVersion: v1
kind: Service
metadata:
name: web
namespace: demo
spec:
selector:
app: web
ports:
- port: 80
targetPort: 80Here, spec.selector.matchLabels in the Deployment, template.metadata.labels, and the Service's spec.selector all use app: web. Note the Service selector is a plain map, with no matchLabels wrapper.
Set-based selectors
Deployments, ReplicaSets, and Jobs also support matchExpressions, which allows richer queries:
selector:
matchExpressions:
- key: tier
operator: In
values: [frontend, api]
- key: environment
operator: NotIn
values: [dev]The operators are In, NotIn, Exists, and DoesNotExist. All conditions must hold. Services support only equality matching.
Querying from kubectl
The -l flag takes the same kind of selector:
kubectl get pods -n demo -l app=web
kubectl get pods -n demo -l 'tier in (frontend,api),environment!=dev'
kubectl get pods -n demo --show-labelsCommon errors
- Deployment selector does not match the template labels. The API server rejects the manifest with a validation error.
- Service selector has a typo. The Service is created without error but has no endpoints. Check with
kubectl get endpoints web -n demo. An empty list means nothing matched. - Changing a Deployment selector after creation. The
spec.selectorfield is immutable inapps/v1. You must delete and recreate the Deployment. - Overlapping selectors. Two Deployments that select the same Pods will fight over them. Keep selectors specific, for example by combining
appwithcomponent.
Exercise
A Service named api is applied and kubectl get pods shows three healthy Pods labeled app: api-server. The Service has selector: {app: api}. Requests to the Service fail. What command confirms the cause, and how do you fix it?
Show answer
Run kubectl get endpoints api, which shows no addresses because no Pod has the label app: api. Fix it by changing the Service selector to app: api-server (or relabeling the Pods through the Deployment's Pod template) and reapplying.