蓝绿部署是一种用于减少部署新版本应用程序的停机时间的技术。 Martin Fowler 在 2010 年初在他的网站 https://martinfowler.com/bliki/BlueGreenDeployment 上描述了这种技术.html。以下是从这个网站上摘录的一段话:
"One of the challenges with automating deployment is the cut-over itself, taking software from the final stage of testing to live production. You usually need to do this quickly in order to minimize downtime. The blue-green deployment approach does this by ensuring you have two production environments, as identical as possible. At any time one of them, let's say blue for the example, is live. As you prepare a new release of your software you do your final stage of testing in the green environment. Once the software is working in the green environment, you switch the router so that all incoming requests go to the green environment - the blue one is now idle."
这是蓝绿部署的逻辑架构:
蓝绿部署的关键是要有一个能够按需从蓝环境切换到绿环境的路由或负载均衡器:
这肯定会带来一些好处,例如零停机时间和随时可用的回滚环境。另一方面,这个新环境在会话应用方面也将焕然一新,因此所有用户会话都将丢失。为了减轻这种损失,将应用程序设置为只读模式可能会有所帮助,或者等待所有会话耗尽。
但是,蓝绿部署是关于测试生产环境,而不是测试应用程序本身,这应该在测试和/或 UAT 中完成。
让我们用一个例子来尝试一个蓝绿部署:
- First of all, if you don't have your OpenShift platform up and running, start it as follows:
- Once everything has started, open your browser and point it to https://127.0.0.1:8443/console/.
- Log in as developer, and click on the Nginx icon in Browse Catalog, as follows:
- In the pop-up wizard, click Next and fill in the next form, as shown in the following screenshot:
- In the last step, the platform confirms the creation of the application, which is already being deployed in your project.
- Click on the link labeled Continue to the project overview to see its overview and progress:
The overview of the application created
- Once the application has been successfully deployed in the platform as a container in a pod, click on the route stating http://blue-green-app-myproject.127.0.0.1.nip.io, which should load our example application.
该页面应以蓝色显示一些文本,如下所示:
- To simulate our new version of the application, and thus simulate a switch between the two versions—the blue (the one we just deployed) and the green (the one we are about to deploy)—go back to the catalog and select the Nginx icon, and click Next in the pop-up wizard.
- This time, click on the Advanced options link and fill in the form, as follows:
The Advanced options page
与之前的部署不同,当然是名称,即blue-green-app-2。此外,部署现在指向我们的 Git 存储库的名为 green 的分支,因此我们不会为我们的应用程序自动创建路由。
这样,应用程序就不会暴露在外部。但是,如果不共享路由,用户将不会知道新 URL,但由于这是一项自动化任务,因此 URL 可以根据应用程序名称进行预测,好奇的用户可能会破坏您的新功能。出于这个原因,我们将禁用自动路由创建:
- Once the application has been deployed, we can test how it behaves in the new environment, and once we are satisfied, we can proceed with the switchover.
为此,我们点击左侧的垂直菜单 Applications | Routes 然后点击名为 blue-green-app 的路由。
我们现在应该看到路线的详细信息,如下所示:
- On the page, in the top-right corner, there is a drill-down menu called Actions; click on it and select Edit.
在 Edit 页面上,我们更改了路由的所有设置,甚至是指向的服务层,这是我们需要更改才能从蓝色环境切换的服务层到绿色环境,如以下屏幕截图所示:
- Then, click Save, and refresh the page of our application. We should now see the web page displaying some text in green, as follows:
您当前应用程序的 URL 没有改变,新的精美版本终于上线了。
恭喜!您成功执行了蓝绿部署。
请记住,如果两个版本之间的数据模型发生了细微的变化,那么蓝绿部署应该以不同的方式完成,即将应用程序的蓝色版本设置为只读模式并等待它耗尽所有待处理的写入请求,然后切换到新的绿色版本。