Sub topic 9
Choosing peers: pods, namespaces and IP blocks
This chapter assumes you know how labels and selectors work, can apply YAML with kubectl, and understand that a NetworkPolicy isolates the pods it selects. Here you will learn how a rule names the other side of a connection, called the peer.
The three peer types
Each entry in a from list (ingress) or a to list (egress) is a peer. It uses one of three selectors.
| Selector | Matches | Typical use |
|---|---|---|
| `podSelector` | Pods by label, in the policy's own namespace | Frontend to backend inside one namespace |
| `namespaceSelector` | All pods in namespaces whose labels match | Allow a whole team or environment |
| `ipBlock` | A CIDR range, with optional exceptions | Traffic outside the cluster |
Selecting pods
A bare podSelector in a peer matches pods in the same namespace as the policy. This policy lets app=frontend pods reach app=backend pods on TCP 8080.
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: allow-frontend-to-backend
namespace: demo
spec:
podSelector:
matchLabels:
app: backend
policyTypes:
- Ingress
ingress:
- from:
- podSelector:
matchLabels:
app: frontend
ports:
- protocol: TCP
port: 8080Selecting namespaces
A namespaceSelector on its own matches every pod in each matching namespace. Kubernetes sets the label kubernetes.io/metadata.name on every namespace, and its value is the namespace name. You can select a namespace by name without labeling it yourself.
To narrow the match to certain pods in that namespace, put a podSelector in the same list item as the namespaceSelector.
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: allow-prometheus-scrape
namespace: demo
spec:
podSelector:
matchLabels:
app: backend
policyTypes:
- Ingress
ingress:
- from:
- namespaceSelector:
matchLabels:
kubernetes.io/metadata.name: monitoring
podSelector:
matchLabels:
app: prometheusBoth selectors sit in one list item, so a peer must satisfy both. Only pods labeled app=prometheus in the monitoring namespace are allowed. The next chapter covers what happens when you split them into two items.
Selecting IP ranges
Use ipBlock for addresses that are not pods, such as an external API. It takes a cidr and an optional except list of sub-ranges.
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: allow-external-https
namespace: demo
spec:
podSelector:
matchLabels:
app: backend
policyTypes:
- Egress
egress:
- to:
- ipBlock:
cidr: 203.0.113.0/24
except:
- 203.0.113.128/25
ports:
- protocol: TCP
port: 443The backend pods may reach 203.0.113.0 through 203.0.113.127 on TCP 443. The except range is excluded.
Common errors
- Using
ipBlockfor pods. Pod IPs change when pods are recreated, and some plugins rewrite addresses before policy is applied. Select pods by label instead. - Forgetting the namespace. A bare
podSelectornever reaches pods in other namespaces. Add anamespaceSelectorfor cross-namespace traffic. - Misplaced
except. Anexceptrange must fall inside itscidr. Otherwise the API server rejects the object.
Exercise
Pods in the demo namespace must accept traffic from every pod in the namespace labeled team=payments, whatever pods run there. Which peer should the ingress rule use?
Show answer
A namespaceSelector with matchLabels team: payments and no podSelector. Alone, it matches all pods in each matching namespace.
Exercise
A policy lists a podSelector with app=web as a peer, but web pods in the shop namespace cannot reach the protected pods in demo. Why, and how do you fix it?
Show answer
A bare podSelector matches only pods in the policy's own namespace (demo). Add a namespaceSelector for shop, in the same list item as the podSelector, so both must match.