Labels, selectors, and namespaces
Find related Pods by their labels, then check which controller is responsible for them.
What you will understand
- Use labels and selectors to identify a set of resources.
- Distinguish labels from annotations and their purposes.
- Explain how namespaces and owner references affect what you inspect.
Builds on: Objects, YAML, and apply
Organize objects with namespaces and labels
A store runs catalog and checkout services in one training cluster. Each has an API and some background workers. You want to list just the catalog API Pods. Pod names can change when a Deployment replaces them, so searching for the same names tomorrow is unreliable. A label is a short key-value pair attached to an object, such as app=catalog or tier=api. A selector is a matching rule. Kubernetes can use it to find all objects with the labels you requested, including replacements.
Annotations are key-value metadata for additional information that tools or people can read. The example includes an annotation for the purpose of the exercise. A label selector reads labels. Use labels when people and components need to group objects, and annotations for information such as a source link, an explanation, or a tool-specific setting.
A namespace gives namespaced API objects a scope. You can create a Pod called catalog-demo in selection-lab and a Pod with the same name in a different namespace. A query made in one namespace sees the matches there; -A requests every namespace you are allowed to list. Node objects are cluster-scoped. A namespace by itself only provides naming scope. Traffic isolation and permission boundaries need their own controls.
Which Pods match app=catalog?
Labels give a selector a way to choose many objects. Their names and annotations serve different purposes.
Label the workload
- app=catalog,tier=apiA: catalog / apiboth requirements match
- app=catalog,tier=apiB: catalog / workertier mismatch
Pod A has app=catalog and tier=api. Pod B shares the app but has tier=worker. Their names need not resemble their labels.
Match the label
- AND of two requirementsA selectedincluded
- AND of two requirementsB excludedexcluded
The selector includes A and excludes B. Commas mean AND across requirements; matching one requirement is insufficient.
Store descriptive metadata
- Labels: grouping keysPod Aselection metadata
- Annotation: purpose textPod Adescriptive metadata
An annotation can hold a purpose or URL. It does not become an ordinary label selector dimension.
All shown Pods are in one namespace; label syntax constraints are omitted from the diagram.
Explore another case
Select the objects you need
Save the file as catalog-demo.yaml and use your disposable cluster. The file describes one Pod in selection-lab. Before running the list commands, predict whether the labels make it match each selector. Both list commands should find it.
apiVersion: v1
kind: Pod
metadata:
name: catalog-demo
namespace: selection-lab
labels:
app: catalog
tier: api
environment: training
annotations:
training.example.com/purpose: "Selector exploration"
spec:
containers:
- name: web
image: nginx:1.27.5kubectl create namespace selection-lab
kubectl apply -f catalog-demo.yaml
# Comma-separated requirements are AND, not OR.
kubectl -n selection-lab get pods -l app=catalog,tier=api
# Quote selectors with shell-sensitive parentheses.
kubectl -n selection-lab get pods -l 'environment in (training,staging)'
kubectl -n selection-lab get pods --show-labelsThe first selector uses two equalities joined by a comma. Both have to hold. A Pod with app=catalog and tier=worker fails the second test. The second selector uses in, so the environment label may have either listed value. If your result is empty, run --show-labels, read the namespace, and compare the values one requirement at a time. Some API fields, such as a Service’s selector, accept a simpler map of exact label matches.
Names, owners, and access
A namespace scopes names, an owner reference identifies a particular object, and separate policy governs access.
Same name in two namespaces
- team-a / Pod web
- team-b / Pod web
- Node: cluster scoped
A Pod named web can exist in team-a and team-b. Namespace is part of the address for this resource type.
Owner references contain a UID
- Deployment UID DReplicaSet UID Rowns ReplicaSet
- ReplicaSet UID RPod UID Powns Pod
The Deployment owns a ReplicaSet, and the ReplicaSet owns a Pod through owner references. Shared labels do not create this chain.
Access needs its own rules
- RBAC / NetworkPolicyNamespace team-aexplicit controls
- RBAC / NetworkPolicyNamespace team-bexplicit controls
Namespace scope helps organize names and attach policy. Network and authorization isolation require their own configured mechanisms.
A Deployment-to-ReplicaSet-to-Pod chain is shown in one namespace; Nodes are cluster scoped.
Explore another case
Compare labels with owner references
Now compare selection with ownership. An owner reference in metadata records which API object owns a dependent. A Deployment normally owns a ReplicaSet, and the ReplicaSet owns its Pods. References identify specific owners with UIDs. A Service can select the same Pods to send them traffic while owning none of them. A one-off Pod can also match a label query without having an owner. Use the next commands with a managed Pod from a Deployment exercise. The standalone catalog-demo above has no Deployment owner.
kubectl -n selection-lab get pods --show-labels
# Substitute the actual managed Pod name from a Deployment exercise.
kubectl -n selection-lab get pod ACTUAL_MANAGED_POD \
-o jsonpath='{.metadata.ownerReferences}'What a label change can affect
When a selector controls behavior, each label edit can have a consequence. If you remove the label a Service selects, the routing path can change. Avoid overlapping selectors for independent controllers that manage the same kind of dependent. When you are deciding who will recreate a Pod or what deletion might collect, inspect owner references and the controller. The module lab practices applying and inspecting object metadata.
Check your understanding
A standalone Pod and two managed Pods all match app=catalog. Does one selector make them share an owner?
They share a label match. Ownership comes from the dependents’ owner references and the relevant controller behavior. The list alone cannot tell you what will be recreated after deletion.
Primary documentation
Go to the source
Reviewed 2026-10-27 · Kubernetes 1.37 · Related local exercise