Posted by
arungupta on June 17, 2009 at 11:06 AM PDT
TOTD
#81 explained how to install/configure
nginx for
load-balancing/front-ending a cluster of Rails application running on
GlassFish
Gem. Another popular approach in the Rails community is to
use
Apache HTTPD
+
mod_proxy_balancer.
A user asked the exact details of this
setup on the
GlassFish
Gem Forum. This
Tip
Of
The
Day (TOTD) will
clearly explain the steps.
- Create a simple Rails scaffold and run this application
using GlassFish Gem on 3 separate ports as explained in TOTD
#81.
- Setup and configure HTTPD and mod_proxy_balancer
- Setup and install Apache HTTPD as explained here.
I believe mod_proxy_balancer
and other related modules comes pre-bundled with HTTPD, at least that's
what I observed with Mac OS X 10.5.7. Make sure that the
"mod_proxy_balancer" module is enabled by verifying the following line
is uncommented in "/etc/apache2/httpd.conf":
| LoadModule proxy_balancer_module
libexec/apache2/mod_proxy_balancer.so |
Please note another similar file exists in "/etc/httpd/httpd.conf" but
ignore that one.
- Setup a mod_proxy_balancer cluster by adding the
following fragment in "httpd.conf" as:
The port numbers must exactly match with those used in the first step.
- Specify the ProxyPass directives
to map the cluster to a local path as:
ProxyPass / balancer://glassfishgem/
CustomLog /var/log/glassfishgem.log/apache_access_log combined |
The "/" at the end of "balancer://glassfishgem" is very important to
ensure that all the files are resolved correctly.
- Optionally, the following directive can be added to view
the access log:
| CustomLog
/var/log/glassfishgem.log/apache_access_log combined |
Make sure to create
the directory specified in "CustomLog" directive.
- Now the application is accessible at
"http://localhost/runlogs". If a new GlassFish instance is started then
update the <Proxy> directive and restart your HTTPD as
"sudo httpd -k restart". Dynamic update of BalancerMembers can be
configured as explained here.
TOTD
#81 started the Rails application in root context. You can
alternatively start the application in a non-root context as:
~/tools/jruby/rails/runner >../../bin/jruby -S glassfish -e
production -c myapp
Starting GlassFish server at: 10.0.177.178:3000 in production
environment...
Writing log messages to:
/Users/arungupta/tools/jruby-1.3.0/rails/runner/log/production.log.
Press Ctrl+C to stop.
. . .
~/tools/jruby/rails/runner >../../bin/jruby
-S glassfish -e production -c myapp -p 3001
Starting GlassFish server at: 10.0.177.178:3001 in production
environment...
Writing log messages to:
/Users/arungupta/tools/jruby-1.3.0/rails/runner/log/production.log.
Press Ctrl+C to stop.
. . .
~/tools/jruby/rails/runner >../../bin/jruby
-S glassfish -e production -c myapp -p 3002
Starting GlassFish server at: 10.0.177.178:3002 in production
environment...
Writing log messages to:
/Users/arungupta/tools/jruby-1.3.0/rails/runner/log/production.log.
Press Ctrl+C to stop. |
and then the ProxyPass directive will change to:
ProxyPass /myapp/
balancer://glassfishgem/myapp/
|
The changes are highlighted in bold. And the application is now
accessible at "http://localhost/myapp/runlogs".
After discussing on
Apache
HTTP Server forum, the BalancerMember host/port
can be printd in the log file using a custom log format. So add the
following log format to "/etc/apache2/httpd.conf":
| LogFormat "%h %l %u %t \"%r\" %>s %b
\"%{Referer}i\" \"%{User-agent}i\"
\"%{BALANCER_WORKER_NAME}e\"" custom |
And change the format from the default "combined" to the newly defined
"custom" format as:
| CustomLog /var/log/glassfishgem.com/apache_access_log
custom |
Three subsequent invocations of "http://localhost/runlogs" then prints
the following log entries:
::1 - - [17/Jun/2009:10:53:53 -0700] "GET /runlogs
HTTP/1.1" 304 - "-" "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5;
en-US; rv:1.9.0.11) Gecko/2009060214 Firefox/3.0.11"
"http://localhost:3002"
::1 - - [17/Jun/2009:10:54:04 -0700] "GET /runlogs HTTP/1.1" 200 621
"-" "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US;
rv:1.9.0.11) Gecko/2009060214 Firefox/3.0.11" "http://localhost:3000"
::1 - - [17/Jun/2009:10:54:05 -0700] "GET /runlogs HTTP/1.1" 304 - "-"
"Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.0.11)
Gecko/2009060214 Firefox/3.0.11" "http://localhost:3001" |
As evident from the last fragment of each log line, the load is
distributed amongst three GlassFish Gem instances. More details on load
balancer algorithm are available
here.
Feel free to drop a comment on this blog if you are using
GlassFish in
production for your Rails applications. Several stories are already
available at
rubyonrails+glassfish+stories.
Technorati: glassfish
rubyonrails
apache
httpd
mod_proxy_balancer
loadbalancing
clustering