Sub topic 6
Adding, changing, removing, and querying labels with kubectl
This chapter assumes you can read basic YAML manifests, know what Pods, Deployments, and Services are, and have used kubectl get and kubectl apply. It covers the commands you use to inspect and edit labels on live objects, and the mistakes that come up most often.
Starting point
Labels are key-value pairs in metadata.labels. You can set them in a manifest, and you can also change them on running objects with kubectl label. Take this Pod as the example:
apiVersion: v1
kind: Pod
metadata:
name: web-1
labels:
app.kubernetes.io/name: web
environment: production
tier: frontend
annotations:
example.com/owner: "team-payments"
example.com/runbook: "https://wiki.example.com/runbooks/web"
spec:
containers:
- name: nginx
image: nginx:1.27Querying labels
Plain kubectl get hides labels. Add --show-labels to print them in an extra column.
kubectl get pods --show-labelsTo filter, pass a selector with -l (or --selector). Commas between requirements mean AND. The selector below returns only Pods that have both labels.
kubectl get pods -l environment=production,tier=frontendSet-based selectors need quotes, because the shell would otherwise treat the parentheses and ! as special characters.
kubectl get pods -l 'environment in (production,staging),tier notin (backend)'
kubectl get pods -l 'tier,!canary'The first command matches Pods whose environment is production or staging and whose tier is not backend. The second matches Pods that have a tier key, whatever its value, and have no canary key.
Changing and removing labels
Use kubectl label with key=value to add a label. If the key already exists, kubectl refuses to change it unless you add --overwrite. Put a minus sign after the key to remove it.
kubectl label pod web-1 environment=staging --overwrite
kubectl label pod web-1 tier-The same command can annotate objects, using kubectl annotate. Annotations do not take part in selection, so they only change what people and tools read.
kubectl annotate pod web-1 example.com/owner=team-checkout --overwriteWhat a live label change affects
A label edit takes effect immediately for anything that selects by that label.
- A Service stops sending traffic to a Pod once the Pod no longer matches the Service's selector.
- A ReplicaSet counts only Pods that match its selector. If you relabel one of its Pods so it no longer matches, the ReplicaSet starts a replacement. The relabeled Pod keeps running, which is a handy way to pull a Pod out for debugging.
- Labels in a Deployment's
spec.template.metadata.labelsare part of the Pod template. Changing them triggers a rollout. Runningkubectl labelon one Pod does not change the template, so new Pods will not get that label.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| `already has a value ... --overwrite is false` | The key exists and you tried to set it again | Add `--overwrite` |
| `kubectl get -l` returns nothing | Typo in the key or value, or wrong namespace | Check `--show-labels` and add `-n` |
| Shell error near `(` or `!` | Selector not quoted | Wrap the selector in single quotes |
| Label disappears after a rollout | You labeled a Pod, not the Deployment template | Edit the template in the manifest |
Exercise
A Deployment's Pods lost the label canary=true after a rolling update. You had added it with kubectl label pod. Why did it disappear, and how do you make it stick?
Show answer
The label was set on the individual Pods, not on the Pod template. The rolling update created new Pods from the template, which had no such label. Add the label under spec.template.metadata.labels in the Deployment manifest and apply it.