NetworkPolicy selectors
Start with a connection
A bookstore has a frontend and an inventory API. Only the frontend and an operations monitor should reach the API on TCP port 8080. A NetworkPolicy is a namespaced declaration of permitted network traffic for selected Pods. It needs a compatible enforcement implementation; saving the object alone does not filter packets. This chapter uses direct Pod-to-Pod connections in a fictional topology. Direction refers to the Pod receiving or initiating a connection, not the direction of an HTTP response.
frontend (shop-study) -> API (shop-study): TCP 8080monitor (ops-study) -> API (shop-study): TCP 8080other -> API: not allowed by intended policySelect the protected Pods and allowed peers
The top-level selector identifies the API Pods in shop-study. The selector in the ingress peer identifies frontend Pods; the namespace selector constrains which namespace they can come from. The namespace name label is supplied by Kubernetes. Both selectors inside this single peer must match. A peer with only a podSelector refers to the policy namespace. A peer with only a namespaceSelector refers to all Pods in matching namespaces. An empty top-level selector matches all Pods in the policy namespace. The following example is static; do not apply it to a real cluster.
apiVersion: networking.k8s.io/v1kind: NetworkPolicymetadata: name: api-ingress namespace: shop-studyspec: podSelector: matchLabels: app: api policyTypes: - Ingress ingress: - from: - namespaceSelector: matchLabels: kubernetes.io/metadata.name: shop-study podSelector: matchLabels: app: frontend ports: - protocol: TCP port: 8080Combine peers without broadening access by accident
Separate entries under from are alternatives. A list item containing both selectors is a conjunction. In the contrast below, an arbitrary Pod in ops-study or a local Pod marked role=monitor would match. That is broader than permitting just the monitoring Pods in ops-study. The policy deliberately illustrates the mistake; it is not part of the baseline. Ports within one rule are alternatives, but a peer and a port must both match when both are restricted.
apiVersion: networking.k8s.io/v1kind: NetworkPolicymetadata: name: or-example namespace: shop-studyspec: podSelector: matchLabels: app: api policyTypes: - Ingress ingress: - from: - namespaceSelector: matchLabels: kubernetes.io/metadata.name: ops-study - podSelector: matchLabels: role: monitor ports: - protocol: TCP port: 8080Reason about independent ingress and egress
A Pod can be isolated for inbound connections, outbound connections, both, or neither. An unisolated direction is unrestricted by ordinary NetworkPolicy. Once selected by a policy for that direction, traffic must match an allowance. More policies can add allowances; a policy with an empty rule list cannot subtract a permission from another policy. For a new connection between two Pods, evaluate source egress and destination ingress independently. Reply traffic for a permitted connection is normally allowed implicitly. The source and destination need not share a namespace.
source egress permits? AND destination ingress permits?true AND false = connection denied in this modelAddress ranges and port choices
ipBlock describes a CIDR and optional excluded subranges. Use it for appropriate external ranges, not as a substitute for stable Pod identity. For example, an illustrative range 192.0.2.0/24 with exception 192.0.2.128/25 excludes the upper half of the block. The example uses documentation addresses and makes no live connections. IPv4 and IPv6 are distinct address families; review both when deploying dual-stack. Numeric ports refer to destination transport ports; named ports and endPort ranges require implementation support review. NetworkPolicy does not filter HTTP paths or authenticate users.
ipBlock: cidr: 192.0.2.0/24 except: - 192.0.2.128/25Practice
Read the inventory and flow cases under lab-fixtures/networkpolicy. Predict which peer matches, then explain which direction blocks the connection. Change the conjunction into alternatives on paper and identify one unintended allowance. Use the included offline checker only for its declared model.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
NetworkPolicy selectors — knowledge check
Reason about synthetic segmentation and enforcement evidence.
Two selectors in one ingress peer have which effect?
A Pod-only peer in shop-study selects which Pods?
Another ingress policy allows a connection that a default-deny policy does not allow. What happens?
Source egress denies but destination ingress allows. Can a new connection proceed in the model?
Which is a better ordinary use of ipBlock?