Network Policy Debugging
Advanced15 min
Debug and verify Kubernetes network policies to ensure traffic is correctly allowed or blocked between pods and services.
Prerequisites
- -kubectl configured
- -A CNI that supports NetworkPolicy (Calico, Cilium, etc.)
Steps
1
List network policies in a namespace
View all network policies and what pods they select.
$ kubectl get networkpolicies -n <namespace> -o wide
2
Inspect a network policy's rules
View the ingress and egress rules of a specific policy.
$ kubectl describe networkpolicy <policy-name> -n <namespace>
3
Test connectivity between pods
Verify whether traffic is allowed or blocked between two pods.
$ kubectl exec <source-pod> -- curl -s --connect-timeout 5 http://<target-service>:<port>/health || echo 'Connection blocked or failed'
4
Check which policies apply to a pod
Find all network policies that select a specific pod based on its labels.
$ kubectl get networkpolicies -n <namespace> -o json | jq --arg pod_labels "$(kubectl get pod <pod-name> -n <namespace> -o jsonpath='{.metadata.labels}')" '.items[] | select(.spec.podSelector.matchLabels as $sel | all(to_entries[]; . as $e | ($pod_labels | fromjson)[$e.key] == $e.value)) | .metadata.name'
5
Deploy a test pod for connectivity testing
Launch a temporary pod with networking tools for manual testing.
$ kubectl run nettest --rm -it --restart=Never --image=nicolaka/netshoot -n <namespace> -- bash
Full Script
FAQ
Discussion
Loading comments...