Labels, selectors and annotations
The tags that connect everything.
cd ~/lab && curl -fsSLO https://secopslog.com/lab-files/k8s-fund/labels.tar.gz && tar -xzf labels.tar.gz, which creates ~/lab/labels/. SHA-256: af7d93faece46282be49b3b8210fa45070ca1ea8b40502c3094132b27a7b6940In lesson 6, Deployments, a ReplicaSet kept four Pods running, even though their names were random and changed every time one was replaced. It never tracked those names. It looked for Pods carrying the label app=web, and counted them.
A label is a short key and value, like env=prod, attached to an object. A selector is a question about labels, such as "everything with app=web". ReplicaSets, Services and many kubectl commands find their Pods this way, so one wrong letter in a label can quietly cut an app off from its traffic.
Put labels on Pods
This file creates five small nginx Pods. Each has an app label, most have an env label, and one also has release=canary:
apiVersion: v1kind: Podmetadata:name: web-prodlabels:app: webenv: prodspec:containers:- name: nginximage: nginx:1.30.6-alpine---apiVersion: v1kind: Podmetadata:name: web-staginglabels:app: webenv: stagingrelease: canaryspec:containers:- name: nginximage: nginx:1.30.6-alpine---apiVersion: v1kind: Podmetadata:name: api-prodlabels:app: apienv: prodspec:containers:- name: nginximage: nginx:1.30.6-alpine---apiVersion: v1kind: Podmetadata:name: api-devlabels:app: apienv: devspec:containers:- name: nginximage: nginx:1.30.6-alpine---apiVersion: v1kind: Podmetadata:name: toolboxlabels:app: toolsspec:containers:- name: nginximage: nginx:1.30.6-alpine
--show-labels adds a LABELS column with every label on each Pod. That gets hard to read once objects carry many labels, so -L app,env turns the keys you care about into columns of their own. The toolbox Pod has no env label, so its ENV cell is empty.
Select by equality
-l (short for --selector) keeps only the objects whose labels match. Commas mean "and":
env=prod found one api Pod and one web Pod. app=web,env!=prod asked for web Pods whose env is anything but prod, and only web-staging is left.
Select by set
Set-based selectors test a label against a list, or only check whether a key exists. Quote them, because the shell would otherwise read the brackets and the !:
Look closely at the notin result. toolbox is in it, with an empty ENV column. It has no env label at all, and "not in (prod)" is true for a Pod with no env. The same applies to !=. If you mean "has an env, and it is not prod", write env,env notin (prod). !release matched the four Pods without that key, and a bare release matched the one Pod that has it.
Labels can be added and removed on a running object. A trailing - removes a key:
toolbox now has env=dev, and the RELEASE column is empty everywhere. Nothing restarted. A label is just data on the object.
How a ReplicaSet counts its Pods
Back to the ReplicaSet. It counts its Pods with its selector, so changing the label on one of them changes the count. Create a small Deployment and relabel one of its Pods:
After the relabel there are three Pods. The one now labelled app=debug still runs, but the ReplicaSet no longer counts it, saw only one app=shop Pod, and started a new one, younger than the other two. The OWNER column shows the other side: the relabelled Pod has no owner any more. The ReplicaSet let go of it. This is a real debugging trick: pull a misbehaving Pod out of its ReplicaSet (and out of any Service that selects by the same label), and look at it in peace while a replacement takes over. You have to delete it yourself later, since nothing owns it.
A Service with a typo
A Service gives a group of Pods one stable address; lesson 8, Services, covers it properly. For now one fact matters: a Service picks its Pods by label, and sends traffic to every Pod its selector matches. This one has a typo in it:
apiVersion: v1kind: Servicemetadata:name: webspec:selector:app: wbe # typo: should be "web"ports:- port: 80
The Service was created without complaint. Its EndpointSlice, the list of Pod addresses the Service forwards to, shows <unset> under ENDPOINTS: no Pod matches the selector. kubectl get service -o wide prints the SELECTOR column, and asking for Pods with that exact selector returns No resources found. Those two commands together find this problem fastest. Kubernetes cannot warn you, because a selector that matches nothing yet is perfectly valid.
Fix the selector, then run kubectl get endpointslice -l kubernetes.io/service-name=web again until the ENDPOINTS column shows two addresses. It takes a second or two:
kubectl set selector replaced the selector, and the EndpointSlice now lists two addresses, one for web-prod and one for web-staging. The Service kept its name and its cluster IP the whole time. Only the list of Pods behind it changed.
Annotations
Annotations are also key and value pairs on an object, but nothing selects by them. They are for information meant for people and tools: a contact, a ticket number, a build ID. Their values can be long and contain any text:
kubectl describe lists both annotations. The same sentence as a label value is rejected: label values are limited to 63 characters of letters, digits, -, _ and ., and must start and end with a letter or digit. That limit is a good guide. If you would ever select by it, make it a label. If it is just something to read, make it an annotation. Lesson 10, Rolling updates, uses one, kubernetes.io/change-cause, to describe each release.
Hands-on
1. List the web and api Pods, leaving out dev: kubectl get pods -l 'app in (web,api),env notin (dev)' -L app,env.
2. List every Pod that has no env label: kubectl get pods -l '!env' -L app.
3. Clean up with kubectl delete -f pods.yaml -f web-svc.yaml, kubectl delete deployment shop and kubectl delete pod -l app=debug, then check with kubectl get pods.
What you should see: step 1 returns api-prod, web-prod and web-staging. api-dev is excluded, and toolbox is neither web nor api. Step 2 returns the three shop Pods, including the one labelled debug, because kubectl create deployment gave them no env label. After step 3, kubectl get pods says No resources found in default namespace.
Remember
ReplicaSets and Services find their Pods by selector, never by name, so a label typo silently leaves a Service with no endpoints. notin and != also match objects that lack the key.
kubectl get endpointslice -l kubernetes.io/service-name=web shows <unset>. What do you check first?<unset> means no Pod matched.-o wide showed app=wbe, and no Pod had that label.ports list on a container does not decide which Pods a Service selects.-l 'env notin (prod)' return?toolbox showed up with an empty ENV column.env,env notin (prod), which requires the key first.app label on one Pod of a two-replica Deployment. What happens?app=debug.kubectl label succeeded. Ownership does not lock labels.