Skip to main content
Let’s pick up where the troubleshooting begins.

Inspect the existing deny policy

Bob first inspects the NetworkPolicy that is supposed to provide a default deny for ingress in the default namespace:
The top of the stored policy looks straightforward: the podSelector is an empty selector ({}), and policyTypes contains Ingress.
However, further down the document there is an ingress section with a rule that includes podSelector: {} under from. That empty selector matches all pods in the namespace — meaning the rule permits ingress from every pod.
In selector contexts an empty map ({}) means “match all.” For podSelector: {} that equates to every pod in the namespace. So an explicit from: - podSelector: {} actually allows traffic from all namespace pods — which defeats a default-deny policy.
Because of that permissive rule, the resource named default-deny-ingress was, in practice, allowing all namespace-internal ingress. Any subsequent “allow” policy would be redundant while this permissive rule exists.

Fix the default-deny baseline

To turn the resource back into a true deny-all baseline Bob keeps the same resource metadata but removes the permissive rule by setting ingress to an empty list. An empty ingress: [] means “no sources are allowed”:
Then Bob saves the corrected YAML to a file and replaces the live resource:
Be careful with kubectl replace — it overwrites the existing resource. Use kubectl replace -f only when you intend to fully replace the live object with the file contents. If you want to create or update, kubectl apply -f is generally safer.
With the corrected baseline in place, the cluster now denies ingress to pods by default, and fine-grained allow policies can be layered on top.

Add specific allow rules (front-end → middleware → MySQL)

Bob creates two focused ingress policies that open only the intended paths.
  1. Allow frontend → middleware (targets middleware pods):
Because this policy contains ingress rules, Kubernetes treats it as an ingress policy even though policyTypes is omitted.
  1. Allow middleware → MySQL (targets MySQL pods):
Notice what is intentionally omitted: there is no policy that allows frontend → MySQL. Under a default-deny baseline, omitting a rule is how you block traffic — you simply do not open the path. Apply both policies:

Verify connectivity (quick Netcat checks)

Bob tests connectivity from inside pods using nc. He uses -w1 so checks time out quickly when connections are blocked.
  • From a frontend pod to the middleware service on port 80 — expected: open.
  • From the frontend pod to the MySQL service on port 3306 — expected: blocked (times out).
  • From a middleware pod to the MySQL service on port 3306 — expected: open.
Example console session:
All three intended outcomes are met: the application works, and the database answers only to the middleware tier — exactly Bob’s intent.
A stylized diagram titled "All Three Rules Pass" showing frontend-deploy, middleware, and mysql represented by smiling blue pod icons with locks and labels. Green arrows and checkmarks show allowed frontend→middleware→mysql traffic, while a red arc with a lock and cross marks a blocked direct frontend→mysql connection.
Only the correct tier reaches the database; every other pod is turned away.

Quick summary & best practices

  • Always inspect the actual ingress/egress rules — resource names can be misleading.
  • podSelector: {} or omitting selector means “match all” in selector contexts.
  • Use ingress: [] to explicitly deny all sources for the selected pods.
  • Build explicit allow policies to create minimal paths (principle of least privilege).
  • Test connectivity from within pods; quick TCP checks with nc -zv -w1 are helpful.
Table: policies applied in this example

Watch Video

Practice Lab