Why Kubernetes Needs Labels: Pods, Deployments and Nodes
A name identifies one object. A label puts an object in a group, and Kubernetes finds almost everything through groups: a Deployment finds its pods, a Service its endpoints, the scheduler its nodes, with a label selector, a query like app=web. Nothing is wired by name. That is why a Deployment without a selector is rejected, why a typo in a Service selector silently sends traffic nowhere, and why a stray pod with the right label starts receiving traffic it should never see.
Everything below was run on a three-node kind v0.33.0 cluster (Kubernetes 1.37.0): one control plane and two workers labelled topology.kubernetes.io/zone=zone-a and zone-b.
The Short Answer
| Object | Who reads its labels | What breaks without them |
|---|---|---|
| Pod | ReplicaSet, Service (EndpointSlice controller), PodDisruptionBudget, NetworkPolicy, topology spread, pod affinity | Not required: it runs, but no controller, Service or policy can find it |
| Deployment | You (kubectl get deploy -l ...), tools such as Helm and Argo CD |
Nothing: its own labels are optional. Its spec.selector and pod template labels are required |
| Node | The scheduler (nodeSelector, node affinity, topology spread), DaemonSets |
Pods with a nodeSelector stay Pending; zone spreading has nothing to spread over |
Labels, Selectors and Their Rules
A label is a key/value pair in metadata.labels. A selector is a query over labels:
kubectl get pods -l app=web # equality
kubectl get pods -l 'app=web,!version' # has app=web and no version label
kubectl get pods -l 'app in (web,api),version' # set-based, plus "has the key"
kubectl get pods -l 'tier notin (backend)'
kubectl get pods -L version,topology.kubernetes.io/zone # labels as columns
kubectl get pods --show-labels
Labels are meant to be queried, so they are short and strict. A value is at most 63 characters:
error: invalid label value: "app=this-value-is-way-too-long-for-a-kubernetes-label-because-it-has-more-than-63-chars": must be no more than 63 bytes
The key’s name part allows letters, digits, -, _ and ., optionally behind a DNS prefix (example.com/team):
The Pod "gpu" is invalid: metadata.labels: Invalid value: "my app": name part must consist of alphanumeric characters, '-', '_' or '.', and must start and end with an alphanumeric character (e.g. 'MyName', or 'my.name', or '123-abc', regex used for validation is '([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9]')
Anything that is not used for selecting (a commit URL, a long description, a JSON blob) goes in an annotation: the same 200-character value that kubectl label rejected was accepted by kubectl annotate. Annotations can’t be selected on.
Deployments: the Selector Is Required
In apps/v1 a Deployment, ReplicaSet, StatefulSet and DaemonSet must have spec.selector, and it must match the pod template’s labels (a ReplicaSet and a StatefulSet without one fail with spec.selector: Required value too). A Deployment with no selector:
The Deployment "nosel" is invalid:
* spec.selector: Required value
* spec.template.metadata.labels: Invalid value: {"app":"web"}: `selector` does not match template `labels`
A selector that doesn’t match the template (app: web vs app: frontend):
The Deployment "mismatch" is invalid: spec.template.metadata.labels: Invalid value: {"app":"frontend"}: `selector` does not match template `labels`
An empty selector, which would match every pod in the namespace:
The Deployment "nolabels" is invalid: spec.selector: Invalid value: {}: empty selector is invalid for deployment
The minimal correct form:
apiVersion: apps/v1
kind: Deployment
metadata:
name: web # metadata.labels: optional
spec:
replicas: 3
selector:
matchLabels:
app: web # which pods belong to this Deployment
template:
metadata:
labels:
app: web # must include everything in the selector
tier: frontend # extra labels are fine
spec:
containers:
- name: nginx
image: nginx:1.29
kubectl create deployment api --image=nginx:1.29 writes the same structure with app: api in all three places.
The Selector Is Immutable
kubectl patch deploy web --type=merge \
-p '{"spec":{"selector":{"matchLabels":{"app":"web2"}},"template":{"metadata":{"labels":{"app":"web2"}}}}}'
The Deployment "web" is invalid: spec.selector: Invalid value: {"matchLabels":{"app":"web2"}}: field is immutable
Changing it means deleting and recreating the Deployment, so choose a stable key (app, app.kubernetes.io/name) and keep things that change, like version, out of the selector. Template labels can change: adding version: v2 to the template started a normal rollout (a new ReplicaSet, pods replaced), because the template is part of the pod spec hash.
Removing a selector label from the template is rejected again with `selector` does not match template `labels`.
pod-template-hash
The Deployment adds one label of its own to the ReplicaSet’s selector and to every pod:
$ kubectl get rs -o wide
NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR
web-544449d88b 3 3 3 6s nginx nginx:1.29 app=web,pod-template-hash=544449d88b
Each ReplicaSet of a rollout has a different hash, so the old and new ReplicaSets select different pods while both exist. It also means a pod you create by hand with only app=web is never adopted by the ReplicaSet.
The Selector Is the Ownership
A ReplicaSet doesn’t remember its pods by name. Every time it syncs it counts the pods that match its selector, and that count is what it compares with replicas.
Remove the label from a running pod:
kubectl label pod web-544449d88b-22fcc app-
The ReplicaSet saw two pods and created web-544449d88b-tzxlr within seconds. The relabelled pod kept running, with its ownerReferences gone and out of the Service’s EndpointSlice: a live copy taken out of rotation, useful for debugging a misbehaving replica. Delete it yourself afterwards, nothing else will.
Put the label back:
kubectl label pod web-544449d88b-22fcc app=web
The ReplicaSet counted four, adopted the old pod and deleted one: SuccessfulDelete Deleted pod: web-544449d88b-tzxlr, the newest.
Services: Traffic Goes Wherever the Labels Are
A Service has no list of pods. The EndpointSlice controller keeps one up to date with every ready pod that matches spec.selector:
$ kubectl get endpointslices -l kubernetes.io/service-name=web
NAME ADDRESSTYPE PORTS ENDPOINTS AGE
web-llbfh IPv4 80 10.244.1.2,10.244.2.3,10.244.2.2 2s
A Typo Fails Silently
A Service with app: Web (capital W) was created without any error, matched nothing, and had an empty EndpointSlice:
NAME ADDRESSTYPE PORTS ENDPOINTS AGE
typo-kp66f IPv4 <unset> <unset> 2s
Requests to it fail at the connection, not with a 503:
wget: can't connect to remote host (10.96.171.184): Connection refused
kubectl describe svc shows Endpoints: empty. Label values are case-sensitive.
Any Pod With the Label Gets Traffic
The Service doesn’t care who created a pod. A busybox pod started with kubectl run imposter --image=busybox:1.38 --labels=app=web -- sleep 3600 joined the web EndpointSlice as soon as it was ready, next to the three nginx pods. Of 8 requests to http://web, 6 returned the nginx page and 2 failed with Connection refused, the imposter’s share. The ReplicaSet ignored it (no pod-template-hash), so nothing reported a problem.
Labels like app=web are shared by everything in the namespace: two teams, two charts or a test pod using the same value end up behind each other’s Services. Make selectors specific (app.kubernetes.io/name plus app.kubernetes.io/instance).
Pods: Labels Are Optional, Being Found Is Not
A label is not required on a pod. The API server accepts a pod with no labels at all, and it is scheduled and runs normally. Only controllers need labels on their pods (a Deployment, ReplicaSet, StatefulSet, DaemonSet or Job selector must match its pod template), so every pod they create has them.
What a pod without labels loses is being found, since everything that groups pods uses a label query, never a name:
| What | Without labels |
|---|---|
| Service | The pod never matches a selector, so it gets no traffic |
| ReplicaSet / Deployment | Nothing owns it: deleted or on a failed node, it is gone for good |
| PodDisruptionBudget, topology spread, pod affinity | It isn’t counted: no eviction protection, no spreading |
kubectl get pods -l ... |
Never returned; only its name finds it |
kubectl expose has no labels to copy into a selector:
kubectl expose pod gpu --port=80
error: couldn't retrieve selectors via --selector flag or introspection: the pod has no labels and cannot be exposed
A pod without labels is also hard to make:
kubectl runaddsrun=<name>, even with an empty--labels=:kubectl run solo --image=nginx:1.29 --labels=still gotrun=solo. Only--overrides='{"metadata":{"labels":null}}'created a pod with no labels.- A scheduled pod gets its node’s zone label in Kubernetes 1.37 (next section), so only a pod that hasn’t been scheduled yet, like the
Pendinggpupod, keeps an empty label set.
A pod without labels is fine for a throwaway debug pod. Anything that should get traffic, be replaced, or be protected during a drain needs labels.
Kubernetes 1.37 Adds Topology Labels to Pods
With the PodTopologyLabelsAdmission feature (beta, on by default in 1.37) a pod gets its node’s topology.kubernetes.io/zone and region labels when it is scheduled. Every pod in the test cluster showed it:
NAME READY STATUS RESTARTS AGE LABELS
web-544449d88b-22fcc 1/1 Running 0 6s app=web,pod-template-hash=544449d88b,tier=frontend,topology.kubernetes.io/zone=zone-a
That has a side effect. solo2, a pod created with no labels at all, ended up with only topology.kubernetes.io/zone=zone-b, and kubectl expose pod solo2 succeeded, using that label as the selector:
$ kubectl get svc solo2 -o jsonpath='{.spec.selector}'
{"topology.kubernetes.io/zone":"zone-b"}
$ kubectl get endpointslices -l kubernetes.io/service-name=solo2
NAME ADDRESSTYPE PORTS ENDPOINTS AGE
solo2-m6lw8 IPv4 80 10.244.2.3,10.244.2.4,10.244.2.2 15s
The Service sent traffic to every pod in zone-b, including two web pods. Only a pod that isn’t scheduled yet (the Pending gpu pod above) still has no labels and gives the error. Pass --selector to kubectl expose instead of relying on introspection.
PodDisruptionBudgets and Topology Spread
Both count pods by label, and both accept a selector that matches nothing.
kubectl create pdb web --selector=app=web --min-available=2
kubectl create pdb typo --selector=app=Web --min-available=2
NAME MIN AVAILABLE MAX UNAVAILABLE ALLOWED DISRUPTIONS AGE
typo 2 N/A 0 3s
web 2 N/A 1 3s
typo reported NoPods: No matching pods found in its events and protects nothing. With only typo left, kubectl drain labels-worker2 evicted both web pods on that node at once, leaving one of three running; the web PDB would have allowed one eviction at a time.
A topology spread constraint without labelSelector is accepted with a warning, and spreads nothing:
Warning: spec.template.spec.topologySpreadConstraints[0].labelSelector: a null labelSelector results in matching no pod
The pods still landed 3 and 3 in the test, from the scheduler’s default spreading, which is why a missing labelSelector is easy to miss.
Nodes: How the Scheduler Picks Them
Every node gets labels from the kubelet:
| Label | Example | Set by |
|---|---|---|
kubernetes.io/hostname |
labels-worker |
kubelet |
kubernetes.io/os, kubernetes.io/arch |
linux, arm64 |
kubelet |
topology.kubernetes.io/zone, region |
zone-a |
kubelet (--node-labels) or cloud controller |
node.kubernetes.io/instance-type |
(cloud nodes only) | cloud controller |
node-role.kubernetes.io/control-plane |
(empty) | kubeadm |
Anything else (disktype=ssd, workload=batch, gpu=true) you add yourself. Then pods ask for it:
spec:
nodeSelector:
disktype: nvme
With no node labelled disktype=nvme the pod stays Pending:
Warning FailedScheduling default-scheduler 0/3 nodes are available: 1 node(s) had untolerated taint(s), 2 node(s) didn't match Pod's node affinity/selector. preemption: 0/3 nodes are available: 3 Preemption is not helpful for scheduling.
kubectl label node labels-worker2 disktype=nvme and the pod was running on that node within seconds. Node affinity, taints and the cloud node-group equivalents are in Kubernetes Node Selectors.
Node Labels Are Read at Scheduling Time
- Pods stay where they are. After
kubectl label node labels-worker2 disktype-thegpupod kept running there.nodeSelectoris only checked when a pod is scheduled. - DaemonSets follow the label. A DaemonSet with
nodeSelector: {disktype: ssd}started a pod onlabels-worker2when it got the label, and deleted it again when the label was removed.
Node Labels Added With kubectl Don’t Survive the Node
- Role labels are cosmetic.
kubectl label node labels-worker node-role.kubernetes.io/worker=madeROLESshowworkerinkubectl get nodes. Nothing schedules on it. - Recreating the node object loses them. After
kubectl delete node labels-workerand a kubelet restart the node registered again withdisktype=ssdand the zone (both from the kubelet’s--node-labels), but without thenode-rolelabel added by kubectl. Autoscaled and replaced nodes work the same way: put labels in the node group or the kubelet configuration. - The kubelet can’t set every label.
--node-labels=node-role.kubernetes.io/worker=stopped the kubelet from starting:
failed to validate kubelet flags: unknown 'kubernetes.io' or 'k8s.io' labels specified with --node-labels: [node-role.kubernetes.io/worker]
--node-labels in the 'kubernetes.io' namespace must begin with an allowed prefix (kubelet.kubernetes.io, node.kubernetes.io) or be in the specifically allowed set (beta.kubernetes.io/arch, beta.kubernetes.io/instance-type, beta.kubernetes.io/os, failure-domain.beta.kubernetes.io/region, failure-domain.beta.kubernetes.io/zone, kubernetes.io/arch, kubernetes.io/hostname, kubernetes.io/os, node.kubernetes.io/instance-type, topology.kubernetes.io/region, topology.kubernetes.io/zone)
This is on purpose: a compromised node must not be able to label itself into the control-plane pool. Your own prefixes (disktype, example.com/pool) are fine.
- The kubelet owns its own labels.
kubectl label node labels-worker kubernetes.io/hostname=renamed --overwritewas accepted, and stayed until the kubelet restarted and set it back. Don’t editkubernetes.io/*labels on nodes.
Recommended Labels
Kubernetes suggests a shared set, which Helm charts and most tools use:
metadata:
labels:
app.kubernetes.io/name: web # the application
app.kubernetes.io/instance: web-prod # this installation of it
app.kubernetes.io/version: "1.29"
app.kubernetes.io/component: frontend
app.kubernetes.io/part-of: shop
app.kubernetes.io/managed-by: helm
Select on name and instance: together they are unique per installation and never change. Keep version out of selectors, since the selector is immutable.
Quick Reference
# Look
kubectl get pods --show-labels
kubectl get pods -L app,version
kubectl get nodes -L topology.kubernetes.io/zone,disktype
kubectl get pods -l 'app=web,version in (v1,v2),!canary'
# What does this object select?
kubectl get deploy web -o jsonpath='{.spec.selector}'
kubectl get svc web -o jsonpath='{.spec.selector}'
kubectl get endpointslices -l kubernetes.io/service-name=web
# Change
kubectl label pod <pod> tier=frontend
kubectl label pod <pod> tier=backend --overwrite
kubectl label pod <pod> tier- # remove
kubectl label node <node> disktype=ssd
# Take a pod out of a Deployment and Service (the ReplicaSet replaces it)
kubectl label pod <pod> app-
Important Notes
- Selectors, not names: ReplicaSets, Services, PDBs and the scheduler all find objects by label query.
- Deployment selector: required, non-empty, must match the template, and immutable.
- Label changes move ownership: removing a selector label orphans the pod and creates a replacement; adding it back deletes the newest pod.
- Typos don’t fail: a Service or PDB that matches nothing is valid, with empty endpoints or zero protection.
- Any matching pod gets traffic, whoever created it. Use specific selectors.
- Kubernetes 1.37 pods carry their zone label; never
kubectl exposea pod without--selector. - Node labels act at scheduling time, except for DaemonSets. Labels added with kubectl are lost when the node is replaced.
- Long or non-queryable data goes in annotations; label values are limited to 63 characters.
See Also
- Labels and Selectors
- Recommended Labels
- Well-Known Labels, Annotations and Taints
- Kubernetes Node Selectors: nodeSelector, Node Affinity, Taints and nodeName
- Kubernetes Rolling Updates: What Actually Happens, Measured
- kubectl run vs kubectl create: Pods, Controllers and the Traps
- Kubernetes Troubleshooting: 10 Failures, Reproduced and Fixed