29
Deploying Rails Applications on EC2 With help from Capistrano/Rubber

Deploying Rails on EC2 using Rubber (Slides Only)

Embed Size (px)

DESCRIPTION

My presentation on deploying rails applications with rubber on ec2 - Boston Ruby Group, 7/8/08

Citation preview

Page 1: Deploying Rails on EC2 using Rubber (Slides Only)

Deploying Rails Applications on EC2

With help from Capistrano/Rubber

Page 2: Deploying Rails on EC2 using Rubber (Slides Only)

About Me

• Matthew Conway

• Director of Engineering at SnapMyLife

• email: [email protected]

• twitter: mattconway

• github: wr0ngway

Page 3: Deploying Rails on EC2 using Rubber (Slides Only)

About SnapMyLife

• Mobile Photo Sharing Site

• http://snapmylife.com

• Deploys to EC2 with rubber/capistrano

• Approximately 10m pageviews/month

Page 4: Deploying Rails on EC2 using Rubber (Slides Only)
Page 5: Deploying Rails on EC2 using Rubber (Slides Only)

Rubyists turn me on!

Page 6: Deploying Rails on EC2 using Rubber (Slides Only)

Amazon Elastic Compute Cloud (EC2)• Hardware (virtual) as a service

• Scriptable cloud computing

• Have to assume all instances are transient (this has benefits!)

• Easy ami bundling for custom servers, support for static IPs, security groups

Page 7: Deploying Rails on EC2 using Rubber (Slides Only)

EC2 Problems

• Transient

• Redundancy

• Backups!

• Scripted instance construction for short downtimes while rebuilding

Page 8: Deploying Rails on EC2 using Rubber (Slides Only)

EC2 Problems (cont.)

• DNS Issue

• Use elastic (static) ips for web/mail roles

• DNS round robin with a short TTL

• Monitor web instances from each other, remove from round robin if a failure

Page 9: Deploying Rails on EC2 using Rubber (Slides Only)

EC2 Problems (cont.)

• Not good for sending email

• SPAM central

• IPs are blacklisted

• Use something like authsmtp.com or smtp.com, or host your mail server elsewhere

Page 10: Deploying Rails on EC2 using Rubber (Slides Only)

Our Deployment Needs

• Configure multiple instances as easily as one

• Easy bootstrapping and extension - didn't want to force need for a custom image at start

Page 11: Deploying Rails on EC2 using Rubber (Slides Only)

Rubber

• Free and open sourced

• http://github.com/wr0ngway/rubber/wikis

• Makes it easy to get up and running on ec2 with a single instance

• Allows one to add in instances to scale as needed on a role by role basis

Page 12: Deploying Rails on EC2 using Rubber (Slides Only)

Start Demo

• Create a simple rails app

• Setup a single instance to host it

• Lifted from quickstart on rubber wiki: http://github.com/wr0ngway/rubber/wikis

Page 13: Deploying Rails on EC2 using Rubber (Slides Only)

Demo - create rails app

rails rubbertest; cd rubbertest./script/generate scaffold post title:string body:text

Add to app/views/posts/index.html.erb :<%= `hostname` %> <br /><%= ObjectSpace.each_object(Mongrel::HttpServer) {

|i| @port = i.port};@port %> <br />

<%= `hostname -i` %> <br /><%= require 'open-uri' open("http://169.254.169.254/latest/meta-data/public-ipv4").read() %> <br />

Page 14: Deploying Rails on EC2 using Rubber (Slides Only)

Demo - Install rubber./script/plugin install git://github.com/wr0ngway/rubber.git./script/generate vulcanize complete_mysql

edit config/rubber/rubber.yml:

aws_access_key: ****aws_secret_access_key: ****aws_account: ****ec2_key_name: ec2_key_file:staging_roles: app,memcached,mysql_master,web,web_munin

Page 15: Deploying Rails on EC2 using Rubber (Slides Only)

Demo - create instancecap rubber:create# use alias: production# use roles: app,memcached,mysql_master,web,web_munincap rubber:bootstrapcap deploy:setupcap rubber:monit:stopcap deploy:cold

Or use convenience task to do all the above:cap rubber:create_staging

Page 16: Deploying Rails on EC2 using Rubber (Slides Only)

Demo - view results

• Uses /etc/hosts for demo

• site: http://production.foo.com

• munin: http://production.foo.com:8080

Page 17: Deploying Rails on EC2 using Rubber (Slides Only)

Rubber (cont.)

• My attempt at getting people to share deployment recipes

• Uses rails generators, so you can customize your config as you grow

• ec2 conveniences such as easy ami bundling, support for static IPs, security groups

Page 18: Deploying Rails on EC2 using Rubber (Slides Only)

Rubber Features

• Capistrano rocks! rubber is a capistrano plugin

• DRY configuration

• Role and/or host based configuration of instances

Page 19: Deploying Rails on EC2 using Rubber (Slides Only)

Rubber Configuration System

• All configuration for your instances lives in your project's source tree and gets applied on deploy

• Configuration system uses ERB templates for config files

• Configuration system handles dynamically adding instances

Page 20: Deploying Rails on EC2 using Rubber (Slides Only)

Config templatemunin host config

<% rubber_instances.each do |i| %>[<%= i.full_name %>] address <%= i.internal_ip %> use_node_name yes<% end %>

Page 21: Deploying Rails on EC2 using Rubber (Slides Only)

Configuration (cont.)

• Configuration templates for sharing deployment scenarios (vulcanize generator)

• Start with a single instance and add more up as needed - no configuration edits needed

• Use whatever AMIs you want (so long as they are debian based :)

Page 22: Deploying Rails on EC2 using Rubber (Slides Only)

Rubber Features (cont.)

• Use /etc/hosts or updates dynamic dns entries automatically

• Easily/quickly create standalone staging/dev/loadtest instances that mirror production setup

Page 23: Deploying Rails on EC2 using Rubber (Slides Only)

Anti-Features

• Initial experience is fairly turnkey, but you will need some expertise as you grow

• Tied to Rails, shouldn’t be hard to divorce

• Not (yet) a gem

• Many other TODOs, feel free to contribute

• No dynamic instances (like poolparty) - you create them all statically. Works for us.

Page 24: Deploying Rails on EC2 using Rubber (Slides Only)

Demo +1 app serverALIAS=app01 ROLES=app,memcached cap rubber:createcap rubber:bootstrapcap deploy:setupcap deploy... check load balancing at http://production.foo.com/postsALIAS=app01 cap rubber:destroy... check load balancing at http://production.foo.com/posts

Page 25: Deploying Rails on EC2 using Rubber (Slides Only)

Individual Staging Servers

• Incredibly valuable, you know it works before you deploy for real

• Quick and easy

• cp config/environments/production.rb config/environments/matt.rb

• RAILS_ENV=matt cap rubber:create_staging

• Mimics production

Page 26: Deploying Rails on EC2 using Rubber (Slides Only)

Complex setup

ALIAS=web01 ROLES=web,internal_web cap rubber:createALIAS=app01 ROLES=app cap rubber:createALIAS=app02 ROLES=app cap rubber:createALIAS=db01 ROLES=mysql_master cap rubber:createALIAS=db02 ROLES=mysql_slave cap rubber:createcap rubber:bootstrapcap deploy:setupcap deploy:cold

Page 27: Deploying Rails on EC2 using Rubber (Slides Only)

Mysql cluster setup./script generate mysql_clusterALIAS=data01 ROLES=mysql_data cap rubber:createALIAS=data02 ROLES=mysql_data cap rubber:createALIAS=data03 ROLES=mysql_data cap rubber:createALIAS=sql01 ROLES=mysql_sql cap rubber:createALIAS=sql02 ROLES=mysql_sql cap rubber:createALIAS=mgm01 ROLES=mysql_mgm cap rubber:createcap rubber:bootstrapcap deploy:setupcap deploy:cold

Page 28: Deploying Rails on EC2 using Rubber (Slides Only)

Bring it all Down

• cap rubber:destroy_all

• Elapsed cost, a few cents

• No idle hardware

• No obsolete hardware

• We can do it all again without any additional effort

Page 29: Deploying Rails on EC2 using Rubber (Slides Only)

Questions?

• Alternatives listed in FAQ in rubber github wiki