Sub topic 2
Labels: identifying attributes for objects
This chapter assumes you can read basic YAML manifests, run simple kubectl commands, and know what a Pod, Deployment, and Service are. Labels are the glue between these objects, so the working model matters more than any single command.
What a label is
A label is a key-value pair stored in an object's metadata.labels map. Kubernetes does not interpret labels itself. You choose them, and other objects or tools read them to decide which objects belong together.
Labels are identifying. Their purpose is to answer questions such as "which Pods make up the web frontend?" or "which objects are in production?". Anything that groups, selects, or filters objects should be a label.
apiVersion: v1
kind: Pod
metadata:
name: web-1
labels:
app.kubernetes.io/name: web
environment: production
tier: frontend
spec:
containers:
- name: nginx
image: nginx:1.27This Pod carries three labels. Each key appears once, and each key has exactly one value.
Labels are many-to-many
One object can have many labels, and many objects can share the same label. There is no hierarchy and no uniqueness requirement. A label such as tier: frontend can sit on Pods, Services, and Deployments at the same time.
This is different from an object's name. A name identifies one object within its namespace. A label identifies a group that cuts across names, and one object can belong to several groups at once.
Labels can change at runtime
Labels are not fixed at creation. You can add, change, or remove them on a live object. Because selectors are evaluated continuously, a label change takes effect immediately in everything that selects on it.
- A Service starts or stops sending traffic to a Pod when that Pod's labels start or stop matching the Service selector.
- A ReplicaSet counts only Pods whose labels match its selector. If you remove the matching label from a Pod, the ReplicaSet no longer owns it and creates a replacement.
The second behavior is a useful debugging technique. Relabeling a misbehaving Pod takes it out of service and out of its controller's count, but leaves it running so you can inspect it.
kubectl get pods --show-labels
kubectl label pod web-1 tier-The first command lists each Pod with all its labels. The second removes the tier label from web-1. The trailing hyphen means "remove this key".
Labels are for selection
The reason labels exist is the selector. A selector is a rule such as environment=production that picks every object whose labels satisfy it. Services, Deployments, kubectl, and the scheduler all use selectors, and later chapters cover each. For now, one sample query shows the idea:
kubectl get pods -l environment=production,tier=frontendThis returns only Pods that have both labels with those exact values.
Common mistakes
- Treating labels as free-form notes. Labels have syntax limits and are indexed for fast queries, so they should hold short identifying values. Long or descriptive data belongs elsewhere.
- Using a label that drifts. If
environmentis set by hand and gets out of date, selectors silently match the wrong objects. Set labels in manifests, not by hand, for anything that matters. - Changing labels that a controller depends on without checking what selects on them.
Exercise
A ReplicaSet with selector app=web manages three running Pods. You run kubectl label pod web-2 app- on one of them. What happens to the Pod and to the ReplicaSet?
Show answer
The Pod keeps running, but it no longer matches the selector, so the ReplicaSet stops counting it. The ReplicaSet sees only two matching Pods and creates a new one to restore three. The unlabeled Pod is left running, outside the ReplicaSet's control.