Sub topic 15
Testing policies with temporary pods
This chapter assumes you can apply manifests with kubectl, know how labels and namespaces work, and have already written an ingress policy such as allow-frontend-to-backend. It also assumes your cluster's network plugin enforces NetworkPolicy.
Why test with a temporary pod
A policy matches traffic by pod labels and namespace. The most direct test is to create a client pod with the labels you want to check and see whether it can connect. The pod is temporary, so it leaves nothing behind.
kubectl run test --rm -it --image=busybox:1.36 --restart=Never --labels=app=frontend -n demo -- wget -qO- -T 3 http://backend:8080--rm -itattaches to the pod and deletes it when the command ends.--restart=Nevercreates a bare pod, not a Deployment.--labels=app=frontendgives the pod the identity the policy expects.-n demoplaces the pod in the namespace you are testing.-T 3sets a 3-second timeout. A dropped packet gives no error, so without a timeout the command hangs.
Test both directions
A test that passes proves little unless you also show that the wrong client fails. For allow-frontend-to-backend, run three checks:
- Allowed client: labels
app=frontendindemo. Expect the response body. - Wrong labels: repeat the command with
--labels=app=other. Expect a timeout. - Wrong namespace: run from
-n defaultand usehttp://backend.demo:8080. Expect a timeout.
Reading the result
| Output from `wget` | Meaning |
|---|---|
| Response body | A policy allows the traffic, or no policy selects the target pod. |
| `download timed out` | Packets were dropped. This is the usual sign of a policy block. |
| `bad address` | Name resolution failed. Check DNS egress before blaming the ingress rule. |
| `Connection refused` | The packet reached the pod, so no policy blocked it. Nothing is listening on that port. |
Separating DNS from connectivity
Under a default-deny egress policy, a test pod that uses a service name can fail at the DNS lookup before it sends any traffic to the backend. To check the policy alone, connect by pod IP:
kubectl get pod -n demo -l app=backend -o wide
kubectl run test --rm -it --image=busybox:1.36 --restart=Never --labels=app=frontend -n demo -- wget -qO- -T 3 http://10.244.1.7:8080Replace 10.244.1.7 with the IP shown in the first command. If the IP works and the name fails, fix the DNS egress rule.
Common mistakes
- Wrong labels on the test pod. A typo makes a correct policy look broken.
- Wrong namespace. A pod without
-nruns in your current context namespace. - No baseline test. Run the command before you apply the policy. A policy only counts as working if it changes the result.
- A plugin that ignores policies. If the "wrong client" test succeeds, check that your network plugin enforces NetworkPolicy. Some do not, and they ignore the objects without reporting an error.
Exercise
After you apply default-deny egress, a test pod running wget http://backend:8080 fails with "bad address". Connecting to the backend pod's IP works. What is the cause and what is the fix?
Show answer
Egress to cluster DNS is blocked, so the name cannot be resolved. Pod-to-pod traffic is allowed, which is why the IP works. Apply an egress policy like allow-dns-egress that permits UDP and TCP port 53 to the kube-dns pods in kube-system.