首页 > 解决方案 > 如何配置 capistrano 以在一台服务器上部署 puma 和 nginx 并在另一台服务器上部署 resque?

问题描述

我正在准备 capistrano 将 ruby​​ on rails 应用程序部署到 AWS。应用程序服务器将在 bastian 主机之后。

我有两台服务器 server1 和 server2。我想在 server1 上部署和运行 puma、nginx,并在 server2 上运行 resque worker 和 resque 调度程序。我知道角色,这是我到目前为止的配置:

# deploy/production.rb
web_instances = [web-instance-ip]
worker_instances = [worker-instance-ip]
role :app, web_instances
role :web, web_instances
role :worker, worker_instances

set :deploy_user, ENV['DEPLOY_USER'] || 'ubuntu'
set :branch, 'master'
set :ssh_options, {
  forward_agent: true,
  keys: ENV['SSH_KEY_PATH'],
  proxy: Net::SSH::Proxy::Command.new("ssh -i '#{ENV['SSH_KEY_PATH']}' #{fetch(:deploy_user)}@#{ENV['BASTIAN_PUBLIC_IP']} -W %h:%p"),
}

set :puma_role, :app

我不确定我应该做什么或如何编写任务,方法是确保 puma 启动、重启仅在 server1 上完成,resque、resque 调度程序启动重启等仅在 server2 上处理。虽然在每个实例上都完成了诸​​如提取最新代码、捆绑安装等常见任务?

标签: ruby-on-railsnginxpumacapistrano3

解决方案


这可以通过使用role限制为每个服务器运行的任务和一些挂钩来触发您的自定义任务来实现。您的deploy/production.rb文件将与此类似。

web_instances = [web-instance-ip]
worker_instances = [worker-instance-ip]
role :app, web_instances
role :web, web_instances
role :worker, worker_instances

set :deploy_user, ENV['DEPLOY_USER'] || 'ubuntu'
set :branch, 'master'
set :ssh_options, {
  forward_agent: true,
  keys: ENV['SSH_KEY_PATH'],
  proxy: Net::SSH::Proxy::Command.new("ssh -i '#{ENV['SSH_KEY_PATH']}' #{fetch(:deploy_user)}@#{ENV['BASTIAN_PUBLIC_IP']} -W %h:%p"),
}

# This will run on server with web role only
namespace :puma do
  task :restart do
    on roles(:web) do |host|
      with rails_env: fetch(:rails_env) do
        ** Your code to restart puma server **
      end
    end
  end
end

# This will run on server with worker role only
namespace :resque do
  task :restart do
    on roles(:worker) do |host|
      with rails_env: fetch(:rails_env) do
        ** Your code to restart resque server **
      end
    end
  end
end

after :deploy, 'puma:restart'
after :deploy, 'resque:restart'

查看文档以获取有关设置部署的命令和挂钩的更多信息。


推荐阅读