IP 주소 또는 포트 수준(OSI 계층 3 또는 4)에서 트래픽 흐름을 제어하려는 경우, 클러스터의 특정 애플리케이션에 대해 쿠버네티스 네트워크폴리시(NetworkPolicy) 사용을 고려할 수 있다.
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: test
namespace: default
spec:
podSelector:
matchLabels:
service: msaB
policyTypes:
- Ingress
- Egress
ingress:
- from:
- ipBlock:
cidr: 172.17.0.0/16
except:
- 172.17.1.0/24
- namespaceSelector:
matchLabels:
role: A-product
- podSelector:
matchLabels:
service: msaA
ports:
- protocol: TCP
port: 8080
egress:
- to:
- ipBlock:
cidr: 10.0.0.0/24
ports:
- protocol: TCP
port: 5978
Pod, Namespace, IPBlock의 3가지 타입으로 제어 가능
ingress:
- from:
- ipBlock:
cidr: 172.17.0.0/16
except:
- 172.17.1.0/24
- namespaceSelector:
matchLabels:
role: A-product
- podSelector:
matchLabels:
service: msaA
$ k get ns --show-labels
NAME STATUS AGE LABELS
A-product Active 1d role=A-product
B-product Active 1d <none>
$ k get po --show-labels
NAME READY STATUS RESTARTS AGE LABELS
msaA 1/1 Running 0 1d service=msaA
msaB 1/1 Running 0 1d service=msaB
네임스페이스 기준으로는 A-product 네임스페이스에 속하는 모든 pod의 트래픽을 허용할 것이고
파드 기준으로는 msaA 파드로부터의 트래픽을 허용할 것이다.
- namespaceSelector:
matchLabels:
role: A-product
- podSelector:
matchLabels:
service: msaA
- namespaceSelector:
matchLabels:
role: A-product
podSelector:
matchLabels:
service: msaA