使用 Docker 部署到生产中:零停机时间 [英] Deploying with Docker into production: Zero downtime

查看:35
本文介绍了使用 Docker 部署到生产中:零停机时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我没有看到如何使用 Docker 实现零停机部署.

Im failing to see how it is possible to achieve zero-downtime deployments with Docker.

假设我有一个运行 MyWebApp 的 PHP 容器,由同一服务器上的 Nginx 容器提供服务.然后我更改了一些代码,因为 Docker 容器是不可变的,我必须再次构建/部署 MyWebApp 容器并更改代码.在执行此操作所需的时间内 MyWebApp 已关闭...

Let's say I have a PHP container running MyWebApp being served by an Nginx container on the same server. I then change some code, as Docker containers are immutable I have to build/deploy the MyWebApp container again with the code changes. During the time it takes to do this MyWebApp is down for the count...

以前我会使用 Ansible 或类似工具来部署我的代码,然后将新的发布目录符号链接到网络目录...零停机时间!

Previously I would use Ansible or similar to do deploy my code, then symlink the new release directory to the web dir... zero-downtime!

是否可以使用 Docker 和单个服务器应用程序实现零停机部署?

推荐答案

你可以做一些蓝绿部署 与您的容器,使用 nginx upstreams's:

You could do some kind of blue-green deployment with your containers, using nginx upstreams's:

upstream containers {
  server 127.0.0.1:9990;  # blue
  server 127.0.0.1:9991;  # green
}

location ~ \.php$ {
  fastcgi_pass containers;
  ...
}

然后,在部署容器时,您必须在端口映射之间交替:

Then, when deploying your containers, you'll have to alternate between port mappings:

# assuming php-fpm runs on port 9000 inside the container
# current state: green container running, need to deploy blue
# get last app version
docker pull my_app
# remove previous container (was already stopped)
docker rm blue
# start new container
docker run -p 9990:9000 --name blue my_app
# at this point both containers are running and serve traffic
docker stop green
# nginx will detect failure on green and stop trying to send traffic to it

要部署绿色,请更改颜色名称和端口映射.

To deploy green, change color name and port mapping.

您可能想要摆弄上游服务器入口参数以加快切换速度,或者在您的堆栈中使用 haproxy 并手动(或通过管理套接字自动)管理后端.

You might want to fiddle with upstream server entry parameters to make the switchover faster, or use haproxy in your stack and manually (or automatically via management socket) manage backends.

如果出现问题,只需docker start the_previous_colordocker stop the_latest_color.

If things go wrong, just docker start the_previous_color and docker stop the_latest_color.

由于您使用 Ansible,您可以使用它来编排这个过程,甚至可以将冒烟测试添加到组合中,以便在出现问题时自动触发回滚.

Since you use Ansible, you could use it to orchestrate this process, and even add smoke tests to the mix so a rollback is automatically triggered if something goes wrong.

这篇关于使用 Docker 部署到生产中:零停机时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆