Sub topic 13
Labels, Selectors, and Namespaces
This chapter assumes you know what a Pod is, can read basic YAML, and have used kubectl get. Labels and selectors are how Kubernetes objects find each other. Namespaces divide one cluster into separate areas.
Labels: key-value tags on objects
A label is a key-value pair attached to an object's metadata. Labels carry no meaning for Kubernetes itself. You choose them, and other objects use them to find the labeled object.
In the Deployment from earlier chapters, the Pod template sets app: web:
template:
metadata:
labels:
app: webEvery Pod that this template creates carries that label. You can add more keys, such as tier: frontend or env: staging. A label key may have an optional prefix, such as example.com/team. Label values are limited to 63 characters and must start and end with an alphanumeric character.
Selectors: querying by label
A selector picks out the objects whose labels match a condition. Selectors are how loose coupling works in Kubernetes. A Service does not hold a list of Pods. It holds a selector, and the matching Pods are its backends.
Two forms exist:
- Equality-based:
app=weborenv!=prod. - Set-based:
env in (staging, prod)ortier notin (db).
The kubectl command accepts selectors with -l:
kubectl get pods -l app=web
kubectl get pods -l 'env in (staging,prod)'
kubectl get pods --show-labelsSeveral objects use selectors:
- A Service selects the Pods it routes traffic to.
- A ReplicaSet, and the Deployment above it, selects the Pods it owns and counts.
- A scheduler rule such as
nodeSelectorselects the nodes a Pod may run on.
If you change a label on a running Pod, it can fall out of a selector. A ReplicaSet that no longer sees the Pod creates a replacement. A Service stops sending traffic to it. This is a common way to isolate one Pod for debugging.
Namespaces: dividing a cluster
A namespace is a named scope inside a cluster. Names must be unique within a namespace but not across namespaces. Two teams can each have a Deployment called web if they use different namespaces.
A new cluster usually has these namespaces:
| Namespace | Purpose |
|---|---|
| `default` | Where objects go if you name no namespace |
| `kube-system` | Objects created by Kubernetes itself, such as system components |
| `kube-public` | Data readable by all clients, rarely used |
| `kube-node-lease` | Node heartbeat objects |
Create and use a namespace like this:
kubectl create namespace staging
kubectl apply -f deployment.yaml -n staging
kubectl get pods -n staging
kubectl get pods --all-namespacesNot every object lives in a namespace. Nodes and Namespaces themselves are cluster-scoped. Run kubectl api-resources --namespaced=false to list them.
Namespaces are not a hard security boundary on their own. By default, Pods in different namespaces can still reach each other over the network. Namespaces become useful for isolation when you combine them with RBAC, resource quotas, and network policies. Later chapters cover RBAC.
A Service name resolves through cluster DNS. Inside the same namespace, web is enough. From another namespace, use web.staging or the full form web.staging.svc.cluster.local.
Labels versus namespaces
Use a namespace for ownership and access boundaries, such as one per team or environment. Use labels for grouping and selection within that scope, such as which Pods belong to one application. A selector only matches objects in its own namespace.
Exercise
A Service in namespace staging has the selector app=web. A Pod labeled app=web runs in namespace default. Does the Service route traffic to it? Why or why not?
Show answer
No. A Service selector only matches Pods in the Service's own namespace. The Pod's label matches, but it lives in a different namespace, so it is not selected.
Exercise
Which command lists all Pods in every namespace that have the label tier=frontend?
Show answer
kubectl get pods --all-namespaces -l tier=frontend