Network Policy Blocked
Error message
Connection timed out — blocked by NetworkPolicy
Quick summary
A Kubernetes NetworkPolicy denies traffic between the source and destination Pod, causing connection failures.
Why this happens
A NetworkPolicy denies ingress to the destination Pod from the source Pod
A NetworkPolicy denies egress from the source Pod to the destination
The NetworkPolicy selector does not include the source Pod's labels
Multiple NetworkPolicies create conflicting rules
Minimal example
spec:
podSelector:
matchLabels:
app: api
ingress:
- from:
- podSelector:
matchLabels:
app: frontend # blocks all other sourcesspec:
podSelector:
matchLabels:
app: api
ingress:
- from:
- podSelector:
matchLabels:
app: frontend
- podSelector:
matchLabels:
app: worker # also allow workerThe NetworkPolicy only allows frontend to reach api. The fix adds worker to the allowed sources.
How to diagnose
List NetworkPolicies:
kubectl get networkpolicy -ADescribe the relevant NetworkPolicy:
kubectl describe networkpolicy <name>Check Pod labels to see if they match the policy selectors
Test connectivity:
kubectl exec <source-pod> -- curl <destination>
How to fix
Update the NetworkPolicy to allow traffic from the source Pod
Add the source Pod's labels to the NetworkPolicy's ingress rule
Create a new NetworkPolicy that explicitly allows the required traffic
Remove the NetworkPolicy if it is too restrictive (last resort)
How to prevent
Document all NetworkPolicies and the traffic they allow/deny
Test NetworkPolicies in staging before applying to production
Use default-deny policies and explicitly allow required traffic