Labels, selectors, annotations and namespaces
How Kubernetes groups objects without hard links: labels and selectors, how controllers adopt and release Pods, annotations for everything else, and namespaces.
Labels, selectors, annotations and namespaces
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- Labels are short key/value pairs used to select objects. Selectors are queries over them, either equality-based (env=prod) or set-based (env in (staging,canary), version).
- Controllers and Services find their Pods only by selector, so labels are live wiring. A matching Pod gets adopted; relabel a Pod out and it is orphaned and replaced.
- A Deployment's selector is immutable and must match its template's labels. Pick it once, keep it narrow, and never reuse it across workloads.
- Annotations hold non-identifying metadata for tools and people (commit SHAs, runbook links, controller settings). They can be large, up to 256 KiB per object, and are never selected on.
- A namespace scopes names, RBAC, quotas and policies. It is not a network or security boundary by itself; that takes RBAC, NetworkPolicy and Pod Security.
Grouping without pointers
A Deployment manages some Pods. A Service sends traffic to some Pods. A PodDisruptionBudget protects some Pods. None of them keeps a list of Pod names, because Pods come and go every few minutes and a list would always be stale. Instead, each one describes the Pods it wants with a query, and Kubernetes evaluates that query against labels on the Pods.
Think of a library that shelves books by coloured stickers rather than by a catalogue of titles. “Every book with a green sticker belongs to the children’s section” stays true no matter how many green books arrive or leave.
Labels
Labels live in metadata.labels as string key/value pairs. Keys may have a DNS-style prefix (app.kubernetes.io/name); the name part and the value are each at most 63 characters, starting and ending with an alphanumeric character and containing only letters, digits, -, _ and .. The API server enforces this. Trying to label a Pod with a 70-character value:
error: invalid label value: "owner=xxxxxxxxxxxx...": must be no more than 63 bytes
Kubernetes recommends a shared vocabulary under app.kubernetes.io/: name, instance, version, component, part-of and managed-by. Tools such as Helm set them, and dashboards group by them.
Selectors
A selector is a query over labels. Equality-based terms use =, == and !=. Set-based terms use in, notin, a bare key (the label exists) and !key (it doesn’t). Commas mean AND. In the lab, four Pods were labelled with app, env and version:
$ kubectl get pods -L app,env,version
NAME READY STATUS APP ENV VERSION
checkout-prod 0/1 Pending checkout prod v1
checkout-staging 0/1 Pending checkout staging v2
search-canary 0/1 Pending search canary v2
search-prod 0/1 Pending search prod v1
$ kubectl get pods -l env=prod -o name
pod/checkout-prod
pod/search-prod
$ kubectl get pods -l 'env in (staging,canary),app!=search' -o name
pod/checkout-staging
$ kubectl get pods -l 'version,env notin (prod)' -o name
pod/checkout-staging
pod/search-canary
In manifests, Services take a simple equality map. Workload controllers use a LabelSelector that supports both forms:
selector:
matchLabels:
app: checkout
matchExpressions:
- {key: env, operator: In, values: [prod, canary]}
- {key: tier, operator: DoesNotExist}
Labels are live wiring
Because controllers select by label, changing labels changes ownership. The lab shows both directions with a 3-replica Deployment called cart.
Adoption. A stray Pod was created by hand with the same labels as the ReplicaSet’s Pods (app=cart plus its pod-template-hash). Within seconds it carried an ownerReference to ReplicaSet/cart-5d44d56888, and because the ReplicaSet now had four Pods for three replicas, it deleted one: the stray went to Terminating. The controller does not care who made a Pod, only whether it matches.
Release. One of the real Pods was relabelled app=quarantine. Its ownerReferences emptied, and the ReplicaSet created a fourth Pod to replace it:
cart-5d44d56888-2k768 Pending quarantine
cart-5d44d56888-jjh8c Pending cart
cart-5d44d56888-v6xnj Pending cart
cart-5d44d56888-vvmtr Pending cart
That is a real debugging trick: pull a misbehaving Pod out of its Deployment and Service while it keeps running for inspection.
It is also why selectors must not overlap. Two Deployments that both select app: web will fight over each other’s Pods. The API protects you partly: a Deployment’s selector must match its own template labels, and once created it is immutable:
The Deployment "cart" is invalid: spec.selector: Invalid value: {"matchLabels":{"app":"cart2"}}: field is immutable
Changing a selector means creating a new Deployment.
Annotations
Annotations sit next to labels in metadata.annotations, but they are for information you will never select on. Values can be any string, and the total for one object can reach 256 KiB; the lab API server rejected a larger one with may not be more than 262144 bytes. Uses include:
- Build and release data:
deploy.example.com/commit: 3f9c2e1, a runbook URL, an owning team’s channel. - Settings for controllers and tools: ingress controller tuning, Prometheus scrape hints,
kubectl.kubernetes.io/last-applied-configuration. - State the system records, such as
deployment.kubernetes.io/revision, which appeared oncartwithout anyone setting it.
Rule of thumb: if a selector will ever need it, use a label; if it is just information, use an annotation.
Namespaces
A namespace is a scope for names. Two teams can each have a Deployment called api if they live in different namespaces. Most resources are namespaced; Nodes, PersistentVolumes, StorageClasses, ClusterRoles and Namespaces themselves are not, as kubectl api-resources --namespaced=false shows. A new cluster starts with default, kube-system, kube-public and kube-node-lease, and every namespace automatically gets a kubernetes.io/metadata.name label so NetworkPolicies can select it by name.
Namespaces are where per-team policy attaches: RoleBindings, ResourceQuotas and LimitRanges, Pod Security labels and NetworkPolicies. Deleting a namespace deletes everything in it, which makes them convenient for short-lived environments and dangerous to delete by mistake.
What a namespace does not do is isolate. Pods in different namespaces can reach each other over the network unless network policies say otherwise, and they share the same nodes and kernel. Multi-tenancy covers what it takes to make namespaces a real tenant boundary.
About the video
The embed is Nick True’s “What are Kubernetes Labels and Label Selectors?” (May 2020, 7 minutes). Its chapters cover what label selectors are, set-based operations, selector expressions, creating Pods with labels, listing them, selecting and deleting by label, and writing selector expressions. That is the first half of this lesson, done at the terminal. It does not cover adoption, annotations or namespaces, which the lesson adds. English was judged from the English title, description and caption track; there is no audio-track metadata. Google Cloud Tech’s short video in deeper covers namespaces, though it calls a namespace a “virtual cluster”, which oversells the isolation you get.
What was run: the label queries, the 63-byte error, the adoption of a stray Pod and release of a relabelled one, the immutable-selector error, the namespace labels and the 256 KiB annotation limit all ran against a local v1.37.3 API server whose node runs no containers, on 2026-11-28.
Where this fits
Now the controller that most of those labels serve: Deployments and ReplicaSets.
Check yourself
You create a bare Pod by hand whose labels happen to match a running ReplicaSet's selector. What happens?
The ReplicaSet adopts it by adding an ownerReference, now counts one Pod too many, and deletes one to get back to its desired count. It may delete your hand-made Pod or one of its own. Labels are live wiring, not decoration.
A Pod in a Deployment is misbehaving and you want to keep it for debugging without it receiving traffic. What is a neat way?
Change the label the ReplicaSet and Service select on, for example app=quarantine. The ReplicaSet releases it, removing its ownerReference, and creates a replacement, and the Service stops sending it traffic. The original keeps running for you to inspect, until you delete it.
Should a Git commit SHA go in a label or an annotation?
Usually an annotation. You won't select Pods by commit, and annotations allow any string. A label works only if you really need to query by it, and the value fits the label rules of 63 characters from a limited set.
A teammate says the staging namespace isolates staging from production in the same cluster. What would you check?
Whether there are NetworkPolicies, because by default Pods in any namespace can reach each other. Also RBAC bindings scoped per namespace, ResourceQuotas, and Pod Security labels. A namespace only scopes names and policies; it does not isolate anything on its own.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.