> ## Documentation Index
> Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Opening Paths Through Default Deny The Problem

> Configuring Kubernetes NetworkPolicies to implement a default deny and allow only specific ingress paths between frontend, middleware, and MySQL while testing and avoiding common misconfigurations.

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.](https://mintcdn.com/kodekloud-c4ac6d9a/UCHQITKCSrYPqxtk/images/Learn-By-Doing-Kubernetes-Network-Policies/Opening-Paths/Opening-Paths-Through-Default-Deny-The-Problem/kubernetes-safe-broken-frontend-middleware-mysql.jpg?fit=max\&auto=format\&n=UCHQITKCSrYPqxtk\&q=85\&s=afd350c2516a072b797421a83689504f)

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:

| Component | Pod label example | Service port |
| - | -: | -: |
| Frontend pods | `app=frontend` | `80` |
| Middleware pod | `app=middleware` | `80` |
| MySQL pod | `app=mysql` | `3306` |

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:

```bash theme={null}
kubectl get networkpolicy default-deny-ingress -n default -o 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):

```yaml theme={null}
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: default-deny-ingress
  namespace: default
spec:
  podSelector: {}        # selects all pods in the namespace
  policyTypes:
  - Ingress
  # no 'ingress' rules -> deny all ingress to selected pods
```

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)

```yaml theme={null}
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: frontend-to-middleware
  namespace: default
spec:
  podSelector:
    matchLabels:
      app: middleware
  policyTypes:
  - Ingress
  ingress:
  - from:
    - podSelector:
        matchLabels:
          app: frontend
    ports:
    - protocol: TCP
      port: 80
```

Example: allow middleware → mysql (MySQL allows only middleware to TCP/3306)

```yaml theme={null}
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: middleware-to-mysql
  namespace: default
spec:
  podSelector:
    matchLabels:
      app: mysql
  policyTypes:
  - Ingress
  ingress:
  - from:
    - podSelector:
        matchLabels:
          app: middleware
    ports:
    - protocol: TCP
      port: 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:

```bash theme={null}
kubectl get pods -l app=frontend -o wide -n default
```

2. Exec into a frontend pod and test:

```bash theme={null}
kubectl exec -it <frontend-pod> -n default -- sh
# inside the pod:
# try middleware (should succeed)
nc -vz middleware 80

# try mysql (should fail)
nc -vz mysql 3306
```

3. Exec into the middleware pod and test MySQL:

```bash theme={null}
kubectl exec -it <middleware-pod> -n default -- sh
# inside the pod:
nc -vz mysql 3306    # should succeed
```

Alternate test (run a one-off netcat pod)

```bash theme={null}
kubectl run --rm -it netcheck --image=busybox --restart=Never -- sh
# from inside:
nc -vz middleware 80
nc -vz mysql 3306
```

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

* Kubernetes NetworkPolicy docs: [https://kubernetes.io/docs/concepts/services-networking/network-policies/](https://kubernetes.io/docs/concepts/services-networking/network-policies/)
* Netcat (nc): [https://en.wikipedia.org/wiki/Netcat](https://en.wikipedia.org/wiki/Netcat)
* CNIs that support NetworkPolicy: Calico, Cilium, Weave Net, Canal (check your cluster provider docs)

> [card] [Watch Video](https://learn.kodekloud.com/user/courses/learn-by-doing-kubernetes-network-policies/module/6a4de0c0-fe7e-48e4-8401-0f55ef9eaf32/lesson/cf012e8f-c633-400e-ade4-d0ac0e82dd7f)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.