Sub topic 3
Namespaces and their labels
This chapter assumes you can apply YAML with kubectl, know what a Pod and a Namespace are, and understand how labels and selectors match objects. It covers how network policies target other namespaces, which is done through namespace labels.
Why namespace labels matter
A NetworkPolicy lives in one namespace and only selects pods in that namespace. To allow traffic from pods in another namespace, a rule needs a way to name that namespace. Policies cannot refer to a namespace by its name directly. They use a namespaceSelector, which matches labels on the Namespace object.
So the question to ask is: what labels does the source namespace actually carry?
Automatic name label
Kubernetes adds the label kubernetes.io/metadata.name to every namespace. Its value is the namespace's name. This is the most reliable way to select one namespace by name.
Check it with:
kubectl get namespace monitoring --show-labelsThe output includes kubernetes.io/metadata.name=monitoring. Older clusters (before Kubernetes 1.21) don't set this label automatically. There you must add your own label.
Adding your own labels
Custom labels let you group namespaces, for example by environment or by team. Add one with:
kubectl label namespace monitoring purpose=observabilityA policy can then select every namespace with that label:
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: allow-from-observability
namespace: demo
spec:
podSelector:
matchLabels:
app: backend
policyTypes:
- Ingress
ingress:
- from:
- namespaceSelector:
matchLabels:
purpose: observabilityThis allows ingress to the backend pods from all pods in any namespace labeled purpose=observability. Because no podSelector is present, every pod in those namespaces is allowed.
Namespace selector scope
The meaning of a selector depends on where it appears in the rule.
| Rule contains | Which pods match |
|---|---|
| `podSelector` only | Pods with those labels, in the policy's own namespace |
| `namespaceSelector` only | All pods in namespaces with those labels |
| Both in one list item | Pods with those labels, in namespaces with those labels |
The last row is the AND form. The earlier spine example uses it for Prometheus: pods labeled app=prometheus in the monitoring namespace.
Common mistakes
- Using a namespace's name where a label is expected.
namespaceSelector: {matchLabels: {name: monitoring}}only works if you added anamelabel yourself. - Forgetting that an empty
namespaceSelector: {}matches every namespace. - Labeling the wrong object. The label must be on the Namespace, not on the pods inside it.
Exercise
A policy in namespace demo has the rule from: [{namespaceSelector: {matchLabels: {team: data}}}]. Which pods can send traffic to the selected pods, and what must be true of the source namespace?
Show answer
Every pod in any namespace that carries the label team=data can connect. The label must be on the Namespace object itself, set with a command such as kubectl label namespace <name> team=data. Labels on the pods do not matter.