Sub topic 14
When a NetworkPolicy blocks traffic
This chapter assumes you can read a Service, its EndpointSlices, and kubectl describe output, and that you know labels and namespaces. The earlier layers (selector, ports, endpoints, DNS) are already checked. The Service looks healthy, but requests still fail.
What a NetworkPolicy does
A NetworkPolicy is a Kubernetes object that restricts which Pods may send traffic to, or receive traffic from, other Pods. The CNI plugin enforces it. The API server accepts the object either way. If your CNI does not implement NetworkPolicy, the policy is silently ignored and nothing is blocked.
Three rules explain most surprises:
- A Pod is open to all traffic until a policy selects it. Once any policy selects it for Ingress or Egress, only traffic that some policy allows is permitted in that direction.
- Policies only add allowances. There are no deny rules, and overlapping policies combine as a union.
- Policies match Pod IPs, after the Service IP has been translated. In a rule, use the container port (the
targetPort), not the Serviceport.
Recognizing the symptom
A blocked connection usually hangs until it times out. A closed port gives "connection refused" immediately. Combine that with what you already know:
| Observation | Likely cause |
|---|---|
| Service has ready endpoints, but connections time out | Ingress policy on the backend Pods |
| `nslookup` times out, but a Pod IP works directly | Egress policy on the client blocks DNS |
| Works from one namespace, hangs from another | Policy allows only certain namespaces |
| "Connection refused" | Not a policy problem. Check ports and the container |
Inspecting policies
List the policies in the backend's namespace and see which Pods they select:
kubectl get networkpolicy -n default
kubectl describe networkpolicy -n default
kubectl get pods -n default --show-labelsCompare each policy's podSelector with the Pod labels. An empty podSelector: {} selects every Pod in the namespace.
A default-deny policy and an allow rule
This pair isolates the web Pods for ingress, then allows only Pods labeled role: client to reach port 80:
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: default-deny-ingress
spec:
podSelector: {}
policyTypes:
- Ingress
---
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: allow-client-to-web
spec:
podSelector:
matchLabels:
app: web
policyTypes:
- Ingress
ingress:
- from:
- podSelector:
matchLabels:
role: client
ports:
- protocol: TCP
port: 80A client without the role: client label now times out, even though the Service and endpoints are correct. Port 80 is the container port. Writing 8080 would block everything.
DNS and default-deny egress
A default-deny Egress policy also blocks DNS queries. Names stop resolving and the error looks like a CoreDNS failure. Allow DNS explicitly:
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: allow-dns-egress
spec:
podSelector: {}
policyTypes:
- Egress
egress:
- to:
- namespaceSelector:
matchLabels:
kubernetes.io/metadata.name: kube-system
ports:
- protocol: UDP
port: 53
- protocol: TCP
port: 53Confirming a policy is the cause
Test from a Pod that the policy should allow, then from one it should block. If only the allowed one connects, the policy is working as written. You can also compare behavior from a temporary Pod with and without the permitted label. If a policy you expect to block does nothing, check that your CNI supports NetworkPolicy.
Exercise
A client Pod in a namespace with a default-deny Egress policy gets "bad address" from nslookup, but wget to the backend Pod IP on port 80 would work if egress to it were allowed. What is the cause, and what do you add?
Show answer
Egress is isolated, so DNS queries to CoreDNS are blocked and the name cannot resolve. Add an egress rule allowing UDP and TCP port 53 to the kube-system namespace (where CoreDNS runs), as in allow-dns-egress. A separate egress rule is still needed to reach the backend.