本节介绍应用部署上线和切换应用访问的常用策略。虽然可以根据需要灵活使用以下策略,但由于本节使用了同一组验证的应用,因此请按顺序进行下面3个Lab操作。
在部署应用前,需要将https://github.com/liuxiaoyu-git/bluegreen.git的代码fork到自己的GitHub账户下,下文将用“MY_GITHUB”代表用户各自的GitHub账户名。
蓝绿部署是最常见的一种不间断的应用升级部署的方式,蓝绿部署的目的是安全稳定地发布新版本应用,并在必要时回滚。蓝绿部署的特点是在一个时间点请求会100%地发给蓝绿部署中的一个目标。
蓝绿部署通常生产环境需要两组配置,一组是active的生产环境的配置,一组是inactive的配置。用户访问的时候,只让用户访问active的服务器集群。蓝色环境(active)运行旧版本应用version1。当要升级到version2,先在绿色环境中部署新版本应用,并进行测试。如果测试没问题,就可以把路由指向绿色环境。
$ oc new-project USER-ID-bluegreen
$ oc new-app https://github.com/MY_GITHUB/bluegreen.git#master --name=blue
$ oc expose service blue --name=bluegreen
$ oc get route bluegreen
NAME HOST/PORT PATH SERVICES PORT TERMINATION WILDCARD
bluegreen bluegreen-mywar.apps.cluster-beijing-c70a.beijing-c70a.example.opentlc.com blue 8080-tcp None
$ oc new-app https://github.com/MY_GITHUB/bluegreen.git#green --name=green
$ oc patch route/bluegreen -p '{"spec":{"to":{"name":"green"}}}'
我把金丝雀发布和A/B测试放在一起,因为它们的特点都是在一个时间点允许将请求根据需要流向不同的目标,不过金丝雀发布和A/B测试有不同的目的。金丝雀发布又名灰度发布,是用逐步过渡的方法实现新版本应用上线的一种发布策略;而A/B测试主要是用来测试一个应用的不同实现版本的差异化表现的方法,例如评估两个不同界面风格的应用哪个更受用户的欢迎、其性能差异等。
$ oc project USER-ID-bluegreen
$ oc edit route bluegreen
。。。
spec:
host: bluegreen-mywar.apps.cluster-beijing-c70a.beijing-c70a.example.opentlc.com
port:
targetPort: 8080-tcp
subdomain: ""
to:
kind: Service
name: green
weight: 50
alternateBackends:
- kind: Service
name: blue
weight: 50
wildcardPolicy: None
。。。
$ oc get route bluegreen
NAME HOST/PORT PATH SERVICES PORT TERMINATION WILDCARD
bluegreen bluegreen-mywar.apps.cluster-beijing-c70a.beijing-c70a.example.opentlc.com green(50%),blue(50%) 8080-tcp None
$ while true;do curl $(oc get route bluegreen -o template --template '{{.spec.host}}') | grep -e green -e blue;done
$ oc set route-backends bluegreen --adjust blue=30%
$ oc set route-backends bluegreen --adjust blue=+10%
$ oc set route-backends bluegreen --equal
还可用一个Service对应多个DeploymentConfig,然后通过调整DeploymentConfig中运行的Pod实例数实现在不同版本的应用分配流量。
$ oc new-project USER-ID-ab
$ oc new-app openshift/deployment-example:v1 --name=ab-example --labels=ab-example=true
$ oc expose svc/ab-example --name=ab-example
$ oc delete dc/ab-example
$ oc new-app openshift/deployment-example:v1 --name=ab-example-a --labels=ab-example=true
$ oc new-app openshift/deployment-example:v2 --name=ab-example-b --labels=ab-example=true SUBTITLE=Shard-B COLOR=red
$ oc delete svc/ab-example-a
$ oc delete svc/ab-example-b
$ curl $(oc get route ab-example -o template --template '{{.spec.host}}') | grep h2
$ oc scale dc/ab-example-a --replicas=0
$ oc scale dc/ab-example-a --replicas=1; oc scale dc/ab-example-b --replicas=0
下面首先部署version 1版的blue应用,再更改应用到新版本version 2后再次部署blue,当发现部署后的新版本应用出现问题,可以通过部署回滚快速切换到以前运行正常的version 1版本应用。
$ oc project USER-ID-bluegreen
$ oc set route-backends bluegreen --adjust blue=100%
' version 1'+
$ oc get dc blue
NAME REVISION DESIRED CURRENT TRIGGERED BY
blue 2 1 1 config
$ curl $(oc get route bluegreen -o template --template '{{.spec.host}}') | grep version
$ oc rollout undo dc/blue
deploymentconfig.apps.openshift.io/blue rolled back
$ oc get build
NAME TYPE FROM STATUS STARTED DURATION
blue-1 Source Git@31c4e83 Complete About an hour ago 1m14s
blue-2 Source Git@e7df24a Complete About an hour ago 1m29s
blue-3 Source Git@69b099b Complete About an hour ago 1m26s
$ oc describe build blue-2 | grep 'Image Digest'
Image Digest: sha256:5fae117dd73f9dc28cb3228a8efc2a53cf15289bfd8224ef2163de59f4930c05
$ oc describe is blue | grep blue
Name: blue
Labels: app=blue
Image Repository: default-route-openshift-image-registry.apps.cluster-beijing-c70a.beijing-c70a.example.opentlc.com/mywar/blue
* image-registry.openshift-image-registry.svc:5000/mywar/blue@sha256:e7dac0f2191e385b25c39ec5bf2108c75a7d6770417551b03e5f79b1f8e989d7
image-registry.openshift-image-registry.svc:5000/mywar/blue@sha256:5fae117dd73f9dc28cb3228a8efc2a53cf15289bfd8224ef2163de59f4930c05
image-registry.openshift-image-registry.svc:5000/mywar/blue@sha256:085591991c22f4f76ab9ffa0f4247eb6047710b99547c7514f38168f6e25d5cd
$ oc rollback blue
deploymentconfig.apps.openshift.io/blue deployment #6 rolled back to blue-3
Warning: the following images triggers were disabled: blue:latest
You can re-enable them with: oc set triggers dc/blue --auto
$ oc rollback blue --to-version=1