Sub topic 17
Restricting traffic with NetworkPolicy
This chapter assumes you can apply YAML with kubectl, know how Pod labels and Services work, and have a cluster whose CNI plugin enforces NetworkPolicy (for example Calico or Cilium).
The default is open
By default, every Pod can reach every other Pod in the cluster. A NetworkPolicy is a namespaced object that restricts this. It selects Pods by label and lists the traffic those Pods may receive (ingress) or send (egress).
Two rules explain most of the behavior:
- A Pod that no policy selects accepts all traffic.
- Once any policy selects a Pod for a direction, only traffic that some policy allows in that direction is permitted. Policies only add allowances, so they never conflict.
Allow only the frontend
This policy selects the app: web Pods and allows ingress only from Pods labeled app: frontend on TCP port 80.
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: web-allow-frontend
spec:
podSelector:
matchLabels:
app: web
policyTypes:
- Ingress
ingress:
- from:
- podSelector:
matchLabels:
app: frontend
ports:
- protocol: TCP
port: 80The top-level podSelector picks the protected Pods. Each entry under from names an allowed source. The ports list limits the destination port on the protected Pod.
Selecting sources
A from entry can use these selector types:
| Selector | Matches |
|---|---|
| `podSelector` | Pods in the policy's own namespace |
| `namespaceSelector` | All Pods in namespaces with matching labels |
| `ipBlock` | A CIDR range, usually for traffic from outside the cluster |
Placement within the list changes the meaning. Putting namespaceSelector and podSelector in the same list item means "both must match". Putting them in separate items means "either may match". This is a common source of policies that are more open than intended.
Start from default deny
To lock down a namespace, first select every Pod and allow nothing:
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: default-deny-ingress
spec:
podSelector: {}
policyTypes:
- IngressThe empty podSelector matches all Pods in the namespace. With no ingress rules, nothing is allowed in. Then add policies like web-allow-frontend for each permitted flow.
Common errors
- Nothing is enforced. The API server accepts NetworkPolicy objects even when the CNI plugin ignores them. Confirm your plugin supports them by testing a deny policy.
- Egress breaks DNS. If you restrict egress, Pods can no longer query CoreDNS unless you allow UDP and TCP port 53 to the DNS Pods.
- Wrong namespace. A policy applies only in its own namespace. A
podSelectorsource does not match Pods elsewhere. - Label mismatch. A selector that matches no Pods fails silently. Check with
kubectl get pods --show-labels.
Exercise
You apply the default-deny-ingress policy to a namespace and then the web-allow-frontend policy. A Pod labeled app=frontend in a different namespace cannot reach web. Why, and how would you allow it?
Show answer
The podSelector source matches only Pods in the policy's own namespace. Add a namespaceSelector that matches the frontend namespace's labels, in the same from item as the podSelector so that both must match.