Sub topic 2
Labels and selectors as the targeting mechanism
This chapter assumes you can create Pods and Deployments, apply YAML with kubectl, and read basic label syntax. It covers how a NetworkPolicy chooses which pods it governs and which pods it talks about.
Why labels matter for policies
A NetworkPolicy has no list of pod names or IP addresses. Pod IPs change whenever a pod is recreated, so a policy that depended on them would break constantly. Instead, a policy describes pods by their labels. A label is a key-value pair attached to an object, such as app: backend. A selector is a query that matches objects by their labels.
Selectors appear in two places in a policy:
spec.podSelectorpicks the pods the policy applies to.podSelectorinside a rule picks the pods on the other end of the traffic, called peers.
Selecting the target pods
The top-level spec.podSelector is always evaluated in the policy's own namespace. This policy applies to pods labeled app=backend in demo:
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: backend-policy
namespace: demo
spec:
podSelector:
matchLabels:
app: backend
policyTypes:
- IngressAn empty selector, podSelector: {}, matches every pod in the namespace. You will use that later for default-deny policies.
Two selector forms
matchLabels is a map of exact key-value pairs. All pairs must match.
matchExpressions is a list of conditions with an operator: In, NotIn, Exists, or DoesNotExist.
podSelector:
matchLabels:
app: backend
matchExpressions:
- key: tier
operator: In
values: [api, worker]
- key: debug
operator: DoesNotExistIf you use both forms, a pod must satisfy every condition in both. This selector matches pods with app=backend, a tier of api or worker, and no debug label.
Checking what a selector matches
Before relying on a policy, list the pods its selector would pick. The -l flag uses the same label logic:
kubectl get pods -n demo -l app=backend --show-labels
kubectl get pods -n demo -l 'tier in (api,worker),!debug'If the output is empty, the policy selects nothing and does nothing.
Common errors
- Label mismatch. The pod template in a Deployment must carry the label. Labels on the Deployment object itself do not reach its pods. Check
spec.template.metadata.labels. - Typos fail silently. A selector that matches no pods is valid. Kubernetes accepts the policy and applies it to nothing.
- Wrong namespace. A policy only selects pods in its own namespace, so
metadata.namespacemust match where the pods run. - Changed labels. Relabeling a running pod changes which policies apply to it immediately.
Exercise
A policy in namespace demo has podSelector: {matchLabels: {app: api}}. A pod labeled app=api runs in namespace staging. Does the policy apply to it, and which command would you use to list the pods the policy does select?
Show answer
No. The top-level podSelector only matches pods in the policy's own namespace, demo. To list the selected pods, run kubectl get pods -n demo -l app=api --show-labels.
Exercise
What does podSelector: {} match, and why is that useful?
Show answer
It matches every pod in the policy's namespace. It lets a single policy, such as a default-deny policy, apply to all pods without naming labels.