
- Namespace:
default - App: three-tier
- frontend: Deployment
frontend-deploy, 2 pods, labelapp=frontend - middleware: single pod (assume
app=middleware) - mysql: single pod (assume
app=mysql)
- frontend: Deployment
- Services:
- frontend Service → port 80
- middleware Service → port 80
- mysql Service → port 3306
- A NetworkPolicy named
default-deny-ingressalready exists and is intended to provide the baseline deny.
What Bob wants (three rules)
- Frontend pods → middleware (allow TCP/80).
- Middleware → MySQL (allow TCP/3306).
- Frontend must NOT be allowed to connect to MySQL directly.
- The frontend is exposed to users and is the most likely target for compromise. Even if the app appears to work today, overly permissive network controls could let a compromised frontend reach the database. Enforcing that only middleware talks to MySQL applies least privilege and reduces blast radius.
Least privilege: configure each tier so it can reach only the services it needs and nothing more. This reduces the attack surface if a pod is compromised.
- Two NetworkPolicy resources in the
defaultnamespace:frontend-to-middleware— allow frontend → middleware on TCP/80.middleware-to-mysql— allow middleware → mysql on TCP/3306.
- Names are meaningless; only the
specdetermines behavior. - If
default-deny-ingressis misconfigured (selects wrong pods, placed in wrong namespace, sets the wrongpolicyTypes, or contains permissive ingress rules), it might not deny anything. If that happens, adding allow rules won’t restore least privilege — the database could still be reachable directly from the frontend.
NetworkPolicy objects are enforced only if your CNI supports them (examples: Calico, Cilium, Weave Net). Confirm your CNI implements NetworkPolicy before relying on these rules.
- Inspect
default-deny-ingressto confirm it actually denies ingress to the intended pods. - Fix it if it’s not a true deny.
- Add
frontend-to-middlewareandmiddleware-to-mysql(on top of the fixed deny). - Test connectivity from inside pods using
nc(netcat) or similar:- frontend → middleware: must succeed
- middleware → mysql: must succeed
- frontend → mysql: must fail
- Get the policy YAML:
- Check these fields:
metadata.namespace— is itdefault?spec.podSelector— does it select the pods you expect to protect?spec.policyTypes— does it includeIngress?spec.ingress— are there any permissive rules that accidentally allow traffic?
- This policy selects all pods in the namespace and denies all ingress (because there are no
ingressrules):
- policy placed in the wrong namespace.
podSelectortargets the wrong label(s) (so the deny protects nothing).policyTypesset only toEgress(so ingress is not denied).- The policy includes an
ingressrule that inadvertently allows traffic (for example, a broadfrom).
- Create a NetworkPolicy that selects the destination pods and allows ingress only from the specific source pods and port.
- The
middleware-to-mysqlpolicy allows only pods labeledapp=middlewareto connect to MySQL on 3306. Becausedefault-deny-ingressblocks all other ingress, frontend pods (which are notapp=middleware) cannot reach MySQL directly.
- Find a frontend pod name:
- Exec into a frontend pod and test:
- Exec into the middleware pod and test MySQL:
- What is wrong inside
default-deny-ingress?- Most likely: it doesn’t actually select the pods it’s supposed to protect, it’s in the wrong namespace, it has the wrong
policyTypes, or it contains permissive ingress rules. Inspect the YAML (seekubectl get networkpolicy ... -o yaml) and correct those fields.
- Most likely: it doesn’t actually select the pods it’s supposed to protect, it’s in the wrong namespace, it has the wrong
- Once it’s fixed, how do you write the policies that open only the required paths?
- Create NetworkPolicies that select the destination pods and add minimal
ingressrules that allow traffic only from the specific source pod selectors and ports required (examples shown above). Keep the deny baseline in place, and add only the small, specific allow policies.
- Create NetworkPolicies that select the destination pods and add minimal
- Kubernetes NetworkPolicy docs: https://kubernetes.io/docs/concepts/services-networking/network-policies/
- Netcat (nc): https://en.wikipedia.org/wiki/Netcat
- CNIs that support NetworkPolicy: Calico, Cilium, Weave Net, Canal (check your cluster provider docs)