SlideShare una empresa de Scribd logo
1 de 26
Descargar para leer sin conexión
Rails in da Cloud
Почему?
• Сатанисты хотят в облака. WTF?
• Это модно.
• Это удобно, выгодно, отказоустойчиво и дёшево.




                                                   2
Цель
Запуск rails в Amazon EC2




                            3
План
•   Запуск инстанса
•   Подключение к нему
•   Настройка окружения
•   Деплой
•   ??????
•   PROFIT!




                          4
5
6
7
8
9
10
11
12
13
14
15
16
17
Connect!
                      ~ $   mv Downloads/igas.pem .ssh/igas.pem
                      ~ $   cd .ssh
                 ~/.ssh $   chmod 400 igas.pem
                 ~/.ssh $   ssh -i igas.pem ubuntu@ec2-184-72-181-253.compute-1.amazonaws.com
ubuntu@ip-10-203-42-73:~$   exit
                      ~ $   cat ~/.ssh/id_rsa.pub| pbcopy
                 ~/.ssh $   ssh -i igas.pem ubuntu@ec2-184-72-181-253.compute-1.amazonaws.com
ubuntu@ip-10-203-42-73:~$   echo 'ssh-rsa AAAAB3NEww== igasgeek@me.com' >> .ssh/authorized_keys
ubuntu@ip-10-203-42-73:~$   sudo apt-get update
ubuntu@ip-10-203-42-73:~$   sudo apt-get -y upgrade
ubuntu@ip-10-203-42-73:~$   sudo apt-get -y install git-core build-essential zlib1g-dev 
                            libssl-dev libreadline-gplv2-dev libyaml-dev 
                            python-software-properties libffi-dev nginx-light 
                            postgresql-server-dev-9.1
ubuntu@ip-10-203-42-73:~$   sudo bash -c 'echo "RAILS_ENV=production" >> /etc/environment'
ubuntu@ip-10-203-42-73:~$   echo "gem: --no-ri --no-rdoc" > ~/.gemrc
ubuntu@ip-10-203-42-73:~$   curl -L https://get.rvm.io | bash -s stable --ruby
ubuntu@ip-10-203-42-73:~$   source $HOME/.rvm/scripts/rvm
ubuntu@ip-10-203-42-73:~$   sudo rm /etc/nginx/sites-enabled/default
ubuntu@ip-10-203-42-73:~$   sudo vi /etc/nginx/sites-enabled/test
ubuntu@ip-10-203-42-73:~$   sudo service nginx start




                                                                                                  18
nginx
 upstream unicorn {
   server unix:/tmp/testaws.socket fail_timeout=0;
 }

 server {
   listen 80 default;
   server_name 184.72.181.253;

     root /home/ubuntu/www/testaws/current/public;
     access_log /var/log/nginx/testaws.log;
     rewrite_log on;

     location / {
       proxy_pass http://unicorn;
       proxy_redirect    off;

       proxy_set_header     Host              $host;
       proxy_set_header     X-Real-IP         $remote_addr;
       proxy_set_header     X-Forwarded-For   $proxy_add_x_forwarded_for;
     }
     location ~ ^/assets/    {
       expires max;
       break;
     }
 }




                                                                            19
Capfile
 load 'deploy'
 load 'deploy/assets'
 load 'config/deploy' # remove this line to skip loading any of the default tasks




                                                                                    20
Gemfile
 source 'https://rubygems.org'
 gem 'rails', '3.2.9'
 gem 'pg'
 gem 'libv8', '~> 3.11.8.3'
 group :assets do
   gem 'sass-rails',   '~> 3.2.3'
   gem 'coffee-rails', '~> 3.2.1'
   gem 'uglifier', '>= 1.0.3'
   gem 'therubyracer', '~> 0.11.0beta5'
 end
 gem 'jquery-rails'
 gem 'rvm-capistrano'
 gem 'unicorn'
 gem 'capistrano'




                                          21
config/deploy.rb
 require 'bundler/capistrano'
 require "rvm/capistrano"
 set :rvm_ruby_string, '1.9.3'
 set :application,     'testaws'
 set :user,            'ubuntu'
 set :scm,             'git'
 set :repository,      'git@github.com:igas/testaws.git'
 set :branch,          'master'
 set :deploy_to,       "/home/#{user}/www/#{application}"
 set :use_sudo,        false
 set :domain,          '184.72.181.253'
 default_run_options[:pty] = true
 ssh_options[:forward_agent] = true

 server domain, :web, :app, :db, primary: true

 namespace :deploy do
   task :start, except: { no_release: true } do
     run "cd #{current_path} && bundle exec unicorn -c config/unicorn.rb -D"
   end

   task :stop, except: { no_release: true } do
     run "cd #{current_path} && kill `cat tmp/pids/unicorn.#{application}.pid`"
   end
 end




                                                                                  22
config/unicorn.rb
 env = ENV["RAILS_ENV"] || "development"
 user = "ubuntu"
 app = "testaws"
 root_path = "/home/#{user}/www/#{app}"
 current_path = "#{root_path}/current"
 shared_path = "#{root_path}/shared"
 log_path = "#{shared_path}/log"

 worker_processes 2
 preload_app true
 timeout 240
 listen "/tmp/#{app}.socket", backlog: 64
 pid "#{shared_path}/pids/unicorn.#{app}.pid"
 working_directory current_path
 stderr_path "#{shared_path}/log/unicorn.stderr.log"
 stdout_path "#{shared_path}/log/unicorn.stdout.log"

 before_fork do |server, worker|
   defined?(ActiveRecord::Base) and
     ActiveRecord::Base.connection.disconnect!
 end

 after_fork do |server, worker|
   defined?(ActiveRecord::Base) and
     ActiveRecord::Base.establish_connection
 end




                                                       23
unicorn
 #! /bin/sh

 PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
 DAEMON=/usr/local/rvm/gems/ree-1.8.7-2010.02/bin/unicorn
 DAEMON_OPTS="-c /web_apps/example_rack_app/unicorn.rb -E production -D"
 NAME=unicorn
 DESC="Unicorn app for example_rack_app"
 PID=/web_apps/example_rack_app/pids/unicorn.pid

 case "$1" in
   start)
 !   echo -n "Starting $DESC: "
 !   $DAEMON $DAEMON_OPTS
 !   echo "$NAME."
 !   ;;
   stop)
 !   echo -n "Stopping $DESC: "
          kill -QUIT `cat $PID`
 !   echo "$NAME."
 !   ;;
   restart)
 !   echo -n "Restarting $DESC: "
          kill -QUIT `cat $PID`
 !   sleep 1
 !   $DAEMON $DAEMON_OPTS
 !   echo "$NAME."




                                                                           24
unicorn
 sudo chmod +x /etc/init.d/unicorn
 sudo /usr/sbin/update-rc.d -f unicorn defaults
 /etc/init.d/unicorn start




                                                  25
Вопросы?




           Igas, Ruby Ninja

Más contenido relacionado

La actualidad más candente

Operation Oriented Web Applications / Yokohama pm7
Operation Oriented Web Applications / Yokohama pm7Operation Oriented Web Applications / Yokohama pm7
Operation Oriented Web Applications / Yokohama pm7
Masahiro Nagano
 
"Ops Tools with Perl" 2012/05/12 Hokkaido.pm
"Ops Tools with Perl" 2012/05/12 Hokkaido.pm"Ops Tools with Perl" 2012/05/12 Hokkaido.pm
"Ops Tools with Perl" 2012/05/12 Hokkaido.pm
Ryosuke IWANAGA
 

La actualidad más candente (20)

Building and Testing Puppet with Docker
Building and Testing Puppet with DockerBuilding and Testing Puppet with Docker
Building and Testing Puppet with Docker
 
Ansible loves Python, Python Philadelphia meetup
Ansible loves Python, Python Philadelphia meetupAnsible loves Python, Python Philadelphia meetup
Ansible loves Python, Python Philadelphia meetup
 
dotCloud and go
dotCloud and godotCloud and go
dotCloud and go
 
How to Develop Puppet Modules: From Source to the Forge With Zero Clicks
How to Develop Puppet Modules: From Source to the Forge With Zero ClicksHow to Develop Puppet Modules: From Source to the Forge With Zero Clicks
How to Develop Puppet Modules: From Source to the Forge With Zero Clicks
 
Ansible tips & tricks
Ansible tips & tricksAnsible tips & tricks
Ansible tips & tricks
 
Dtalk shell
Dtalk shellDtalk shell
Dtalk shell
 
Ansible fest Presentation slides
Ansible fest Presentation slidesAnsible fest Presentation slides
Ansible fest Presentation slides
 
Using Ansible Dynamic Inventory with Amazon EC2
Using Ansible Dynamic Inventory with Amazon EC2Using Ansible Dynamic Inventory with Amazon EC2
Using Ansible Dynamic Inventory with Amazon EC2
 
Perl dancer
Perl dancerPerl dancer
Perl dancer
 
KubeCon EU 2016: Custom Volume Plugins
KubeCon EU 2016: Custom Volume PluginsKubeCon EU 2016: Custom Volume Plugins
KubeCon EU 2016: Custom Volume Plugins
 
Nginx Workshop Aftermath
Nginx Workshop AftermathNginx Workshop Aftermath
Nginx Workshop Aftermath
 
Integrating icinga2 and the HashiCorp suite
Integrating icinga2 and the HashiCorp suiteIntegrating icinga2 and the HashiCorp suite
Integrating icinga2 and the HashiCorp suite
 
Operation Oriented Web Applications / Yokohama pm7
Operation Oriented Web Applications / Yokohama pm7Operation Oriented Web Applications / Yokohama pm7
Operation Oriented Web Applications / Yokohama pm7
 
"Ops Tools with Perl" 2012/05/12 Hokkaido.pm
"Ops Tools with Perl" 2012/05/12 Hokkaido.pm"Ops Tools with Perl" 2012/05/12 Hokkaido.pm
"Ops Tools with Perl" 2012/05/12 Hokkaido.pm
 
DevOps with Fabric
DevOps with FabricDevOps with Fabric
DevOps with Fabric
 
Testing your infrastructure with litmus
Testing your infrastructure with litmusTesting your infrastructure with litmus
Testing your infrastructure with litmus
 
Asynchronous PHP and Real-time Messaging
Asynchronous PHP and Real-time MessagingAsynchronous PHP and Real-time Messaging
Asynchronous PHP and Real-time Messaging
 
DevOps(2) : Vagrant - (MOSG)
DevOps(2) : Vagrant  -  (MOSG)DevOps(2) : Vagrant  -  (MOSG)
DevOps(2) : Vagrant - (MOSG)
 
Vagrant file samples for various Hadoop distributions
Vagrant file samples for various Hadoop distributionsVagrant file samples for various Hadoop distributions
Vagrant file samples for various Hadoop distributions
 
Go初心者がGoでコマンドラインツールの作成に挑戦した話
Go初心者がGoでコマンドラインツールの作成に挑戦した話Go初心者がGoでコマンドラインツールの作成に挑戦した話
Go初心者がGoでコマンドラインツールの作成に挑戦した話
 

Similar a EC2

Railsconf2011 deployment tips_for_slideshare
Railsconf2011 deployment tips_for_slideshareRailsconf2011 deployment tips_for_slideshare
Railsconf2011 deployment tips_for_slideshare
tomcopeland
 
Deploying Rails Applications with Capistrano
Deploying Rails Applications with CapistranoDeploying Rails Applications with Capistrano
Deploying Rails Applications with Capistrano
Almir Mendes
 
X64服务器 lnmp服务器部署标准 new
X64服务器 lnmp服务器部署标准 newX64服务器 lnmp服务器部署标准 new
X64服务器 lnmp服务器部署标准 new
Yiwei Ma
 
4069180 Caching Performance Lessons From Facebook
4069180 Caching Performance Lessons From Facebook4069180 Caching Performance Lessons From Facebook
4069180 Caching Performance Lessons From Facebook
guoqing75
 
Facebook的缓存系统
Facebook的缓存系统Facebook的缓存系统
Facebook的缓存系统
yiditushe
 

Similar a EC2 (20)

Railsconf2011 deployment tips_for_slideshare
Railsconf2011 deployment tips_for_slideshareRailsconf2011 deployment tips_for_slideshare
Railsconf2011 deployment tips_for_slideshare
 
Continuous Delivery: The Next Frontier
Continuous Delivery: The Next FrontierContinuous Delivery: The Next Frontier
Continuous Delivery: The Next Frontier
 
Ubic-public
Ubic-publicUbic-public
Ubic-public
 
Vagrant for real
Vagrant for realVagrant for real
Vagrant for real
 
Deploying Rails Applications with Capistrano
Deploying Rails Applications with CapistranoDeploying Rails Applications with Capistrano
Deploying Rails Applications with Capistrano
 
Puppet @ Seat
Puppet @ SeatPuppet @ Seat
Puppet @ Seat
 
X64服务器 lnmp服务器部署标准 new
X64服务器 lnmp服务器部署标准 newX64服务器 lnmp服务器部署标准 new
X64服务器 lnmp服务器部署标准 new
 
Vagrant for real
Vagrant for realVagrant for real
Vagrant for real
 
Vagrant for real (codemotion rome 2016)
Vagrant for real (codemotion rome 2016)Vagrant for real (codemotion rome 2016)
Vagrant for real (codemotion rome 2016)
 
Practical Chef and Capistrano for Your Rails App
Practical Chef and Capistrano for Your Rails AppPractical Chef and Capistrano for Your Rails App
Practical Chef and Capistrano for Your Rails App
 
PuppetConf 2016: The Challenges with Container Configuration – David Lutterko...
PuppetConf 2016: The Challenges with Container Configuration – David Lutterko...PuppetConf 2016: The Challenges with Container Configuration – David Lutterko...
PuppetConf 2016: The Challenges with Container Configuration – David Lutterko...
 
Challenges of container configuration
Challenges of container configurationChallenges of container configuration
Challenges of container configuration
 
4069180 Caching Performance Lessons From Facebook
4069180 Caching Performance Lessons From Facebook4069180 Caching Performance Lessons From Facebook
4069180 Caching Performance Lessons From Facebook
 
Docker, the Future of DevOps
Docker, the Future of DevOpsDocker, the Future of DevOps
Docker, the Future of DevOps
 
Toolbox of a Ruby Team
Toolbox of a Ruby TeamToolbox of a Ruby Team
Toolbox of a Ruby Team
 
Facebook的缓存系统
Facebook的缓存系统Facebook的缓存系统
Facebook的缓存系统
 
Burn down the silos! Helping dev and ops gel on high availability websites
Burn down the silos! Helping dev and ops gel on high availability websitesBurn down the silos! Helping dev and ops gel on high availability websites
Burn down the silos! Helping dev and ops gel on high availability websites
 
Docker deploy
Docker deployDocker deploy
Docker deploy
 
infra-as-code
infra-as-codeinfra-as-code
infra-as-code
 
Vagrant for real codemotion (moar tips! ;-))
Vagrant for real codemotion (moar tips! ;-))Vagrant for real codemotion (moar tips! ;-))
Vagrant for real codemotion (moar tips! ;-))
 

EC2

  • 1. Rails in da Cloud
  • 2. Почему? • Сатанисты хотят в облака. WTF? • Это модно. • Это удобно, выгодно, отказоустойчиво и дёшево. 2
  • 4. План • Запуск инстанса • Подключение к нему • Настройка окружения • Деплой • ?????? • PROFIT! 4
  • 5. 5
  • 6. 6
  • 7. 7
  • 8. 8
  • 9. 9
  • 10. 10
  • 11. 11
  • 12. 12
  • 13. 13
  • 14. 14
  • 15. 15
  • 16. 16
  • 17. 17
  • 18. Connect! ~ $ mv Downloads/igas.pem .ssh/igas.pem ~ $ cd .ssh ~/.ssh $ chmod 400 igas.pem ~/.ssh $ ssh -i igas.pem ubuntu@ec2-184-72-181-253.compute-1.amazonaws.com ubuntu@ip-10-203-42-73:~$ exit ~ $ cat ~/.ssh/id_rsa.pub| pbcopy ~/.ssh $ ssh -i igas.pem ubuntu@ec2-184-72-181-253.compute-1.amazonaws.com ubuntu@ip-10-203-42-73:~$ echo 'ssh-rsa AAAAB3NEww== igasgeek@me.com' >> .ssh/authorized_keys ubuntu@ip-10-203-42-73:~$ sudo apt-get update ubuntu@ip-10-203-42-73:~$ sudo apt-get -y upgrade ubuntu@ip-10-203-42-73:~$ sudo apt-get -y install git-core build-essential zlib1g-dev libssl-dev libreadline-gplv2-dev libyaml-dev python-software-properties libffi-dev nginx-light postgresql-server-dev-9.1 ubuntu@ip-10-203-42-73:~$ sudo bash -c 'echo "RAILS_ENV=production" >> /etc/environment' ubuntu@ip-10-203-42-73:~$ echo "gem: --no-ri --no-rdoc" > ~/.gemrc ubuntu@ip-10-203-42-73:~$ curl -L https://get.rvm.io | bash -s stable --ruby ubuntu@ip-10-203-42-73:~$ source $HOME/.rvm/scripts/rvm ubuntu@ip-10-203-42-73:~$ sudo rm /etc/nginx/sites-enabled/default ubuntu@ip-10-203-42-73:~$ sudo vi /etc/nginx/sites-enabled/test ubuntu@ip-10-203-42-73:~$ sudo service nginx start 18
  • 19. nginx upstream unicorn { server unix:/tmp/testaws.socket fail_timeout=0; } server { listen 80 default; server_name 184.72.181.253; root /home/ubuntu/www/testaws/current/public; access_log /var/log/nginx/testaws.log; rewrite_log on; location / { proxy_pass http://unicorn; proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } location ~ ^/assets/ { expires max; break; } } 19
  • 20. Capfile load 'deploy' load 'deploy/assets' load 'config/deploy' # remove this line to skip loading any of the default tasks 20
  • 21. Gemfile source 'https://rubygems.org' gem 'rails', '3.2.9' gem 'pg' gem 'libv8', '~> 3.11.8.3' group :assets do gem 'sass-rails', '~> 3.2.3' gem 'coffee-rails', '~> 3.2.1' gem 'uglifier', '>= 1.0.3' gem 'therubyracer', '~> 0.11.0beta5' end gem 'jquery-rails' gem 'rvm-capistrano' gem 'unicorn' gem 'capistrano' 21
  • 22. config/deploy.rb require 'bundler/capistrano' require "rvm/capistrano" set :rvm_ruby_string, '1.9.3' set :application, 'testaws' set :user, 'ubuntu' set :scm, 'git' set :repository, 'git@github.com:igas/testaws.git' set :branch, 'master' set :deploy_to, "/home/#{user}/www/#{application}" set :use_sudo, false set :domain, '184.72.181.253' default_run_options[:pty] = true ssh_options[:forward_agent] = true server domain, :web, :app, :db, primary: true namespace :deploy do task :start, except: { no_release: true } do run "cd #{current_path} && bundle exec unicorn -c config/unicorn.rb -D" end task :stop, except: { no_release: true } do run "cd #{current_path} && kill `cat tmp/pids/unicorn.#{application}.pid`" end end 22
  • 23. config/unicorn.rb env = ENV["RAILS_ENV"] || "development" user = "ubuntu" app = "testaws" root_path = "/home/#{user}/www/#{app}" current_path = "#{root_path}/current" shared_path = "#{root_path}/shared" log_path = "#{shared_path}/log" worker_processes 2 preload_app true timeout 240 listen "/tmp/#{app}.socket", backlog: 64 pid "#{shared_path}/pids/unicorn.#{app}.pid" working_directory current_path stderr_path "#{shared_path}/log/unicorn.stderr.log" stdout_path "#{shared_path}/log/unicorn.stdout.log" before_fork do |server, worker| defined?(ActiveRecord::Base) and ActiveRecord::Base.connection.disconnect! end after_fork do |server, worker| defined?(ActiveRecord::Base) and ActiveRecord::Base.establish_connection end 23
  • 24. unicorn #! /bin/sh PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin DAEMON=/usr/local/rvm/gems/ree-1.8.7-2010.02/bin/unicorn DAEMON_OPTS="-c /web_apps/example_rack_app/unicorn.rb -E production -D" NAME=unicorn DESC="Unicorn app for example_rack_app" PID=/web_apps/example_rack_app/pids/unicorn.pid case "$1" in start) ! echo -n "Starting $DESC: " ! $DAEMON $DAEMON_OPTS ! echo "$NAME." ! ;; stop) ! echo -n "Stopping $DESC: " kill -QUIT `cat $PID` ! echo "$NAME." ! ;; restart) ! echo -n "Restarting $DESC: " kill -QUIT `cat $PID` ! sleep 1 ! $DAEMON $DAEMON_OPTS ! echo "$NAME." 24
  • 25. unicorn sudo chmod +x /etc/init.d/unicorn sudo /usr/sbin/update-rc.d -f unicorn defaults /etc/init.d/unicorn start 25
  • 26. Вопросы? Igas, Ruby Ninja