Sub topic 13
How Multiple Policies Combine
This chapter assumes you know how a NetworkPolicy isolates pods, how ingress and egress rules work, and how to apply YAML with kubectl. It covers what happens when several policies select the same pod.
Policies only add permissions
NetworkPolicy has no deny rule and no rule ordering. A policy that selects a pod isolates it, and the policy's rules list what is allowed. When more than one policy selects the same pod, Kubernetes takes the union of all their rules. Traffic is allowed if at least one policy allows it.
This has two consequences:
- Adding a policy can only widen what a pod accepts, once the pod is already isolated.
- You cannot override or revoke an allow rule from another policy. To remove access, edit or delete the policy that grants it.
Ingress and egress are evaluated separately. A policy with policyTypes: [Ingress] never changes egress, and the reverse is also true.
A worked example
Start with a default-deny policy for the demo namespace. Then add two allow policies that both select app: backend: one for the frontend (shown in an earlier chapter) and one for Prometheus.
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: default-deny-all
namespace: demo
spec:
podSelector: {}
policyTypes:
- Ingress
- Egress
---
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: 8080
---
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: prometheusThe backend pods now accept ingress from two sources. Frontend pods in demo can connect on TCP 8080. Prometheus pods in monitoring can connect on any port, because the second policy has no ports field. Everything else is denied by the baseline.
Common mistakes
- Expecting a policy to block something another policy allows. The union means the broader allow wins. If a pod is reachable when you expect it to be blocked, list every policy that selects it.
- Forgetting that a rule without
portsallows all ports. In the Prometheus policy above, omittingportsopens every port on the backend to Prometheus. Addportsif you want to narrow it. - Overlapping selectors by accident. An empty
podSelector: {}in one policy selects every pod in the namespace, so its rules apply to pods you may have meant to target separately. - Assuming order matters. Policies are not applied in sequence. The names and creation times have no effect.
To see which policies select a pod, check their selectors with kubectl get networkpolicy -n demo -o yaml, or use kubectl describe networkpolicy -n demo, which shows the pod selector and rules for each policy.
Exercise
Pods labeled app=backend are selected by two policies. Policy A allows ingress from app=frontend on TCP 8080. Policy B allows ingress from app=batch on TCP 9090. Can an app=frontend pod connect to a backend pod on TCP 9090?
Show answer
No. The backend accepts the union of the two rules: frontend on 8080, and batch on 9090. Rules are not mixed across policies, so frontend on 9090 matches neither allow rule. The traffic is denied because the pod is isolated and no policy allows that combination.