Deploying and Monitoring Ruby on Rails a Practical Guide
Total Page:16
File Type:pdf, Size:1020Kb
Mit hotfixes von Carsten Bormann 2011-03-05 + 2013-02-28 Deploying and Monitoring Ruby on Rails A practical guide Mathias Meyer and Jonathan Weiss, 25.05.2009 Danke! Peritor GmbH Mashed up with... 2011-03-05 Deployment/Scaling/ Caching/Debugging jan krutisch mindmatters gmbh&co. kg [email protected] http://mindmatters.de Deployment Real artists ship! Komponenten client (Browser) rails database client Heute: (Browser) ➔ railsthin mongrel (∨database unicorn) mongrel ist schnell apache ist schneller client webserver rails Dateisystem public/ database rails ist nicht threadsafe client rails webserver rails database Dateisystem rails public/ webserver rails webserver rails oldschool: CGI / SCGI / FastCGI newschool: http mongrel versus... mod_proxy_balancer (in Apache/nginx/lighttpd) oder ein richtiger proxy oder ein richtiger balancer Varnish HAProxy, Squid, etc. true school: passenger Apache-Modul client rails webserver rails database rails Dateisystem public/ mod_passenger automagisch. it just works. http://modrails.com Infrastructure Infrastructure 5 Simple Rails Setup One Rails instance handles all requests Rails is single-threaded: There is only one concurrent request 6 Rails Setup 7 Rails Setup 8 Typical Rails Setup •! A load-balancer distributes the incoming requests •! Some load-balancers will deliver static requests themselves •! Several Rails instances handle all requests •! Number of concurrent requests equals number of Rails instances 9 Rails Setup Options 10 Deployment Questions Apache? Pound? mod_proxy_balancer? FastCGI? Mongrel? Proxy? Load-balancer? Nginx? mod_rails? Ebb? HA-Proxy? Reverse Proxy? Swiftiply? Phusion Passenger? Thin? Pen? Rails Application Server? Lighttpd? 11 What we are going to cover today Rails Application Server Proxy/Web Server •! FastCGI •! Apache2 •! Mongrel •! Nginx •! mod_rails / Phussion Passenger •! Lighttpd •! JRuby + Glassfish & Co. •! HA-Proxy 12 FastCGI 13 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 •! Hard to debug (FastCGI protocol) 14 FastCGI 15 FastCGI 16 17 Mongrel 18 Mongrel •! Developed by Zed Shaw as an alternative to FastCGI •! Complete HTTP-Server that can load arbitrary Ruby-servlets •! Built-in Rails support 19 Mongrel 20 Mongrel Apache 2.2 – mod_proxy_balancer Lighttpd Nginx HA-Proxy Pound 21 Mongrel Cluster Utility to manage several Mongrel instances 22 Mongrel Cluster Control Mongrels 23 Mongrel and Apache 2.2 Define Mongrel Cluster in Apache 24 Simple Mongrel and Apache 2.2 Redirect all traffic to the Mongel cluster 25 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 •! Compress output if supported 26 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) •! Not so easy on mass/virtual hosting 27 mod_rails 28 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 29 Install Phusion Passenger Install Apache module Load and activate in Apache 30 Install Phusion Passenger Install Passenger with nginx (installs custom nginx) Configure nginx 31 Customized Phusion Passenger Control Rails instance number 32 Control Phusion Passenger Restart after deployment: 33 Phusion Passenger One machine 34 Phusion Passenger One machine Multiple machines 35 Phusion Passenger •! Ready for production •! Makes setup easier – on the single machine level •! Multiple servers still require load balancer •! Suitable for mass-hosting •! upcoming standard way of deploying Rails 36 JRuby 37 •! 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: Glassfish, JBoss, Tomcat, Jetty, … 38 JRuby on Rails 39 JRuby on Glassfish One machine 40 JRuby on Glassfish One machine Multiple machines 41 Setup JRuby on Glassfish 1.! Download JRuby and Glassfish 2.! From http://blog.headius.com/2008/08/zero-to-production-in-15-minutes.html 42 Warble Configuration Define min/max Rails runtimes 43 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 •! There is no Step 3 44 Rails Setup 45 Proxy Options 46 Proxy Requirements •! Hide cluster backend from the user •! Load-balancer backend instances •! Recognize down hosts •! Fair scheduler •! (Deliver static content) 47 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 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 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 •! The stock Apache is quite resource-hungry compared to pure proxy solutions 51 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 •! Recent Passenger support 52 Nginx Configuration Simple proxy example Get complete version here: http://brainspl.at/nginx.conf.txt 53 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 •! No equivalent for many Apache modules 54 Lighttpd •! Lightweight and fast webserver •! Balancing proxy support •! Good FastCGI support •! Used to be popular – until Mongrel came around 55 Lighttpd Configuration Simple proxy example 56 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 •! (No Passenger support) 57 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 See also similar Pound and Pen 58 HAProxy Simple proxy example 59 HAProxy Pro •! Mature, stable, robust, and fast •! TCP and HTTP balancing Con •! Few Rails examples •! Usually not needed in a Rails setup 60 Recommended Setups 61 Small Site Recommendation Apache 2.2 with mod_rails / Phusion Passenger 62 Medium Site Recommendation •! Apache/Nginx as the frontend proxy •! Passenger/mod_rails as the backend •! Deliver static files with Apache 63 Large Rails Setup Recommendation •! Redundant load-balancer •! Redundant proxy / web •! Passenger/mod_rails 64 Heavy Static Files Recommendation •! Deliver static assets through separate web server farm •! Passenger/mod_rails 65 Java Shop Recommendation •! Deliver a Rails-WAR file and you are done •! Integrate with existing Java landscape and infrastructure 66 67 Application Server Handler 68 Application Server Handler 69 Application Server Handler 70 Rack 71 Rack Handler 72 Rack 73 Rack Middleware 74 Rack Middleware •! Authentication (HTTP, OpenID) •! Sessions •! Routing •! Caching •! Logging •! Debugging 75 Support Frameworks Web Server •! Rails •! CGI •! Merb •! FastCGI •! Camping •! Mongrel •! Sinatra •! Phusion Passenger •! Mack •! JRuby Servlets •! Maveric •! WEBrick •! Halcyon •! Ebb •! Ramaze •! Thin •! Rum •! LiteSpeed •! Vintage •! Swiftcore •! Waves •! Unicorn 76 Remarks 77 Ruby Enterprise Edition •! Copy-On-Write patches to Ruby 1.8 •! Saves memory when spawning several Rails instances •! Used by Phusion PassengerDead: if available ➔ 1.9.3 ➔ 2.0.0 78 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 be2011-03-05 somewhat dated 79 deploy-prozess prozesse (mongrels) müssen neugestartet werden. Bei Passenger: touch tmp/restart.txt capistrano > cap deploy:migrations 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",