Even with correct YAML, NetworkPolicies can fail silently. This topic covers how to debug and apply production-grade practices.
β Goal: Go from βwhy isnβt this working?β to βtraffic flows exactly as intended.β
Like a network engineer with a packet sniffer:
- You donβt guess β you observe, test, and verify
- You have a checklist for common failures
# Check if Cilium/Calico is running
kubectl get pods -n kube-system | grep -E 'cilium|calico'
# If using Flannel (default in k3s) β NetworkPolicy **wonβt work!**
β Fix: Install Cilium:
curl -L --remote-name <https://github.com/cilium/cilium-cli/releases/latest/download/cilium-linux-amd64.tar.gz>
tar xzvfC cilium-linux-amd64.tar.gz /usr/local/bin
cilium install
# Check if policy matches your Pod labels
kubectl describe netpol my-policy
# List Pods that should be affected
kubectl get pods -l app=backend
β
Common mistake: Typo in labels (app: backned vs app: backend)
# Start a debug Pod in the same namespace
kubectl run debug --image=busybox --rm -it -- sh
# Test ingress: can others reach target?
/ # wget -qO- <http://backend> --timeout=3
# Test egress: can target reach others?
kubectl exec deploy/backend -- wget -qO- <http://database> --timeout=3
β
Use --timeout to avoid hanging