《OpenShift 4.x HOL教程汇总》
说明:本文已经在OpenShift 4.12环境中验证
本章节我们先部署一个微服务的新版本,然后基于Istio流量控制功能实现灰度发布的微服务版本升级切换。通过VirtualService的流量控制机制可以实现不同场景的灰度发布,本文实现以下两种灰度发布。
$ oc apply -f recommendation/kubernetes/Deployment-v2.yml -n ${ISTIO_APP}
$ oc get pod -n ${ISTIO_APP} -l app=recommendation
NAME READY STATUS RESTARTS AGE
recommendation-v1-dd8544f7c-s64sx 2/2 Running 0 56m
recommendation-v2-5494578985-mx7ft 2/2 Running 0 18m
$ ./scripts/run.sh ${INGRESS_GATEWAY}/customer
customer => preference => recommendation v2 from 'recommendation-v2-5494578985-mx7ft': 1
customer => preference => recommendation v1 from 'recommendation-v1-dd8544f7c-s64sx': 96
customer => preference => recommendation v2 from 'recommendation-v2-5494578985-mx7ft': 2
customer => preference => recommendation v1 from 'recommendation-v1-dd8544f7c-s64sx': 97
customer => preference => recommendation v2 from 'recommendation-v2-5494578985-mx7ft': 3
customer => preference => recommendation v1 from 'recommendation-v1-dd8544f7c-s64sx': 98
...
apiVersion: networking.istio.io/v1beta1
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/v1beta1
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 -
$ oc apply -n ${ISTIO_APP} -f istiofiles/virtual-service-recommendation-v2.yml
$ oc apply -n ${ISTIO_APP} -f istiofiles/destination-rule-recommendation-v1-v2.yml
customer => preference => recommendation v2 from 'recommendation-v2-5494578985-mx7ft': 24
customer => preference => recommendation v2 from 'recommendation-v2-5494578985-mx7ft': 25
customer => preference => recommendation v2 from 'recommendation-v2-5494578985-mx7ft': 26
...
$ oc apply -n ${ISTIO_APP} -f istiofiles/virtual-service-recommendation-v1.yml
customer => preference => recommendation v1 from 'recommendation-v1-dd8544f7c-s64sx': 159
customer => preference => recommendation v1 from 'recommendation-v1-dd8544f7c-s64sx': 160
customer => preference => recommendation v1 from 'recommendation-v1-dd8544f7c-s64sx': 161
...
apiVersion: networking.istio.io/v1beta1
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 -n ${ISTIO_APP} -f istiofiles/virtual-service-recommendation-v1_and_v2.yml
$ oc apply -n ${ISTIO_APP} -f istiofiles/virtual-service-recommendation-v1_and_v2_75_25.yml
通过VirtualService对请求转发分配策略,我们还可实现其它复杂的场景,其中灰度发布就是一种常用的场景。
apiVersion: networking.istio.io/v1beta1
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 -n ${ISTIO_APP} -f istiofiles/virtual-service-safari-recommendation-v2.yml
$ curl -A Safari $INGRESS_GATEWAY/customer
customer => preference => recommendation v2 from 'recommendation-v2-5494578985-mx7ft': 24
$ curl -A Firefox $INGRESS_GATEWAY/customer
customer => preference => recommendation v1 from 'recommendation-v1-dd8544f7c-s64sx': 276
$ oc apply -n ${ISTIO_APP} -f istiofiles/virtual-service-mobile-recommendation-v2.yml
$ curl -A "Mozilla/5.0 Version/5.0.2 Mobile/8J2 Safari" $INGRESS_GATEWAY/customer
customer => preference => recommendation v2 from 'recommendation-v2-5494578985-mx7ft': 25
$ curl $INGRESS_GATEWAY/customer
customer => preference => recommendation v1 from 'recommendation-v1-dd8544f7c-s64sx': 277