다양한 트래픽 제어 기법을 제공하여 마이크로서비스 간의 통신을 보다 효율적이고 안정적으로 관리할 수 있다. 이 중에서 4개를 알아볼 예정이고, 상에서는 Routing과 Traffic Shifting을 알아본다.

 

Routing

라우팅은 클라이언트 요청을 올바른 서비스나 서비스의 특정 버전으로 보내는 방식이다. 이것은 새롭게 배포한 버전에 클라이언트가 바로 요청을 보내지 않고, 내부에서 테스트한 후에 요청을 보낼 수 있도록 활용할 수 있다. 이전에 사용한 VirtualService와 DestinationRule을 추가하면 된다. 

 

리소스는 다음과 같이 있다고 가정하자. 

apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:
name: test-gateway
spec:
selector:
istio: ingressgateway
servers:
- port:
number: 80
name: http
protocol: HTTP
hosts:
- "test.io"
---
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: test-vs
spec:
hosts:
- "test.io"
gateways:
- test-gateway
http:
- route:
- destination:
host: test
---

 

 

요청은 보는 것과 같이 Application V1로 가게 될 것이다. 서비스를 등록할 때 app: test를 사용했기에 새롭게 배포하는 v2의 경우도 로드밸런싱을 통해서 트래픽이 가게 된다. 

 

이것을 서비스를 하나 더 만드는 것이 아니라 DestinationRule의 subsets을 사용해서 해결 가능하다. 

apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
name: test
spec:
host: test.default.svc.cluster.local
subsets:
- name: version-v1
labels:
version: v1
- name: version-v2
labels:
version: v2

 

이렇게 하면 라벨의 버전에 따라서 서브셋이 구별되며, VirtualService에서 라우팅을 수정 해주면 된다. 

 

apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: test-vs
spec:
hosts:
- "test.io"
gateways:
- test-gateway
http:
- match: ## 여기서 원하는 규칙을 추가할 수 있음
- headers:
x-istio-internal:
exact: "internal" # x-istio-internal 헤더가 internal과 동일할 경우 v2로 요청을 라우팅
route:
- destination:
host: test
subset: version-v2
- route:
- destination:
host: test
subset: version-v1

 

x-istio-internal에 값을 넣었을 경우에만 v2로 요청을 보낼 수 있어서 내부적으로 테스트도 가능하다.  

 

Traffic Shifting

새로운 버전의 서비스로 트래픽을 점진적으로 전환하는 방법이다. 카나리 배포랑 유사하다고 생각하면 된다. 기존 환경과 유사하고, VirtualService만 수정해 주면 된다.

 

apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: test
spec:
hosts:
- "test.io"
gateways:
- test-gateway
http:
- route:
- destination:
host: test
subset: version-v1
weight: 90
- destination:
host: test
subset: version-v2
weight: 10

 

이렇게 설정하면 90%의 트래픽은 v1으로 10%의 트래픽은 v2로 라우팅된다. 이것은 수동으로 조정하거나, CI/CD 도구로 트래픽을 완전히 옮기면 된다. 아니면 flagger라는 도구를 사용하면 canary 기능을 대신해 주고, VirtualService까지 대신 작성해 준다. 궁금한 사람들은 찾아보자.