> ## 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.

# Default Deny The Problem

> Describes using a default deny Kubernetes NetworkPolicy to block all pod ingress, then incrementally add minimal allow rules for frontend to middleware and middleware to MySQL traffic.

The first policy Bob writes should close every door.

In this cluster (two nodes running Canal), NetworkPolicy objects are actually enforced. In the `default` namespace there is a simple three‑tier app split into:

* Frontend: a Deployment named `frontend-deploy` with two pods, both labeled `app=frontend`. This is what users talk to.
* Middleware: a single pod labeled `app=middleware`. It accepts requests from the frontend and reads/writes data.
* Database: a MySQL pod labeled `app=mysql`.

Each tier is exposed via a ClusterIP Service. The frontend and middleware services listen on port 80; the MySQL service listens on port 3306.

| Tier | Kubernetes object(s) | Pod label | Service port |
| - | - | - | - |
| Frontend | `Deployment` (`frontend-deploy`) | `app=frontend` | `80` |
| Middleware | `Pod` | `app=middleware` | `80` |
| Database | `Pod` (MySQL) | `app=mysql` | `3306` |

Right now there are no NetworkPolicies in the namespace. That means:

* A frontend pod can reach the middleware (intended).
* A frontend pod can also reach MySQL on port `3306` (not intended).

If an attacker compromises a frontend pod, the database is only one step away. Bob wants to stop that by closing unnecessary network paths first and then opening the exact paths the app needs. This approach is called a default deny (also known as a baseline policy).

![A stylized security diagram titled "Secure First, Working Later" showing frontend, middleware, and MySQL service pods with padlock icons and blocked red links inside a default namespace. A user labeled "Bob" is shown on the left and services are listed with ports (frontend/middleware Port 80, MySQL Port 3306) under a "Default Deny" policy.](https://mintcdn.com/kodekloud-c4ac6d9a/UCHQITKCSrYPqxtk/images/Learn-By-Doing-Kubernetes-Network-Policies/Default-Deny/Default-Deny-The-Problem/secure-first-working-later-default-deny.jpg?fit=max\&auto=format\&n=UCHQITKCSrYPqxtk\&q=85\&s=606e445a4707b3d9cabdfbb2f75b11aa)

A default deny intentionally blocks all ingress traffic to selected pods. Expect the app to stop working until you add explicit allow rules for the paths it needs.

## Bob’s workflow (practical steps)

1. Inspect the namespace: list pods, services, and existing NetworkPolicy objects.
   * Example commands:
     * `kubectl get pods -n default -o wide`
     * `kubectl get svc -n default`
     * `kubectl get networkpolicy -n default`
2. Confirm the cluster CNI supports NetworkPolicy enforcement. Canal (Calico + Flannel) supports NetworkPolicy via Calico components.
   * Reference: [Kubernetes NetworkPolicy docs](https://kubernetes.io/docs/concepts/services-networking/network-policies/)
3. Test current connectivity from a frontend pod:
   * Exec into a frontend pod and curl the middleware and MySQL service to observe reachable paths:
     * `kubectl exec -it <frontend-pod> -n default -- curl -sS http://middleware:80`
     * `kubectl exec -it <frontend-pod> -n default -- nc -vz mysql 3306`
4. Write and apply a default deny NetworkPolicy named `default-deny-ingress` that selects every pod and declares `Ingress` as a policy type, but provides no ingress rules — this blocks incoming connections to all pods in the namespace.
5. With the deny present, add a second policy `default-allow-ingress` that explicitly allows everything. This creates two policies selecting the same pods but with opposite effects; in NetworkPolicy semantics the union of allowed traffic applies.
6. Experiment with deleting and re-applying policies to learn how different policy shapes express “no traffic” vs specific allows.

## Default deny examples

A minimal default deny NetworkPolicy (selects all pods and denies all ingress):

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

A policy that explicitly allows all ingress (selects all pods and permits any source/port):

```yaml theme={null}
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: default-allow-ingress
spec:
  podSelector: {}
  ingress:
  - {}                   # an empty ingress rule matches all sources/ports
```

## How NetworkPolicy rules combine

* NetworkPolicy semantics are additive. If one policy selects a pod and denies ingress (no ingress rules) and another policy selects the same pod and allows ingress (for example `ingress: - {}`), the union of the two policies results in the allowed traffic taking effect — effectively permitting connections that the allow policy describes.
* Practically: a `default-deny` baseline plus targeted allow policies is the recommended pattern. The deny provides a safe starting point; allow policies open only required paths.

| Question | Answer |
| - | - |
| When one policy denies everything and another allows everything, which one wins? | Allow wins in practice because NetworkPolicy permissions are the union of all matching policies. If any matching policy allows a connection, it is permitted. |
| What’s an alternate way to express “no traffic” in a NetworkPolicy? | Create a policy that selects the target pods and declares `policyTypes: [Ingress]` without any `ingress` rules (or with an empty `ingress: []`). That explicitly denies all ingress to selected pods. |

Tip: Use a strict default deny as a baseline. Then write minimal, intent-driven allow policies (e.g., frontend -> middleware on port 80, middleware -> mysql on 3306). Test each allow rule incrementally to restore only the required connectivity.

## Quick checklist and references

* Checklist:
  * Confirm CNI supports NetworkPolicy (Calico/Canal do).
  * Apply `default-deny-ingress` first.
  * Add specific allow policies for required paths:
    * Frontend → Middleware (`port: 80`, source `app=frontend` → dest `app=middleware`)
    * Middleware → MySQL (`port: 3306`, source `app=middleware` → dest `app=mysql`)
  * Verify connectivity after each change using `kubectl exec` and tools like `curl` or `nc`.

* Useful links:
  * [Kubernetes NetworkPolicy concept](https://kubernetes.io/docs/concepts/services-networking/network-policies/)
  * [Calico NetworkPolicy docs](https://docs.projectcalico.org/security/network-policy)
  * [Kubernetes Services](https://kubernetes.io/docs/concepts/services-networking/service/)

Keep the default deny as your baseline and add minimal allow rules to match the application’s real communication patterns.

> [card] [Watch Video](https://learn.kodekloud.com/user/courses/learn-by-doing-kubernetes-network-policies/module/bd8e8eab-1b78-46b4-8c63-e6e640399c54/lesson/ec4f0167-62f5-4969-be4c-b191bf8c3621)


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