πŸ” Kubernetes Network Policies Demystified – From Basics to Advanced (2025 Edition)

In Kubernetes, everything is designed to be dynamic and open by default β€” including network communication. While that’s great for flexibility, it’s a nightmare for security.

Enter:

πŸ” Kubernetes Network Policies – your firewall for Pod-level communication.

Whether you’re building a multi-tenant app or locking down microservices, Network Policies give you fine-grained control over who can talk to whom.

In this blog, we’ll break it all down β€” simply, clearly, and with real-world examples.


🧠 What Are Network Policies in Kubernetes?

By default, all pods in Kubernetes can talk to each other. No restrictions.

Network Policies are rules that:

  • Allow or block traffic to or from pods
  • Work at Layer 3 & 4 (IP + port) level
  • Are enforced by a CNI plugin like Calico, Cilium, or Weave

βœ… Think of it like iptables for pods β€” but declarative and namespace-aware.


βš™οΈ Prerequisite: Your CNI Plugin Must Support It

Not all CNIs enforce Network Policies.

CNI PluginNetworkPolicy Support
Calicoβœ… Yes (Full)
Ciliumβœ… Yes + eBPF
Weaveβœ… Yes (Basic)
Flannel❌ No (use with Calico)
AWS VPC CNIβœ… Yes (with extra config)

πŸ” You can check support by reviewing your cluster’s network plugin or running:

kubectl get pods -n kube-system

Look for names like calico-node, cilium-agent, etc.


🧱 Basic Anatomy of a NetworkPolicy

apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: allow-nginx
  namespace: default
spec:
  podSelector:
    matchLabels:
      app: nginx
  policyTypes:
    - Ingress
  ingress:
    - from:
        - podSelector:
            matchLabels:
              app: frontend

Key Sections:

  • podSelector: Which pods this policy applies to
  • policyTypes: Ingress, Egress, or both
  • ingress/egress: Allowed sources or destinations

πŸ”’ Ingress vs Egress

TypeControls
IngressWho can connect to the selected pods
EgressWhere the selected pods can connect to

By default:

  • If no NetworkPolicy exists, traffic is allowed.
  • If one NetworkPolicy exists, and it has no rules β€” it blocks everything (deny by default starts here).

πŸ“¦ Use Case Examples

1️⃣ Allow Only Frontend to Talk to Nginx

podSelector:
  matchLabels:
    app: nginx
ingress:
  - from:
      - podSelector:
          matchLabels:
            app: frontend

🚫 This blocks all other pods from reaching Nginx.


2️⃣ Allow Ingress from Same Namespace Only

ingress:
  - from:
      - namespaceSelector:
          matchLabels:
            name: default

βœ… Useful for isolating tenants by namespace.


3️⃣ Allow Egress to Internet (DNS & HTTPS only)

egress:
  - to:
      - ipBlock:
          cidr: 0.0.0.0/0
    ports:
      - protocol: TCP
        port: 443
      - protocol: UDP
        port: 53
policyTypes:
  - Egress

βœ… Allows your pod to pull images, call APIs, or resolve DNS β€” but nothing else.


4️⃣ Deny All Ingress

podSelector:
  matchLabels:
    app: sensitive
ingress: []

🚫 Fully blocks all inbound traffic to pods with label app=sensitive.


5️⃣ Allow Traffic from a Specific Namespace

ingress:
  - from:
      - namespaceSelector:
          matchLabels:
            project: web

πŸ” Great for multi-team clusters with isolated dev, staging, and prod namespaces.


πŸ§ͺ Testing Network Policies

You can test enforcement using tools like:

  • kubectl exec into a pod and try to curl another service
  • Use network testing tools:

Example:

kubectl exec -it test-pod -- curl nginx.default.svc.cluster.local

🧠 Advanced NetworkPolicy Tips

TipExplanation
βœ… Start with Allow-Only PoliciesDeclare what’s allowed, assume rest is blocked
πŸ” Combine with RBAC & SecretsNetworkPolicy β‰  identity or data protection
πŸ›  Use Labels StrategicallyGroup and select pods with intent
🧾 Version Control YAMLsApply with CI/CD and GitOps
🚧 Avoid Wide ipBlocksToo broad? It’s no longer secure
πŸ“‘ Use Calico Enterprise or Cilium UIFor visual policy dashboards

πŸ“š Recommended Tools

ToolPurpose
Netpol VisualizerView policies as graphs
Calico Policy ManagerGUI for Calico security
K9sCLI for Kubernetes navigation
KubescapeSecurity audits for policies
OPA + GatekeeperPolicy-as-code validation engine

πŸ›‘ Network Policy β‰  Firewall for Nodes

Important:
Network Policies only affect Pod-to-Pod communication.

They don’t:

  • Block host networking
  • Protect node-level traffic (use firewalls for that)
  • Replace TLS or mutual authentication

πŸš€ Final Thoughts

Network Policies may seem overwhelming at first, but they’re critical for zero trust Kubernetes.

Start simple: deny all ingress to a pod.
Then allow only what’s required β€” nothing more.

πŸ” In today’s cloud-native world:

  • Apps are dynamic.
  • Threats are real.
  • Secure defaults + declarative controls = resilient systems.

🧩 TL;DR β€” Network Policy Quick Recap

ItemExample
Block all ingressingress: []
Allow frontend β†’ backendpodSelector-based
Allow DNS & HTTPS egressegress + ports
Apply to namespacenamespaceSelector
VisualizeNetpol Visualizer, Calico UI

Category: 
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments