【问题标题】:How to configure capistrano to deploy puma and nginx on one server and resque on another?如何配置 capistrano 以在一台服务器上部署 puma 和 nginx 并在另一台服务器上部署 resque?
【发布时间】:2019-03-02 22:59:46
【问题描述】:

我正在准备 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-rails nginx puma capistrano3


    【解决方案1】:

    假设,您已按以下方式定义角色

    role :puma_nginx_role, 'server1.com'
    role :resque_role, 'server2.com'
    

    现在在您的 config/deploy.rb 文件中定义一个 rake 任务,例如:

    namespace :git do
      desc 'To push the code'
      task :push do
        execute "git push"
      end
    end
    

    现在假设上面的例子应该在 server1 上运行,你所要做的就是

    namespace :git do
      desc 'To push the code'
      task :push, :roles => [:puma_nginx_role] do
        execute "git push"
      end
    end
    

    因此,你告诉 capistrano 配置,git:push 应该在角色 :puma_nginx_role 上执行,而后者又会在 server1 上运行它。 com。您必须修改 tasks 以运行 puma/nginx/resque 并根据角色进行更改。

    【讨论】:

      【解决方案2】:

      这可以通过使用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'
      

      查看docs,了解有关设置部署的命令和挂钩的更多信息。

      【讨论】:

        猜你喜欢
        • 2017-12-27
        • 1970-01-01
        • 2010-11-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-05-26
        • 2011-11-21
        相关资源
        最近更新 更多