Deploying and Monitoring Ruby on Rails A practical guide Mathias - - PowerPoint PPT Presentation

deploying and monitoring ruby on rails
SMART_READER_LITE
LIVE PREVIEW

Deploying and Monitoring Ruby on Rails A practical guide Mathias - - PowerPoint PPT Presentation

Deploying and Monitoring Ruby on Rails A practical guide Mathias Meyer and Jonathan Weiss, 02.09.2008 Peritor GmbH Who are we? Jonathan W Jonathan Weiss eiss Consultant for Peritor GmbH in Berlin Specialized in Rails, Scaling,


slide-1
SLIDE 1

Deploying and Monitoring Ruby on Rails

A practical guide

Mathias Meyer and Jonathan Weiss, 02.09.2008 Peritor GmbH

slide-2
SLIDE 2

2

Who are we?

Jonathan W Jonathan Weiss eiss

  • 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

slide-3
SLIDE 3

3

Who are we?

Mathias Meyer 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

slide-4
SLIDE 4

4

Agenda

Infrastructure Deployment Monitoring Q & A Practical Session

slide-5
SLIDE 5

5

Infrastructure Infrastructure

slide-6
SLIDE 6

6

Simple Rails Setup

One Rails instance handles all requests Rails is single-threaded: There is only one concurrent request

slide-7
SLIDE 7

7

Rails Setup

slide-8
SLIDE 8

8

Rails Setup

slide-9
SLIDE 9

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
slide-10
SLIDE 10

10

Rails Setup Options

slide-11
SLIDE 11

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?

slide-12
SLIDE 12

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
slide-13
SLIDE 13

13

FastCGI

slide-14
SLIDE 14

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)
slide-15
SLIDE 15

15

FastCGI

slide-16
SLIDE 16

16

FastCGI

slide-17
SLIDE 17

17

slide-18
SLIDE 18

18

Mongrel

slide-19
SLIDE 19

19

Mongrel

  • Developed by Zed Shaw as an alternative to FastCGI
  • Complete HTTP-Server that can load arbitrary Ruby-servlets
  • Built-in Rails support
slide-20
SLIDE 20

20

Mongrel

slide-21
SLIDE 21

21

Mongrel

Apache 2.2 – mod_proxy_balancer Lighttpd Nginx HA-Proxy Pound

slide-22
SLIDE 22

22

Mongrel Cluster

Utility to manage several Mongrel instances

slide-23
SLIDE 23

23

Mongrel Cluster

Control Mongrels

slide-24
SLIDE 24

24

Mongrel and Apache 2.2

Define Mongrel Cluster in Apache

slide-25
SLIDE 25

25

Simple Mongrel and Apache 2.2

Redirect all traffic to the Mongel cluster

slide-26
SLIDE 26

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
slide-27
SLIDE 27

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
slide-28
SLIDE 28

28

mod_rails

slide-29
SLIDE 29

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
slide-30
SLIDE 30

30

Install Phusion Passenger

Install Apache module Load and activate in Apache

slide-31
SLIDE 31

31

Customized Phusion Passenger

Control Rails instance number

slide-32
SLIDE 32

32

Control Phusion Passenger

Restart after deployment:

slide-33
SLIDE 33

33

Phusion Passenger

One machine

slide-34
SLIDE 34

34

Phusion Passenger

One machine Multiple machines

slide-35
SLIDE 35

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
slide-36
SLIDE 36

36

JRuby

slide-37
SLIDE 37

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

slide-38
SLIDE 38

38

JRuby on Rails

slide-39
SLIDE 39

39

JRuby on Glassfish

One machine

slide-40
SLIDE 40

40

JRuby on Glassfish

One machine Multiple machines

slide-41
SLIDE 41

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
slide-42
SLIDE 42

42

Warble Configuration

Define min/max Rails runtimes

slide-43
SLIDE 43

43

Rails Setup

slide-44
SLIDE 44

44

Proxy Options

slide-45
SLIDE 45

45

Proxy Requirements

  • Hide cluster backend from the user
  • Load-balancer backend instances
  • Recognize down hosts
  • Fair scheduler
  • (Deliver static content)
slide-46
SLIDE 46

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
slide-47
SLIDE 47

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
slide-48
SLIDE 48

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
slide-49
SLIDE 49

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
slide-50
SLIDE 50

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
slide-51
SLIDE 51

51

Nginx Configuration

Simple proxy example Get complete version here: http://brainspl.at/nginx.conf.txt

slide-52
SLIDE 52

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
slide-53
SLIDE 53

53

Lighttpd

  • Lightweight and fast webserver
  • Balancing proxy support
  • Good FastCGI support
  • Used to be popular – until Mongrel came around
slide-54
SLIDE 54

54

Lighttpd Configuration

Simple proxy example

slide-55
SLIDE 55

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
slide-56
SLIDE 56

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

slide-57
SLIDE 57

57

HAProxy

Simple proxy example

slide-58
SLIDE 58

58

HAProxy

Pro

  • Mature, stable, robust, and fast
  • TCP and HTTP balancing

Con

  • Few Rails examples
  • Usually not needed in a Rails setup
slide-59
SLIDE 59

59

Recommended Setups

slide-60
SLIDE 60

60

Small Site

Recommendation

Apache 2.2 with mod_rails / Phusion Passenger

slide-61
SLIDE 61

61

Medium Site

Recommendation

  • Apache 2.2 as the frontend proxy
  • Use Mongrel or mod_rails as the

backend

  • Deliver static files with Apache
slide-62
SLIDE 62

62

Medium Site

Recommendation

  • Apache 2.2 as the frontend proxy
  • Use Mongrel or mod_rails as the

backend

  • Deliver static files with Apache
slide-63
SLIDE 63

63

Large Rails Setup

Recommendation

  • Redundant load-balancer
  • Redundant proxy / web
  • Mongrel / mod_rails
slide-64
SLIDE 64

64

Heavy Static Files

Recommendation

  • Deliver static assets

through separate web server farm

  • Mongrel or mod_rails
slide-65
SLIDE 65

65

Java Shop

Recommendation

  • Deliver a Rails-WAR file and you are

done

  • Integrate with existing Java

landscape and infrastructure

slide-66
SLIDE 66

66

Remarks

slide-67
SLIDE 67

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
slide-68
SLIDE 68

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

slide-69
SLIDE 69

69

In The Future, Watch

Fuzed

Erlang based load balancing Dynamic registration

JRuby

Faster Runtime Java Integration

Phusion Passenger

Enterprise Ruby Performance Usability

slide-70
SLIDE 70

70

Infrastructure Deployment

slide-71
SLIDE 71

71

slide-72
SLIDE 72

72

Deployment Options

slide-73
SLIDE 73

73

Deployment Options

slide-74
SLIDE 74

74

Deployment Options

slide-75
SLIDE 75

75

Deployment Options

slide-76
SLIDE 76

76

What does Capistrano do?

slide-77
SLIDE 77

77

Capistrano Deployment Cycle

slide-78
SLIDE 78

78

Requirements

slide-79
SLIDE 79

79

What doesn’t Capistrano do?

  • Plan your initial server setup
  • Configure basic services
slide-80
SLIDE 80

80

Basic Ingredients

  • The cap command
  • Variables
  • Roles
  • Tasks
  • Namespaces
slide-81
SLIDE 81

81

Basic Ingredients - cap

Your one-stop deployment shop

slide-82
SLIDE 82

82

Basic Ingredients - Variables

  • Configure basic project information
  • Override Capistrano’s default assumptions
  • Once set, variables are available globally
  • Defined using the set method
slide-83
SLIDE 83

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
slide-84
SLIDE 84

84

Basic Ingredients - Roles

Define custom roles as you please Can be reused when defining tasks

slide-85
SLIDE 85

85

Basic Ingredients - Tasks

  • Define an atomic set of actions for Capistrano
  • Can be called from the command line
  • Or other tasks
slide-86
SLIDE 86

86

Basic Ingredients - Tasks

To find all the tasks available in your project, use

slide-87
SLIDE 87

87

Basic Ingredients - Namespaces

Group tasks together logically Namespaces and tasks are separated with “:”

slide-88
SLIDE 88

88

Get Your Capistrano On

slide-89
SLIDE 89

89

Get Your Capistrano On

Capfile, the place to include more recipes

slide-90
SLIDE 90

90

Get Your Capistrano On

config/deploy.rb, application specific configuration

slide-91
SLIDE 91

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
slide-92
SLIDE 92

92

Get Your Capistrano On

slide-93
SLIDE 93

93

Get Your Capistrano On

  • Capistrano expects a directory structure
  • Can be created with cap deploy:setup
slide-94
SLIDE 94

94

The Deployment Lifecycle

slide-95
SLIDE 95

95

The Deployment Lifecycle

Check the prerequisites:

slide-96
SLIDE 96

96

The Deployment Lifecycle

Set up your application for the first time

slide-97
SLIDE 97

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
slide-98
SLIDE 98

98

The Deployment Lifecycle

slide-99
SLIDE 99

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
slide-100
SLIDE 100

100

The Deployment Lifecycle

slide-101
SLIDE 101

101

Common Capistrano Tasks

Deploy and run migrations Run only the migrations Restart application servers Rollback to the previous release

slide-102
SLIDE 102

102

Have Your Shell, and Eat It Too

slide-103
SLIDE 103

103

Invoking any Command

slide-104
SLIDE 104

104

Deployment Strategies

slide-105
SLIDE 105

105

Deployment Strategies

Direct checkout (from scratch) on the servers

slide-106
SLIDE 106

106

Deployment Strategies

Keep a cached copy of the current SCM head

slide-107
SLIDE 107

107

Deployment Strategies

Check out locally and transfer

slide-108
SLIDE 108

108

Give it a little Spin

Capistrano expects a script called spin in script/process For Passenger

slide-109
SLIDE 109

109

Customizing Capistrano

slide-110
SLIDE 110

110

Write your own Tasks

slide-111
SLIDE 111

111

Write your own Tasks

slide-112
SLIDE 112

112

Namespace your Tasks

slide-113
SLIDE 113

113

Callbacks

Execute a task before another runs Execute a task after another has finished Callbacks are run in the order they’re defined

slide-114
SLIDE 114

114

Useful Variables

slide-115
SLIDE 115

115

Transactions

slide-116
SLIDE 116

116

Transactions

slide-117
SLIDE 117

117

Transactions

slide-118
SLIDE 118

118

Transactions

slide-119
SLIDE 119

119

The Rest

  • Gem dependencies
  • Support for deploying through gateway servers
  • Server setup with deprec gem
  • Lack of documentation
slide-120
SLIDE 120

120

One Click Deploy

slide-121
SLIDE 121

121

Webistrano

slide-122
SLIDE 122

122

Webistrano

  • Web-UI to Capistrano
  • Manages projects and their stages
  • Alerting and Accounting
  • Scriptable and extendable
  • BSD License

http://labs.peritor.com/webistrano

slide-123
SLIDE 123

123

Macistrano

  • Mac-GUI to Webistrano
  • Fire and monitor deployments from your desktop

http://github.com/mattmatt/macistrano

slide-124
SLIDE 124

124

Infrastructure Pratical Session

slide-125
SLIDE 125

125

Practical Capistrano

slide-126
SLIDE 126

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
slide-127
SLIDE 127

127

Infrastructure Monitoring

slide-128
SLIDE 128

128

The two questions of monitoring

slide-129
SLIDE 129

129

  • 1. Is everything still running?
slide-130
SLIDE 130

130

  • 2. What are the trends?
slide-131
SLIDE 131

131

Monit

  • Process-level monitoring
  • Checks PID-files, ports, and permissions
  • Reacts by executing a script and/or alerting
slide-132
SLIDE 132

132

Monit

MySQL

slide-133
SLIDE 133

133

Monit

Apache

slide-134
SLIDE 134

134

Monit

Mongrel

slide-135
SLIDE 135

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
slide-136
SLIDE 136

136

Munin

slide-137
SLIDE 137

137

Munin

slide-138
SLIDE 138

138

Munin

slide-139
SLIDE 139

139

Other Tools

  • Nagios
  • Big Brother
  • New Relic RPM
  • FiveRuns
  • JMX
slide-140
SLIDE 140

140

Q & A

slide-141
SLIDE 141

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: kontakt@peritor.com

Peritor GmbH - Alle Rechte vorbehalten