Skip to main content
Bob locked down his cluster, and now the app is safe — but broken. He wants to restore only the network paths the app actually needs, one at a time — and nothing more.
A stylized Kubernetes architecture diagram titled "Safe, and Also Broken" showing frontend, middleware, and MySQL deployments and services with padlock icons and red blocked connections. The graphic labels ports (80, 3306), a default-deny ingress, and a user called "Bob" at the left.
Overview
  • Namespace: default
  • App: three-tier
    • frontend: Deployment frontend-deploy, 2 pods, label app=frontend
    • middleware: single pod (assume app=middleware)
    • mysql: single pod (assume app=mysql)
  • Services:
    • frontend Service → port 80
    • middleware Service → port 80
    • mysql Service → port 3306
  • A NetworkPolicy named default-deny-ingress already exists and is intended to provide the baseline deny.
Resources and ports: What Bob wants (three rules)
  1. Frontend pods → middleware (allow TCP/80).
  2. Middleware → MySQL (allow TCP/3306).
  3. Frontend must NOT be allowed to connect to MySQL directly.
Why rule #3 matters
  • 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.
What Bob plans to add
  • Two NetworkPolicy resources in the default namespace:
    • frontend-to-middleware — allow frontend → middleware on TCP/80.
    • middleware-to-mysql — allow middleware → mysql on TCP/3306.
Remember: NetworkPolicies are additive. If the base deny is real, the two allow policies will open only the intended paths. Common complication: a policy named “deny” may not actually deny
  • Names are meaningless; only the spec determines behavior.
  • If default-deny-ingress is misconfigured (selects wrong pods, placed in wrong namespace, sets the wrong policyTypes, 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.
Important: NetworkPolicy enforcement depends on your CNI plugin. If your cluster CNI doesn’t implement NetworkPolicy (or is misconfigured), none of these policies will be enforced.
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.
Bob’s order of work
  1. Inspect default-deny-ingress to confirm it actually denies ingress to the intended pods.
  2. Fix it if it’s not a true deny.
  3. Add frontend-to-middleware and middleware-to-mysql (on top of the fixed deny).
  4. Test connectivity from inside pods using nc (netcat) or similar:
    • frontend → middleware: must succeed
    • middleware → mysql: must succeed
    • frontend → mysql: must fail
Inspect the deployed policy
  • Get the policy YAML:
  • Check these fields:
    • metadata.namespace — is it default?
    • spec.podSelector — does it select the pods you expect to protect?
    • spec.policyTypes — does it include Ingress?
    • spec.ingress — are there any permissive rules that accidentally allow traffic?
A minimal, correct default deny for ingress
  • This policy selects all pods in the namespace and denies all ingress (because there are no ingress rules):
Common misconfigurations to look for
  • policy placed in the wrong namespace.
  • podSelector targets the wrong label(s) (so the deny protects nothing).
  • policyTypes set only to Egress (so ingress is not denied).
  • The policy includes an ingress rule that inadvertently allows traffic (for example, a broad from).
How to allow just the required paths
  • Create a NetworkPolicy that selects the destination pods and allows ingress only from the specific source pods and port.
Example: allow frontend → middleware (middleware allows only traffic from frontend to TCP/80)
Example: allow middleware → mysql (MySQL allows only middleware to TCP/3306)
Why these policies enforce rule #3 (frontend must NOT reach MySQL)
  • The middleware-to-mysql policy allows only pods labeled app=middleware to connect to MySQL on 3306. Because default-deny-ingress blocks all other ingress, frontend pods (which are not app=middleware) cannot reach MySQL directly.
Testing connectivity from pods
  1. Find a frontend pod name:
  1. Exec into a frontend pod and test:
  1. Exec into the middleware pod and test MySQL:
Alternate test (run a one-off netcat pod)
Quick answers to the follow-up questions
  • 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 (see kubectl get networkpolicy ... -o yaml) and correct those fields.
  • 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 ingress rules 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.
Links and references

Watch Video