本系列OpenShift Servic Mesh教程是基于Red Hat官方公开发行的《Introducing Istio Service Mesh for Micoservices》出版物,我将所有操作在OpenShift 4.2.x环境中进行了验证。喜欢读英文或者需要了解更多和场景相关知识点的小伙伴可以通过上面的链接下载该书慢慢阅读。
本章节我们先部署一个微服务的新版本,然后基于Istio流量控制功能实现灰度发布的微服务版本升级切换。通过VirtualService的流量控制机制可以实现不同场景的灰度发布,本文实现以下两种灰度发布。
$ oc apply -f recommendation/kubernetes/Deployment-v2.yml
$ oc get pod
NAME READY STATUS RESTARTS AGE
customer-77dc47d7f8-szhd5 2/2 Running 1 44m
preference-v1-55476494cf-xm4dq 2/2 Running 0 44m
recommendation-v1-67976848-4l4s7 2/2 Running 0 44m
recommendation-v2-599867df6c-5ccdx 2/2 Running 0 38s
$ ./scripts/run.sh http://${INGRESS_GATEWAY}/customer
customer => preference => recommendation v2 from '3cbba7a9cde5': 1
customer => preference => recommendation v1 from '67976848-4l4s7': 96
customer => preference => recommendation v2 from '3cbba7a9cde5': 2
customer => preference => recommendation v1 from '67976848-4l4s7': 97
customer => preference => recommendation v2 from '3cbba7a9cde5': 3
customer => preference => recommendation v1 from '67976848-4l4s7': 98
...
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: recommendation
spec:
hosts:
- recommendation
http:
- route:
- destination:
host: recommendation # host对应的recommendation的Service的url,可以是short或long的url
subset: version-v2 # subset对应的是DestinationRule中的subset名称,DestinationRule会按照这个lable找到对应的pod
weight: 100
在istiofiles/destination-rule-recommendation-v1-v2.yml中定义DestinationRule对象。
apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
name: recommendation
spec:
host: recommendation
subsets:
- labels:
version: v1 # DestinationRule会按照这个lable找到对应的pod
name: version-v1
- labels:
version: v2
name: version-v2
执行以下命令分别创建VirtualService和DestinationRule,注意:当只运行第一个命令创建VirtualService后,第一个窗口会报错“customer => Error: 500 - ErrorInternal Server Error”,这是由于没有找到VirtualService中使用的subnet,这个subnet是在DestinationRule中定义的。
$ oc apply -f istiofiles/virtual-service-recommendation-v2.yml
$ oc apply -f istiofiles/destination-rule-recommendation-v1-v2.yml
customer => preference => recommendation v2 from '3cbba7a9cde5': 24
customer => preference => recommendation v2 from '3cbba7a9cde5': 25
customer => preference => recommendation v2 from '3cbba7a9cde5': 26
...
$ oc apply -f istiofiles/virtual-service-recommendation-1.yml
customer => preference => recommendation v1 from '67976848-4l4s7': 859
customer => preference => recommendation v1 from '67976848-4l4s7': 860
customer => preference => recommendation v1 from '67976848-4l4s7': 861
...
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: recommendation
spec:
hosts:
- recommendation
http:
- route:
- destination:
host: recommendation
subset: version-v1
weight: 90
- destination:
host: recommendation
subset: version-v2
weight: 10
执行命令,更新VirtualService策略。
$ oc apply -f istiofiles/virtual-service-recommendation-v1_and_v2.yml
$ oc apply -f istiofiles/virtual-service-recommendation-v1_and_v2_75_25.yml
通过VirtualService对请求转发分配策略,我们还可实现其它复杂的场景,其中灰度发布就是一种常用的场景。
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: recommendation
spec:
hosts:
- recommendation
http:
- match:
- headers:
baggage-user-agent:
regex: .*Safari.*
route:
- destination:
host: recommendation
subset: version-v2
- route:
- destination:
host: recommendation
subset: version-v1
执行命令修改VirtualService策略。
$ oc apply -f istiofiles/virtual-service-safari-recommendation-v2.yml
$ curl -A Safari $INGRESS_GATEWAY/customer
$ curl -A Firefox $INGRESS_GATEWAY/customer
$ oc apply -f istiofiles/virtual-service-mobile-recommendation-v2.yml
$ curl -A "Mozilla/5.0 Version/5.0.2 Mobile/8J2 Safari" $INGRESS_GATEWAY/customer
$ curl $INGRESS_GATEWAY/customer