728x90
정의
K8S 클러스터에서 네트워크 트래픽을 제어하기 위한 리소스이다. 주로 아래의 3가지를 정의한다.
- Pod 선택 (label selector 사용)
- 트래픽 방향 (ingress, egress)
- 허용할 네트워크 트래픽의 조건 (IP 블록, 포트 등등)
예시
1. 모든 트래픽 허용
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: allow-all
namespace: default
spec:
podSelector: {} # 모든 Pod 선택
ingress:
- {} # 모든 ingress 허용
egress:
- {} # 모든 egress 허용
policyTypes:
- Ingress
- Egress
2. 특정 Pod에 대한 Ingress 허용
app: backend
라벨이 있는 Pod에 대해, 특정 IP 에서의 접근만 허용한다.
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: restrict-ingress
namespace: default
spec:
podSelector:
matchLabels:
app: backend
ingress:
- from:
- ipBlock:
cidr: 192.168.1.0/24 # 특정 IP 대역
ports:
- protocol: TCP
port: 80 # 80번 포트로 허용
policyTypes:
- Ingress
3. 특정 egress 차단
app: frontend
라벨이 있는 Pod 에성 외부로의 모든 트래픽을 차단한다.
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: deny-egress
namespace: default
spec:
podSelector:
matchLabels:
app: frontend
egress: [] # 모든 egress 차단
policyTypes:
- Egress
4. 특정 Namespace 간의 통신 허용
app: database
라벨이 있는 Pod에 대해 namespace: dev
의 모든 Pod에서만 접근을 허용한다.
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: namespace-isolated-ingress
namespace: prod
spec:
podSelector:
matchLabels:
app: database
ingress:
- from:
- namespaceSelector:
matchLabels:
name: dev # dev 네임스페이스에서만 허용
policyTypes:
- Ingress
5. 특정 포트만 허용
app: web
라벨이 있는 Pod에서 TCP 443(HTTPS)만 허용한다.
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: allow-https-only
namespace: default
spec:
podSelector:
matchLabels:
app: web
ingress:
- ports:
- protocol: TCP
port: 443
policyTypes:
- Ingress
6. 클러스터 외부로의 트래픽 차단
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: block-external-traffic
namespace: default
spec:
podSelector: {} # 모든 Pod
egress:
- to:
- ipBlock:
cidr: 10.0.0.0/8 # 내부 IP 대역만 허용
policyTypes:
- Egress
7. 특정 Pod 간 통신 허용
app: frontend
에서 app: backend
Pod으로의 트래픽만 허용한다.
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: frontend-to-backend
namespace: default
spec:
podSelector:
matchLabels:
app: backend
ingress:
- from:
- podSelector:
matchLabels:
app: frontend # frontend에서만 허용
policyTypes:
- Ingress
8. 기본 차단 정책과 선택적 허용
default-deny-with-specific-allow
정책이 모든 트래픽을 기본적으로 차단.allow-specific
정책이:app-b
에서app-a
로의 ingress 허용.app-a
에서app-c
로의 egress 허용.
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: default-deny-with-specific-allow
namespace: default
spec:
podSelector: {} # 네임스페이스 내 모든 Pod
ingress: [] # 기본적으로 모든 ingress 차단
egress: [] # 기본적으로 모든 egress 차단
policyTypes:
- Ingress
- Egress
---
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: allow-specific
namespace: default
spec:
podSelector:
matchLabels:
app: app-a
ingress:
- from:
- podSelector:
matchLabels:
app: app-b
egress:
- to:
- podSelector:
matchLabels:
app: app-c
728x90
'Engineering > K8S' 카테고리의 다른 글
ConfigMap 과 Secret (0) | 2024.12.23 |
---|---|
ResourceQuota 와 LimitRange (0) | 2024.12.23 |
RBAC (0) | 2024.12.23 |
Init Container 와 멀티 컨테이너 패턴 (1) | 2024.12.20 |
application Probe (0) | 2024.12.20 |