Deploying and Monitoring
Ruby on Rails
A practical guide
Mathias Meyer and Jonathan Weiss, 25.05.2009 Peritor GmbH
Mit ho
tfixes
von Ca
rsten
Borma
nn
2011-
03-05
+ 2013-
02-28
Danke!
Deployment/Scaling/
Caching/Debugging
jan krutisch
mindmatters gmbh&co. kg
jan.krutisch@mindmatters.de
http://mindmatters.de
Mashed
up w
ith...
2011-
03-05
database
client
(Browser)mongrel
database
client
(Browser)rails
Heute:
➔ thi
n
(
∨
unico
rn
)
database
client
rails
webserver
Dateisystem
public/
database
client
rails
webserver
Dateisystem
public/
rails
rails
oldschool:
mod_proxy_balancer
oder ein richtiger
balancer
HAProxy, Squid, etc.
mod_passenger
database
client
rails
webserver
Dateisystem
public/
rails
rails
automagisch.
it just works.
5
Infrastructure
Infrastructure
6
Simple Rails Setup
One Rails instance handles all requests
7
8
9
Typical Rails Setup
•! A load-balancer distributes the incoming requests
•! Some load-balancers will deliver static requests themselves •! Several Rails instances handle all requests
10
11
Deployment Questions
Mongrel? Apache? Ebb? FastCGI? mod_rails? Thin? Nginx? Lighttpd? Pound? mod_proxy_balancer? HA-Proxy? Phusion Passenger? Load-balancer? Proxy? Rails Application Server? Swiftiply? Reverse Proxy? Pen?12
What we are going to cover today
Rails Application Server
•! FastCGI •! Mongrel
•! mod_rails / Phussion Passenger •! JRuby + Glassfish & Co.
Proxy/Web Server
•! Apache2 •! Nginx •! Lighttpd •! HA-Proxy
13
14
FastCGI
•! Protocol to communicate with long-running CGI applications
•! Usage of either mod_fcgi with Apache 1.3 or mod_fcgi with Lighttpd •! Proxy local and remote FastCGI instances
•! Oldest way of deploying Rails •! Deprecated and unstable
15
16
18
19
Mongrel
•! Developed by Zed Shaw as an alternative to FastCGI
•! Complete HTTP-Server that can load arbitrary Ruby-servlets •! Built-in Rails support
20
21
Mongrel
Apache 2.2 – mod_proxy_balancer Lighttpd Nginx HA-Proxy Pound22
Mongrel Cluster
23
Mongrel Cluster
24
Mongrel and Apache 2.2
Define Mongrel Cluster in Apache
25
Simple Mongrel and Apache 2.2
26
A more complex example
•! Redirect dynamic requests •! Serve static content
•! Support cached pages
•! Support maintenance page •! Enable client-side caching of
images, stylesheets, and JavaScript
27
Mongrel
•! Very robust
•! Strict HTTP parser
•! Easy to debug (HTTP!)
•! Used to be defacto deployment setup with Apache 2.2 and mod_proxy_balancer •! Can be a bit difficult to setup (mongrel_cluster, ports, Apache)
28
29
mod_rails a.k.a Phusion Passenger
•! Module for Apache 2.2 (and Nginx)
•! Allows Apache to control Rails instances
•! Apache starts and stops application instances depending on the application load •! Able to run any Rack-compatible Ruby application (Merb, Sinatra & Co.)
•! Only manages Rails on one host - no remote instances •! Combine with HTTP-Proxy / balancing solution
30
Install Phusion Passenger
Install Apache module31
Install Phusion Passenger
Install Passenger with nginx (installs custom nginx)
32
Customized Phusion Passenger
Control Rails instance number33
Control Phusion Passenger
34
Phusion Passenger
35
Phusion Passenger
36
Phusion Passenger
•! Ready for production
•! Makes setup easier – on the single machine level •! Multiple servers still require load balancer
•! Suitable for mass-hosting
37
38
•! Ruby Runtime on the Java Virtual Machine •! Implemented in Java and Ruby
•! Compiles Ruby into Java-bytecode
•! Integrates with Java code and libraries •! Java’s promises of native threads and JIT
•! Allows for Ruby/Rails applications to be packaged as WAR files •! WAR files deployable on any J2EE-container:
39
40
JRuby on Glassfish
41
JRuby on Glassfish
42
Setup JRuby on Glassfish
1.! Download JRuby and Glassfish
43
Warble Configuration
Define min/max Rails runtimes
44
Glassfish for development
Shrunk-down version of Glassfish v3 - packaged as a gem Can run any Rack-based application
Installation:
•! $ jruby -S gem install glassfish •! $ glassfish
45
46
47
Proxy Requirements
•! Hide cluster backend from the user •! Load-balancer backend instances •! Recognize down hosts
•! Fair scheduler
48
Apache 2.2
•! Apache 2.2 introduced mod_proxy_balancer
•! mod_proxy_balancer can speak to multiple backends and balance requests •! Apache can acts as a pure proxy or can also serve static files
49
Apache 2.2
•! Apache 2.2 introduced mod_proxy_balancer
•! mod_proxy_balancer can speak to multiple backends and balance requests •! Apache can acts as a pure proxy or can also serve static files
50
Apache 2.2
•! Apache 2.2 introduced mod_proxy_balancer
•! mod_proxy_balancer can speak to multiple backends and balance requests •! Apache can acts as a pure proxy or can also serve static files
51
Apache 2.2
Pro
•! Stable, robust, and mature
•! Many people know how to work with Apache
•! Integrates well with other modules (SVN, DAV, Auth, …)
Con
•! Apache can be complicated to configure
52
Nginx – From Russia with love
•! Nginx - popular Russian webserver with good proxy support •! Can load-balance multiple backends and deliver static content •! Quite popular with Mongrel as the Rails backend
53
Nginx Configuration
Simple proxy exampleGet complete version here:
54
Nginx
Pro
•! Stable, robust, and fast
•! Uses fewer resources (CPU and RAM) than Apache for proxy-mode and static files •! Simpler configuration file
•! Can directly talk to memcached - SSI
Con
•! More documentation would be nice
55
Lighttpd
•! Lightweight and fast webserver •! Balancing proxy support
•! Good FastCGI support
56
Lighttpd Configuration
Simple proxy example57
Lighttpd
Pro
•! Fast and lightweight
•! Uses fewer resources (CPU and RAM) than Apache for proxy-mode and static files •! Simpler configuration file
Con
•! Unstable for some people •! Slow development cycle
•! More documentation would be nice
•! Configuration file can be too simple (virtual host aliasing) •! No equivalent for many Apache modules
58
HA-Proxy
•! HAProxy – reliable, high performance TCP/HTTP load balancer •! Proxying and content inspection
•! No content serving, just a proxy
•! Mature proxy module (fair scheduler) •! ACL support
59
HAProxy
60
HAProxy
Pro
•! Mature, stable, robust, and fast •! TCP and HTTP balancing
Con
•! Few Rails examples
61
62
Small Site
Recommendation
Apache 2.2 with mod_rails / Phusion Passenger
63
Medium Site
Recommendation
•! Apache/Nginx as the frontend proxy •! Passenger/mod_rails as the backend •! Deliver static files with Apache
64
Large Rails Setup
Recommendation
•! Redundant load-balancer •! Redundant proxy / web •! Passenger/mod_rails
65
Heavy Static Files
Recommendation
•! Deliver static assets
through separate web server farm
66
Java Shop
Recommendation
•! Deliver a Rails-WAR file and you are
done
•! Integrate with existing Java
68
69
70
71
72
73
74
75
Rack Middleware
•! Authentication (HTTP, OpenID) •! Sessions •! Routing •! Caching •! Logging •! Debugging76
Support
Frameworks
•! Rails •! Merb •! Camping •! Sinatra •! Mack •! Maveric •! Halcyon •! Ramaze •! Rum •! Vintage •! WavesWeb Server
•! CGI •! FastCGI •! Mongrel •! Phusion Passenger •! JRuby Servlets •! WEBrick •! Ebb •! Thin •! LiteSpeed •! Swiftcore •! Unicorn77
78
Ruby Enterprise Edition
•! Copy-On-Write patches to Ruby 1.8
•! Saves memory when spawning several Rails instances •! Used by Phusion Passenger if available
Dea
d:
➔ 1.
9.3
79
Thin, Ebb, Evented Mongrel & Co.
•! Alternatives to Mongrel
•! Claim to be faster, lighter, and what have you
•! Rendering “Hello World” is usually not your bottleneck
Stick with stable and robust Mongrel, or better yet, with Passenger
Might
be so
mewha
t dated
prozesse (mongrels)
müssen neugestartet
werden.
Bei Passenger:
konfiguration in
ruby-dateien. Yay!
set
:application
,
"name"
set
:repository
,
"http://kenn.i.net/svn/trunk/name"
role
:app
,
"mog.i.net"
role
:web
,
"mog.i.net"
role
:db
,
"mog.i.net"
,
:primary
=>
true
require
'lib/cap/backgroundrb_recipes'
after
'deploy:update_code'
,
:set_symlinks
task
:set_symlinks
do
run
"ln -s #{shared_path}/config/database.yml #{release_path}/config/
database.yml"
run
"ln -s #{shared_path}/data #{release_path}/public/data"
AWE13
Gruppe/
04
danke!
AWE13
Gruppe/
04
danke!
Erweitern um eigene
"Recipes"
Kommandos werden
parallel auf allen Servern
ausgeführt
Unterstützung diverser
SCM-Systeme
Erlaubt (relativ) einfache
rollbacks, jeder
Nicht auf Rails
beschränkt!
80
Infrastructure
Deployment
82
83
84
85
86
87
88
89
What doesn’t Capistrano do?
•! Plan your initial server setup •! Configure basic services
90
Basic Ingredients
•! The cap command •! Variables
•! Roles •! Tasks
91
Basic Ingredients - cap
Your one-stop deployment shop92
Basic Ingredients - Variables
•! Configure basic project information
•! Override Capistrano’s default assumptions •! Once set, variables are available globally •! Defined using the set method
93
Basic Ingredients - Roles
•! Define types of servers •! Default roles
•! :www •! :app •! :db
•! All can point to the same server •! But all three must be defined
94
Basic Ingredients - Roles
Define custom roles as you please95
Basic Ingredients - Tasks
•! Define an atomic set of actions for Capistrano •! Can be called from the command line
96
Basic Ingredients - Tasks
97
Basic Ingredients - Namespaces
Group tasks together logically
98
99
Get Your Capistrano On
100
Get Your Capistrano On
101
Capistrano’s Defaults
•! Your SCM is Subversion
•! Deployment directory is /u/apps/#{application_name} •! User for SCM and SSH is the currently logged-in user •! Commands are run with sudo
102
103
Get Your Capistrano On
•! Capistrano expects a directory structure •! Can be created with cap deploy:setup
104
105
The Deployment Lifecycle
Check the prerequisites:106
The Deployment Lifecycle
Set up your application for the first time107
The Deployment Lifecycle
The initial deployment1.! Checks the revision from the local machine
2.! Checks out the code on the remote machines
3.! Sets a link called current pointing to the lates release
4.! Runs the migrations
108
109
The Deployment Lifecycle
Subsequent deployments1.! Checks the revision from the local machine
2.! Checks out the code on the remote machines
3.! Updates current link
110
111
Common Capistrano Tasks
Deploy and run migrationsRun only the migrations
Restart application servers
112
113
114
115
Deployment Strategies
116
Deployment Strategies
117
Deployment Strategies
Check out locally and transfer119
120
121
122
123
Callbacks
Execute a task before another runs
Execute a task after another has finished
124
125
126
127
128
129
The Rest
•! Gem dependencies
•! Support for deploying through gateway servers •! Parallel execution on multiple servers
131
Vlad the Deployer
•! Simple, Rake-based deployment solution
132
Vlad the Deployer
Load133
Vlad the Deployer
134
Vlad the Deployer
•! Simple, sometimes too simple
•! No callback mechanism, instead the Rake syntax must be used
135
Chef
•! System Integration Framework
•! Written in Ruby – inspired by Puppet •! Manage multiple nodes
•! Configuration •! Packages
136
137
Recipes
138
Recipes
139
Recipes
140
chef-deploy
Enable141
Chef and chef-deploy
•! In heavy development: sometimes unstable and hard to debug •! Be prepared to get your hands dirty
•! Better suited for configuration than deployment •! Community recipes at
154
Infrastructure
Monitoring
155
156
157
158
Monit
•! Process-level monitoring
•! Checks PID-files, ports, and permissions
•! Reacts by executing a script and/or alerting •! Has a web GUI for administering nodes
159
Monit
MySQL160
Monit
Apache161
Monit
Mongrel162
God
•! Ruby-based configuration •! Easily extendible
•! Can work with event- and poll-based conditions •! Comes with less recipes than Monit
163
God
Mongrel164
Munin
•! Host-level monitoring
•! Master periodically asks nodes for local data
•! Checks system resources and records historical data •! Allows to recognize trends and make predictions •! Alerting support
165
166
167
168
Exception Monitoring
•! Monitor errors raised by your application •! Usually notifying you by email
•! Good old exception_notification Plugin •! Exceptions as a Service
169
170
171
New Relic
•! Rails Performance monitoring •! Live view into your application
172
Other Tools
•! Nagios •! Big Brother •! New Relic RPM •! FiveRuns •! JMX173
Infrastructure
174
175
Search
Full text search
176
Full Text Search Engine
Separate Service
•! Creates full text index
•! Application queries search daemon •! Index update through application or
database