Why do requests fail with 502 after adding a Kubernetes egress NetworkPolicy?
A NetworkPolicy that lists Egress in its policyTypes allows only the outgoing traffic its rules name. Calling another service by name starts with a lookup to the cluster DNS Service on port 53, so a default-deny egress policy without a DNS rule breaks every call made by name, even when the policy allows the call itself. The pods stay Running and Ready and the app answers with an error from the failed lookup. Existing connections keep working, so it often shows when pods are replaced. In this incident a security hardening added the policy and the next rollout exposed it.
What it looks like
- Requests fail with 502 or 503 while every pod is Running and Ready.
- The app's error names a failed DNS lookup of the service it calls.
- The dependency answers fine when called from another namespace.
- It started after a NetworkPolicy change, often only once pods were replaced.
Commands that find the cause
kubectl logs -n checkout-api <pod-name> --tail=50The container's output for its current run. Read a whole error line, not only its first words: an error from a call to another service names which step failed, and each step (finding the address, connecting, reading the answer) points somewhere different.
kubectl get networkpolicy -n checkout-api -o yamlRead policyTypes, then each egress rule's to and ports. Once a policy lists Egress for a pod, that pod may send only the traffic some rule allows. Check every kind of traffic the app needs to make its calls, not only the call itself.
kubectl get pods -n checkout-apiRead STATUS, READY and RESTARTS together. Running with READY n/n and a steady RESTARTS count is normal. When every pod looks healthy and requests still fail, the problem is in what the pods depend on, not in the pods.
kubectl get events -n checkout-api --sort-by=.lastTimestampNewest last; read TYPE, REASON and MESSAGE. Warning rows name the object involved and what failed. A rollout leaves Normal rows for the new pods; a quiet event list means Kubernetes itself saw nothing wrong.
kubectl get svc inventory-api -n inventory-apiThe Service checkout-api calls on every request. Read TYPE, CLUSTER-IP and PORT(S). A Service that exists with a cluster IP and the expected port is reachable by name, from pods that are allowed to ask for that name.
Root cause
The new default-deny egress policy allowed inventory-api but not DNS on port 53, so after the rollout the pods could not resolve its name and requests failed.
The fix
Add a DNS rule to the egress policy and keep the rest of it:
kubectl -n checkout-api patch networkpolicy default-deny-egress --type=json -p '[{"op":"add","path":"/spec/egress/-","value":{"to":[{"namespaceSelector":{"matchLabels":{"kubernetes.io/metadata.name":"kube-system"}},"podSelector":{"matchLabels":{"k8s-app":"kube-dns"}}}],"ports":[{"protocol":"UDP","port":53},{"protocol":"TCP","port":53}]}}]'How to prevent it
Ship every default-deny egress policy with a rule that allows DNS to the cluster DNS pods on UDP and TCP port 53. Test a new policy by replacing one pod and making a call by name before rolling it out everywhere.
Run it on your own cluster
SnowOps Labs, the free hands-on Kubernetes lab, injects this incident into a cluster you run yourself, so you can work through it with your own tools. Open the lab page for this incident.
More Kubernetes incidents
- CrashLoopBackOff after a config change
- Service returns 503 while every pod is Running
- OOMKilled and exit code 137
- Pods fail to start after a Secret change
- Healthy pods restarted by a liveness probe
- Slow service with low CPU and no restarts
- Rollout stuck on ImagePullBackOff
- Pods stuck Pending with insufficient CPU