Nginx

為什麼亞馬遜 ec2 在部署後會停機,而其他相同的伺服器卻沒有?

  • May 29, 2013

在過去的幾個月裡,這讓我感到困惑……我承認我是伺服器管理的新手,我嘗試在 EC2 上執行一個生產 Rails 應用程序。雖然我學到了很多東西並且使用它感覺很舒服,但我不知道為什麼在部署我的程式碼後請求需要大約 1-2 分鐘才能通過。從我通過Google收集的所有資訊來看,這是乘客開始備份的事情。該解決方案似乎正在製作一個滾動重啟腳本。

我的伺服器設置是:

  • Amazon EC2 Ubuntu 12.04 EBS 小實例
  • Nginx
  • 乘客
  • PostgreSQL 9.2
  • 紅寶石 1.9.3
  • 導軌 3.2.11
  • 使用 Capistrano 進行部署

這裡發生的所有非常基本的東西……堆棧的任何部分幾乎沒有任何自定義配置。我首先使用 Rubber 部署到 Amazon,然後從頭開始建構伺服器,因為我認為 Rubber 是部署後緩慢啟動的原因……但它仍然掛起。當然,我可以滾動重啟,添加另一個實例和負載均衡器以及第三個實例作為數據庫實例,但目前它對這個項目沒有意義。

我在其他主機(centOS 上的 rails、nginx、passenger 和 postgresql)上有一些幾乎相同的伺服器設置,它們的部署速度要快得多。它們在上限部署完成和第一個請求通過之間幾乎沒有相同的延遲。

添加我的上限部署文件

require 'rvm/capistrano'
require 'bundler/capistrano'
require 'sidekiq/capistrano'

set :stages, %w(production staging)
set :default_stage, "staging"
require 'capistrano/ext/multistage'

set :application, "bundio"
set :repository,  "git@bitbucket.org:my/project.git"
set :scm, "git"
set :shared_children, shared_children + %w{public/uploads} # include public/uploads in the shared folder to be symlinked across releases

ssh_options[:forward_agent] = true
ssh_options[:keys] = [File.join(ENV["HOME"], ".ec2", "my-keypair")]
set :deploy_to, "/var/www/apps/project"
set :rails_env, "production"
set :rvm_type, :system
set :rvm_path, "/usr/local/rvm"
set :keep_releases, 3

set :deploy_via, :remote_cache

desc "check production task"
task :check_production do

 if stage.to_s == "production"
   puts " \n Are you REALLY sure you want to deploy to production?"
   puts " \n Enter the password to continue\n "
   password = STDIN.gets[0..12] rescue nil
   if password != 'doubleCheck'
     puts "\n !!! WRONG PASSWORD !!!"
     exit
   end

 end

end



#############
### Hooks ###
#############

before "deploy", "check_production"

# if you want to clean up old releases on each deploy uncomment this:
after "deploy:restart", "deploy:cleanup"

after "deploy:update_code", "deploy:symlink_shared"
after "deploy:update_code", "deploy:migrate"
after "deploy", "deploy:restart_daemons" 


#################
### End hooks ###
#################

set :bundle_without, [:development,:test]

namespace :deploy do
 namespace :assets do
   task :precompile, :roles => :web, :except => { :no_release => true } do
     begin
       from = source.next_revision(current_revision)
     rescue
       err_no = true
     end
     if err_no || capture("cd #{latest_release} && #{source.local.log(from)} vendor/assets/ app/assets/ | wc -l").to_i > 0
       run %Q{cd #{latest_release} && #{rake} RAILS_ENV=#{rails_env} #{asset_env} assets:precompile}
     else
       logger.info "Skipping asset pre-compilation because there were no asset changes"
     end
   end
 end
 task :link_db do
   #run "ln -s #{shared_path}/config/database.yml #{latest_release}/config/database.yml"
 end

 task :start do ; end
 task :stop do ; end
 task :restart, :roles => :app, :except => { :no_release => true } do
   run "#{try_sudo} touch #{File.join(current_path,'tmp','restart.txt')}"
 end
 task :restart_daemons, :roles => :app do
   sudo "monit restart all -g daemons"
 end
 desc "Symlink shared/* files"
 task :symlink_shared, :roles => :app do
   run "ln -nfs #{shared_path}/config/database.yml #{release_path}/config/database.yml"
 end

end

我在部署和第一個請求到來之間經歷了同樣的滯後。如果您有少量 RAM,乘客可能需要幾分鐘才能起床。

乘客企業版保證您可以在不停機的情況下進行部署:http ://www.modrails.com/documentation/Users%20guide%20Nginx.html#PassengerRollingRestarts

引用自:https://serverfault.com/questions/511071