Sub topic 17
Troubleshooting policy problems
This chapter assumes you can write ingress and egress rules, know how default-deny works, and have run a temporary test pod. It covers how to find out why traffic is blocked, or why it is allowed when it should not be.
Start with the symptom
Most problems fall into two groups. Traffic is blocked that you expected to work, or traffic is allowed that you expected to be blocked. Work through the checks in order.
When traffic is allowed but should not be
- The plugin does not enforce policies. The API server accepts a NetworkPolicy even if the network plugin ignores it. Confirm that your plugin supports policies.
- No policy selects the pod. A pod is isolated only when a policy's
podSelectormatches it for that direction. Check the labels on the pod. - Another policy allows it. Policies are additive. One broad rule in any policy that selects the pod is enough to allow the traffic.
- The wrong direction. An ingress-only policy does not restrict egress. Check
policyTypes.
When traffic is blocked but should not be
- DNS is blocked. Under egress restrictions, a lookup fails and the request looks like a connection error. Test with an IP address. If that works, add the DNS egress rule.
- The labels do not match. Selectors fail silently. Compare them with the real labels.
- The namespace label is missing. A
namespaceSelectormatches namespace labels, not names. The labelkubernetes.io/metadata.nameis set automatically on current clusters. - AND was written as OR, or the reverse. Check the indentation of the
-markers underfromorto. - Egress is allowed but the destination ingress is not. Both ends must permit the connection.
- The port is wrong. Match the container's port, not the Service port, in most plugins.
Useful commands
kubectl get networkpolicy -n demo
kubectl describe networkpolicy allow-frontend-to-backend -n demo
kubectl get pods -n demo --show-labels
kubectl get namespaces --show-labelsdescribe prints the policy in a readable form. It shows which pods it selects and how it reads each rule. Compare this output with your intent.
Then repeat the test with the client labeled the way you expect:
kubectl run test --rm -it --image=busybox:1.36 --restart=Never --labels=app=frontend -n demo -- wget -qO- -T 3 http://backend:8080A timeout usually means a drop. An immediate "connection refused" means the traffic reached the pod, so the policy is not the cause.
Exercise
Pods in demo cannot reach a database by name after you apply a default-deny egress policy, but connecting by the database's IP address works. What is the cause, and how do you fix it?
Show answer
Egress is denied to cluster DNS, so name lookup fails. Add an egress policy that allows UDP and TCP port 53 to the kube-dns pods in the kube-system namespace, as in allow-dns-egress.
Exercise
A pod labeled app=backend still accepts connections from every pod, though you applied a policy meant to restrict it. List two possible causes.
Show answer
The network plugin may not enforce policies. Or another policy that selects the pod may contain a broader allow rule. A mismatch between the policy's podSelector and the pod's labels would also leave the pod unisolated.