一个常见的用例是将流量从微服务的一个版本的逐渐迁移到另一个版本。在 Istio 中,您可以通过配置一系列规则来实现此目标。这些规则将一定比例的流量路由到一个或另一个服务。在本任务中,您将会把 50% 的流量发送到 reviews:v1
,另外,50% 的流量发送到 reviews:v3
。接着,再把 100% 的流量发送到 reviews:v3
来完成迁移。
https://istio.io/latest/zh/docs/tasks/traffic-management/traffic-shifting/
1)应用默认目标规则
https://istio.io/latest/zh/docs/examples/bookinfo/#apply-default-destination-rules
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 ---
2)将所有流量路由到各个微服务的 v1
版本,虚拟路由中的路由配置和目标规则的配置是对应的,具体版本匹配是在目标规则中控制的
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 ---
3)此时请求http://127.0.0.1/productpage (外部访问通过istio-ingressgateway控制入口) 的流量走向
4、把 50% 的流量从 reviews:v1
转移到 reviews:v3
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
此时看v1和v3的请求量还是差不多的,量少是看不出来效果的
5)将所有流量切换到v3
apiVersion: networking.istio.io/v1alpha3 kind: VirtualService metadata: name: reviews spec: hosts: - reviews http: - route: - destination: host: reviews subset: v3
现在流量就都到v3上面了
使用 Istio 的权重路由功能将 reviews
服务的流量迁移到新版本。使用容器编排平台的部署功能来进行版本迁移完全不同,容器编排平台使用了实例扩容来对流量进行管理。