kubernetes

istio traffic management

misankim 2023. 3. 10. 19:12

istio traffic management

 

 

# 이미 구성된 환경

 

gke 클러스터
asm(istio 도 무관)
bookinfo 샘플 앱(gateway, virtualservice 포함)

 

 

# destination rule 생성

-> 룰은 목적지에 대한 라우팅 룰일뿐 실제 라우팅을 정의하는 것을 virtualservice 리소스

 

kubectl apply -f https://raw.githubusercontent.com/istio/istio/master/samples/bookinfo/networking/destination-rule-all.yaml

kubectl get destinationrules

kubectl get destinationrules -o yaml

 

 

apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
  name: productpage
spec:
  host: productpage
  subsets:
  - name: v1
    labels:
      version: v1
---
apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
  name: reviews
spec:
  host: reviews
  subsets:
  - name: v1
    labels:
      version: v1
  - name: v2
    labels:
      version: v2
  - name: v3
    labels:
      version: v3
---
apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
  name: ratings
spec:
  host: ratings
  subsets:
  - name: v1
    labels:
      version: v1
  - name: v2
    labels:
      version: v2
  - name: v2-mysql
    labels:
      version: v2-mysql
  - name: v2-mysql-vm
    labels:
      version: v2-mysql-vm
---
apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
  name: details
spec:
  host: details
  subsets:
  - name: v1
    labels:
      version: v1
  - name: v2
    labels:
      version: v2
---

 

 

# 각 서비스의 모든 트래픽을 v1 으로 라우팅하도록 virtualservice 생성

 

kubectl apply -f https://raw.githubusercontent.com/istio/istio/master/samples/bookinfo/networking/virtual-service-all-v1.yaml

kubectl get virtualservices

 

 

apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: productpage
spec:
  hosts:
  - productpage
  http:
  - route:
    - destination:
        host: productpage
        subset: v1
---
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: reviews
spec:
  hosts:
  - reviews
  http:
  - route:
    - destination:
        host: reviews
        subset: v1
---
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: ratings
spec:
  hosts:
  - ratings
  http:
  - route:
    - destination:
        host: ratings
        subset: v1
---
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: details
spec:
  hosts:
  - details
  http:
  - route:
    - destination:
        host: details
        subset: v1
---

 

 

리뷰 서비스의 트래픽이 v1 버전으로 모두 라우팅되는지 확인

 

 

# 사용자의 아이디를 기반으로 특정 버전의 리뷰 서비스로 라우팅하도록 virtualservice 업데이트

 

kubectl apply -f https://raw.githubusercontent.com/istio/istio/master/samples/bookinfo/networking/virtual-service-reviews-test-v2.yaml

 

 

apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: reviews
spec:
  hosts:
    - reviews
  http:
  - match:
    - headers:
        end-user:
          exact: jason
    route:
    - destination:
        host: reviews
        subset: v2
  - route:
    - destination:
        host: reviews
        subset: v1

 

 

curl -c cookies.txt -F "username=jason" -L -X POST http://$GATEWAY_URL/login

cookie_info=$(grep -Eo "session.*" ./cookies.txt)
cookie_name=$(echo $cookie_info | cut -d' ' -f1)
cookie_value=$(echo $cookie_info | cut -d' ' -f2)

siege -c 5 http://$GATEWAY_URL/productpage --header "Cookie: $cookie_name=$cookie_value"

 

리뷰 서비스 v2 버전의 트래픽양이 증가하는지 확인 

 

 

# 다시 모든 버전의 각 서비스로 라우팅되도록 destination rule 제거

 

kubectl delete -f https://raw.githubusercontent.com/istio/istio/master/samples/bookinfo/networking/virtual-service-all-v1.yaml

 

리뷰 서비스의 트래픽이 v1~v3 버전으로 균일하게 라우팅되는지 확인

 

 

# 각 서비스의 버전을 점진적으로 다른 버전으로 전환

 

각 서비스의 버전을 v1 버전으로 라우팅

 

kubectl apply -f https://raw.githubusercontent.com/istio/istio/master/samples/bookinfo/networking/virtual-service-all-v1.yaml

 

리뷰 서비스의 트래픽을 v1 버전과 v3 버전으로 50:50 비율로 라우팅 

kubectl apply -f https://raw.githubusercontent.com/istio/istio/master/samples/bookinfo/networking/virtual-service-reviews-50-v3.yaml

 

 

apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: reviews
spec:
  hosts:
    - reviews
  http:
  - route:
    - destination:
        host: reviews
        subset: v1
      weight: 50
    - destination:
        host: reviews
        subset: v3
      weight: 50

 

 

리뷰 서비스의 트래픽을 v3 버전으로 전부 라우팅

 

kubectl apply -f https://raw.githubusercontent.com/istio/istio/master/samples/bookinfo/networking/virtual-service-reviews-v3.yaml

 

 

apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: reviews
spec:
  hosts:
    - reviews
  http:
  - route:
    - destination:
        host: reviews
        subset: v3

 

 

다시 각 서비스의 트래픽이 v1~v3 버전으로 균일하게 라우팅되도록 virtualservice 삭제

 

kubectl delete -f https://raw.githubusercontent.com/istio/istio/master/samples/bookinfo/networking/virtual-service-all-v1.yaml