Testing and Troubleshooting NetworkPolicies
What You'll Learn
You'll learn practical techniques to verify that your NetworkPolicies are working as intended and diagnose connectivity problems between pods.
Why Testing NetworkPolicies Matters
NetworkPolicies act like firewall rules for your pods, but unlike physical firewalls, you can't just "look" at the traffic flow. You need deliberate testing to confirm they're blocking and allowing the right connections. Think of it like testing door locks in a building—you need to actually try opening doors to verify which ones are secured.
Testing Techniques
Manual Connection Tests
Use temporary test pods to verify connectivity:
kubectl run test-pod --rm -it --image=busybox -- sh
From inside this pod, try reaching your target pod using wget or nc (netcat) commands to test if traffic is allowed or blocked.
Systematic Validation
Test both positive cases (connections that should work) and negative cases (connections that should be blocked). If your policy allows only frontend pods to reach backend pods on port 8080, verify:
- Frontend pods can connect to backend:8080 ✓
- Other pods cannot connect to backend:8080 ✗
Troubleshooting Common Issues
Check Policy Application
Verify your NetworkPolicy exists and targets the right pods using label selectors:
kubectl get networkpolicy
kubectl describe networkpolicy <policy-name>
Verify CNI Support
Confirm your cluster's CNI plugin supports NetworkPolicies—not all do (as you learned in "NetworkPolicy CNI Requirements").
Review Pod Labels
Mismatched labels are the most common cause of policy failures. Double-check that pod labels match your policy's podSelector exactly.
Key Takeaway: Testing NetworkPolicies requires actively attempting connections from test pods and systematically verifying both allowed and denied traffic flows match your intended security posture.