Successfully reported this slideshow.

Deploying Plack Web Applications: OSCON 2011

19,169 views

Published on

Published in: Technology
  • Be the first to comment

Deploying Plack Web Applications: OSCON 2011

  1. 1. Deploying PlackWeb Applications Tatsuhiko Miyagawa O’Reilly OSCON 2011
  2. 2. Me• Tatsuhiko Miyagawa• @miyagawa• Lives in San Francisco, CA• Software Engineer, DotCloud
  3. 3. My Software• Plack• PSGI• cpanminus• carton (NEW!)
  4. 4. PSGI
  5. 5. PSGI in 3 min.
  6. 6. #
PSGImy
$app
=
sub
{



my
$env
=
shift;



return
[








200,







[
‘Content‐Type’,
‘text/plain’
],







[
‘Hello
World’
],



];};
  7. 7. Catalyst CGI::App Jifty Mojolicious Dancer Plack::Middleware PSGI Plack::Handler::* (CGI, FCGI, Apache)Apache lighttpd HTTP::Server::PSGI mod_psgi PerlbalStarman Twiggy uWSGI Corona Feersum
  8. 8. Mason CGI::Application Jifty CatalystMason::CGIHandler CGI.pm Catalyst::Engine mod_perl CGI fastcgi HTTP::Server ::Simple Apache IIS lighttpd nginx
  9. 9. Catalyst CGI::App Jifty Mojolicious Dancer Plack::Middleware PSGI Plack::Handler::* (CGI, FCGI, Apache)Apache lighttpd HTTP::Server::PSGI mod_psgi PerlbalStarman Twiggy uWSGI Corona Feersum
  10. 10. Momentum
  11. 11. Catalyst 5.9Built-in PSGI/Plack support
  12. 12. CGI::Application 4.5 Built-in PSGI support
  13. 13. Dancer, Mojolicious
  14. 14. 20+Web Frameworks
  15. 15. 25+Web Servers/Handlers
  16. 16. 150+Plack Middleware
  17. 17. Cloud (PaaS)
  18. 18. Still not convinced?
  19. 19. “FastCGI is good enough”? http://xrl.us/psgireddit
  20. 20. Deploying PlackWeb applications
  21. 21. 1)Server Environment
  22. 22. 2)Code Deployment
  23. 23. 3)Application Environment
  24. 24. 1)Server Environment
  25. 25. Writing a PSGI app?
  26. 26. Maypole Mason Mojolicious Sledge Catalyst Spoon PageKit AxKit Egg Gantry Continuity SolsticeMojolicious Tripletail Konstrukt Reaction Jifty WebGUI OpenInteract Squatting Dancer CGI::Application Ark Angelos Noe Schenker Tatsumaki Amon Apache2::WebApp Web::Simple Apache2::REST SweetPea Hydrant Titanium
  27. 27. Pick one.Whichever you like.
  28. 28. Deploying a PSGI app?
  29. 29. HTTP::Server::PSGI CGI FastCGI mod_perl mod_perl2 Fastpass Starman Starlet TwiggyHTTP::Server::Simple::PSGI Feersum Corona mod_psgi evpsgi nginx_psgi Pelrbal uWSGIFCGI::EV AnyEvent::FCGI AnyEvent::HTTPD Prack/nack Mongrel2
  30. 30. Pick one.Whichever you like.
  31. 31. “I don’t know which I like the most.”
  32. 32. Some hints
  33. 33. Based on my experience and your experience
  34. 34. HTTP::Server::PSGI• Plack Built-in• Pure Perl• Single-process, cross platform• Experimental IPv6, SSL• Automatic reloading with -r
  35. 35. HTTP::Server::PSGI• Plack Built-in• Pure Perl• Single-process, cross platform• Experimental IPv6, SSL• Automatic reloading with -r Great for development and testing
  36. 36. HTTP::Server::Simple::PSGI• Zero non-core dependencies (other than HTTP::Server::Simple)• Single-process, extensible with H::S::S• Works on 5.005_*• Dancer’s default web server
  37. 37. HTTP::Server::Simple::PSGI• Zero non-core dependencies (other than HTTP::Server::Simple)• Single-process, extensible with H::S::S• Works on 5.005_*• Dancer’s default web server Great for embedding e.g. Android
  38. 38. CGI• Plack::Handler::CGI• Almost universally available• Quite slow and inefficient• Reasonably easy to run from the shell
  39. 39. CGI• Plack::Handler::CGI• Almost universally available• Quite slow and inefficient• Reasonably easy to run from the shell Last resort for Win32/IIS laptop web apps
  40. 40. mod_perl• Plack::Handler::Apache[12]• Lots of hooks and Apache modules• Porting from mod_perl to PSGI might be tricky. (There are some tools/attempts)
  41. 41. mod_perl• Plack::Handler::Apache[12]• Lots of hooks and Apache modules• Porting from mod_perl to PSGI might be tricky. (There are some tools/attempts) Not recommended unless you’ve hugely invested in mod_perl
  42. 42. Perlbal::Plugin::PSGI• Pure perl load balancer• LiveJournal, TypePad etc.• Reproxying, Plugins etc.• Danga::Socket (non-blocking)
  43. 43. Perlbal::Plugin::PSGI• Pure perl load balancer• LiveJournal, TypePad etc.• Reproxying, Plugins etc.• Danga::Socket (non-blocking) Considered EXPERIMENTAL
  44. 44. mod_psgi• Apache module to run PSGI apps• No mod_perl hooks• Just persistent PSGI apps• Simpler
  45. 45. mod_psgi• Apache module to run PSGI apps• No mod_perl hooks• Just persistent PSGI apps• Simpler Considered EXPERIMENTAL
  46. 46. nginx_mod_psgi• nginx native PSGI extension• https://github.com/yko/ngx_mod_psgi• Pretty new
  47. 47. nginx_mod_psgi• nginx native PSGI extension• https://github.com/yko/ngx_mod_psgi• Pretty new Considered EXPERIMENTAL
  48. 48. Mongrel2• Zed Shaw• ZeroMQ• Pretty cool stuff• lestrrat (Plack::Handler::Mongrel2)• jockway (AnyEvent::Mongrel2)
  49. 49. Mongrel2• Zed Shaw• ZeroMQ• Pretty cool stuff• lestrrat (Plack::Handler::Mongrel2)• jockway (AnyEvent::Mongrel2) Considered EXPERIMENTAL
  50. 50. Recommended Servers?
  51. 51. Standalone HTTP Servers
  52. 52. FastCGI daemons
  53. 53. Put it behind the frontend (nginx etc.)
  54. 54. Starman• Standalone HTTP• Prefork, app preloading• Dynamic process management• TCP/UNIX Sockets• Partial HTTP/1.1 support• Graceful restarts
  55. 55. Starman (cont.)• The Lacuna Expanse• perl.org• cpanratings.perl.org• etc.
  56. 56. Starlet• Standalone HTTP server• Prefork, app preloading• HTTP/1.0• Max/min spare workers• Hot deploy via Server::Starter
  57. 57. Starlet (cont.)• Livedoor• Hatena• etc.
  58. 58. Twiggy• Standalone HTTP server• AnyEvent based• Tiny memory footprint
  59. 59. Twiggy (cont.)• mixi• SAY Media• beta.metacpan.org
  60. 60. Feersum• Standalone HTTP server• EV.xs• picohttpparser (C library)• AnyEvent compatible• Lighting fast
  61. 61. Plack::Handler::FCGI• Standalone FastCGI daemon (or web server mode)• Preforking• TCP/UNIX sockets• FCGI.xs• Little bit of process management
  62. 62. fastpass• github.com/miyagawa/fastpass• Standalone FastCGI daemon• preforking, dynamic process management• Net::FastCGI (no XS)• TCP/UNIX sockets
  63. 63. FCGI::EV/AE::FCGI• Standalone FastCGI daemon• EV/AnyEvent based• asynchronous, non-blocking
  64. 64. uWSGI• FastCGI-like protocol• Optimized for *SGI• built-in nginx/cherokee support• AnyEvent support in the works• Lots of configurations• harakiri mode
  65. 65. uWSGI (cont.)• DotCloud.com (more server types coming!)
  66. 66. Proc. UNIX Graceful Proto, Prefork Async TCP/IP XS/Perl Mgmt. Sockets RestartsStarman HTTP ✔ ✔ ✔ ✔ ✔ XS Starlet HTTP ✔ ✔ ✔ ✔ XS/PP Twiggy HTTP ✔ ✔ ✔ XS/PPFeersum HTTP ✔ ✔ XSFastCGI FCGI ✔ ✔ ✔ ✔ XSfastpass FCGI ✔ ✔ ✔ ✔ ✔ PPAE::FCGI FCGI ✔ ✔ ✔ PPFCGI::EV FCGI ✔ ✔ ✔ XSuWSGI uWSGI ✔ ✔ ✔ ✔ ✔ C/XS
  67. 67. General guideline:
  68. 68. “Traditional” MVC apps?Lots of RDBMS queries?Coming from mod_perl?
  69. 69. Preforking servers Put it behind nginx.
  70. 70. Lots of I/O bound requests?New app or can rewrite to use AE? Resource limited environment?
  71. 71. Asynchronous servers Twiggy or Feersum.
  72. 72. HTTP or FastCGI?
  73. 73. HTTP Standalone☺• Put it behind frontend (nginx, lighttpd)• Most web servers support reverse proxy• As well as load balancers (perlbal, varnish)• Easy for human to debug and test• nginx + unix socket workers work well
  74. 74. HTTP Standalone ☹• You may have to configure X-Forwarded-* and ReverseProxy middleware• Keep-alives can be problematic in dumb servers like mod_proxy on apache2• Path mismatch on frontend/backend could cause headaches
  75. 75. FastCGI ☺• Many web servers support fastcgi• No need for X-Forwarded-* configuration• Potentially faster binary protocol between web server and app server• “Hot deploy” via UNIX sockets
  76. 76. FastCGI ☹• Binary protocol, may not be easy to debug• Requires C/XS (but see fastpass)• Hard to get SCRIPT_NAME/PATH_INFO right (e.g. lighttpd, mod_fcgid)
  77. 77. Questions/Suggestions?
  78. 78. 2)Code Deployment
  79. 79. Conventions = good.
  80. 80. Web app convention:
  81. 81. 1) PSGI endpoint
  82. 82. 2) CPAN dependencies
  83. 83. 1)
app.psgi2)
Makefile.PL
  84. 84. >
git
commit
‐m
‘releasing
v1.0.0’>
git
tag
v1.0.0>
git
push
‐‐tags>
(checking
out
code)>
cpanm
‐‐installdeps
.>
(restart
app
servers
gracefully)
  85. 85. cpanm --installdeps .
  86. 86. <3
  87. 87. Problem
  88. 88. Pulls the latest versions of CPAN today.
  89. 89. Many solutions:Shipwright, MyCPAN, orepan, DPAN
  90. 90. Cartoninspired by bundler
  91. 91. Carton• App-specific local environment (local::lib)• Dep-tree analysis, including versions.• Single-file,VCS friendly• Freezing/locking versions• Recreates mirror on deploy• Conservative Updates
  92. 92. >
carton
install>
git
commit
‐m
‘releasing
v1.0.0’




carton.lock
Makefile.PL>
git
tag
v1.0.0>
git
push
‐‐tags>
(checking
out
code)>
carton
install
>
(restart
app
servers
gracefully)
  93. 93. dotCloud will doall of this for you :) DISCLAIMER: I work for dotCloud.
  94. 94. github.com/miyagawa/carton
  95. 95. 3)Application Environment
  96. 96. Useful middleware for deployments
  97. 97. SizeLimit Port of Apache::SizeLimitSupported by Starman, Starlet, uWSGI
  98. 98. use
Plack::Builder;builder
{

enable
“SizeLimit”,



max_unshared_size_in_kb
=>
4096,



check_every_n_requests
=>
5;

$app;}
  99. 99. ServerStatus::Lite Port of mod_status Starlet, Starman
  100. 100. use
Plack::Builder;builder
{

enable
“ServerStatus::Lite”,



path
=>
‘/server‐status’;

$app;}
  101. 101. RuntimeAdds X-Runtime: header
  102. 102. use
Plack::Builder;builder
{

enable
“Runtime”;

$app;}
  103. 103. use
Plack::Builder;builder
{

enable
“Runtime”;

$app;}
  104. 104. %
curl
‐I
http://localhost:5000/HTTP/1.0
200
OKServer:
HTTP::Server::PSGIX‐Runtime:
0.00029
  105. 105. ErrorDocument Customize error pages 500/503/404 etc.
  106. 106. use
Plack::Builder;builder
{

enable
“ErrorDocument”,



500
=>
‘/home/www/errors/500.html’;

$app;};
  107. 107. ThrottleRate-limiting framework
  108. 108. use
Plack::Builder;builder
{

enable
“Throttle::Hourly”,




max
=>
200,



backend
=>
...,



path
=>
qr!^/api!;

$app;}
  109. 109. Expires Port of mod_expiresCache static files on proxy/clients
  110. 110. use
Plack::Builder;builder
{

enable
“Expires”,



content_type
=>
qr!^image/!,



expires
=>
‘access
plus
3
months’;

$app;};
  111. 111. Rewritemod_rewrite-ish URL rewriting pure perl syntax
  112. 112. use
Plack::Builder;builder
{

enable
‘Rewrite’,
rules
=>
sub
{



s{^/here}{/there};







return
301
if
s{^/foo/}{/bar/};



return
201
if
$_
eq
‘/favicon.ico’;



return
503
if
‐e
‘/tmp/maint.txt’;

};

$app;}
  113. 113. Auth::Basic/Digest Add Basic authenticationwith any Auth::Simple backends
  114. 114. use
Plack::Builder;use
Authen::Simple::Kerberos;my
$auth
=
Authen::Simple::Kerberos‐>new(

realm
=>
‘REALM.MYCOMPANY.COM’,);builder
{

enable
“Auth::Basic”,



authenticator
=>
$auth;

$app;};
  115. 115. PSGI Deployment Survey
  116. 116. ~100 responses Thank you!
  117. 117. What category describes your apps best?
  118. 118. Intranet 9% Personal 21%Small to mid-scale 49% Large scale 21%
  119. 119. Where is it hosted?
  120. 120. VPS Colocation Own Data Center Enterprise Home Cloud
  121. 121. Operating Systems?
  122. 122. Ubuntu Debian CentOS RHEL LinuxFreeBSDMac OS XWindows Solaris 0 10 20 30 40
  123. 123. Perl versions?
  124. 124. 604530150 “5.8” “5.10” “5.12” “5.14”
  125. 125. Web Frameworks?
  126. 126. (in-house) Catalyst Mojolicious DancerCGI::Application Tatsumaki Amon2 Mason 0 10 20 30 40
  127. 127. When did you migrate or start using PSGI?
  128. 128. 2009 Year 2010 Q2 2010 Q4 2011 Q2 Future
  129. 129. Which PSGI server?
  130. 130. Starman Starlet TwiggyPlack::Handler::FCGI mod_perl 2 HTTP::Server::PSGI H::S::Simple::PSGI CGI Feersum uWSGI mod_perl 1.3 0 15 30 45 60
  131. 131. FastCGI web servers mod_fcgid 8%lighttpd 20% nginx 40% mod_fastcgi 33%
  132. 132. Frontend web servers nginxmod_proxy perlbal Varnish lighttpd Squid
  133. 133. How did you measure the right # of servers and worker processes?
  134. 134. Guess Defaults 7% 6% by RAMby CPU 40% 19% by req. time 27%
  135. 135. How do you keeplogs of your apps?
  136. 136. Log4perldaemontools syslog logrotateLog::Dispatch Log::Minimal supervisor runit Others
  137. 137. Results will be posted onhttp://blog.plackperl.org/
  138. 138. One more thing...
  139. 139. REPLInteractiveDebugger
  140. 140. Thanks! Check out Booth 527for FREE DotCloud T-shirt

×