Sub topic 5
Anatomy of a NetworkPolicy
This chapter assumes you can apply YAML manifests with kubectl, know what pods, labels, and namespaces are, and are comfortable with ports and CIDR notation. It also assumes your cluster runs a network plugin that enforces policies. Without one, the objects below are accepted by the API server but have no effect.
The four parts of the spec
A NetworkPolicy is a namespaced object in the networking.k8s.io/v1 API group. Its spec has four fields that matter:
| Field | Purpose |
|---|---|
| `podSelector` | Chooses which pods in the policy's namespace the policy applies to. |
| `policyTypes` | Lists `Ingress`, `Egress`, or both. |
| `ingress` | Rules that allow incoming traffic to the selected pods. |
| `egress` | Rules that allow outgoing traffic from the selected pods. |
Only podSelector is required. The other fields have defaults, which are covered below.
A complete example
This policy applies to pods labeled app=backend in the demo namespace. It allows incoming traffic from pods labeled app=frontend, and only on TCP port 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: 8080Read it from the top down:
metadata.namespacesets where the policy lives. A policy only ever selects pods in its own namespace.spec.podSelectoris the target. Here it matches the backend pods.policyTypesstates which directions the policy governs. ListingIngressisolates the backend pods for incoming traffic.- Each entry in
ingressis a rule. A rule has afromlist of allowed peers and aportslist of allowed ports.
Rule structure
Ingress rules use from. Egress rules use to. In both cases the entry is a list of peers, and each peer is one of podSelector, namespaceSelector, or ipBlock. The ports list restricts which protocol and port pairs the rule permits.
A rule allows traffic that matches its peers and its ports. If you omit from (or to), the rule matches any peer. If you omit ports, it matches any port.
The structure of a policy can be pictured as a tree.
Defaults and common mistakes
- An empty
podSelector: {}selects every pod in the namespace. It does not mean "no pods". policyTypesdefaults depend on the rules. If you omit it,Ingressis always set, andEgressis added only when the policy has anegresssection. To avoid surprises, listpolicyTypesexplicitly.- An empty
ingress: []or a missingingresssection withIngressinpolicyTypesallows nothing. The pods are isolated and no rule permits traffic. - The policy has no deny rules. You only describe what is allowed. Everything else is denied once a pod is selected for that direction.
Exercise
A policy has podSelector: {}, policyTypes: [Ingress], and no ingress section. In the demo namespace, what traffic does it affect, and what is the result?
Show answer
It selects every pod in demo and isolates them for ingress. Because no rule allows anything, all incoming traffic to those pods is denied. Egress is not affected, because Egress is not in policyTypes.