Sub topic 7
Labeling namespaces and selecting them
This chapter assumes you can run kubectl, read YAML, and know what Pods, Deployments, and Services are. You should also know that a namespace scopes names and objects inside one cluster.
Why namespaces carry labels
A namespace is an object like any other, so it has metadata.labels. Labels are key/value pairs. Other objects use label selectors to find namespaces by those pairs instead of by name.
Two features depend on this:
- NetworkPolicy uses
namespaceSelectorto say which namespaces may send traffic. - Pod Security Admission reads specific labels on a namespace to decide which Pod security standard to apply.
Namespaces are not labeled by default, apart from one label that Kubernetes adds itself (see below). Grouping labels such as team or environment are yours to define.
Setting labels
You can label a namespace in the manifest when you create it:
apiVersion: v1
kind: Namespace
metadata:
name: team-a
labels:
team: a
pod-security.kubernetes.io/enforce: baselineThe team: a label is a custom label for grouping. The pod-security.kubernetes.io/enforce label is read by the Pod Security Admission controller, which rejects Pods that violate the baseline standard. A later chapter covers it in detail.
You can also add or change labels on an existing namespace:
kubectl label namespace team-a environment=dev
kubectl label namespace team-a environment=staging --overwrite
kubectl get namespaces --show-labels
kubectl get namespaces --selector team=aChanging an existing label needs --overwrite. Without it, kubectl refuses. The --selector flag (or -l) filters the list to namespaces whose labels match.
The automatic name label
Kubernetes sets one label on every namespace: kubernetes.io/metadata.name. Its value is the namespace's name. This lets a selector match a namespace by name, even though selectors only match labels.
kubectl get namespace team-a --show-labelsThe output includes kubernetes.io/metadata.name=team-a. You do not need to add this label, and you should not try to change it.
Selecting namespaces in a NetworkPolicy
A namespaceSelector picks namespaces by label. The Pods in those namespaces become the allowed sources of traffic. This policy admits traffic from team-b into team-a:
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: allow-from-team-b
namespace: team-a
spec:
podSelector: {}
policyTypes:
- Ingress
ingress:
- from:
- namespaceSelector:
matchLabels:
kubernetes.io/metadata.name: team-bHere podSelector: {} means "all Pods in team-a". The namespaceSelector matches the namespace whose automatic name label is team-b. To allow several namespaces at once, select on a shared label such as environment: dev instead of listing names.
Common errors
- Selecting by a label that does not exist. A typo such as
team: Ainstead ofteam: amatches nothing. The policy then allows nothing, and you see no error. Check withkubectl get namespaces --selector team=a. - Forgetting that labels are case-sensitive. Keys and values must match exactly.
- Relying on a policy that is not enforced. A NetworkPolicy has no effect unless the cluster's network plugin enforces it.
- Letting anyone edit namespace labels. Selectors trust labels. A user who can change a namespace's labels can change which policies apply to it. Restrict permission to update namespaces.
Exercise
A NetworkPolicy in team-a uses namespaceSelector with matchLabels: {environment: dev}. You create a new namespace team-c but forget to label it. Can Pods in team-c reach team-a through this policy, and how can you check which namespaces currently match?
Show answer
No. The selector matches only namespaces labeled environment=dev, and an unlabeled team-c does not match. Run kubectl get namespaces --selector environment=dev to list the matching namespaces. Fix team-c with kubectl label namespace team-c environment=dev.