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

# Testing Policy Support Across CNIs Hands On

> Hands-on experiment comparing Flannel and Canal CNIs to show that Calico enforces Kubernetes NetworkPolicy while Flannel alone does not, using a deny-all policy.

In this lesson we run a short experiment to confirm how different CNI plugins handle Kubernetes NetworkPolicies. We'll compare a Flannel-only cluster with a Canal cluster (Flannel + Calico) and apply the same "deny-all" NetworkPolicy to observe enforcement differences.

Bob starts with a warm-up question: What is a CNI for?

Answer: to establish and manage container networking — creating pod IPs, routing between pods, and (optionally) enforcing NetworkPolicy.

![A slide titled "What Is a CNI For?" showing two panels labeled cluster1 (with Flannel) and cluster2 (with a Canal box containing Flannel and Calico icons). A small avatar labeled "Bob" is on the left and a KodeKloud logo sits in the bottom-left.](https://mintcdn.com/kodekloud-c4ac6d9a/UCHQITKCSrYPqxtk/images/Learn-By-Doing-Kubernetes-Network-Policies/Flannel-vs-Canal/Testing-Policy-Support-Across-CNIs-Hands-On/what-is-cni-flannel-canal-calico.jpg?fit=max\&auto=format\&n=UCHQITKCSrYPqxtk\&q=85\&s=a67eb36f71b40b10038e02a919326945)

Overview

* Goal: verify whether a "deny-all" NetworkPolicy actually blocks traffic on two clusters that use different CNIs.
* Cluster layout:
  * cluster1: Flannel (pod networking only; no NetworkPolicy enforcement)
  * cluster2: Canal (Flannel for networking + Calico for policy enforcement)

## 1) Inspect cluster1 (Flannel)

Switch kubeconfig context to cluster1 and list pods across all namespaces to find the Flannel DaemonSet:

```bash theme={null}
$ kubectl config use-context cluster1
Switched to context "cluster1".
$ kubectl get pods -A
NAMESPACE       NAME                           READY   STATUS    RESTARTS   AGE
kube-flannel    kube-flannel-ds-c8tv7         1/1     Running   0          4m29s
kube-flannel    kube-flannel-ds-kd4cc         1/1     Running   0          4m29s
kube-flannel    kube-flannel-ds-pnxjx         1/1     Running   0          4m29s
```

DS = DaemonSet — one Flannel pod per node. To confirm the plugin type you can inspect the CNI configuration on the control plane:

```bash theme={null}
$ ssh cluster1-controlplane
$ cat /etc/cni/net.d/10-flannel.conflist
{
  "name": "cbr0",
  "cniVersion": "0.3.1",
  "plugins": [
    {
      "type": "flannel",
      …
```

This shows the cluster is configured with Flannel.

### Create two test pods (cluster1)

Create two NGINX pods in the `default` namespace and check their IPs:

```bash theme={null}
$ kubectl run kodekloud  --image=nginx --port=80
pod/kodekloud created
$ kubectl run community --image=nginx --port=80
pod/community created
$ kubectl get pods -o wide
NAME        READY   STATUS    RESTARTS   AGE   IP           NODE
community   1/1     Running   0          12s   10.244.2.3   cluster1-node02
kodekloud   1/1     Running   0          18s   10.244.2.2   cluster1-node02
```

From `kodekloud` curl the `community` pod IP to verify connectivity (should return the NGINX welcome page):

```bash theme={null}
$ kubectl exec kodekloud -- curl 10.244.2.3
<html>
<head>
<title>Welcome to nginx!</title>
...
```

### Apply a "deny-all" NetworkPolicy (cluster1)

Create the following policy which intends to block all ingress and egress for pods in the `default` namespace:

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

Apply it:

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

Re-run the curl from `kodekloud` to `community`:

```bash theme={null}
$ kubectl exec kodekloud -- curl 10.244.2.3
<html>
<head>
<title>Welcome to nginx!</title>
...
```

Observation and explanation:

* Even after the policy object exists, traffic is still allowed. Flannel sets up the pod network but does not implement NetworkPolicy enforcement. The NetworkPolicy resource is stored in the API server, but Flannel does not enforce it — so connectivity remains unchanged.

## 2) Inspect cluster2 (Canal = Flannel + Calico)

Switch context to cluster2 and create two more NGINX pods, `alpha` and `beta`:

```bash theme={null}
$ kubectl config use-context cluster2
Switched to context "cluster2".
$ kubectl run alpha --image=nginx --port=80
pod/alpha created
$ kubectl run beta --image=nginx --port=80
pod/beta created
$ kubectl get pods -o wide
NAME    READY   STATUS    RESTARTS   AGE   IP           NODE
alpha   1/1     Running   0          20s   10.244.1.2   cluster2-node01
beta    1/1     Running   0          14s   10.244.1.3   cluster2-node01
```

Verify connectivity before applying policy:

```bash theme={null}
$ kubectl exec alpha -- curl 10.244.1.3
<html>
<head>
<title>Welcome to nginx!</title>
...
```

Apply the same `deny-all` policy in the `default` namespace on cluster2:

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

After Calico reads and enforces the policy, requests are blocked:

```bash theme={null}
$ kubectl exec alpha -- curl 10.244.1.3
# No response — request hangs (traffic dropped by Calico)
```

Observation and explanation:

* Canal uses Flannel for pod networking but runs Calico on each node for policy enforcement. Calico implements and enforces NetworkPolicy; therefore the same deny-all policy blocks traffic in cluster2.

## Why the different behavior?

Summary table:

| Cluster | CNI components | NetworkPolicy enforcement? | Result after applying `deny-all` |
| - | - | - | - |
| cluster1 | Flannel only | No | Traffic still allowed (policy not enforced) |
| cluster2 | Flannel + Calico (Canal) | Yes (Calico enforces) | Traffic blocked (policy enforced) |

Key point: A NetworkPolicy resource only has effect when the CNI plugin actually implements enforcement. Flannel provides networking but does not enforce policies; Calico provides enforcement.

A NetworkPolicy object only takes effect when the cluster's CNI plugin implements enforcement. Some CNI plugins (like Calico) enforce NetworkPolicy; others (like Flannel alone) do not.

Additional notes and best practices

* The empty `podSelector: {}` in the policy selects all pods in the namespace — that is intentional for a true "deny-all" policy.
* When testing NetworkPolicy behavior, always confirm:
  * Which CNI components are installed (DaemonSets, Config files).
  * That Calico or another policy-capable CNI is present when expecting enforcement.
* For reproducible verification use pod IPs shown by `kubectl get pods -o wide` and `kubectl exec` to run client requests.

Conclusion

* A NetworkPolicy will only restrict traffic if the underlying CNI implements enforcement. For hands-on policy work and verifiable rule enforcement, use clusters that include a policy-capable CNI (e.g., Calico). From here on, Bob will continue experiments only on clusters where NetworkPolicies are actually enforced.

> [card] [Watch Video](https://learn.kodekloud.com/user/courses/learn-by-doing-kubernetes-network-policies/module/e7fde9a6-ce21-4c90-a3c6-a7530076e87e/lesson/73de139a-d2f2-485c-a45d-36a75e3b70ed)

> [card] [Practice Lab](https://learn.kodekloud.com/user/courses/learn-by-doing-kubernetes-network-policies/module/e7fde9a6-ce21-4c90-a3c6-a7530076e87e/lesson/934f4a48-cee1-4871-8afa-83892e3010b1)


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