Q: Service mesh vs NetworkPolicy — when do you use each?
Service mesh vs NetworkPolicy is not an either-or choice: NetworkPolicy is a built-in L3/L4 firewall enforced by the CNI that decides which pods may connect, while a service mesh adds identity, mutual TLS, L7 authorization,.
Want to master this scenario in a live sandbox? KodeKloud's CKA & CKAD Hands-On Certification Track covers this exact problem with hands-on terminal drills.
🛠️ Production Runbook & Step-by-Step Resolution
Direct Answer: <strong>Service mesh vs NetworkPolicy</strong> is not an either-or choice: NetworkPolicy is a built-in L3/L4 firewall enforced by the CNI that decides which pods may connect, while a service mesh adds identity, mutual TLS, L7 authorization, retries and telemetry through proxies. Use NetworkPolicy for baseline isolation in every cluster and add a mesh when you need encrypted service-to-service traffic, request-level policy or traffic management. Interviewers ask this to check you can weigh capability against operational cost.
Mental Model: Two Different Layers
They answer different questions and are enforced by different components.
- NetworkPolicy: built into the Kubernetes API, enforced by the CNI (Calico, Cilium, Antrea). Matches pods, namespaces and IP blocks on TCP/UDP/SCTP ports. Allows or isolates connections; it does not encrypt or inspect HTTP.
- Service mesh: Istio, Linkerd or Cilium service mesh. Sidecar proxies, or ambient mode in Istio with node-level ztunnel and optional waypoint proxies, enforce mTLS and L7 rules. Linkerd and ambient mode reduce the sidecar overhead, but you still operate additional control plane components and certificates.
- Identity: NetworkPolicy relies on labels and IPs. A mesh issues workload identities (for example SPIFFE IDs) tied to certificates, so rules can say which service account may call which endpoint.
- Cost: NetworkPolicy has near-zero runtime cost. A mesh adds control plane components, proxy resources, upgrade work and debugging complexity.
- Cilium note: Cilium implements both: standard NetworkPolicy via eBPF, plus its own L7-aware policies and optional mesh features, so the boundary between the two is blurring in some stacks.
Hands-on: Service Mesh vs NetworkPolicy Configs Side by Side
The same intent, only the backend may call the database, expressed at two layers.
# L3/L4: NetworkPolicy
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: db-allow-backend
namespace: prod
spec:
podSelector:
matchLabels: { app: db }
policyTypes: [Ingress]
ingress:
- from:
- podSelector:
matchLabels: { app: backend }
ports:
- { protocol: TCP, port: 5432 }
---
# Identity and L7: Istio
apiVersion: security.istio.io/v1
kind: AuthorizationPolicy
metadata:
name: db-allow-backend
namespace: prod
spec:
selector:
matchLabels: { app: db }
action: ALLOW
rules:
- from:
- source:
principals: ["cluster.local/ns/prod/sa/backend"]
---
apiVersion: security.istio.io/v1
kind: PeerAuthentication
metadata:
name: default
namespace: prod
spec:
mtls:
mode: STRICT
- NetworkPolicy: default deny ingress in the namespace, then allow pods with label
app: backendto reach the database on port 5432. Default deny is added with an empty podSelector and policyTypes Ingress; then each service gets an explicit allow rule, so the namespace is closed by default. - Istio AuthorizationPolicy: allow only the backend service account identity, which survives pod IP changes and works with mTLS.
- PeerAuthentication:
STRICTmode rejects plaintext traffic between meshed workloads.
When to Use Which
Decide from requirements, not trends.
- NetworkPolicy only: you need namespace and pod isolation, default deny, and tier separation, and your compliance does not demand in-cluster encryption or per-route authorization.
- Mesh needed: you must encrypt east-west traffic with mTLS, authorize by workload identity or HTTP method and path, do canary traffic splitting, or collect uniform golden-signal metrics. Request-level rules such as allowing only GET on
/api/ordersare only possible with an L7-aware layer. - Both: the common production answer. NetworkPolicy provides a kernel-level backstop that works even for pods without a proxy, and the mesh provides identity and L7 control.
- Neither alone is enough: a mesh can be bypassed by pods that are not injected or by hostNetwork pods, so do not treat it as a replacement for network isolation.
- Compliance angle: if a regulation requires encryption in transit between services, a mesh with STRICT mTLS (or CNI-level transparent encryption such as WireGuard) is the control, not NetworkPolicy.
Failure Modes and Interactions
The two layers interact; plan for it.
# Verify enforcement from an unauthorised pod
kubectl run probe -n prod --rm -it --image=nicolaka/netshoot --restart=Never -- nc -zv -w 3 db 5432
# Expect a timeout (NetworkPolicy drop) or an RBAC: access denied response (mesh)
istioctl x describe pod <db-pod> -n prod
- Policy blocks the mesh: a default-deny policy must allow DNS and the traffic needed by proxies and the control plane, such as webhook and xDS ports for your mesh, or injection and certificate issuance fail.
- Source IP and ports: with sidecars, NetworkPolicy sees proxy traffic and mesh ports. Test policies after enabling the mesh.
- Partial rollout: PERMISSIVE mTLS during migration allows plaintext; track and move to STRICT.
- Operational load: proxy upgrades, resource overhead and extra hops affect latency and incident debugging; measure before committing cluster-wide.
Production Practice and Verification
Roll out in stages and verify enforcement.
- Baseline first: default-deny NetworkPolicy plus DNS allow in each namespace, tested in CI.
- Adopt mesh incrementally: start with namespaces that handle sensitive data, in PERMISSIVE then STRICT mode.
- Verify: attempt a blocked call from an unauthorized pod and confirm it is dropped (
hubble observeor mesh access logs); confirm mTLS withistioctlorlinkerd viztooling. - Cost check: estimate per-pod proxy CPU and memory and the added upgrade burden before rollout; in ambient mode the cost shifts to node-level components but still exists.
- NetworkPolicy is a CNI-enforced L3/L4 allowlist; a service mesh adds mTLS identity, L7 authorization and traffic control via proxies.
- I use NetworkPolicy everywhere and add a mesh only when encryption, per-route policy or traffic management justify the cost.
- I verify with blocked-call tests, flow logs and mTLS status tools, and roll out STRICT mode in stages.
Senior hiring managers and bar raisers evaluate how you prioritize during outages. Avoid these common anti-patterns:
- Treating a mesh as a replacement for NetworkPolicy Un-injected and hostNetwork pods can bypass it, so network-level isolation is still needed.
- Adopting a mesh only for observability Lighter tools such as eBPF-based flow visibility may meet the need without proxy overhead.
- Applying default-deny without allowing mesh and DNS traffic Sidecar injection, certificate issuance and name resolution then fail in confusing ways.
- Staying on PERMISSIVE mTLS indefinitely Plaintext traffic remains accepted, so the encryption benefit is never realised.