Nextgen Blog
HomeLearnAIQuantumRoboticsSecuritySpaceAbout
Nextgen Blog

By Anubhav โ€” full stack engineer, IIT Mandi CSE. Deep dives into AI, software engineering, and the technologies shaping the next generation of tech.

Navigate

  • Home
  • Learn
  • Blog
  • Archive
  • About

Topics

  • Artificial Intelligence
  • Quantum Computing
  • Robotics
  • Cybersecurity
  • Space Tech

Connect

Built with โค๏ธ & By - Anubhav

ยฉ 2026 Nextgen Blog by Anubhav. All rights reserved.

Privacy PolicyTerms of Service
Phase 04 ยท Operations & SecurityChapter 21 of 33
20.
Phase 04Read time ~16 minDifficulty intermediate

Kubernetes Network Policies ๐ŸŒ๐Ÿ”

Warrior, you've reached a very important production-security chapter.

In this chapter
  • 01What Is NetworkPolicy?
  • 02Ingress vs Egress
  • 03Ingress
  • 04Egress
  • 05NetworkPolicy Architecture
  • 06Important: NetworkPolicy Isn't Implemented by the API Object Alone
  • 07Basic NetworkPolicy
  • 08podSelector
  • 09policyTypes
  • 10from
  • 11Port Restrictions
  • 12Complete Backend Ingress Policy
  • 13Important Behavior: Policies Are Additive
  • 14Default-Deny
  • 15Default-Deny Ingress
  • 16Why podSelector: {}?
  • 17Default-Deny Egress
  • 18Default Deny Both
  • 19The Production Pattern
  • 20Namespace Selectors
  • 21Namespace + Pod Selector
  • 22Important YAML Structure
  • 23Multiple from Entries
  • 24Backend + PostgreSQL Example
  • 25PostgreSQL Ingress Policy
  • 26Redis Policy
  • 27Why Protect Databases?
  • 28NetworkPolicy Is Not Authentication
  • 29NetworkPolicy + RBAC
  • 30RBAC
  • 31NetworkPolicy
  • 32Egress Policies
  • 33Egress Example
  • 34Egress Can Get Tricky
  • 35DNS Is Especially Important
  • 36DNS Policy Concept
  • 37Why DNS Matters
  • 38NetworkPolicy and Services
  • 39NetworkPolicy and Ingress
  • 40Default-Deny + Explicit Allow
  • 41Complete Example Architecture
  • 42A More Complete Backend Policy
  • 43NetworkPolicy Doesn't Mean "Deny Everything Automatically"
  • 44Namespace Boundary
  • 45Zero-Trust Network Model
  • 46NetworkPolicy + ServiceAccount
  • 47Common Mistakes
  • 48Mistake 1 โ€” Allowing everything
  • 49Mistake 2 โ€” Default-deny egress without DNS
  • 50Mistake 3 โ€” Wrong labels
  • 51Mistake 4 โ€” Wrong namespace
  • 52Mistake 5 โ€” Wrong port
  • 53Mistake 6 โ€” Assuming NetworkPolicy is enforced everywhere
  • 54Debugging NetworkPolicy
  • 55Step 1
  • 56Step 2
  • 57Step 3
  • 58Step 4
  • 59Step 5
  • 60Step 6
  • 61Debugging Mental Model
  • 62Interview Questions
  • 63Q1. What is NetworkPolicy?
  • 64Q2. Ingress vs Egress?
  • 65Q3. What does podSelector do?
  • 66Q4. What does an empty podSelector: {} mean?
  • 67Q5. Can NetworkPolicy control traffic across namespaces?
  • 68Q6. Does NetworkPolicy replace RBAC?
  • 69Q7. Does NetworkPolicy provide database authentication?
  • 70Q8. What is default-deny?
  • 71Q9. Why can default-deny egress break applications?
  • 72Q10. Who actually enforces NetworkPolicy?
  • 73Select Pods:
  • 74Select namespace:
  • 75Select ports:
  • 76Default deny:
  • 77One-line memory trick:
  • 78Next โ†’ Chapter 21: Logging ๐Ÿ“‹
In this chapter (78 sections)
01What Is NetworkPolicy?02Ingress vs Egress03Ingress04Egress05NetworkPolicy Architecture06Important: NetworkPolicy Isn't Implemented by the API Object Alone07Basic NetworkPolicy08podSelector09policyTypes10from11Port Restrictions12Complete Backend Ingress Policy13Important Behavior: Policies Are Additive14Default-Deny15Default-Deny Ingress16Why podSelector: {}?17Default-Deny Egress18Default Deny Both19The Production Pattern20Namespace Selectors21Namespace + Pod Selector22Important YAML Structure23Multiple from Entries24Backend + PostgreSQL Example25PostgreSQL Ingress Policy26Redis Policy27Why Protect Databases?28NetworkPolicy Is Not Authentication29NetworkPolicy + RBAC30RBAC31NetworkPolicy32Egress Policies33Egress Example34Egress Can Get Tricky35DNS Is Especially Important36DNS Policy Concept37Why DNS Matters38NetworkPolicy and Services39NetworkPolicy and Ingress40Default-Deny + Explicit Allow41Complete Example Architecture42A More Complete Backend Policy43NetworkPolicy Doesn't Mean "Deny Everything Automatically"44Namespace Boundary45Zero-Trust Network Model46NetworkPolicy + ServiceAccount47Common Mistakes48Mistake 1 โ€” Allowing everything49Mistake 2 โ€” Default-deny egress without DNS50Mistake 3 โ€” Wrong labels51Mistake 4 โ€” Wrong namespace52Mistake 5 โ€” Wrong port53Mistake 6 โ€” Assuming NetworkPolicy is enforced everywhere54Debugging NetworkPolicy55Step 156Step 257Step 358Step 459Step 560Step 661Debugging Mental Model62Interview Questions63Q1. What is NetworkPolicy?64Q2. Ingress vs Egress?65Q3. What does podSelector do?66Q4. What does an empty podSelector: {} mean?67Q5. Can NetworkPolicy control traffic across namespaces?68Q6. Does NetworkPolicy replace RBAC?69Q7. Does NetworkPolicy provide database authentication?70Q8. What is default-deny?71Q9. Why can default-deny egress break applications?72Q10. Who actually enforces NetworkPolicy?73Select Pods:74Select namespace:75Select ports:76Default deny:77One-line memory trick:78Next โ†’ Chapter 21: Logging ๐Ÿ“‹

We learned in Chapter 11 that Kubernetes networking generally allows Pods to communicate across the cluster.

That's powerfulโ€”but it can also be dangerous.

Imagine:

Frontend Pod
Backend Pod
Worker Pod
Redis Pod
PostgreSQL Pod
Monitoring Pod

If networking is completely unrestricted, a compromised Pod may be able to attempt connections to many other workloads.

We want:

Frontend โ†’ Backend โœ…
Backend โ†’ PostgreSQL โœ…
Backend โ†’ Redis โœ…
Worker โ†’ PostgreSQL โœ…
Frontend โ†’ PostgreSQL โŒ
Frontend โ†’ Redis โŒ
Random Pod โ†’ PostgreSQL โŒ

This is what NetworkPolicy helps us achieve.


1. What Is NetworkPolicy?#

A Kubernetes NetworkPolicy is an API object used to control network traffic to and/or from Pods.

In simple terms:

NetworkPolicy acts like a firewall for Pod network traffic.

Mental model:

              NetworkPolicy
                    โ”‚
          โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
          โ–ผ                   โ–ผ
       Ingress              Egress
          โ”‚                   โ”‚
     Incoming             Outgoing
      traffic              traffic

2. Ingress vs Egress#

These two words are extremely important.

Ingress#

Traffic coming into a Pod.

Client
  โ”‚
  โ”‚ incoming
  โ–ผ
Backend Pod

That's ingress.


Egress#

Traffic leaving a Pod.

Backend Pod
    โ”‚
    โ”‚ outgoing
    โ–ผ
PostgreSQL

That's egress.

So:

Ingress = IN
Egress  = OUT

3. NetworkPolicy Architecture#

Suppose:

Frontend
   โ”‚
   โ–ผ
Backend
   โ”‚
   โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ–บ PostgreSQL
   โ”‚
   โ””โ”€โ”€โ”€โ”€โ”€โ”€โ–บ Redis

We can define:

Frontend
   โ”‚
   โ”‚ allowed
   โ–ผ
Backend
 
Backend
   โ”‚
   โ”œโ”€โ”€ allowed โ†’ PostgreSQL
   โ””โ”€โ”€ allowed โ†’ Redis
 
Frontend
   โ”‚
   โ””โ”€โ”€ blocked โ†’ PostgreSQL

This creates network segmentation.


4. Important: NetworkPolicy Isn't Implemented by the API Object Alone#

This is a very important Kubernetes concept.

Creating:

kind: NetworkPolicy

doesn't magically create firewall rules unless your cluster's networking implementation supports and enforces NetworkPolicy.

The actual enforcement is provided by the cluster's network plugin/CNI.

Examples include:

  • Cilium
  • Calico
  • other NetworkPolicy-capable implementations

So:

NetworkPolicy object
       โ†“
CNI / networking implementation
       โ†“
Actual traffic enforcement

5. Basic NetworkPolicy#

Let's start with a simple policy.

Suppose:

app=backend

We want to control incoming traffic to those Pods.

apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
 
metadata:
  name: backend-policy
  namespace: production
 
spec:
  podSelector:
    matchLabels:
      app: backend
 
  policyTypes:
    - Ingress
 
  ingress:
    - from:
        - podSelector:
            matchLabels:
              app: frontend

Let's understand this carefully.


6. podSelector#

podSelector:
  matchLabels:
    app: backend

This selects the Pods that the policy protects.

So:

app=backend

means:

Backend Pod A
Backend Pod B
Backend Pod C

are affected.


7. policyTypes#

policyTypes:
  - Ingress

This means we're defining ingress behavior.

We're controlling:

Incoming traffic

to the selected Pods.


8. from#

from:
  - podSelector:
      matchLabels:
        app: frontend

This says:

Allow traffic from Pods labeled app=frontend.

So:

Frontend
   โ”‚
   โ”‚ โœ…
   โ–ผ
Backend

But:

Random Pod
   โ”‚
   โ”‚ โŒ
   โ–ผ
Backend

assuming the policy is otherwise isolating ingress to the selected backend Pods.


9. Port Restrictions#

We can make the policy more specific.

Suppose Node.js backend listens on:

3000

Then:

ingress:
  - from:
      - podSelector:
          matchLabels:
            app: frontend
 
    ports:
      - protocol: TCP
        port: 3000

Now:

Frontend โ†’ Backend:3000 โœ…

but a different port isn't allowed by this rule.


10. Complete Backend Ingress Policy#

apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
 
metadata:
  name: backend-ingress
  namespace: production
 
spec:
  podSelector:
    matchLabels:
      app: backend
 
  policyTypes:
    - Ingress
 
  ingress:
    - from:
        - podSelector:
            matchLabels:
              app: frontend
 
      ports:
        - protocol: TCP
          port: 3000

Architecture:

                 production
                     โ”‚
          โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
          โ–ผ                     โ–ผ
      Frontend               Backend
      app=frontend           app=backend
          โ”‚                     โ–ฒ
          โ”‚ TCP:3000            โ”‚
          โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                    โœ…

11. Important Behavior: Policies Are Additive#

NetworkPolicies are generally additive.

Suppose you have:

Policy A โ†’ allow Frontend
Policy B โ†’ allow Worker

Then the resulting allowed traffic is effectively:

Frontend โœ…
Worker   โœ…

not:

Only Worker โŒ

Adding another policy doesn't automatically override the previous one.

Think:

Policy A
   +
Policy B
   +
Policy C
   =
Combined allowed rules

12. Default-Deny#

Now we reach one of the most important production patterns:

Default deny

Instead of saying:

Allow everything
except bad traffic

you can use:

Deny by default
and explicitly allow required traffic

This is much closer to a least-privilege networking model.


13. Default-Deny Ingress#

Suppose we want to deny incoming traffic to all Pods in production unless another policy explicitly allows it.

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

Notice:

podSelector: {}

Empty selector means:

Select all Pods in the namespace.

So:

All production Pods
        โ”‚
        โ–ผ
Default deny ingress

14. Why podSelector: {}?#

This:

podSelector: {}

means:

All Pods in this namespace

Whereas:

podSelector:
  matchLabels:
    app: backend

means:

Only backend Pods

15. Default-Deny Egress#

You can also deny outgoing traffic by default.

apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
 
metadata:
  name: default-deny-egress
  namespace: production
 
spec:
  podSelector: {}
 
  policyTypes:
    - Egress

Now:

Production Pods
      โ”‚
      โ–ผ
Outgoing traffic blocked
unless explicitly allowed

16. Default Deny Both#

For strong segmentation:

apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
 
metadata:
  name: default-deny
  namespace: production
 
spec:
  podSelector: {}
 
  policyTypes:
    - Ingress
    - Egress

This gives you:

Ingress โ†’ denied by default
Egress  โ†’ denied by default

Then you explicitly allow what is needed.


17. The Production Pattern#

A very common conceptual model is:

Step 1
โ”€โ”€โ”€โ”€โ”€โ”€
Deny everything
 
        โ†“
 
Step 2
โ”€โ”€โ”€โ”€โ”€โ”€
Allow frontend โ†’ backend
 
        โ†“
 
Step 3
โ”€โ”€โ”€โ”€โ”€โ”€
Allow backend โ†’ database
 
        โ†“
 
Step 4
โ”€โ”€โ”€โ”€โ”€โ”€
Allow backend โ†’ Redis
 
        โ†“
 
Step 5
โ”€โ”€โ”€โ”€โ”€โ”€
Allow required DNS / external services

This is much safer than:

Everything โ†’ Everything

18. Namespace Selectors#

So far we've used:

podSelector:

But what if the source Pod is in another namespace?

You can use:

namespaceSelector:

Example:

from:
  - namespaceSelector:
      matchLabels:
        team: frontend

Meaning:

Allow traffic from Pods in namespaces labeled team=frontend.


19. Namespace + Pod Selector#

You can combine:

from:
  - namespaceSelector:
      matchLabels:
        environment: production
 
    podSelector:
      matchLabels:
        app: frontend

This means:

Allow Pods matching app=frontend from namespaces matching environment=production.

This is useful for larger clusters.


20. Important YAML Structure#

This can be confusing:

from:
  - namespaceSelector:
      matchLabels:
        environment: production
    podSelector:
      matchLabels:
        app: frontend

The two selectors under the same list item are combined.

Think:

Namespace must match
        AND
Pod must match

21. Multiple from Entries#

Consider:

from:
  - podSelector:
      matchLabels:
        app: frontend
 
  - podSelector:
      matchLabels:
        app: worker

This means:

Frontend OR Worker

can access the selected destination, subject to the specified ports.

So:

Frontend โ”€โ”€โ”
           โ”œโ”€โ”€โ–บ Backend
Worker โ”€โ”€โ”€โ”€โ”˜

22. Backend + PostgreSQL Example#

Let's create a realistic application.

production
โ”‚
โ”œโ”€โ”€ frontend
โ”‚
โ”œโ”€โ”€ backend
โ”‚
โ”œโ”€โ”€ postgres
โ”‚
โ””โ”€โ”€ redis

Desired network:

Frontend
   โ”‚
   โ–ผ
Backend
   โ”‚
   โ”œโ”€โ”€โ–บ PostgreSQL
   โ”‚
   โ””โ”€โ”€โ–บ Redis

Blocked:

Frontend โ”€โ”€Xโ”€โ”€โ–บ PostgreSQL
Frontend โ”€โ”€Xโ”€โ”€โ–บ Redis
 
Random Pod โ”€โ”€Xโ”€โ”€โ–บ Backend
Random Pod โ”€โ”€Xโ”€โ”€โ–บ PostgreSQL

23. PostgreSQL Ingress Policy#

Suppose PostgreSQL listens on:

5432

Policy:

apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
 
metadata:
  name: postgres-policy
  namespace: production
 
spec:
  podSelector:
    matchLabels:
      app: postgres
 
  policyTypes:
    - Ingress
 
  ingress:
    - from:
        - podSelector:
            matchLabels:
              app: backend
 
      ports:
        - protocol: TCP
          port: 5432

Result:

Backend
   โ”‚
   โ”‚ TCP 5432
   โ–ผ
PostgreSQL
   โœ…

24. Redis Policy#

Redis normally listens on:

6379

Policy:

apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
 
metadata:
  name: redis-policy
  namespace: production
 
spec:
  podSelector:
    matchLabels:
      app: redis
 
  policyTypes:
    - Ingress
 
  ingress:
    - from:
        - podSelector:
            matchLabels:
              app: backend
 
      ports:
        - protocol: TCP
          port: 6379

Now:

Backend โ”€โ”€TCP 6379โ”€โ”€โ–บ Redis
                    โœ…

25. Why Protect Databases?#

Imagine an attacker compromises:

Frontend Pod

Without network restrictions:

Frontend
   โ”‚
   โ”œโ”€โ”€โ–บ Backend
   โ”œโ”€โ”€โ–บ Redis
   โ””โ”€โ”€โ–บ PostgreSQL

With NetworkPolicy:

Frontend
   โ”‚
   โ””โ”€โ”€โ–บ Backend
          โ”‚
          โ”œโ”€โ”€โ–บ Redis
          โ””โ”€โ”€โ–บ PostgreSQL

This limits lateral movement.


26. NetworkPolicy Is Not Authentication#

Very important:

NetworkPolicy doesn't replace:

Database passwords
TLS
Authentication
Authorization

For example:

NetworkPolicy
โ†’ Can this Pod establish network traffic?
 
Database authentication
โ†’ Does this client have valid credentials?

You should use both.


27. NetworkPolicy + RBAC#

These solve different problems.

RBAC#

Controls:

Who can interact with Kubernetes API?

NetworkPolicy#

Controls:

Which Pods can communicate over the network?

So:

RBAC
โ†’ Kubernetes API permissions
 
NetworkPolicy
โ†’ Network traffic permissions

28. Egress Policies#

Ingress protects incoming connections.

But you also need to consider outgoing traffic.

Imagine:

Backend Pod
    โ”‚
    โ”œโ”€โ”€โ–บ PostgreSQL
    โ”œโ”€โ”€โ–บ Redis
    โ”œโ”€โ”€โ–บ External API
    โ””โ”€โ”€โ–บ Internet

Do you really want the backend to talk to everything?

Maybe not.

You could restrict egress.


29. Egress Example#

Suppose backend should access PostgreSQL:

egress:
  - to:
      - podSelector:
          matchLabels:
            app: postgres
 
    ports:
      - protocol: TCP
        port: 5432

And:

policyTypes:
  - Egress

Now:

Backend
   โ”‚
   โ””โ”€โ”€โ–บ PostgreSQL:5432
           โœ…

Other outbound traffic may be denied if the Pod is isolated for egress and no other policy allows it.


30. Egress Can Get Tricky#

Suppose your Node.js backend needs:

PostgreSQL
Redis
OpenAI API
Google API
DNS
Monitoring

If you enable strict default-deny egress:

Backend
  โ”‚
  โ”œโ”€โ”€ PostgreSQL โŒ
  โ”œโ”€โ”€ Redis โŒ
  โ”œโ”€โ”€ OpenAI โŒ
  โ”œโ”€โ”€ Google โŒ
  โ””โ”€โ”€ DNS โŒ

until you explicitly allow required destinations.

This is why NetworkPolicy rollout should be tested carefully.


31. DNS Is Especially Important#

Kubernetes applications commonly use DNS:

backend-service
postgres-service
redis-service

If you block all egress and forget DNS, your application may stop resolving service names.

Typical cluster DNS traffic uses:

UDP 53
TCP 53

depending on the DNS path and implementation.

A policy may need to allow traffic to the cluster DNS service.


32. DNS Policy Concept#

A common conceptual rule looks like:

egress:
  - to:
      - namespaceSelector:
          matchLabels:
            kubernetes.io/metadata.name: kube-system
 
    ports:
      - protocol: UDP
        port: 53
 
      - protocol: TCP
        port: 53

Exact labels and DNS architecture should be verified for your cluster.

The important idea is:

Application
    โ”‚
    โ–ผ
DNS
    โ”‚
    โ–ผ
Service name โ†’ Service IP

33. Why DNS Matters#

Your backend might use:

postgres-service

instead of:

10.42.1.27

If DNS is blocked:

postgres-service
       โ†“
โŒ Can't resolve

Application fails.


34. NetworkPolicy and Services#

A Service doesn't bypass NetworkPolicy.

Remember:

Client
  โ†“
Service
  โ†“
Pod

NetworkPolicy applies to Pod traffic.

For example:

Frontend
  โ†“
backend-service
  โ†“
Backend Pod

The policy needs to allow the actual traffic reaching the backend Pods.


35. NetworkPolicy and Ingress#

Suppose:

Internet
   โ†“
Ingress Controller
   โ†“
frontend-service
   โ†“
Frontend Pod

Your NetworkPolicy needs to account for the actual source Pods involved in the traffic path.

You cannot simply assume:

"Internet"

is represented as a normal Kubernetes Pod selector.

Ingress controllers often run as Pods themselves, so policies may need to permit traffic from the controller's namespace/labels.


36. Default-Deny + Explicit Allow#

A production design might start with:

All ingress โ†’ DENY
All egress โ†’ DENY

Then:

Ingress Controller โ†’ Frontend
Frontend โ†’ Backend
Backend โ†’ PostgreSQL
Backend โ†’ Redis
Backend โ†’ DNS
Backend โ†’ required external services
Monitoring โ†’ required endpoints

Everything else:

โŒ

This is a powerful security architecture.


37. Complete Example Architecture#

                         INTERNET
                            โ”‚
                            โ–ผ
                     Ingress Controller
                            โ”‚
                            โ–ผ
                       Frontend Pods
                            โ”‚
                         TCP 3000
                            โ”‚
                            โ–ผ
                       Backend Pods
                       /           \
                      /             \
                TCP 5432           TCP 6379
                    โ”‚                  โ”‚
                    โ–ผ                  โ–ผ
               PostgreSQL            Redis

Network rules:

Ingress Controller โ†’ Frontend    โœ…
Frontend โ†’ Backend                โœ…
Backend โ†’ PostgreSQL              โœ…
Backend โ†’ Redis                   โœ…
 
Frontend โ†’ PostgreSQL             โŒ
Frontend โ†’ Redis                  โŒ
Random Pod โ†’ PostgreSQL           โŒ
Random Pod โ†’ Redis                โŒ

38. A More Complete Backend Policy#

apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
 
metadata:
  name: backend-policy
  namespace: production
 
spec:
  podSelector:
    matchLabels:
      app: backend
 
  policyTypes:
    - Ingress
 
  ingress:
    - from:
        - podSelector:
            matchLabels:
              app: frontend
 
      ports:
        - protocol: TCP
          port: 3000

This protects backend ingress.

Then PostgreSQL has its own policy:

backend โ†’ PostgreSQL

Redis has:

backend โ†’ Redis

This is much easier to reason about than one gigantic policy.


39. NetworkPolicy Doesn't Mean "Deny Everything Automatically"#

Another common misconception.

Creating:

kind: NetworkPolicy

doesn't mean all traffic in the cluster is suddenly blocked.

Isolation happens for Pods selected by policies and for the relevant direction.

For example:

podSelector:
  matchLabels:
    app: backend

means:

Backend Pods are the selected targets

Other Pods aren't automatically selected by that policy.


40. Namespace Boundary#

Remember Chapter 8:

Namespace

isn't automatically a network security boundary.

Without NetworkPolicy:

Namespace A
   โ”‚
   โ””โ”€โ”€โ”€โ”€โ–บ Namespace B

may be able to communicate.

With NetworkPolicy:

Namespace A
   โ”‚
   โ”‚ policy
   โ–ผ
Namespace B

you can explicitly control communication.


41. Zero-Trust Network Model#

A useful security philosophy is:

Don't trust network location by default.

Instead of:

Inside cluster = trusted

think:

Every communication path
must be explicitly justified.

For example:

Frontend โ†’ Backend
allowed
 
Frontend โ†’ DB
not allowed

This is network-level least privilege.


42. NetworkPolicy + ServiceAccount#

These can work together for defense in depth.

For example:

Backend Pod
   โ”‚
   โ”œโ”€โ”€ ServiceAccount
   โ”‚       โ†“
   โ”‚      RBAC
   โ”‚
   โ””โ”€โ”€ NetworkPolicy
           โ†“
      Allowed traffic

Even if one layer is misconfigured, another layer may reduce exposure.

But neither replaces the other.


43. Common Mistakes#

Mistake 1 โ€” Allowing everything#

ingress:
  - {}

This can effectively allow all ingress to selected Pods.

Use carefully.


Mistake 2 โ€” Default-deny egress without DNS#

Result:

Service name
   โ†“
DNS fails
   โ†“
Application fails

Mistake 3 โ€” Wrong labels#

Policy:

app: frontend

Actual Pod:

app: web

Result:

Policy doesn't select intended Pods.

Labels are critical.


Mistake 4 โ€” Wrong namespace#

You may write:

namespaceSelector:

but the namespace doesn't have the expected label.

Traffic gets blocked.


Mistake 5 โ€” Wrong port#

Backend:

3000

Policy allows:

8080

Result:

Connection blocked

Mistake 6 โ€” Assuming NetworkPolicy is enforced everywhere#

The cluster's networking implementation must support NetworkPolicy enforcement.


44. Debugging NetworkPolicy#

Suppose:

Frontend โ†’ Backend

is failing.

Check:

Step 1#

Pods:

kubectl get pods -n production --show-labels

Step 2#

Policies:

kubectl get networkpolicy -n production

Step 3#

Inspect:

kubectl describe networkpolicy backend-policy \
  -n production

Step 4#

Check Services:

kubectl get svc -n production

Step 5#

Check DNS:

nslookup backend-service

Step 6#

Test connectivity from a Pod:

curl http://backend-service:3000

45. Debugging Mental Model#

If:

Frontend โ†’ Backend

doesn't work:

Frontend Pod
     โ”‚
     โ–ผ
DNS resolution?
     โ”‚
     โ–ผ
Service exists?
     โ”‚
     โ–ผ
Service has endpoints?
     โ”‚
     โ–ผ
Backend Pod ready?
     โ”‚
     โ–ผ
NetworkPolicy allows?
     โ”‚
     โ–ผ
Correct port?

Don't immediately blame NetworkPolicy.


46. Interview Questions#

Q1. What is NetworkPolicy?#

A Kubernetes API object used to define allowed network traffic to and/or from selected Pods, enforced by a compatible networking implementation.

Q2. Ingress vs Egress?#

Ingress = incoming traffic
Egress = outgoing traffic

Q3. What does podSelector do?#

Selects Pods to which the policy applies.

Q4. What does an empty podSelector: {} mean?#

Select all Pods in the policy's namespace.

Q5. Can NetworkPolicy control traffic across namespaces?#

Yes, using namespace selectors and appropriate policy rules.

Q6. Does NetworkPolicy replace RBAC?#

No.

RBAC โ†’ Kubernetes API permissions
NetworkPolicy โ†’ Network traffic

Q7. Does NetworkPolicy provide database authentication?#

No.

Database authentication is separate.

Q8. What is default-deny?#

Deny traffic by default for selected Pods/directions, then explicitly allow required communication.

Q9. Why can default-deny egress break applications?#

Because applications may need DNS and external/internal services that have not yet been explicitly allowed.

Q10. Who actually enforces NetworkPolicy?#

A compatible cluster networking implementation/CNI.


๐Ÿง  Chapter 20 Cheat Sheet#

NetworkPolicy
     โ”‚
     โ”œโ”€โ”€ Ingress
     โ”‚      โ†“
     โ”‚    Incoming
     โ”‚
     โ””โ”€โ”€ Egress
            โ†“
          Outgoing

Select Pods:#

podSelector:

Select namespace:#

namespaceSelector:

Select ports:#

ports:
  - protocol: TCP
    port: 3000

Default deny:#

podSelector: {}
 
policyTypes:
  - Ingress

or:

podSelector: {}
 
policyTypes:
  - Egress

or both.


๐ŸŽฏ The Production Pattern to Remember#

For your Node.js/Next.js architecture:

                     INTERNET
                         โ”‚
                         โ–ผ
                Ingress Controller
                         โ”‚
                         โ–ผ
                     Frontend
                         โ”‚
                         โ”‚ allowed
                         โ–ผ
                     Backend
                    /        \
                   /          \
              allowed        allowed
                 โ–ผ              โ–ผ
            PostgreSQL        Redis

NetworkPolicy:

Internet โ†’ Frontend          controlled
Frontend โ†’ Backend            โœ…
Backend โ†’ PostgreSQL           โœ…
Backend โ†’ Redis                โœ…
 
Frontend โ†’ PostgreSQL          โŒ
Frontend โ†’ Redis               โŒ
Random Pod โ†’ Backend           โŒ
Random Pod โ†’ PostgreSQL        โŒ

And security overall:

                    Kubernetes Security
                           โ”‚
          โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
          โ–ผ                โ–ผ                 โ–ผ
        RBAC         NetworkPolicy     Container Security
          โ”‚                โ”‚                 โ”‚
          โ–ผ                โ–ผ                 โ–ผ
     API access       Network access    Runtime access
          โ”‚                โ”‚                 โ”‚
          โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                           โ–ผ
                    Least Privilege

One-line memory trick:#

RBAC controls "who can use the Kubernetes API"; NetworkPolicy controls "who can talk to whom over the network."


Next โ†’ Chapter 21: Logging ๐Ÿ“‹#

We'll move into Observability and learn how to answer:

"My Pod is running, but something is wrong. How do I find out what happened?"

We'll cover:

kubectl logs
Container logs
Multi-container logs
Previous/crashed container logs
Logging architecture
Centralized logging
ELK / EFK
Fluent Bit
Loki
Structured JSON logs
Correlation IDs
Log levels
Production Node.js logging

And we'll build a practical logging architecture for a Node.js + Next.js + Kubernetes application.

Finished this chapter?

Mark it as complete to track your mastery.

Previous ChapterChapter 19 โ€” Kubernetes Security ๐Ÿ”Next ChapterChapter 21 โ€” Kubernetes Logging ๐Ÿ“‹
PrevNext