Sub topic 7
Controlling incoming traffic with ingress rules
This chapter assumes you know pods, Services, labels, namespaces, and how to apply YAML with kubectl. It also assumes your cluster runs a network plugin that enforces NetworkPolicy. Without one, the objects are accepted but have no effect.
What an ingress rule does
An ingress rule describes traffic allowed to enter the pods that a policy selects. The policy's spec.podSelector picks the target pods. Each entry under ingress then lists allowed sources (from) and allowed destination ports on the target pods (ports).
A policy that lists Ingress in policyTypes isolates the selected pods for incoming traffic. After that, only traffic matching some ingress rule can reach them.
A first ingress policy
This policy lets frontend pods reach backend pods on TCP port 8080 and nothing else.
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: 8080Read it in three parts:
podSelectortargets pods labeledapp=backendin thedemonamespace.fromallows sources labeledapp=frontend. With nonamespaceSelector, the source must be in the policy's own namespace.portslimits the allowed traffic to TCP 8080 on the backend pod.
The port is the port the pod listens on, not a Service port. Policies act on pod traffic after Service address translation.
Rule structure
Each item in the ingress list is a separate rule. Inside one rule, a packet must satisfy both the from and the ports parts. Inside from, a packet needs to match only one entry.
| Field | Omitted | Empty list `[]` |
|---|---|---|
| `from` | Matches any source | Not valid as a match; treat as no restriction on sources only if the rule exists |
| `ports` | Matches any port | Matches any port |
| `ingress` | Policy has no allow rules | Policy has no allow rules |
The simplest case to remember is a rule with only ports. It allows that port from any source, including other namespaces and external addresses.
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: allow-http-from-anywhere
namespace: demo
spec:
podSelector:
matchLabels:
app: web
policyTypes:
- Ingress
ingress:
- ports:
- protocol: TCP
port: 80Common mistakes
- Forgetting that a policy with
Ingressand noingressrules blocks all incoming traffic to the selected pods. - Expecting a policy to block traffic from a pod to itself or from its own node. Plugins typically allow both.
- Writing the Service port instead of the container port.
- Assuming a policy in one namespace affects pods in another. A policy only selects pods in its own namespace.
Check your understanding
Exercise
A policy selects pods labeled app=db, lists Ingress in policyTypes, and has one ingress rule with only a ports entry for TCP 5432. Which sources can connect to the db pods, and on which ports?
Show answer
Any source can connect, but only on TCP 5432. The rule has no from clause, so it matches every source. All other ports are blocked because the pods are isolated for ingress.