Service Mesh and NetworkPolicy: A Practical Guide to Combining L4 and L7 Security
As microservices architecture (MSA) has become the mainstream, application complexity has grown exponentially. In an environment where dozens of services talk to each other, controlling who communicates, over which path, and under what conditions is no longer just network management—it is a matter of survival.
We usually think of firewalls when we think about security. In Kubernetes, though, security can never be complete with a single firewall. There are two powerful mechanisms with different roles: NetworkPolicy and Service Mesh.
This article clearly explains how these two technologies work (L4 vs L7) and, from a practitioner’s perspective, dives into how to combine them without conflict to build the strongest possible security architecture.
Network-Level Firewall: The Role of NetworkPolicy (Layer 4)
NetworkPolicy is a Kubernetes-native feature that handles the most basic network access control. It is similar to traditional firewall rules.
How it works: NetworkPolicy allows or blocks traffic based on IP addresses and port numbers. You define rules such as “this Pod is only allowed TCP communication on port 8080.” This layer never inspects the application payload.
When to use it:
- When a specific service must be blocked at the source from reaching the external internet or a particular port in another namespace.
- When you want to enforce the principle of least privilege at the lowest level by allowing only the minimum required network connections.
💡 Hands-on example: Restricting access to port 8080 (L4)
If you want backend-service to be reachable only from the internal auth-service on port 8080, use a NetworkPolicy.
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: restrict-backend-access
namespace: default
spec:
podSelector:
matchLabels:
app: backend-service
policyTypes:
- Ingress
ingress:
- from:
- podSelector:
matchLabels:
app: auth-service # 오직 이 레이블을 가진 파드만 허용
ports:
- protocol: TCP
port: 8080 # 8080 포트만 허용Application-Level Traffic Control: The Power of Service Mesh (Layer 7)
A service mesh provides an infrastructure layer for service-to-service communication. Typical implementations include Istio and Linkerd, which use the sidecar pattern to deploy a proxy container (e.g., Envoy) next to each Pod.
The magic of the sidecar pattern: The sidecar proxy intercepts all inbound and outbound traffic. When it inspects traffic, it does not look only at ports; it can enforce controls based on application-level information such as HTTP headers, HTTP methods, and request paths. That is the essence of L7 control.
When to use it:
- “Allow only GET requests and reject all POST requests.”
- “Process only requests that include a specific header (
X-Client-ID).” - When you want to automatically apply authentication (mTLS) for service-to-service communication.
💡 Hands-on example: Restricting HTTP methods (L7)
If you want incoming requests to the user-api service to allow only the GET method, use a Service Mesh AuthorizationPolicy.
apiVersion: security.istio.io/v1beta1
kind: AuthorizationPolicy
metadata:
name: restrict-get-method
namespace: default
spec:
selector:
matchLabels:
app: user-api
action: ALLOW
rules:
- http:
method: GET # 오직 GET 메서드만 허용
paths: ["/users"]L4 vs L7: Which Is Stronger, and How Should You Combine Them?
This is the most important question. The answer to “Which one is stronger?” is: “You need both; they play different roles.”
| Feature | NetworkPolicy (L4) | Service Mesh (L7) |
|---|---|---|
| Layer | Network layer (Layer 4) | Application layer (Layer 7) |
| What it controls | IP addresses, ports (TCP/UDP) | HTTP methods, headers, URI paths |
| How it works | Kubernetes-native firewall rules | Traffic inspection via sidecar proxy |
| Strengths | Most basic network access control | Fine-grained control based on business logic |
| Overhead | Very low (kernel level) | Moderate (proxy overhead possible) |
🛡️ Optimal Combination Strategy for Defense in Depth
The goal of a security architecture is to achieve defense in depth. That means not relying on a single security control, but building multiple layers of defense.
-
Outermost defense line (L4 – NetworkPolicy):
- Goal: Block incoming traffic from outside, or access to completely wrong ports, at the source.
- Example: “This service will only open port 8080; port 22 is blocked.”
-
Internal communication control (L7):
- Example: “Even if traffic arrived on port 8080, reject the request if it is
POST /api/v1/admin.”
- Example: “Even if traffic arrived on port 8080, reject the request if it is
Conclusion: The safest approach is to use L4 for basic access control and L7 for business-logic-level access control.
🚀 Summary and Practical Application Guide
| Scenario | Technology needed | How to implement |
|---|---|---|
| Block external exposure | L4 (network) | NetworkPolicy or Ingress Controller configuration |
| Restrict access to specific APIs | L7 (application) | Service mesh (Istio, etc.) or API Gateway |
| Apply least privilege | L4 + L7 | Use both to narrow communication paths |
With this multi-layered defense-in-depth strategy, even if one layer is breached, the next layer’s controls still protect you—resulting in a much stronger system.
Nodelog는 모든 콘텐츠의 내용과 출처를 공개 전에 검토합니다. 환경(OS·버전)에 따라 결과가 달라질 수 있는 기술 정보는 공식 문서와 함께 확인하며, 검토 기준과 정정 원칙은 편집 정책에서 안내합니다. 오류를 발견하시면 이메일로 제보해 주세요 — 확인 후 신속히 정정합니다.
Comments
Be the first to comment.