NetworkPolicies: Layer 3/4 Micro-Segmentation (Ingress & Egress Rules)
Lock down pod traffic with label-based ingress and egress NetworkPolicies.
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 prodand 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
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.
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.
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.
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.
How it works, step by step
Step 1 of 41. 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.
- 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 prodin 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 prodfirst: 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
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.
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.
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 prodwithout 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?
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
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.