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

查看:253
本文介绍了使用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或类似的代码来部署我的代码,然后将新的发行目录符号链接到Web目录...零停机时间!

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:

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天全站免登陆