deploying and monitoring ruby on rails

141
Deploying and Monitoring Ruby on Rails A practical guide Mathias Meyer and Jonathan Weiss, 02.09.2008 Peritor GmbH

Upload: oleksiy-kovyrin

Post on 11-Apr-2015

2.871 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Deploying and Monitoring Ruby on Rails

Deploying and Monitoring

Ruby on Rails A practical guide

Mathias Meyer and Jonathan Weiss, 02.09.2008

Peritor GmbH

Page 2: Deploying and Monitoring Ruby on Rails

2

Who are we?

Jonathan Weiss

•  Consultant for Peritor GmbH in Berlin

•  Specialized in Rails, Scaling, Deployment, and Code Review

•  Webistrano - Rails deployment tool

•  FreeBSD Rubygems and Ruby on Rails maintainer

http://www.peritor.com

http://blog.innerewut.de

Page 3: Deploying and Monitoring Ruby on Rails

3

Who are we?

Mathias Meyer

•  Independent Contractor

•  Specialized in Rails, Performance/Database Tuning, Deployment, and Refactoring

•  Macistrano – Webistrano-Client for Mac OS X (and soon the iPhone)

http://www.paperplanes.de

Page 4: Deploying and Monitoring Ruby on Rails

4

Agenda

Infrastructure Deployment Monitoring Q & A Practical Session

Page 5: Deploying and Monitoring Ruby on Rails

5

Infrastructure Infrastructure

Page 6: Deploying and Monitoring Ruby on Rails

6

Simple Rails Setup

One Rails instance handles all requests

Rails is single-threaded: There is only one concurrent request

Page 7: Deploying and Monitoring Ruby on Rails

7

Rails Setup

Page 8: Deploying and Monitoring Ruby on Rails

8

Rails Setup

Page 9: Deploying and Monitoring Ruby on Rails

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

•  Number of concurrent requests equals number of Rails instances

Page 10: Deploying and Monitoring Ruby on Rails

10

Rails Setup Options

Page 11: Deploying and Monitoring Ruby on Rails

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?

Page 12: Deploying and Monitoring Ruby on Rails

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

Page 13: Deploying and Monitoring Ruby on Rails

13

FastCGI

Page 14: Deploying and Monitoring Ruby on Rails

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

•  Hard to debug (FastCGI protocol)

Page 15: Deploying and Monitoring Ruby on Rails

15

FastCGI

Page 16: Deploying and Monitoring Ruby on Rails

16

FastCGI

Page 17: Deploying and Monitoring Ruby on Rails

17

Page 18: Deploying and Monitoring Ruby on Rails

18

Mongrel

Page 19: Deploying and Monitoring Ruby on Rails

19

Mongrel

•  Developed by Zed Shaw as an alternative to FastCGI

•  Complete HTTP-Server that can load arbitrary Ruby-servlets

•  Built-in Rails support

Page 20: Deploying and Monitoring Ruby on Rails

20

Mongrel

Page 21: Deploying and Monitoring Ruby on Rails

21

Mongrel

Apache 2.2 – mod_proxy_balancer Lighttpd Nginx HA-Proxy Pound

Page 22: Deploying and Monitoring Ruby on Rails

22

Mongrel Cluster

Utility to manage several Mongrel instances

Page 23: Deploying and Monitoring Ruby on Rails

23

Mongrel Cluster

Control Mongrels

Page 24: Deploying and Monitoring Ruby on Rails

24

Mongrel and Apache 2.2

Define Mongrel Cluster in Apache

Page 25: Deploying and Monitoring Ruby on Rails

25

Simple Mongrel and Apache 2.2

Redirect all traffic to the Mongel cluster

Page 26: Deploying and Monitoring Ruby on Rails

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

•  Compress output if supported

Page 27: Deploying and Monitoring Ruby on Rails

27

Mongrel

•  Very robust

•  Strict HTTP parser

•  Easy to debug (HTTP!)

•  Defacto standard deployment 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

Page 28: Deploying and Monitoring Ruby on Rails

28

mod_rails

Page 29: Deploying and Monitoring Ruby on Rails

29

mod_rails a.k.a Phusion Passenger

•  Fairly new module for Apache 2.2

•  Allows Apache to control Rails instances

•  Apache starts and stops application instances depending on the application load

•  Very easy to setup

•  Able to run any RACK-compatible Ruby application (Merb & Co.)

•  Only manages Rails on one host - no remote instances

•  Combine with HTTP-Proxy / balancing solution

Page 30: Deploying and Monitoring Ruby on Rails

30

Install Phusion Passenger

Install Apache module

Load and activate in Apache

Page 31: Deploying and Monitoring Ruby on Rails

31

Customized Phusion Passenger

Control Rails instance number

Page 32: Deploying and Monitoring Ruby on Rails

32

Control Phusion Passenger

Restart after deployment:

Page 33: Deploying and Monitoring Ruby on Rails

33

Phusion Passenger

One machine

Page 34: Deploying and Monitoring Ruby on Rails

34

Phusion Passenger

One machine Multiple machines

Page 35: Deploying and Monitoring Ruby on Rails

35

Phusion Passenger

•  Fairly new but ready for production

•  Makes setup easier – on the single machine level

•  Multiple servers still require load balancer

•  Suitable for mass-hosting

•  The upcoming standard way of deploying Rails

Page 36: Deploying and Monitoring Ruby on Rails

36

JRuby

Page 37: Deploying and Monitoring Ruby on Rails

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, …

Page 38: Deploying and Monitoring Ruby on Rails

38

JRuby on Rails

Page 39: Deploying and Monitoring Ruby on Rails

39

JRuby on Glassfish

One machine

Page 40: Deploying and Monitoring Ruby on Rails

40

JRuby on Glassfish

One machine Multiple machines

Page 41: Deploying and Monitoring Ruby on Rails

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

Page 42: Deploying and Monitoring Ruby on Rails

42

Warble Configuration

Define min/max Rails runtimes

Page 43: Deploying and Monitoring Ruby on Rails

43

Rails Setup

Page 44: Deploying and Monitoring Ruby on Rails

44

Proxy Options

Page 45: Deploying and Monitoring Ruby on Rails

45

Proxy Requirements

•  Hide cluster backend from the user

•  Load-balancer backend instances

•  Recognize down hosts

•  Fair scheduler

•  (Deliver static content)

Page 46: Deploying and Monitoring Ruby on Rails

46

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

Page 47: Deploying and Monitoring Ruby on Rails

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

Page 48: Deploying and Monitoring Ruby on Rails

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

Page 49: Deploying and Monitoring Ruby on Rails

49

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

Page 50: Deploying and Monitoring Ruby on Rails

50

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

Page 51: Deploying and Monitoring Ruby on Rails

51

Nginx Configuration

Simple proxy example

Get complete version here:

http://brainspl.at/nginx.conf.txt

Page 52: Deploying and Monitoring Ruby on Rails

52

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

Page 53: Deploying and Monitoring Ruby on Rails

53

Lighttpd

•  Lightweight and fast webserver

•  Balancing proxy support

•  Good FastCGI support

•  Used to be popular – until Mongrel came around

Page 54: Deploying and Monitoring Ruby on Rails

54

Lighttpd Configuration

Simple proxy example

Page 55: Deploying and Monitoring Ruby on Rails

55

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

Page 56: Deploying and Monitoring Ruby on Rails

56

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

Page 57: Deploying and Monitoring Ruby on Rails

57

HAProxy

Simple proxy example

Page 58: Deploying and Monitoring Ruby on Rails

58

HAProxy

Pro

•  Mature, stable, robust, and fast

•  TCP and HTTP balancing

Con

•  Few Rails examples

•  Usually not needed in a Rails setup

Page 59: Deploying and Monitoring Ruby on Rails

59

Recommended Setups

Page 60: Deploying and Monitoring Ruby on Rails

60

Small Site

Recommendation

Apache 2.2 with mod_rails / Phusion Passenger

Page 61: Deploying and Monitoring Ruby on Rails

61

Medium Site

Recommendation

•  Apache 2.2 as the frontend proxy

•  Use Mongrel or mod_rails as the backend

•  Deliver static files with Apache

Page 62: Deploying and Monitoring Ruby on Rails

62

Medium Site

Recommendation

•  Apache 2.2 as the frontend proxy

•  Use Mongrel or mod_rails as the backend

•  Deliver static files with Apache

Page 63: Deploying and Monitoring Ruby on Rails

63

Large Rails Setup

Recommendation

•  Redundant load-balancer

•  Redundant proxy / web

•  Mongrel / mod_rails

Page 64: Deploying and Monitoring Ruby on Rails

64

Heavy Static Files

Recommendation

•  Deliver static assets through separate web server farm

•  Mongrel or mod_rails

Page 65: Deploying and Monitoring Ruby on Rails

65

Java Shop

Recommendation

•  Deliver a Rails-WAR file and you are done

•  Integrate with existing Java landscape and infrastructure

Page 66: Deploying and Monitoring Ruby on Rails

66

Remarks

Page 67: Deploying and Monitoring Ruby on Rails

67

Ruby Enterprise Edition

•  Copy-On-Write patches to Ruby 1.8

•  Saves memory when spawning several Rails instances

•  Used by Phusion Passenger if available

Page 68: Deploying and Monitoring Ruby on Rails

68

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

Page 69: Deploying and Monitoring Ruby on Rails

69

In The Future, Watch

Fuzed

Erlang based load balancing

Dynamic registration

JRuby

Faster Runtime

Java Integration

Phusion Passenger

Enterprise Ruby

Performance

Usability

Page 70: Deploying and Monitoring Ruby on Rails

70

Infrastructure Deployment

Page 71: Deploying and Monitoring Ruby on Rails

71

Page 72: Deploying and Monitoring Ruby on Rails

72

Deployment Options

Page 73: Deploying and Monitoring Ruby on Rails

73

Deployment Options

Page 74: Deploying and Monitoring Ruby on Rails

74

Deployment Options

Page 75: Deploying and Monitoring Ruby on Rails

75

Deployment Options

Page 76: Deploying and Monitoring Ruby on Rails

76

What does Capistrano do?

Page 77: Deploying and Monitoring Ruby on Rails

77

Capistrano Deployment Cycle

Page 78: Deploying and Monitoring Ruby on Rails

78

Requirements

Page 79: Deploying and Monitoring Ruby on Rails

79

What doesn’t Capistrano do?

•  Plan your initial server setup

•  Configure basic services

Page 80: Deploying and Monitoring Ruby on Rails

80

Basic Ingredients

•  The cap command

•  Variables

•  Roles

•  Tasks

•  Namespaces

Page 81: Deploying and Monitoring Ruby on Rails

81

Basic Ingredients - cap

Your one-stop deployment shop

Page 82: Deploying and Monitoring Ruby on Rails

82

Basic Ingredients - Variables

•  Configure basic project information

•  Override Capistrano’s default assumptions

•  Once set, variables are available globally

•  Defined using the set method

Page 83: Deploying and Monitoring Ruby on Rails

83

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

•  At least one database server needs to be primary

Page 84: Deploying and Monitoring Ruby on Rails

84

Basic Ingredients - Roles

Define custom roles as you please

Can be reused when defining tasks

Page 85: Deploying and Monitoring Ruby on Rails

85

Basic Ingredients - Tasks

•  Define an atomic set of actions for Capistrano

•  Can be called from the command line

•  Or other tasks

Page 86: Deploying and Monitoring Ruby on Rails

86

Basic Ingredients - Tasks

To find all the tasks available in your project, use

Page 87: Deploying and Monitoring Ruby on Rails

87

Basic Ingredients - Namespaces

Group tasks together logically

Namespaces and tasks are separated with “:”

Page 88: Deploying and Monitoring Ruby on Rails

88

Get Your Capistrano On

Page 89: Deploying and Monitoring Ruby on Rails

89

Get Your Capistrano On

Capfile, the place to include more recipes

Page 90: Deploying and Monitoring Ruby on Rails

90

Get Your Capistrano On

config/deploy.rb, application specific configuration

Page 91: Deploying and Monitoring Ruby on Rails

91

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

Page 92: Deploying and Monitoring Ruby on Rails

92

Get Your Capistrano On

Page 93: Deploying and Monitoring Ruby on Rails

93

Get Your Capistrano On

•  Capistrano expects a directory structure

•  Can be created with cap deploy:setup

Page 94: Deploying and Monitoring Ruby on Rails

94

The Deployment Lifecycle

Page 95: Deploying and Monitoring Ruby on Rails

95

The Deployment Lifecycle

Check the prerequisites:

Page 96: Deploying and Monitoring Ruby on Rails

96

The Deployment Lifecycle

Set up your application for the first time

Page 97: Deploying and Monitoring Ruby on Rails

97

The Deployment Lifecycle

The initial deployment

1.  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

5.  Fires up application servers

Page 98: Deploying and Monitoring Ruby on Rails

98

The Deployment Lifecycle

Page 99: Deploying and Monitoring Ruby on Rails

99

The Deployment Lifecycle

Subsequent deployments

1.  Checks the revision from the local machine

2.  Checks out the code on the remote machines

3.  Updates current link

4.  Restarts application servers

Page 100: Deploying and Monitoring Ruby on Rails

100

The Deployment Lifecycle

Page 101: Deploying and Monitoring Ruby on Rails

101

Common Capistrano Tasks

Deploy and run migrations

Run only the migrations

Restart application servers

Rollback to the previous release

Page 102: Deploying and Monitoring Ruby on Rails

102

Have Your Shell, and Eat It Too

Page 103: Deploying and Monitoring Ruby on Rails

103

Invoking any Command

Page 104: Deploying and Monitoring Ruby on Rails

104

Deployment Strategies

Page 105: Deploying and Monitoring Ruby on Rails

105

Deployment Strategies

Direct checkout (from scratch) on the servers

Page 106: Deploying and Monitoring Ruby on Rails

106

Deployment Strategies

Keep a cached copy of the current SCM head

Page 107: Deploying and Monitoring Ruby on Rails

107

Deployment Strategies

Check out locally and transfer

Page 108: Deploying and Monitoring Ruby on Rails

108

Give it a little Spin

Capistrano expects a script called spin in script/process

For Passenger

Page 109: Deploying and Monitoring Ruby on Rails

109

Customizing Capistrano

Page 110: Deploying and Monitoring Ruby on Rails

110

Write your own Tasks

Page 111: Deploying and Monitoring Ruby on Rails

111

Write your own Tasks

Page 112: Deploying and Monitoring Ruby on Rails

112

Namespace your Tasks

Page 113: Deploying and Monitoring Ruby on Rails

113

Callbacks

Execute a task before another runs

Execute a task after another has finished

Callbacks are run in the order they’re defined

Page 114: Deploying and Monitoring Ruby on Rails

114

Useful Variables

Page 115: Deploying and Monitoring Ruby on Rails

115

Transactions

Page 116: Deploying and Monitoring Ruby on Rails

116

Transactions

Page 117: Deploying and Monitoring Ruby on Rails

117

Transactions

Page 118: Deploying and Monitoring Ruby on Rails

118

Transactions

Page 119: Deploying and Monitoring Ruby on Rails

119

The Rest

•  Gem dependencies

•  Support for deploying through gateway servers

•  Server setup with deprec gem

•  Lack of documentation

Page 120: Deploying and Monitoring Ruby on Rails

120

One Click Deploy

Page 121: Deploying and Monitoring Ruby on Rails

121

Webistrano

Page 122: Deploying and Monitoring Ruby on Rails

122

Webistrano

•  Web-UI to Capistrano

•  Manages projects and their stages

•  Alerting and Accounting

•  Scriptable and extendable

•  BSD License

http://labs.peritor.com/webistrano

Page 123: Deploying and Monitoring Ruby on Rails

123

Macistrano

•  Mac-GUI to Webistrano

•  Fire and monitor deployments from your desktop

http://github.com/mattmatt/macistrano

Page 124: Deploying and Monitoring Ruby on Rails

124

Infrastructure Pratical Session

Page 125: Deploying and Monitoring Ruby on Rails

125

Practical Capistrano

Page 126: Deploying and Monitoring Ruby on Rails

126

Get the slides

•  Connect to “RailsConf Deployment” WLAN

•  The slides are at http://10.0.0.1/

•  The VMs are at 10.0.0.3 – 10.0.0.41

•  Sample configurations files are in the GIT/SVN repositories

Page 127: Deploying and Monitoring Ruby on Rails

127

Infrastructure Monitoring

Page 128: Deploying and Monitoring Ruby on Rails

128

The two questions of monitoring

Page 129: Deploying and Monitoring Ruby on Rails

129

1. Is everything still running?

Page 130: Deploying and Monitoring Ruby on Rails

130

2. What are the trends?

Page 131: Deploying and Monitoring Ruby on Rails

131

Monit

•  Process-level monitoring

•  Checks PID-files, ports, and permissions

•  Reacts by executing a script and/or alerting

Page 132: Deploying and Monitoring Ruby on Rails

132

Monit

MySQL

Page 133: Deploying and Monitoring Ruby on Rails

133

Monit

Apache

Page 134: Deploying and Monitoring Ruby on Rails

134

Monit

Mongrel

Page 135: Deploying and Monitoring Ruby on Rails

135

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

Page 136: Deploying and Monitoring Ruby on Rails

136

Munin

Page 137: Deploying and Monitoring Ruby on Rails

137

Munin

Page 138: Deploying and Monitoring Ruby on Rails

138

Munin

Page 139: Deploying and Monitoring Ruby on Rails

139

Other Tools

•  Nagios

•  Big Brother

•  New Relic RPM

•  FiveRuns

•  JMX

Page 140: Deploying and Monitoring Ruby on Rails

140

Q & A

Page 141: Deploying and Monitoring Ruby on Rails

141

141

Peritor GmbH

Teutonenstraße 16 14129 Berlin

Telefon: +49 (0)30 69 20 09 84 0 Telefax: +49 (0)30 69 20 09 84 9

Internet: www.peritor.com E-Mail: [email protected]

Peritor GmbH - Alle Rechte vorbehalten