如何用capistrano进行滚动部署? [英] How do you do a rolling deploy with capistrano?

查看:178
本文介绍了如何用capistrano进行滚动部署?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们在负载平衡器后面有2个实例,它们与乘客运行相同的rails应用程序。部署时,服务器启动时间会导致请求超时。因此,我们有一个脚本,通过从LB中单独更新每个Web服务器,使用帽子部署,测试动态页面加载,将其放回到LB。

We have 2 instances behind a load balancer running the same rails app with passenger. When we deploy, the server startup time causes requests to timeout. As a result we have a script that updates each webserver individually by taking one off the LB, deploying with cap, testing a dynamic page load, putting it back on the LB.

我们如何用一个命令让我们的capistrano为我们做这个?我已经能够将其设置为同时部署到所有实例,但是它们都同时重新启动,并导致该站点不可用20秒。

How can we get capistrano to do this for us with one command? I have been able to set it up to deploy to all instances simultaneously but they all restart at the same time and cause the site to be unavailable for 20 seconds.

我在这里缺少什么?似乎这样应该是一个常见的模式。

What am I missing here? Seems like this should be a common pattern.

推荐答案

在capistrano中序列化部署并不直接,的服务器之间的运作。为了重新解决这个问题,看起来你有一小部分服务器,并希望每个服务器依次离线以更新部署。

It's not actually that straightforward to serialize deployment in capistrano, which likes to parallelize all of its operations between servers. To restate the issue, it seems like you have a handful of servers and want to take each one offline in sequence to update the deployment.

诀窍是覆盖 deploy:create_symlink 任务在您的部署配置中:

The trick is to override the deploy:create_symlink task in your deployment configuration:

def perform_task_offline options
  sudo "take_this_server_offline", options
  yield
  sudo "put_this_server_online", options
end

def create_symlink_task options
  # does what your existing deploy:create_symlink did, something like:
  run "rm -f /web/current && ln -s #{release_path} /web/current", options
end

namespace :deploy do

  task :create_symlink, {once: true, except: {no_release: true}} do
    deployed_servers = Array.new

    roles[:app].servers.each do |current_server|
      options = {hosts: current_server}
      deployed_servers.push current_server
      perform_task_offline(options) { create_symlink_task options }
    end
  end
end

在这种情况下 perform_task_offline 包括在指定的服务器上执行的命令在选项中,将它从负载平衡器中删除,而 yield 包含 create_symlink_task ,它创建了部署符号链接。

In this case perform_task_offline includes commands that execute on the server specified in options that remove it from the load balancer while it yields the block including create_symlink_task, which creates the deployment symlink.

然后,您应该可以运行标准的 cap 命令部署,您将看到服务器顺序脱机,创建当前符号链接,然后重新启动。

You should then be able to run the standard cap command to deploy, and you'll see the servers sequentially go offline, create the "current" symlink, then come back up.

请注意,上述代码跟踪已成功部署到 deployment_servers 的服务器。如果您希望能够回滚主动失败的部署(也就是说,在部署期间,该故障在部署本身发生)仅在以前部署到的服务器上,则需要在 on_rollback do 阻止,但只能在 deploy_servers 中。

Note that the above code tracks the servers that have successfully been deployed to with deployed_servers. If you want to be able to rollback an active failed deployment (that is, the failure happens during deployment itself) on only the servers that had previously been deployed to, you'll need a similar loop inside of an on_rollback do block, but over only the deployed_servers.

这篇关于如何用capistrano进行滚动部署?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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