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

# Writing Default Deny Policies Hands On

> Hands-on demonstration of using Kubernetes NetworkPolicies to create default-deny ingress rules, test effects, and restore minimal allowed service-to-service traffic

In this hands-on walkthrough, Bob inspects a Kubernetes cluster, verifies application connectivity, and experiments with default-deny NetworkPolicies to lock down a namespace. Objectives:

* Inspect cluster resources and CNI
* Verify service connectivity using a pod
* Create and apply default-deny ingress NetworkPolicies
* Observe differences between absent `ingress` and `ingress: []`
* Outline how to restore only the minimal required traffic paths

## Inspect the cluster

Bob starts by listing pods and services in the `default` namespace to understand what's running:

```bash theme={null}
$ kubectl get pods,svc
NAME                                              READY   STATUS    RESTARTS   AGE
pod/frontend-deploy-744dddcc79-f7bp4              1/1     Running   0          58s
pod/frontend-deploy-744dddcc79-jt2zg              1/1     Running   0          58s
pod/middleware                                    1/1     Running   0          58s
pod/mysql                                         1/1     Running   0          58s
```

Below the pods there are three services (two serving on port 80 and one on port 3306).

He confirms there are no existing NetworkPolicies in `default`:

```bash theme={null}
$ kubectl get networkpolicy -n default
# No network policies are present yet
```

To determine which CNI is enforcing network policies, Bob lists pods across all namespaces and inspects `kube-system`:

```bash theme={null}
$ kubectl get pods -A
# In kube-system there are pods whose names begin with Canal
```

He opens the Canal CNI config and sees that Canal delegates policy enforcement to Calico. The active CNI is Canal (with Calico providing policy enforcement). For more detail, see the Canal and Calico project pages:

* [Canal (Project Calico + Flannel)](https://github.com/projectcalico/canal)
* [Project Calico (network policy enforcement)](https://projectcalico.org/)

NetworkPolicy enforcement depends on your CNI plugin. If your cluster uses a CNI that doesn't support Kubernetes NetworkPolicies, the policies will have no effect—Calico and Canal are common CNIs that support them.

## Verify connectivity between services

With no NetworkPolicies in place, Bob verifies connectivity from a frontend pod to the middleware and MySQL services. He lists frontend pods, chooses one, and runs netcat inside it. Netcat is run with `-z` (scan without sending data), `-v` (verbose), and `-w 3` (3-second timeout) so the command doesn't hang:

```bash theme={null}
$ kubectl get pods --selector app=frontend
NAME                              READY   STATUS    RESTARTS   AGE
frontend-deploy-744dddcc79-f7bp4  1/1     Running   0          8m22s
frontend-deploy-744dddcc79-jt2zg  1/1     Running   0          8m22s

# From a frontend pod, test middleware:80 and mysql:3306
$ kubectl exec frontend-deploy-744dddcc79-f7bp4 -- nc -zv -w 3 middleware 80
Connection to middleware 80 port [tcp/http] succeeded!
$ kubectl exec frontend-deploy-744dddcc79-f7bp4 -- nc -zv -w 3 mysql 3306
Connection to mysql 3306 port [tcp/mysql] succeeded!
```

Both services respond, which is expected when no NetworkPolicies restrict traffic.

## Apply a default-deny ingress policy

Bob writes his first policy to deny all ingress to pods in the `default` namespace. The `podSelector` is empty (`{}`), selecting all pods, `policyTypes` contains only `Ingress`, and no `ingress` rules are provided:

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

Because this policy selects all pods and declares it applies to ingress but contains no `ingress` rules, it prevents any incoming traffic to those selected pods. Bob applies the policy:

```bash theme={null}
$ kubectl apply -f default-deny-ingress.yaml
networkpolicy.networking.k8s.io/default-deny-ingress created
```

From this point, incoming connections to pods in `default` are denied unless another NetworkPolicy explicitly allows them.

## Add a broad allow policy (to demonstrate policy behavior)

Next, Bob adds a second policy, `default-allow-ingress`, that also selects all pods (`podSelector: {}`) but contains a single, empty ingress rule (`- {}`), which matches all sources and allows all incoming traffic:

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

Now two policies select the same pods: one that allows nothing, and one that allows everything. The cluster diagram below illustrates these policies and the service relationships.

![A stylized Kubernetes diagram showing frontend, middleware, and MySQL pods and their services with arrows indicating traffic flows. Locks and labeled policy badges (default-deny-ingress / default-allow-ingress) illustrate network policies controlling access.](https://mintcdn.com/kodekloud-c4ac6d9a/UCHQITKCSrYPqxtk/images/Learn-By-Doing-Kubernetes-Network-Policies/Default-Deny/Writing-Default-Deny-Policies-Hands-On/kubernetes-network-policies-frontend-mysql-diagram.jpg?fit=max\&auto=format\&n=UCHQITKCSrYPqxtk\&q=85\&s=f898e5c4b7e2efe7298f77cce6674b1b)

Bob re-tests connectivity from a frontend pod to the middleware and MySQL services:

```bash theme={null}
$ kubectl exec frontend-deploy-744dddcc79-f7bp4 -- nc -zv -w 3 middleware 80
Connection to middleware 80 port [tcp/http] succeeded!
$ kubectl exec frontend-deploy-744dddcc79-f7bp4 -- nc -zv -w 3 mysql 3306
Connection to mysql 3306 port [tcp/mysql] succeeded!
```

Both connections are still allowed.

NetworkPolicies are additive: when multiple policies select the same pod, traffic is allowed if any matching policy permits it. There is no "priority" where a deny in one policy overrides an allow in another—omitting rules effectively denies, while an explicit allow in any policy permits traffic.

## Expressing “no ingress” — absent `ingress` vs `ingress: []`

Bob explores two equivalent ways to express "no ingress rules." He deletes existing policies and creates a policy named `default-deny-ingress-null` that omits the `ingress` field entirely:

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

He creates it:

```bash theme={null}
$ kubectl create -f /root/default-deny-netpol.yaml
networkpolicy.networking.k8s.io/default-deny-ingress-null created
```

This denies all ingress to the selected pods. An equivalent variant sets `ingress` to an empty list (`ingress: []`). Both forms are functionally identical: they result in no allowed ingress. When the cluster stores or prints these policies, it may show either representation. For example, the cluster might present both policies like:

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

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

Either omitting the `ingress` field or setting it to an empty list (`[]`) yields the same behavior: no ingress is allowed.

## Effect: namespace locked down and application broken

With a default-deny ingress policy in place that selects all pods, the namespace is locked down. Incoming connections required by the application are dropped, so the frontend can no longer reach middleware and middleware can no longer reach MySQL.

![A stylized architecture diagram titled "Locked Down and Broken" showing frontend, middleware, and MySQL pods/services inside a Kubernetes "default" namespace with lock icons and a blocked link between frontend and middleware. Service names and ports (frontend-svc: 80, middleware-svc: 80, mysql-svc: 3306) are also displayed.](https://mintcdn.com/kodekloud-c4ac6d9a/UCHQITKCSrYPqxtk/images/Learn-By-Doing-Kubernetes-Network-Policies/Default-Deny/Writing-Default-Deny-Policies-Hands-On/k8s-locked-broken-frontend-middleware-mysql.jpg?fit=max\&auto=format\&n=UCHQITKCSrYPqxtk\&q=85\&s=6615cafdd317daac7e79ce0bf7ec32c5)

## Next step — restore only the minimal allowed paths

To restore required functionality while keeping the namespace secure, add minimal, specific allow rules that permit only the required service-to-service connections. For example:

* Allow frontend → middleware on port 80
* Allow middleware → mysql on port 3306

A practical approach is to create focused NetworkPolicies that select the destination pod(s) and allow ingress only from specific pod selectors and ports.

## Quick reference: examples and effects

| Policy name | Key fields | Effect |
| - | - | - |
| Default deny (select all) | `podSelector: {}` `policyTypes: ["Ingress"]` | Denies all ingress to selected pods when no `ingress` rules are present. |
| Default allow (empty rule) | `podSelector: {}` `ingress: - {}` | Allows all ingress to selected pods (empty rule matches all sources). |
| No ingress vs empty list | Omitted `ingress` vs `ingress: []` | Both represent "no ingress allowed"; Kubernetes treats them equivalently. |

> Note: in the table above, examples with curly braces are shown as inline code (e.g., `podSelector: {}`) so MDX does not interpret them as object literals.

## Useful commands and links

Commands

* List pods and services: `kubectl get pods,svc`
* List all NetworkPolicies in a namespace: `kubectl get networkpolicy -n <namespace>`
* Apply a policy: `kubectl apply -f <policy.yaml>`
* Test connectivity from a pod: `kubectl exec <pod> -- nc -zv -w 3 <host> <port>`

References

* [Kubernetes NetworkPolicy documentation](https://kubernetes.io/docs/concepts/services-networking/network-policies/)
* [Canal (projectcalico/canal)](https://github.com/projectcalico/canal)
* [Project Calico](https://projectcalico.org/)

If you want, I can show example minimal allow NetworkPolicy manifests for the frontend→middleware and middleware→mysql paths.

> [card] [Watch Video](https://learn.kodekloud.com/user/courses/learn-by-doing-kubernetes-network-policies/module/bd8e8eab-1b78-46b4-8c63-e6e640399c54/lesson/0f4e8cda-e747-4266-8628-e7058bfa791e)

> [card] [Practice Lab](https://learn.kodekloud.com/user/courses/learn-by-doing-kubernetes-network-policies/module/bd8e8eab-1b78-46b4-8c63-e6e640399c54/lesson/e2ec3231-3170-4527-89d5-2664a66206a9)


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