(1)更改forecast的路由规则

[root@master01 ~]# cd /root/cloud-native-istio/10_canary-release/10.3
[root@master01 10.3]# kubectl apply -f vs-forecast-header-based.yaml -n weather

(2)查看规则

[root@master01 10.3]# kubectl get vs forecast-route -o yaml -n weather
[root@master01 10.3]# kubectl apply -f vs-forecast-header-based.yaml -n weather
virtualservice.networking.istio.io/forecast-route configured
[root@master01 10.3]# ls
forecast-v2-deployment.yaml  forecast-v2-destination.yaml  recommendation-all.yaml  vs-forecast-header-based.yaml
[root@master01 10.3]# kubectl get vs forecast-route -o yaml -n weather
apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
  annotations:
    kubectl.kubernetes.io/last-applied-configuration: |
      {"apiVersion":"networking.istio.io/v1alpha3","kind":"VirtualService","metadata":{"annotations":{},"name":"forecast-route","namespace":"weather"},"spec":{"hosts":["forecast"],"http":[{"match":[{"headers":{"User-Agent":{"regex":".*(Chrome/([\\d.]+)).*"}}}],"route":[{"destination":{"host":"forecast","subset":"v2"}}]},{"route":[{"destination":{"host":"forecast","subset":"v1"}}]}]}}
  creationTimestamp: "2023-11-09T01:59:22Z"
  generation: 4
  name: forecast-route
  namespace: weather
  resourceVersion: "732896"
  uid: 37e35b32-7449-4c5f-832a-178fe1a55583
spec:
  hosts:
  - forecast
  http:
  - match:
    - headers:
        User-Agent:
          regex: .*(Chrome/([\d.]+)).*
    route:
    - destination:
        host: forecast
        subset: v2
  - route:
    - destination:
        host: forecast
        subset: v1

说明:浏览器user-agnet包含chrome关键词的会被转发到v2,其它浏览器会被转发到v1

(3)打开chrome浏览器输入http://192.168.1.60:3000/dashboard,点击各个地区进行测试访问,看是否有推荐信息;打开其他浏览器输入http://192.168.1.60:3000/dashboard,点击各个地区进行测试访问,看是否有推荐信息

(4)在浏览器输入http://192.168.1.60:15029/kiali访问kiali查看,观察到浏览器user-agnet包含chrome关键词的会被转发到v2,其它浏览器会被转发到v1

2、基于cookies

有些场景下,我们需要根据登录的用户来分发流量,例如tester用户访问v2版本的应用,而其它用户访问v1版本的应用。这个需求就可以通过http的cookie关键字匹配来创建流量路由规则。

(1)部署frontend的v2版本应用:

[root@master01 ~]# cd /root/cloud-native-istio/10_canary-release/10.5
[root@master01 10.5]# kubectl apply -f frontend-v2-destination.yaml -n weather

(2)查看pod

[root@master01 ~]# kubectl get po -n weather | grep frontend-v2
frontend-v2-7bb7f4556-pqrbt          2/2     Running   0          7h59m

(3)更新DestinationRule

[root@master01 ~]# cd /root/cloud-native-istio/10_canary-release/10.5
[root@master01 10.5]# kubectl apply -f frontend-v2-destination.yaml -n weather

(4)查看规则

[root@master01 10.5]# kubectl get dr frontend-dr -n weather -o yaml
apiVersion: networking.istio.io/v1beta1
kind: DestinationRule
metadata:
  annotations:
    kubectl.kubernetes.io/last-applied-configuration: |
      {"apiVersion":"networking.istio.io/v1alpha3","kind":"DestinationRule","metadata":{"annotations":{},"name":"frontend-dr","namespace":"weather"},"spec":{"host":"frontend","subsets":[{"labels":{"version":"v1"},"name":"v1"},{"labels":{"version":"v2"},"name":"v2"}]}}
  creationTimestamp: "2023-11-09T01:59:24Z"
  generation: 2
  name: frontend-dr
  namespace: weather
  resourceVersion: "737633"
  uid: cba95ab5-9cea-45e0-b48f-2beec1afac2d
spec:
  host: frontend
  subsets:
  - labels:
      version: v1
    name: v1
  - labels:
      version: v2
    name: v2

(5)更新VirtualService

[root@master01 ~]# cd /root/cloud-native-istio/10_canary-release/10.5
[root@master01 10.5]# kubectl apply -f vs-frontend-multiservice-release.yaml -n weather

(6)查看vs路由规则

[root@master01 10.5]# kubectl get vs frontend-route -n weather -o yaml
apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
  annotations:
    kubectl.kubernetes.io/last-applied-configuration: |
      {"apiVersion":"networking.istio.io/v1alpha3","kind":"VirtualService","metadata":{"annotations":{},"name":"frontend-route","namespace":"weather"},"spec":{"gateways":["istio-system/weather-gateway"],"hosts":["*"],"http":[{"match":[{"headers":{"cookie":{"regex":"^(.*?;)?(user=tester)(;.*)?$"}}}],"route":[{"destination":{"host":"frontend","subset":"v2"}}]},{"route":[{"destination":{"host":"frontend","subset":"v1"}}]}]}}
  creationTimestamp: "2023-11-09T01:59:22Z"
  generation: 2
  name: frontend-route
  namespace: weather
  resourceVersion: "814956"
  uid: 6b02afc6-7eb3-4c03-a9d5-14d324f84ce3
spec:
  gateways:
  - istio-system/weather-gateway
  hosts:
  - '*'
  http:
  - match:
    - headers:
        cookie:
          regex: ^(.*?;)?(user=tester)(;.*)?$
    route:
    - destination:
        host: frontend
        subset: v2
  - route:
    - destination:
        host: frontend
        subset: v1

说明:该规则,会将cookie中带有user=tester关键字的请求转发到v2版本的应用里。而其它用户则转发到v1版本应用里。

(7)测试

打开chrome浏览器输入http://192.168.1.60:3000/dashboard,使用tester用户进行登录,随便点点

image-20231109222130175

在浏览器输入http://192.168.1.60:15029/kiali访问kiali查看,观察到tester用户流量转发到v2版本应用里

打开chrome浏览器输入http://192.168.1.60:3000/dashboard,使用非tester用户进行登录,随便点点

image-20231109215137650

在浏览器输入http://192.168.1.60:15029/kiali访问kiali查看,观察到非tester用户流量转发到v1版本应用里

image-20231109215043359