7.5 根据请求信息路由到服务的不同版本

上一章

7.7 管理集群的出口流量

下一章

更多图书

7.6 流量镜像

有时候当我们需要将服务的新版本上线时,虽然我们已经在线下经过测试,但是并没有经过生产环境的真实流量验证,对服务的性能和是否有bug并没有太大的把握,我们希望通过线上的真实流量来验证一下新版本服务,经过生产环境流量验证无误之后,再把生产环境的流量切换到新版本的服务上。在此种场景下,我们就可以使用Istio提供的流量镜像 功能,实时复制线上真实的请求流量到我们的新版本服务上,验证新版本的服务。下面以service-go服务为例:


1 apiVersion: networking.istio.io/v1alpha3
 2 kind: DestinationRule
 3 metadata:
 4   name: service-go
 5 spec:
 6   host: service-go
 7   subsets:
 8   - name: v1
 9     labels:
10       version: v1
11   - name: v2
12     labels:
13       version: v2
14 ---
15 apiVersion: networking.istio.io/v1alpha3
16 kind: VirtualService
17 metadata:
18   name: service-go
19 spec:
20   hosts:
21   - service-go
22   http:
23   - route:
24     - destination:
25         host: service-go
26         subset: v1
27     mirror:
28       host: service-go
29       subset: v2

第24~26行定义了默认路由,表明把所有请求service-go服务的流量都路由到v1版本。

第27~29行的定义表明,把所有请求service-go服务的流量都镜像路由到v2版本上。

【实验】

1)创建测试Pod:


$ kubectl apply -f kubernetes/dns-test.yaml

2)创建service-go服务的路由规则:


$ kubectl apply -f istio/route/virtual-service-go-v1-mirror-v2.yaml

3)打开一个新的终端查看service-go-v2实例的日志信息:


$ POD=$(kubectl get pod | grep service-go-v2 | awk '{print $1}')
$ kubectl logs -f $POD service-go

4)测试访问service-go服务:


$ kubectl exec dns-test -c dns-test -- curl -s http://service-go/env
{"message":"go v1"}

多次执行上述命令,访问service-go服务。你只会访问到service-go服务的v1版本,但是在service-go服务的v2版本的Pod上,你可以查看到如下所示的请求日志,这说明对service-go服务的v1版本的请求被镜像到了v2版本中:


[GIN] 2019/01/13 - 12:44:16 | 200 | 397.207μ

s | 10.244.1.18 | GET /env 
[GIN] 2019/01/13 - 12:44:36 | 200 | 28.28μ

s   | 10.244.1.18 | GET /env
[GIN] 2019/01/13 - 12:44:38 | 200 | 98.168μ

s  | 10.244.1.18 | GET /env 
[GIN] 2019/01/13 - 12:45:25 | 200 | 47.049μ

s  | 10.244.1.18 | GET /env 
[GIN] 2019/01/13 - 12:45:26 | 200 | 32.157μ

s  | 10.244.1.18 | GET /env 
[GIN] 2019/01/13 - 12:45:27 | 200 | 33.921μ

s  | 10.244.1.18 | GET /env

5)清理:


$ kubectl delete -f kubernetes/dns-test.yaml
$ kubectl delete -f istio/route/virtual-service-go-v1-mirror-v2.yaml