一个常见的用例是将流量从一个版本的微服务逐渐迁移到另一个版本。在 Istio 中,您可以通过配置一系列规则来实现此目标, 这些规则将一定百分比的流量路由到一个或另一个服务。在本任务中,您将会把 50% 的流量发送到 reviews:v1,另外 50% 的流量发送到 reviews:v3。然后,再把 100% 的流量发送到 reviews:v3 来完成迁移。
istio中的流量转移常用于蓝绿部署中
首先我们将访问bokinfo所有的流量都指向v1版本
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
| [root@VM-0-13-centos istio-1.5.1] virtualservice.networking.istio.io/productpage created virtualservice.networking.istio.io/reviews configured virtualservice.networking.istio.io/ratings created virtualservice.networking.istio.io/details created [root@VM-0-13-centos istio-1.5.1] 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 ---
|
然后我们把50%的流量转到v1版本,50%的流量转到v3版本
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| [root@VM-0-13-centos istio-1.5.1] virtualservice.networking.istio.io/reviews configured [root@VM-0-13-centos istio-1.5.1] 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
|
刷新浏览器中的 /productpage 页面,大约有50%的几率会看到页面中出带红色星级的评价内容。这是因为v3版本的reviews访问了带星级评级的 ratings 服务,但v1版本却没有。