1. Overview
1.概述
In this tutorial, we’ll describe the six most common deployment strategies.
在本教程中,我们将介绍六种最常见的部署策略。
2. Deployment Strategies
2.部署战略
Whenever we deploy an application, we have to carefully plan the process. We need to consider different aspects and how it affects the user experience. A deployment aims to install or upgrade the application without downtime and without disturbing the user experience.
每当我们部署一个应用程序时,我们必须仔细计划这个过程。我们需要考虑不同的方面,以及它是如何影响用户体验的。部署的目的是在不停机和不干扰用户体验的情况下安装或升级应用程序。
A deployment strategy describes the process of how these upgrades should be performed.
部署策略描述了应如何执行这些升级的过程。。
3. Recreate Deployment Strategy
3.重新制定部署战略
The most basic strategy we discuss is called the “Recreate” deployment strategy. As the name suggests, we stop and then recreate the application. The newly created deployment runs the updated version of the application.
我们讨论的最基本策略被称为 “重新创建 “部署策略。顾名思义,我们停止并重新创建应用程序。新创建的部署运行应用程序的更新版本。
Users will experience some downtime because we need to stop the application before the new version is running. The application won’t be available while the original version is shutting down and the new one is starting.
用户将经历一些停机时间,因为我们需要在新版本运行之前停止应用程序。在原版本关闭和新版本启动时,应用程序将无法使用。
On the other hand, this strategy is easy to set up. We don’t have to manage two different versions of the application simultaneously. If we choose this approach, the updated application will be available for all users immediately. This has a drawback though, because the new version might introduce bugs to the application, and the users will see those as well.
另一方面,这种策略很容易设置。我们不必同时管理两个不同版本的应用程序。如果我们选择这种方法,更新后的应用程序将立即供所有用户使用。不过这也有一个缺点,因为新版本可能会给应用程序引入错误,而用户也会看到这些错误。
We might even need to completely roll back to the previous working version. Unfortunately, the rollback causes downtime, too. To revert the deployment, we need to stop the updated application and recreate the original one.
我们甚至可能需要完全回滚到之前的工作版本。不幸的是,回滚也会导致停机。为了恢复部署,我们需要停止更新的应用程序并重新创建原来的应用程序。
In conclusion, this deployment strategy is fairly simple to set up and manage, but we need to consider its drawbacks before we use it.
总之,这种部署策略的设置和管理相当简单,但我们在使用它之前需要考虑其缺点。
4. Blue-Green Deployment
4.蓝绿色的部署
The next deployment strategy we discuss is called “Blue-green” deployment. In this case, we assign different colors to different application versions. That’s where the name comes from. The original, old version is called blue environment, and the new updated version is called green environment.
我们讨论的下一个部署策略被称为 “蓝绿 “部署。在这种情况下,我们为不同的应用程序版本分配不同的颜色。这就是这个名字的由来。原始的、旧的版本被称为蓝色环境,而新的更新版本被称为绿色环境。
Let’s see how this strategy works. Firstly, we have the blue environment. This is an existing application that handles all user traffic. We would like to update the application without downtime. To accomplish this, we create an almost identical environment. There is a difference, though. The new environment will contain the updated application version. Now, both environments are running our application, but the users are still using the old version:
让我们看看这个策略如何运作。首先,我们有蓝色的环境。这是一个现有的应用程序,处理所有的用户流量。我们想在不停机的情况下更新该应用程序。为了达到这个目的,我们创建一个几乎相同的环境。不过,有一个区别。新环境将包含更新后的应用程序版本。现在,两个环境都在运行我们的应用程序,但用户仍在使用旧版本:。
The above image shows the state when we already started the new version, but users are still using the blue environment. The next step is to switch to a green environment by transferring all user traffic to it. This switch can happen very quickly, so users won’t experience downtime. In addition, they can use the application the same way as before, and they don’t know that their requests are being served by the updated application:
上图显示了我们已经启动新版本时的状态,但用户仍在使用蓝色环境。下一步是通过将所有用户流量转移到绿色环境中来切换。这种切换可以很快发生,所以用户不会经历停机时间。此外,他们可以像以前一样使用应用程序,而且他们不知道他们的请求是由更新的应用程序提供的。
If we need to roll back the changes, it can happen immediately by routing the traffic to the original, blue environment. When the deployment is finished and we don’t want to roll back, we can remove the old, blue environment. Then we rename the updated version from green to blue. The next release can follow the same procedure.
如果我们需要回滚变化,可以通过将流量路由到原来的蓝色环境来立即发生。当部署完成并且我们不想回滚时,我们可以删除旧的蓝色环境。然后我们将更新的版本从绿色重命名为蓝色。下一个版本可以遵循同样的程序。
We can see that this process is preferable in terms of uptime and being able to roll back. However, it has disadvantages too. It has higher resource needs because we create two nearly identical environments. This can be expensive and time-consuming to set up. Secondly, we might run into problems with the rollback if the new version makes some non-backward-compatible changes. For example, changes in the database.
我们可以看到,这个过程在正常运行时间和能够回滚方面是比较好的。然而,它也有缺点。它有更高的资源需求,因为我们创建了两个几乎相同的环境。这可能是昂贵和耗时的设置。其次,如果新版本做了一些不向后兼容的更改,我们可能会在回滚中遇到问题。例如,数据库中的变化。
5. Rolling Update
5 滚动式更新
The next strategy is called “Rolling Update”. It is applicable only if we have multiple instances of an application.
下一个策略被称为 “滚动更新”。它只适用于我们有多个应用程序实例的情况。
Initially, all the instances are running the old version, and all of them can process user requests. This is important here because the process will remove instances, and the remaining ones must be able to serve the users to prevent downtime.
最初,所有的实例都在运行旧版本,而且都能处理用户请求。这一点在这里很重要,因为这个过程将删除实例,剩下的实例必须能够为用户服务,以防止停机。
Firstly, we create a new instance that runs the updated version. When this new instance is started, we consider it a part of the application, so the user requests might already be processed by it:
首先,我们创建一个运行更新版本的新实例。当这个新实例被启动时,我们认为它是应用程序的一部分,所以用户的请求可能已经被它处理。
After this, we remove one from the instances that run the old version. This way, we have two instances running the old version and one instance running the updated version. This indicates that we must be able to support different versions at the same time if we would like to use this deployment strategy. Otherwise, the old or the new instances might not work as expected.
之后,我们从运行旧版本的实例中移除一个。这样,我们有两个运行旧版本的实例和一个运行更新版本的实例。这表明,如果我们想使用这种部署策略,我们必须能够同时支持不同的版本。否则,旧的或新的实例可能无法按预期工作。
The process continues to add instances running the new version and remove their pairs:
这个过程继续添加运行新版本的实例并删除它们的配对。
The deployment is finished when the last instance is replaced with an updated one, and we made sure that the application works as expected. Of course, we can test during the update too. If we find any issues, we can roll back all changes.
当最后一个实例被更新的实例所取代时,部署就完成了,我们确保应用程序按照预期运行。当然,我们也可以在更新期间进行测试。如果我们发现任何问题,我们可以回滚所有的变化。
This deployment strategy can be parameterized to better fit our needs. We can usually set how many instances we want to update at a time. It can be more than one. We were using this value in our example for simplicity.
这种部署策略可以被参数化以更好地满足我们的需求。我们通常可以设置一次要更新多少个实例。它可以是一个以上的。在我们的例子中,为了简单起见,我们使用了这个值。
6. Canary Deployment
6.金丝雀的部署
The following deployment method we examine is called “Canary deployment”. The main idea is that we deploy the update to more and more users in iterations.
我们研究的下面的部署方法被称为 “Canary部署”。其主要思想是,我们在迭代中向越来越多的用户部署更新。。
Initially, only a small part of the users receive the update. After this step, we test the system to make sure the application is working properly after the changes. This way, we can reduce risks, and even if we introduce bugs to the system, only a subset of users will see it.
最初,只有一小部分用户会收到更新。在这一步之后,我们对系统进行测试,以确保更改后的应用程序能够正常工作。这样,我们可以减少风险,即使我们给系统引入了错误,也只有一小部分用户会看到。
When we are sure that the update is successful, we can continue the rollout to all of the users. This can happen incrementally as well, meaning that the update reaches more and more users gradually. For example 25%, 50%, 75%, and then 100%. We can even re-test the system between each increment:
当我们确定更新成功后,我们可以继续向所有用户推广。这也可以逐步发生,也就是说,更新会逐渐到达越来越多的用户。例如,25%、50%、75%,然后是100%。我们甚至可以在每个增量之间重新测试系统。
During testing, we might notice that the application is not working properly. In this case, we need to roll back the changes similar to the previous examples.
在测试过程中,我们可能会注意到,应用程序不能正常工作。在这种情况下,我们需要回滚与前面例子类似的变化。
Canary deployments provide a high level of control, but this might be difficult to implement. We need to make sure only a limited amount of users receive the update. Compared to blue-green deployments where we simply needed to switch traffic from one environment to another, this is more challenging. On the other hand, it gives us more flexibility and reduces risk.
Canary 部署提供了高水平的控制,但这可能难以实施。我们需要确保只有有限的用户收到更新。与我们只需要将流量从一个环境切换到另一个环境的蓝绿色部署相比,这更具挑战性。另一方面,它为我们提供了更多的灵活性并降低了风险。
This process doesn’t need as many resources as the blue-green deployment because we are not duplicating the environments. However, we face the same problem if the new application version makes some breaking changes in the database.
这个过程不需要像蓝绿色部署那样多的资源,因为我们没有复制环境。然而,如果新的应用程序版本在数据库中做出一些破坏性的改变,我们也会面临同样的问题。
The canary deployment strategy involves another interesting aspect. We need to select the target audience who gets the updates first. Ideally, they are a targeted group who would report if they find any problems with the updates. However, we can choose users based on demographics, physical location, device type, etc.
金丝雀部署策略涉及另一个有趣的方面。我们需要选择首先获得更新的目标受众。理想情况下,他们是一个目标群体,如果他们发现更新有任何问题,就会报告。然而,我们可以根据人口统计学、物理位置、设备类型等来选择用户。
7. A/B Testing
7.A/B测试
A/B testing is very similar to canary deployment. It can be considered a variant of it. This process deploys the update to a subset of users, just like canary deployments. However, A/B testing is mainly about getting feedback from the users about our changes. One part of the users keep using “version A” of the application while another part of them uses “version B”:
A/B测试与金丝雀部署非常相似。它可以被认为是它的一个变种。这个过程将更新部署到一个子集的用户,就像金丝雀部署一样。然而,A/B测试主要是为了从用户那里获得关于我们变化的反馈。一部分用户继续使用应用程序的 “版本A”,而另一部分用户使用 “版本B”。
Our goal is to decide if we want to roll out the update to all users. This can be a complicated process. We should consider technical aspects like application performance, but we might also want to receive feedback from users if they like the changes or not. For example, with A/B testing, we can measure if the users are more likely to click on a button if we replace the text on it.
我们的目标是决定我们是否要向所有用户推出该更新。这可能是一个复杂的过程。我们应该考虑技术方面,如应用程序的性能,但我们也可能想从用户那里得到反馈,看他们是否喜欢这些变化。例如,通过A/B测试,我们可以衡量如果我们替换了一个按钮上的文字,用户是否更有可能点击它。
8. Shadow Deployment
8.影子部署
Shadow deployment is similar to blue-green deployment in the sense that it uses two identical environments. One of them is the original production environment. The other one is the shadow.
影子部署与蓝绿部署类似,它使用两个相同的环境。其中一个是原始生产环境。另一个是影子。
However, it is different from the previous deployment strategies. In all of the previous approaches, user requests were served by the updated environment. When we use shadow deployment, both environments receive the requests, but the responses come from the original application version:
然而,它与以前的部署策略不同。在之前的所有方法中,用户请求是由更新的环境提供的。当我们使用影子部署时,两个环境都会收到请求,但响应来自原始应用程序版本:
This way, we don’t have the risk of introducing bugs to the system while we can monitor and test the new version under load.
这样,我们就不会有给系统引入错误的风险,同时我们可以在负载下监测和测试新版本。
On the other hand, having two deployments can be expensive and difficult to manage. Furthermore, we need to ensure that the new version doesn’t have any side effects. For example, if we process payments, we need to mock the payment service in the new environment to avoid charging users twice.
另一方面,有两个部署可能是昂贵的,难以管理。此外,我们需要确保新版本不会产生任何副作用。例如,如果我们处理支付,我们需要在新环境中模拟支付服务,以避免向用户收费两次。
When we are sure the updated version is stable, it can replace the old one, and we can redirect user traffic to it.
当我们确定更新的版本是稳定的,它可以取代旧的版本,我们可以将用户的流量重定向到它。
9. Conclusion
9.结论
In this article, we described what are deployment strategies and compared six different strategies.
在这篇文章中,我们描述了什么是部署策略,并比较了六种不同的策略。
We should consider many aspects and compare the possible deployment strategies before choosing what to use in our system.
在选择在我们的系统中使用什么之前,我们应该考虑许多方面并比较可能的部署策略。