• No results found

Deploying and Monitoring Ruby on Rails

N/A
N/A
Protected

Academic year: 2021

Share "Deploying and Monitoring Ruby on Rails"

Copied!
279
0
0

Loading.... (view fulltext now)

Full text

(1)

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!

(2)

Deployment/Scaling/

Caching/Debugging

jan krutisch

mindmatters gmbh&co. kg

jan.krutisch@mindmatters.de

http://mindmatters.de

Mashed

up w

ith...

2011-

03-05

(3)
(4)
(5)
(6)

database

client

(Browser)

(7)

mongrel

database

client

(Browser)

rails

Heute:

➔ thi

n

(

unico

rn

)

(8)
(9)
(10)

database

client

rails

webserver

Dateisystem

public/

(11)
(12)

database

client

rails

webserver

Dateisystem

public/

rails

rails

(13)
(14)
(15)

oldschool:

(16)
(17)
(18)

mod_proxy_balancer

(19)
(20)

oder ein richtiger

balancer

(21)

HAProxy, Squid, etc.

(22)
(23)
(24)

mod_passenger

database

client

rails

webserver

Dateisystem

public/

rails

rails

(25)

automagisch.

it just works.

(26)
(27)

5

Infrastructure

Infrastructure

(28)

6

Simple Rails Setup

One Rails instance handles all requests

(29)

7

(30)

8

(31)

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

(32)

10

(33)

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?

(34)

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

(35)

13

(36)

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

(37)

15

(38)

16

(39)
(40)

18

(41)

19

Mongrel

•! Developed by Zed Shaw as an alternative to FastCGI

•! Complete HTTP-Server that can load arbitrary Ruby-servlets •! Built-in Rails support

(42)

20

(43)

21

Mongrel

Apache 2.2 – mod_proxy_balancer Lighttpd Nginx HA-Proxy Pound

(44)

22

Mongrel Cluster

(45)

23

Mongrel Cluster

(46)

24

Mongrel and Apache 2.2

Define Mongrel Cluster in Apache

(47)

25

Simple Mongrel and Apache 2.2

(48)

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

(49)

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)

(50)

28

(51)

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

(52)

30

Install Phusion Passenger

Install Apache module

(53)

31

Install Phusion Passenger

Install Passenger with nginx (installs custom nginx)

(54)

32

Customized Phusion Passenger

Control Rails instance number

(55)

33

Control Phusion Passenger

(56)

34

Phusion Passenger

(57)

35

Phusion Passenger

(58)

36

Phusion Passenger

•! Ready for production

•! Makes setup easier – on the single machine level •! Multiple servers still require load balancer

•! Suitable for mass-hosting

(59)

37

(60)

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:

(61)

39

(62)

40

JRuby on Glassfish

(63)

41

JRuby on Glassfish

(64)

42

Setup JRuby on Glassfish

1.! Download JRuby and Glassfish

(65)

43

Warble Configuration

Define min/max Rails runtimes

(66)

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

(67)

45

(68)

46

(69)

47

Proxy Requirements

•! Hide cluster backend from the user •! Load-balancer backend instances •! Recognize down hosts

•! Fair scheduler

(70)

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

(71)

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

(72)

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

(73)

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

(74)

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

(75)

53

Nginx Configuration

Simple proxy example

Get complete version here:

(76)

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

(77)

55

Lighttpd

•! Lightweight and fast webserver •! Balancing proxy support

•! Good FastCGI support

(78)

56

Lighttpd Configuration

Simple proxy example

(79)

57

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

(80)

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

(81)

59

HAProxy

(82)

60

HAProxy

Pro

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

Con

•! Few Rails examples

(83)

61

(84)

62

Small Site

Recommendation

Apache 2.2 with mod_rails / Phusion Passenger

(85)

63

Medium Site

Recommendation

•! Apache/Nginx as the frontend proxy •! Passenger/mod_rails as the backend •! Deliver static files with Apache

(86)

64

Large Rails Setup

Recommendation

•! Redundant load-balancer •! Redundant proxy / web •! Passenger/mod_rails

(87)

65

Heavy Static Files

Recommendation

•! Deliver static assets

through separate web server farm

(88)

66

Java Shop

Recommendation

•! Deliver a Rails-WAR file and you are

done

•! Integrate with existing Java

(89)
(90)

68

(91)

69

(92)

70

(93)

71

(94)

72

(95)

73

(96)

74

(97)

75

Rack Middleware

•! Authentication (HTTP, OpenID) •! Sessions •! Routing •! Caching •! Logging •! Debugging

(98)

76

Support

Frameworks

•! Rails •! Merb •! Camping •! Sinatra •! Mack •! Maveric •! Halcyon •! Ramaze •! Rum •! Vintage •! Waves

Web Server

•! CGI •! FastCGI •! Mongrel •! Phusion Passenger •! JRuby Servlets •! WEBrick •! Ebb •! Thin •! LiteSpeed •! Swiftcore •! Unicorn

(99)

77

(100)

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

(101)

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

(102)
(103)

prozesse (mongrels)

müssen neugestartet

werden.

(104)

Bei Passenger:

(105)
(106)
(107)

konfiguration in

ruby-dateien. Yay!

(108)

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"

(109)

AWE13

Gruppe/

04

danke!

(110)

AWE13

Gruppe/

04

danke!

(111)
(112)

Erweitern um eigene

"Recipes"

(113)

Kommandos werden

parallel auf allen Servern

ausgeführt

(114)
(115)

Unterstützung diverser

SCM-Systeme

(116)

Erlaubt (relativ) einfache

rollbacks, jeder

(117)
(118)

Nicht auf Rails

beschränkt!

(119)

80

Infrastructure

Deployment

(120)

82

(121)

83

(122)

84

(123)

85

(124)

86

(125)

87

(126)

88

(127)

89

What doesn’t Capistrano do?

•! Plan your initial server setup •! Configure basic services

(128)

90

Basic Ingredients

•! The cap command •! Variables

•! Roles •! Tasks

(129)

91

Basic Ingredients - cap

Your one-stop deployment shop

(130)

92

Basic Ingredients - Variables

•! Configure basic project information

•! Override Capistrano’s default assumptions •! Once set, variables are available globally •! Defined using the set method

(131)

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

(132)

94

Basic Ingredients - Roles

Define custom roles as you please

(133)

95

Basic Ingredients - Tasks

•! Define an atomic set of actions for Capistrano •! Can be called from the command line

(134)

96

Basic Ingredients - Tasks

(135)

97

Basic Ingredients - Namespaces

Group tasks together logically

(136)

98

(137)

99

Get Your Capistrano On

(138)

100

Get Your Capistrano On

(139)

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

(140)

102

(141)

103

Get Your Capistrano On

•! Capistrano expects a directory structure •! Can be created with cap deploy:setup

(142)

104

(143)

105

The Deployment Lifecycle

Check the prerequisites:

(144)

106

The Deployment Lifecycle

Set up your application for the first time

(145)

107

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

(146)

108

(147)

109

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

(148)

110

(149)

111

Common Capistrano Tasks

Deploy and run migrations

Run only the migrations

Restart application servers

(150)

112

(151)

113

(152)

114

(153)

115

Deployment Strategies

(154)

116

Deployment Strategies

(155)

117

Deployment Strategies

Check out locally and transfer

(156)

119

(157)

120

(158)

121

(159)

122

(160)

123

Callbacks

Execute a task before another runs

Execute a task after another has finished

(161)

124

(162)

125

(163)

126

(164)

127

(165)

128

(166)

129

The Rest

•! Gem dependencies

•! Support for deploying through gateway servers •! Parallel execution on multiple servers

(167)

131

Vlad the Deployer

•! Simple, Rake-based deployment solution

(168)

132

Vlad the Deployer

Load

(169)

133

Vlad the Deployer

(170)

134

Vlad the Deployer

•! Simple, sometimes too simple

•! No callback mechanism, instead the Rake syntax must be used

(171)

135

Chef

•! System Integration Framework

•! Written in Ruby – inspired by Puppet •! Manage multiple nodes

•! Configuration •! Packages

(172)

136

(173)

137

Recipes

(174)

138

Recipes

(175)

139

Recipes

(176)

140

chef-deploy

Enable

(177)

141

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

(178)

154

Infrastructure

Monitoring

(179)

155

(180)

156

(181)

157

(182)

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

(183)

159

Monit

MySQL

(184)

160

Monit

Apache

(185)

161

Monit

Mongrel

(186)

162

God

•! Ruby-based configuration •! Easily extendible

•! Can work with event- and poll-based conditions •! Comes with less recipes than Monit

(187)

163

God

Mongrel

(188)

164

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

(189)

165

(190)

166

(191)

167

(192)

168

Exception Monitoring

•! Monitor errors raised by your application •! Usually notifying you by email

•! Good old exception_notification Plugin •! Exceptions as a Service

(193)

169

(194)

170

(195)

171

New Relic

•! Rails Performance monitoring •! Live view into your application

(196)

172

Other Tools

•! Nagios •! Big Brother •! New Relic RPM •! FiveRuns •! JMX

(197)

173

Infrastructure

(198)

174

(199)

175

Search

Full text search

(200)

176

Full Text Search Engine

Separate Service

•! Creates full text index

•! Application queries search daemon •! Index update through application or

database

Possible Engines

•! Ferret •! Sphinx •! Solr •! Lucene •! …

References

Related documents

• Electrophysiologic studies show that nicotine increases dopamine release during phasic , as opposed to tonic, firing. • This may underlie the rewarding effect of

Based on data from the Irish Community Innovation Survey 2006-08, the results suggest that Irish-owned firms which engage in external networks with public knowledge sources,

For security-critical systems, this approach allows one to consider security requirements from early on in the development process, within the development context, and in a

Matrices of scattering amplitudes between all combinations of input –output pairs of no-collision states of 2 and 4 photons into 8 modes, for Sylvester and Fourier matrices..

Conclusions can be drawn for this study: that the use of CAD technology in learning architectural design process helps students in producing creative products, the CAD software

The origin of the tort of inducing breach of contract can be found in Lumley v. 6ye.15 In Lumley, the defendant persuaded an opera singer who was under contract to sing exclusively

Hypothesis 2 proposes that closely related concepts compete for attention and thus the prevalence of an old concept should be negatively associated with the number of articles each

Article 42(2) provides that for the purposes of the criminal justice process, telecommunication service providers can record information sent and/or received, and can