• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Deploying Plack Web Applications: OSCON 2011
 

Deploying Plack Web Applications: OSCON 2011

on

  • 16,181 views

 

Statistics

Views

Total Views
16,181
Views on SlideShare
13,785
Embed Views
2,396

Actions

Likes
39
Downloads
0
Comments
0

24 Embeds 2,396

http://blog.plackperl.org 1317
http://localhost 425
http://coderwall.com 398
http://sugyan.no.de 86
http://anainc.tumblr.com 54
http://b.ana.io 32
http://lanyrd.com 20
http://anaioco.tumblr.com 15
http://nuevospowerpoints.blogspot.com 9
http://a0.twimg.com 6
http://www.onlydoo.com 5
http://news.int80.biz 5
https://twitter.com 4
http://mundo-powerpoints.blogspot.com 4
http://b.hatena.ne.jp 3
http://twitter.com 2
http://strawberryj.am 2
http://leapf.org 2
https://localhost 2
http://us-w1.rockmelt.com 1
http://chat.usealice.org 1
http://slideclip.b-prep.com 1
http://www.redditmedia.com 1
http://www.techgig.com 1
More...

Accessibility

Categories

Upload Details

Uploaded via as Apple Keynote

Usage Rights

CC Attribution License

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n

Deploying Plack Web Applications: OSCON 2011 Deploying Plack Web Applications: OSCON 2011 Presentation Transcript

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



my
$env
=
shift;



return
[








200,







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







[
‘Hello
World’
],



];};
  • 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
  • Mason CGI::Application Jifty CatalystMason::CGIHandler CGI.pm Catalyst::Engine mod_perl CGI fastcgi HTTP::Server ::Simple Apache IIS lighttpd nginx
  • 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
  • Momentum
  • Catalyst 5.9Built-in PSGI/Plack support
  • CGI::Application 4.5 Built-in PSGI support
  • Dancer, Mojolicious
  • 20+Web Frameworks
  • 25+Web Servers/Handlers
  • 150+Plack Middleware
  • Cloud (PaaS)
  • Still not convinced?
  • “FastCGI is good enough”? http://xrl.us/psgireddit
  • Deploying PlackWeb applications
  • 1)Server Environment
  • 2)Code Deployment
  • 3)Application Environment
  • 1)Server Environment
  • Writing a PSGI app?
  • 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
  • Pick one.Whichever you like.
  • Deploying a PSGI app?
  • 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
  • Pick one.Whichever you like.
  • “I don’t know which I like the most.”
  • Some hints
  • Based on my experience and your experience
  • HTTP::Server::PSGI• Plack Built-in• Pure Perl• Single-process, cross platform• Experimental IPv6, SSL• Automatic reloading with -r
  • HTTP::Server::PSGI• Plack Built-in• Pure Perl• Single-process, cross platform• Experimental IPv6, SSL• Automatic reloading with -r Great for development and testing
  • 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
  • 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
  • CGI• Plack::Handler::CGI• Almost universally available• Quite slow and inefficient• Reasonably easy to run from the shell
  • 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
  • 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)
  • 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
  • Perlbal::Plugin::PSGI• Pure perl load balancer• LiveJournal, TypePad etc.• Reproxying, Plugins etc.• Danga::Socket (non-blocking)
  • Perlbal::Plugin::PSGI• Pure perl load balancer• LiveJournal, TypePad etc.• Reproxying, Plugins etc.• Danga::Socket (non-blocking) Considered EXPERIMENTAL
  • mod_psgi• Apache module to run PSGI apps• No mod_perl hooks• Just persistent PSGI apps• Simpler
  • mod_psgi• Apache module to run PSGI apps• No mod_perl hooks• Just persistent PSGI apps• Simpler Considered EXPERIMENTAL
  • nginx_mod_psgi• nginx native PSGI extension• https://github.com/yko/ngx_mod_psgi• Pretty new
  • nginx_mod_psgi• nginx native PSGI extension• https://github.com/yko/ngx_mod_psgi• Pretty new Considered EXPERIMENTAL
  • Mongrel2• Zed Shaw• ZeroMQ• Pretty cool stuff• lestrrat (Plack::Handler::Mongrel2)• jockway (AnyEvent::Mongrel2)
  • Mongrel2• Zed Shaw• ZeroMQ• Pretty cool stuff• lestrrat (Plack::Handler::Mongrel2)• jockway (AnyEvent::Mongrel2) Considered EXPERIMENTAL
  • Recommended Servers?
  • Standalone HTTP Servers
  • FastCGI daemons
  • Put it behind the frontend (nginx etc.)
  • Starman• Standalone HTTP• Prefork, app preloading• Dynamic process management• TCP/UNIX Sockets• Partial HTTP/1.1 support• Graceful restarts
  • Starman (cont.)• The Lacuna Expanse• perl.org• cpanratings.perl.org• etc.
  • Starlet• Standalone HTTP server• Prefork, app preloading• HTTP/1.0• Max/min spare workers• Hot deploy via Server::Starter
  • Starlet (cont.)• Livedoor• Hatena• etc.
  • Twiggy• Standalone HTTP server• AnyEvent based• Tiny memory footprint
  • Twiggy (cont.)• mixi• SAY Media• beta.metacpan.org
  • Feersum• Standalone HTTP server• EV.xs• picohttpparser (C library)• AnyEvent compatible• Lighting fast
  • Plack::Handler::FCGI• Standalone FastCGI daemon (or web server mode)• Preforking• TCP/UNIX sockets• FCGI.xs• Little bit of process management
  • fastpass• github.com/miyagawa/fastpass• Standalone FastCGI daemon• preforking, dynamic process management• Net::FastCGI (no XS)• TCP/UNIX sockets
  • FCGI::EV/AE::FCGI• Standalone FastCGI daemon• EV/AnyEvent based• asynchronous, non-blocking
  • uWSGI• FastCGI-like protocol• Optimized for *SGI• built-in nginx/cherokee support• AnyEvent support in the works• Lots of configurations• harakiri mode
  • uWSGI (cont.)• DotCloud.com (more server types coming!)
  • 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
  • General guideline:
  • “Traditional” MVC apps?Lots of RDBMS queries?Coming from mod_perl?
  • Preforking servers Put it behind nginx.
  • Lots of I/O bound requests?New app or can rewrite to use AE? Resource limited environment?
  • Asynchronous servers Twiggy or Feersum.
  • HTTP or FastCGI?
  • 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
  • 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
  • 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
  • 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)
  • Questions/Suggestions?
  • 2)Code Deployment
  • Conventions = good.
  • Web app convention:
  • 1) PSGI endpoint
  • 2) CPAN dependencies
  • 1)
app.psgi2)
Makefile.PL
  • >
git
commit
‐m
‘releasing
v1.0.0’>
git
tag
v1.0.0>
git
push
‐‐tags>
(checking
out
code)>
cpanm
‐‐installdeps
.>
(restart
app
servers
gracefully)
  • cpanm --installdeps .
  • <3
  • Problem
  • Pulls the latest versions of CPAN today.
  • Many solutions:Shipwright, MyCPAN, orepan, DPAN
  • Cartoninspired by bundler
  • 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
  • >
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)
  • dotCloud will doall of this for you :) DISCLAIMER: I work for dotCloud.
  • github.com/miyagawa/carton
  • 3)Application Environment
  • Useful middleware for deployments
  • SizeLimit Port of Apache::SizeLimitSupported by Starman, Starlet, uWSGI
  • use
Plack::Builder;builder
{

enable
“SizeLimit”,



max_unshared_size_in_kb
=>
4096,



check_every_n_requests
=>
5;

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

enable
“ServerStatus::Lite”,



path
=>
‘/server‐status’;

$app;}
  • RuntimeAdds X-Runtime: header
  • use
Plack::Builder;builder
{

enable
“Runtime”;

$app;}
  • use
Plack::Builder;builder
{

enable
“Runtime”;

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

enable
“ErrorDocument”,



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

$app;};
  • ThrottleRate-limiting framework
  • use
Plack::Builder;builder
{

enable
“Throttle::Hourly”,




max
=>
200,



backend
=>
...,



path
=>
qr!^/api!;

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

enable
“Expires”,



content_type
=>
qr!^image/!,



expires
=>
‘access
plus
3
months’;

$app;};
  • Rewritemod_rewrite-ish URL rewriting pure perl syntax
  • 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;}
  • Auth::Basic/Digest Add Basic authenticationwith any Auth::Simple backends
  • use
Plack::Builder;use
Authen::Simple::Kerberos;my
$auth
=
Authen::Simple::Kerberos‐>new(

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

enable
“Auth::Basic”,



authenticator
=>
$auth;

$app;};
  • PSGI Deployment Survey
  • ~100 responses Thank you!
  • What category describes your apps best?
  • Intranet 9% Personal 21%Small to mid-scale 49% Large scale 21%
  • Where is it hosted?
  • VPS Colocation Own Data Center Enterprise Home Cloud
  • Operating Systems?
  • Ubuntu Debian CentOS RHEL LinuxFreeBSDMac OS XWindows Solaris 0 10 20 30 40
  • Perl versions?
  • 604530150 “5.8” “5.10” “5.12” “5.14”
  • Web Frameworks?
  • (in-house) Catalyst Mojolicious DancerCGI::Application Tatsumaki Amon2 Mason 0 10 20 30 40
  • When did you migrate or start using PSGI?
  • 2009 Year 2010 Q2 2010 Q4 2011 Q2 Future
  • Which PSGI server?
  • 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
  • FastCGI web servers mod_fcgid 8%lighttpd 20% nginx 40% mod_fastcgi 33%
  • Frontend web servers nginxmod_proxy perlbal Varnish lighttpd Squid
  • How did you measure the right # of servers and worker processes?
  • Guess Defaults 7% 6% by RAMby CPU 40% 19% by req. time 27%
  • How do you keeplogs of your apps?
  • Log4perldaemontools syslog logrotateLog::Dispatch Log::Minimal supervisor runit Others
  • Results will be posted onhttp://blog.plackperl.org/
  • One more thing...
  • REPLInteractiveDebugger
  • Thanks! Check out Booth 527for FREE DotCloud T-shirt