⚡ ~/naveed K8s Hub
⚡ Portfolio Home 👤 About Naveed Bio & Credentials 📫 Contact Naveed Get In Touch ✍️ Engineering Blog Deep Dives 🎯 Interview Hub 1,000+ Scenarios ☸️ Kubernetes Mastery Hub 24 Modules 🎮 DevOps Arcade & Quizzes Subnet Blitz ⚡ 🗺️ DevOps Roadmaps PDFs & Guides 🤖 Morpheus Analysis AI Quant ↗ 🛠️ Developer Tools Utilities 🧪 Labs & Experiments 📄 Interactive CV & Certs 🔗 All Links & Socials ⚡ Join The Dispatch (Weekly SRE Newsletter) →
Phase 2 — Cluster Administration Module 12 of 24 Free & Open Access

Kubernetes Networking Deep Dive (CNI, NetworkPolicy)

Every Pod gets its own IP, and a CNI plugin makes Pod-to-Pod traffic work across nodes. NetworkPolicy then restricts which Pods may talk to each other, but only if your CNI enforces it. This module covers how CNI works and how to write default-deny policies that block only what you intend.

Last updated: · By Naveed Ahmed, Certified Kubernetes Administrator

12 - Networking Deep Dive (CNI, NetworkPolicy)

Why this matters

Networking is the #1 area where "it works" turns into a multi-hour debugging session. This topic is what actually separates people who can operate a cluster from people who can only deploy YAML into one someone else built.

Read this first — Definitions & Explanations

CNI (Container Network Interface)

Plugin interface that sets up Pod networking: interfaces, IPs, routes. Examples: Calico, Cilium, Flannel.

Pod network

Every Pod gets an IP. Nodes must route Pod ↔ Pod traffic according to the CNI design (overlay or routing-based).

NetworkPolicy

API for allowing/denying traffic to/from Pods. Only enforced if your CNI supports NetworkPolicy. Without support, policies may be stored but not applied.

Default-open reality

Many clusters allow all Pod-to-Pod traffic until you write restrictive NetworkPolicies. Don’t assume isolation exists by default.

CoreDNS

Cluster DNS. Services get DNS names; Pods resolve them via CoreDNS. DNS breakage looks like “random app failures.”

Overlay vs underlay

Official docs (read for detail)

Key Concepts

YouTube search terms

Hands-on lab (on prod-sim)

# Check current CNI
kubectl get pods -n kube-system | grep -i -E 'calico|cilium|flannel|kindnet'
# minikube's default is usually a simple bridge CNI - check what NetworkPolicy support it has

# Default-deny all ingress in a namespace, then allow one specific path
kubectl create namespace netpol-test
kubectl -n netpol-test create deployment web --image=nginx
kubectl -n netpol-test expose deployment web --port=80
kubectl -n netpol-test run client --image=busybox --restart=Never -- sleep 3600

# baseline: client can reach web
kubectl -n netpol-test exec client -- wget -qO- --timeout=2 web

cat <<EOF | kubectl apply -f -
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: default-deny
  namespace: netpol-test
spec:
  podSelector: {}
  policyTypes: ["Ingress"]
EOF
kubectl -n netpol-test exec client -- wget -qO- --timeout=2 web   # should now hang/fail

cat <<EOF | kubectl apply -f -
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: allow-client
  namespace: netpol-test
spec:
  podSelector:
    matchLabels:
      app: web
  policyTypes: ["Ingress"]
  ingress:
  - from:
    - podSelector:
        matchLabels:
          run: client
EOF
kubectl -n netpol-test exec client -- wget -qO- --timeout=2 web   # works again

# DNS troubleshooting drill
kubectl -n netpol-test exec client -- nslookup web.netpol-test.svc.cluster.local
kubectl -n kube-system logs -l k8s-app=kube-dns --tail=50

Note: if your CNI doesn't enforce NetworkPolicy, install Calico for this lab: minikube start -p prod-sim --cni=calico (recreate cluster with this flag).

Common mistakes and how to avoid them

  • Assuming NetworkPolicy works on any cluster. It only takes effect if the CNI enforces it, as Calico and Cilium do. On a CNI without support the objects are accepted and silently ignored.
  • Writing allow policies without a default deny. Pods that no policy selects stay fully open. Start with a default-deny policy, then add specific allows.
  • Forgetting DNS in egress policies. Blocking egress also blocks name resolution. Allow traffic to the cluster DNS Service on port 53.
  • Mixing up AND and OR in selectors. A podSelector and namespaceSelector in the same list item mean AND, while separate list items mean OR. This is a classic exam trap.
  • Overlapping Pod and Service CIDRs with the VPC or each other. Overlaps cause hard-to-trace routing failures. Plan the ranges before building the cluster, because they are difficult to change later.

Quick answers

What does a CNI plugin do?

A Container Network Interface plugin gives each Pod an IP address and connects it to the cluster network so Pods can reach each other across nodes. Calico, Cilium and Flannel are common examples. The container runtime calls the plugin when a Pod sandbox is created.

How do you create a default-deny NetworkPolicy?

Create a policy with an empty podSelector, which matches every Pod in the namespace, and list the policy types to block, such as Ingress and Egress, with no allow rules. Then add specific policies that open only the traffic you need.

Does NetworkPolicy work on every cluster?

No. The API accepts the object everywhere, but only a CNI that implements it, such as Calico or Cilium, enforces it. On a CNI without support, policies are silently ignored, so test with a connection that should be blocked instead of trusting the YAML.

Notes

(fill in your own words after watching + labbing)

📋 Self-Assessment Mastery Checklist (4 Competencies)
🧠 Practice Exam Questions (Module 12 MCQs)
⚡ Take Quiz & Save Progress in Tracker

Review these sample exam questions out loud, test your retrieval, and then unlock official scoring in the interactive tracker.

Question 1: CNI plugins are responsible for:
  • A. Pod network setup (interfaces/IPs/routes)
  • B. Scheduling Pods
  • C. Storing Secrets
  • D. Rendering Helm templates
✓ Correct Answer: A (Pod network setup (interfaces/IPs/routes))
Option A ('Pod network setup (interfaces/IPs/routes)') is the standard production architectural best practice.
Question 2: NetworkPolicy (with a supporting CNI) controls:
  • A. Which traffic Pods can send/receive
  • B. PVC size
  • C. API Priority and Fairness only
  • D. Image pull secrets
✓ Correct Answer: A (Which traffic Pods can send/receive)
Option A ('Which traffic Pods can send/receive') is the standard production architectural best practice.
Question 3: By default, without NetworkPolicies, Pods often can:
  • A. Talk freely within the cluster network (implementation-dependent but commonly open)
  • B. Never talk across namespaces
  • C. Only talk to etcd
  • D. Only use NodePort
✓ Correct Answer: A (Talk freely within the cluster network (implementation-dependent but commonly open))
Option A ('Talk freely within the cluster network (implementation-dependent but commonly open)') is the standard production architectural best practice.
Question 4: CoreDNS provides:
  • A. Cluster DNS resolution
  • B. Service load balancing L4 by itself
  • C. Volume snapshots
  • D. Admission webhooks
✓ Correct Answer: A (Cluster DNS resolution)
Option A ('Cluster DNS resolution') is the standard production architectural best practice.
Question 5: Overlay vs underlay networking refers to:
  • A. How Pod packets are encapsulated/routed on the node network
  • B. Helm chart types
  • C. RBAC models
  • D. StorageClasses
✓ Correct Answer: A (How Pod packets are encapsulated/routed on the node network)
Option A ('How Pod packets are encapsulated/routed on the node network') is the standard production architectural best practice.
← Previous Module (11) Installing & Upgrading Clusters (kubeadm) Next Module (13) → Troubleshooting & Debugging