Interview's MateKubernetes
Interview's Mate

Kubernetes

K8s · Workloads · GitOps · Cloud

Visualizer

DSA Visualizer
All 32 lessons
Custom Playground
Try your own examples

Learning

System Design
31 architecture topics
SQL Learning
160 foundations → production topics
AI Engineering
160 LLM → backend topics
Data Science
160 Python → MLOps topics
Data Engineer
160 SQL → streaming topics
Docker
160 Docker learning topics
Kubernetes
160 K8s fundamentals → cloud
Java LearnSoon
Coming soon
Python Learn
Interview Q&A & internals
JavaScript Learn
Closures, async & DOM Q&A
Tech Learn
Search, practise & track Q&A
AI DSA Practice
Solve coding problems
Theme
Home
Kubernetes topics
Lesson 55/ 160
03. Kubernetes Networking & Traffic Management

NetworkPolicies: Layer 3/4 Micro-Segmentation (Ingress & Egress Rules)

Lock down pod traffic with label-based ingress and egress NetworkPolicies.

IntermediateNetworkPolicy · Ingress · Egress3 quiz questions
Practice DevOps Q&A
In plain wordsDiagramTheoryStep by stepWhen to use itInterview cluesMistakes to avoidQuiz (3)Practice

In plain words

NetworkPolicy is a club list at each Pod door: a deny selection changes the default, then every matching guest rule adds one permitted path. The simple Kubernetes rule is: NetworkPolicy is additive: selected Pods are isolated for a direction when a policy selects them, and traffic is allowed only by matching peer, port, and protocol rules.

Think of it like…

NetworkPolicy is a club list at each Pod door: a deny selection changes the default, then every matching guest rule adds one permitted path.

Where you’ve seen it

During a connectivity incident, the network owner runs kubectl get networkpolicy -A; kubectl describe networkpolicy default-deny-ingress -n prod. Expected evidence: NetworkPolicy is additive: selected Pods are isolated for a direction when a policy selects them, and traffic is allowed only by matching peer, port, and protocol rules. If the run instead shows this symptom, stop and restore the last known state: Controllers and Service semantics differ by CNI, and DNS, health checks, control-plane traffic, or established connections can remain surprising when enforcement is tested.

Words you’ll hear

NetworkPolicy
For this lesson, NetworkPolicy is the controlling idea: NetworkPolicy is additive: selected Pods are isolated for a direction when a policy selects them, and traffic is allowed only by matching peer, port, and protocol rules.
Ingress
Ingress is the checkpoint; run kubectl get networkpolicy -A; kubectl describe networkpolicy default-deny-ingress -n prod and inspect the owner chain. Controllers and Service semantics differ by CNI, and DNS, health checks, control-plane traffic, or established connections can remain surprising when enforcement is tested.
Egress
Egress is the outcome to preserve; the cost is explicit: Layer 3/4 policy limits lateral movement without application identity, but it cannot inspect HTTP semantics and may be ignored by an unenforcing CNI.
Verification evidence
The evidence command is kubectl get networkpolicy -A; kubectl describe networkpolicy default-deny-ingress -n prod. It connects the declaration to this behavior: Lock down pod traffic with label-based ingress and egress NetworkPolicies.

Visual diagram

Loading interactive diagram

Theory & deep dive

How NetworkPolicy reaches the intended state

Lock down pod traffic with label-based ingress and egress NetworkPolicies. NetworkPolicy is additive: selected Pods are isolated for a direction when a policy selects them, and traffic is allowed only by matching peer, port, and protocol rules.

The concrete checkpoint is kubectl get networkpolicy -A; kubectl describe networkpolicy default-deny-ingress -n prod; compare the NetworkPolicy owner, Ingress state, and Egress outcome instead of treating YAML acceptance as proof.

Verify Ingress, not only object creation

Run kubectl get networkpolicy -A; kubectl describe networkpolicy default-deny-ingress -n prod after the change and correlate the result with Events and the owning object. NetworkPolicy is additive: selected Pods are isolated for a direction when a policy selects them, and traffic is allowed only by matching peer, port, and protocol rules.

Key takeaway: NetworkPolicy is implemented only when this invariant remains observable: NetworkPolicy is additive: selected Pods are isolated for a direction when a policy selects them, and traffic is allowed only by matching peer, port, and protocol rules.

Ingress: failure boundary and production cost

Controllers and Service semantics differ by CNI, and DNS, health checks, control-plane traffic, or established connections can remain surprising when enforcement is tested. The prerequisite contract is concrete: NetworkPolicy is additive: selected Pods are isolated for a direction when a policy selects them, and traffic is allowed only by matching peer, port, and protocol rules.

Layer 3/4 policy limits lateral movement without application identity, but it cannot inspect HTTP semantics and may be ignored by an unenforcing CNI. A safe design limits blast radius, records the last good configuration, and uses kubectl get networkpolicy -A; kubectl describe networkpolicy default-deny-ingress -n prod as a deterministic rollback checkpoint.

Recover from Ingress drift

Capture the current state with kubectl get networkpolicy -A; kubectl describe networkpolicy default-deny-ingress -n prod, then correct the controlling manifest rather than patching a child object; the relevant divergence signal is specific: Controllers and Service semantics differ by CNI, and DNS, health checks, control-plane traffic, or established connections can remain surprising when enforcement is tested.

Key takeaway: Accept the trade-off consciously: Layer 3/4 policy limits lateral movement without application identity, but it cannot inspect HTTP semantics and may be ignored by an unenforcing CNI.

How it works, step by step

Step 1 of 4
Traffic author

1. Define the reachability contract

Declare selectors, listeners, routes, or policy and keep endpoint readiness explicit: NetworkPolicy is additive: selected Pods are isolated for a direction when a policy selects them, and traffic is allowed only by matching peer, port, and protocol rules.

NetworkPolicy contract → owned state
Key numbers at this step
Goal
Lock down pod traffic with label-based ingress and egress NetworkPolicies.
Owner
NetworkPolicy

When to use it & trade-offs

What it is

Lock down pod traffic with label-based ingress and egress NetworkPolicies. NetworkPolicy is additive: selected Pods are isolated for a direction when a policy selects them, and traffic is allowed only by matching peer, port, and protocol rules.

Why it matters: Controllers and Service semantics differ by CNI, and DNS, health checks, control-plane traffic, or established connections can remain surprising when enforcement is tested. The invariant at risk is specific: NetworkPolicy is additive: selected Pods are isolated for a direction when a policy selects them, and traffic is allowed only by matching peer, port, and protocol rules. The quickest fact check before changing the design is kubectl get networkpolicy -A; kubectl describe networkpolicy default-deny-ingress -n prod.

When to reach for it

  • Use NetworkPolicy to enforce this invariant: NetworkPolicy is additive: selected Pods are isolated for a direction when a policy selects them, and traffic is allowed only by matching peer, port, and protocol rules.
  • Use Ingress evidence from kubectl get networkpolicy -A; kubectl describe networkpolicy default-deny-ingress -n prod in reviews and incident timelines. Lock down pod traffic with label-based ingress and egress NetworkPolicies.
  • Choose the managed ingress or mesh route only when this cost is justified by governance or scale: Layer 3/4 policy limits lateral movement without application identity, but it cannot inspect HTTP semantics and may be ignored by an unenforcing CNI.

When to avoid or be careful

  • Avoid assuming NetworkPolicy is complete after YAML submission; the required behavior is concrete: NetworkPolicy is additive: selected Pods are isolated for a direction when a policy selects them, and traffic is allowed only by matching peer, port, and protocol rules.
  • Avoid treating the next symptom as random; check the owner and state with kubectl get networkpolicy -A; kubectl describe networkpolicy default-deny-ingress -n prod first: Controllers and Service semantics differ by CNI, and DNS, health checks, control-plane traffic, or established connections can remain surprising when enforcement is tested.
  • Avoid adopting a heavier platform wrapper before measuring its cost: Layer 3/4 policy limits lateral movement without application identity, but it cannot inspect HTTP semantics and may be ignored by an unenforcing CNI.

Native, manual, and managed paths for NetworkPolicy

Built-in API path
Best for: Lock down pod traffic with label-based ingress and egress NetworkPolicies. A concrete incident check is kubectl get networkpolicy -A; kubectl describe networkpolicy default-deny-ingress -n prod.

Strengths: NetworkPolicy is additive: selected Pods are isolated for a direction when a policy selects them, and traffic is allowed only by matching peer, port, and protocol rules.

Watch out: Layer 3/4 policy limits lateral movement without application identity, but it cannot inspect HTTP semantics and may be ignored by an unenforcing CNI. Recovery must also recognize this signal: Controllers and Service semantics differ by CNI, and DNS, health checks, control-plane traffic, or established connections can remain surprising when enforcement is tested.

Datapath-specific extension
Best for: Quickly testing the Ingress assumption with kubectl get networkpolicy -A; kubectl describe networkpolicy default-deny-ingress -n prod; the expected invariant is: NetworkPolicy is additive: selected Pods are isolated for a direction when a policy selects them, and traffic is allowed only by matching peer, port, and protocol rules.

Strengths: kubectl get networkpolicy -A; kubectl describe networkpolicy default-deny-ingress -n prod gives immediate feedback without first building a platform layer.

Watch out: A manual change can hide drift, and unlike NetworkPolicy, it does not make this symptom self-documenting: Controllers and Service semantics differ by CNI, and DNS, health checks, control-plane traffic, or established connections can remain surprising when enforcement is tested.

Managed ingress or mesh
Best for: Teams that need Egress integrated with guardrails; the evidence path is kubectl get networkpolicy -A; kubectl describe networkpolicy default-deny-ingress -n prod.

Strengths: Platform controls can wrap this exact behavior in policy or dashboards: NetworkPolicy is additive: selected Pods are isolated for a direction when a policy selects them, and traffic is allowed only by matching peer, port, and protocol rules.

Watch out: The added platform ownership buys value at this cost: Layer 3/4 policy limits lateral movement without application identity, but it cannot inspect HTTP semantics and may be ignored by an unenforcing CNI.

Interview signals & numbers to know

When this topic comes up

  • Interview anchor: NetworkPolicy is additive: selected Pods are isolated for a direction when a policy selects them, and traffic is allowed only by matching peer, port, and protocol rules.
  • Trace NetworkPolicy to Egress through Ingress; the invariant is: NetworkPolicy is additive: selected Pods are isolated for a direction when a policy selects them, and traffic is allowed only by matching peer, port, and protocol rules.
  • Recognize the symptom: Controllers and Service semantics differ by CNI, and DNS, health checks, control-plane traffic, or established connections can remain surprising when enforcement is tested.
  • Justify the cost: Layer 3/4 policy limits lateral movement without application identity, but it cannot inspect HTTP semantics and may be ignored by an unenforcing CNI.
  • Verify with kubectl get networkpolicy -A; kubectl describe networkpolicy default-deny-ingress -n prod instead of only inspecting YAML.

Benchmarks & rules of thumb

2 -
directions are ingress and egress
3 -
selectors can identify Pods, namespaces, or IP blocks
0 -
implicit deny is not installed until a Pod is selected by a policy

Common mistakes & follow-ups

Common interview mistakes

  • Debugging only the child object misses the NetworkPolicy owner contract: NetworkPolicy is additive: selected Pods are isolated for a direction when a policy selects them, and traffic is allowed only by matching peer, port, and protocol rules.
  • Ignoring this telltale condition leads to unrelated changes: Controllers and Service semantics differ by CNI, and DNS, health checks, control-plane traffic, or established connections can remain surprising when enforcement is tested.
  • Repeating kubectl get networkpolicy -A; kubectl describe networkpolicy default-deny-ingress -n prod without an owner for remediation wastes retry cost. Layer 3/4 policy limits lateral movement without application identity, but it cannot inspect HTTP semantics and may be ignored by an unenforcing CNI.

Likely interviewer follow-ups

  • How would API output and runtime evidence prove this behavior: NetworkPolicy is additive: selected Pods are isolated for a direction when a policy selects them, and traffic is allowed only by matching peer, port, and protocol rules?
  • Which owner object, logs, and Events would you inspect for this symptom-Controllers and Service semantics differ by CNI, and DNS, health checks, control-plane traffic, or established connections can remain surprising when enforcement is tested-?
  • At what threshold would this cost become unacceptable: Layer 3/4 policy limits lateral movement without application identity, but it cannot inspect HTTP semantics and may be ignored by an unenforcing CNI?
Interview tip: Interview sequence: state the invariant, run kubectl get networkpolicy -A; kubectl describe networkpolicy default-deny-ingress -n prod, name the failure, then justify the cost. Invariant: NetworkPolicy is additive: selected Pods are isolated for a direction when a policy selects them, and traffic is allowed only by matching peer, port, and protocol rules. Failure: Controllers and Service semantics differ by CNI, and DNS, health checks, control-plane traffic, or established connections can remain surprising when enforcement is tested. Cost: Layer 3/4 policy limits lateral movement without application identity, but it cannot inspect HTTP semantics and may be ignored by an unenforcing CNI.

Knowledge check

Loading quiz

Put it into practice

Practice related interview Q&A

Apply NetworkPolicies: Layer 3/4 Micro-Segmentation (Ingress & Egress Rules) with generative AI interview Q&A and hands-on practice.

Open DevOps Q&A →Tech Learn Q&A
Previous topic
Traffic Splitting, Header Routing & Mirroring with Gateway API
Next topic
Zero-Trust Default-Deny Network Policies