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

# Fine Grained Ingress Policies Hands On

> Guides fixing a permissive Kubernetes NetworkPolicy, establishing a default deny ingress baseline, adding targeted allow rules for frontend to middleware to MySQL, and verifying connectivity

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:

```bash theme={null}
kubectl get networkpolicy default-deny-ingress -o yaml
```

The top of the stored policy looks straightforward: the `podSelector` is an empty selector (`{}`), and `policyTypes` contains `Ingress`.

```yaml theme={null}
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: default-deny-ingress
  namespace: default
spec:
  podSelector: {}
  policyTypes:
    - 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.

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

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”:

```yaml theme={null}
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: default-deny-ingress
  namespace: default
spec:
  podSelector: {}
  policyTypes:
    - Ingress
  ingress: []
```

Then Bob saves the corrected YAML to a file and replaces the live resource:

```bash theme={null}
kubectl replace -f default-deny-ingress.yaml
```

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):

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

Because this policy contains `ingress` rules, Kubernetes treats it as an ingress policy even though `policyTypes` is omitted.

2. Allow middleware → MySQL (targets MySQL pods):

```yaml theme={null}
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: middleware-to-mysql
  namespace: default
spec:
  podSelector:
    matchLabels:
      app: mysql
  ingress:
    - from:
        - podSelector:
            matchLabels:
              app: middleware
```

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:

```bash theme={null}
kubectl apply -f frontend-to-middleware.yaml
kubectl apply -f middleware-to-mysql.yaml
```

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

```bash theme={null}
$ kubectl apply -f frontend-to-middleware.yaml
networkpolicy.networking.k8s.io/frontend-to-middleware created
$ kubectl apply -f middleware-to-mysql.yaml
networkpolicy.networking.k8s.io/middleware-to-mysql created

$ kubectl exec frontend-deploy-744dddcc79-nwhdc -- \
  nc -zv -w1 middleware-svc 80
middleware-svc (10.105.169.89:80) open

$ kubectl exec frontend-deploy-744dddcc79-nwhdc -- \
  nc -zv -w1 mysql-svc 3306
nc: mysql-svc (10.96.106.81:3306): Operation timed out
command terminated with exit code 1

$ kubectl exec middleware-deploy-abcdef123 -- \
  nc -zv -w1 mysql-svc 3306
mysql-svc (10.96.106.81:3306) open
```

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.](https://mintcdn.com/kodekloud-c4ac6d9a/UCHQITKCSrYPqxtk/images/Learn-By-Doing-Kubernetes-Network-Policies/Opening-Paths/Fine-Grained-Ingress-Policies-Hands-On/frontend-middleware-mysql-network-policy-diagram.jpg?fit=max\&auto=format\&n=UCHQITKCSrYPqxtk\&q=85\&s=09596e9a2099afa21a3a2db21a3eb7aa)

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

| Policy name | Targets (podSelector) | Allows traffic from |
| - | - | - |
| `default-deny-ingress` | `podSelector: {}` | `ingress: []` (no sources allowed) |
| `frontend-to-middleware` | `matchLabels: app: middleware` | `podSelector matchLabels: app: frontend` |
| `middleware-to-mysql` | `matchLabels: app: mysql` | `podSelector matchLabels: app: middleware` |

## Links and references

* [Kubernetes NetworkPolicies — official docs](https://kubernetes.io/docs/concepts/services-networking/network-policies/)
* [kubectl cheat sheet](https://kubernetes.io/docs/reference/kubectl/cheatsheet/)

> [card] [Watch Video](https://learn.kodekloud.com/user/courses/learn-by-doing-kubernetes-network-policies/module/6a4de0c0-fe7e-48e4-8401-0f55ef9eaf32/lesson/510c0854-3375-4188-af47-427cf8f07130)

> [card] [Practice Lab](https://learn.kodekloud.com/user/courses/learn-by-doing-kubernetes-network-policies/module/6a4de0c0-fe7e-48e4-8401-0f55ef9eaf32/lesson/10703f2b-3bcd-4838-bc8e-5b79ce7fb688)


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