Sub topic 11
Restricting ports and protocols
This chapter covers the ports field of a NetworkPolicy rule. It assumes you can read ingress and egress rules, know that a policy isolates the pods it selects, and know TCP, UDP and port numbers.
Where ports fit in a rule
Each ingress or egress rule has two optional parts: a peer list (from or to) and a ports list. When both are present, traffic must match a peer and a port. When ports is omitted, the rule allows all ports for the listed peers. When the peer list is omitted, the rule allows the listed ports for all peers.
Each entry in ports has these fields:
| Field | Meaning | Default |
|---|---|---|
| `protocol` | `TCP`, `UDP` or `SCTP` | `TCP` |
| `port` | A number or a named container port | none |
| `endPort` | Last port of a range, inclusive | none |
Single ports and protocols
The protocol defaults to TCP. DNS needs both UDP and TCP on port 53, so list them as separate entries.
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: backend-ports
namespace: demo
spec:
podSelector:
matchLabels:
app: backend
policyTypes:
- Ingress
ingress:
- from:
- podSelector:
matchLabels:
app: frontend
ports:
- protocol: TCP
port: 8080
- protocol: TCP
port: 9090Frontend pods can now reach the backend on TCP 8080 and 9090. Nothing else is open, and a UDP packet to port 8080 is dropped.
Port ranges
Use endPort to allow a contiguous range. port is the first port and endPort is the last. Both ends are included.
ports:
- protocol: TCP
port: 32000
endPort: 32768endPort must be greater than or equal to port. It can only be used with a numeric port, not a named one.
Named ports
If a container declares a named port, a policy can refer to the name instead of the number.
ports:
- protocol: TCP
port: http-metricsThe name is resolved against the pods that receive the traffic. That keeps the policy valid when different pods use different numbers for the same name.
Common mistakes
- Forgetting UDP. The default protocol is TCP. A rule that lists only
port: 53does not allow UDP DNS queries. - Opening more than intended. A rule with a peer and no
portsallows every port from that peer. - Using the Service port. Policies are evaluated against the pod's port after Service translation. If a Service exposes port 80 and forwards to container port 8080, the policy must allow 8080.
- Assuming SCTP support. SCTP rules only work if your network plugin supports them.
Verifying a port rule
Run the temporary client from earlier chapters against an allowed and a blocked port. The first request should succeed. The second should time out after 3 seconds.
kubectl run test --rm -it --image=busybox:1.36 --restart=Never --labels=app=frontend -n demo -- wget -qO- -T 3 http://backend:8080
kubectl run test --rm -it --image=busybox:1.36 --restart=Never --labels=app=frontend -n demo -- wget -qO- -T 3 http://backend:8081Exercise
A Service exposes port 80 and forwards to container port 8080 on the backend pods. A policy allows ingress from frontend pods on TCP port 80. Clients cannot connect through the Service. Why, and how do you fix it?
Show answer
The policy is evaluated against the pod's destination port after the Service translates the address, so the traffic arrives on 8080, not 80. Change the policy to allow TCP port 8080 (or the named container port).
Exercise
Write a ports entry that allows TCP ports 5000 through 5010 and UDP port 5353.
Show answer
ports: - protocol: TCP port: 5000 endPort: 5010 - protocol: UDP port: 5353