Download, install and configure perlbal to load balance web server
Perlbal is fast and efficient web server, reverse proxy(load balancer). Here are quick steps to get started with it. I have tested perlbal-1.60 on my CentOS 5 box. There are many other possible ways to do the same and the way which worked for me, may not work for you.
Step 1. Download perlbal OR install it via perl cpan, like this:
$ perl -MCPAN -e shell cpan-> install perlbal
Step 2. Find out its sample config (/root/.cpan/build/Perlbal-1.60/doc/config-guide.txt) or if you downloaded and compiled it, file will be there. Put this file in /etc/perlbal as perlbal.conf.
$ mkdir /etc/perlbal $ cp /root/.cpan/build/Perlbal-1.60/doc/config-guide.txt /etc/perlbal/perlbal.conf
Step 3. Update the perlbal.conf file as per your requirements.
for example, we are using it as load balancer, here is sample config
$ vi /etc/perlbal/perlbal.conf CREATE POOL my_apaches SET nodefile = conf/nodelist.dat # IP of backend Apache servers. CREATE SERVICE balancer SET listen = 0.0.0.0:80 SET role = reverse_proxy SET pool = my_apaches SET persist_client = on SET persist_backend = on SET verify_backend = on ENABLE balancer # Keep an internal management port open to reconfigure pool automatically via telnet CREATE SERVICE mgmt SET role = management SET listen = 127.0.0.1:60000 ENABLE mgmt
Step 4. Start perlbal module as daemon.
$ perlbal -dStep 5. Test by connecting through management port
$ telnel 127.0.0.1 60000Step 6. One major concern is that the backend servers will log entries with IP address of load balancer instead of actual user’s IP. To overcome this issue, install and configure mod_rpaf for apache at backend servers. Login in backend server and install the module:
$ cd /usr/src/ $ wget http://stderr.net/apache/rpaf/download/mod_rpaf-0.6.tar.gz $ tar xzf mod_rpaf-0.6.tar.gz $ cd mod_rpaf-0.6 $ apxs -i -c -n mod_rpaf-2.0.so mod_rpaf-2.0.c $ vi /etc/httpd/conf/httpd.conf LoadModule rpaf_module modules/mod_rpaf-2.0.so RPAFenable On RPAFsethostname On RPAFproxy_ips 127.0.0.1 192.168.0.1 ## replace your IP of load balancer RPAFheader X-Forwarded-For $ service httpd restart
Testing
Step 1. Create a simple perl file and put it in cgi-bin directory of all backend servers.
$ vi /var/www/cgi-bin/test.pl #!/usr/bin/perl print "Content-Type: text/html\n\n"; print " Web server load balance testing "; print "Web Server running fine."; # Fetch IP of server my $ifout =`ifconfig eth0 | grep inet`; $ifout =~ m/\s+inet\saddr:([\d\.]+)\s.+/g; print "This page is fetched from: $1 Server.";
Step 2. Start testing from browser and try to access the perl script from your load balancer server. Add more backends (by updating perlbal.conf) and test again. As per the load you will notice that perl script will be fetched from different back ends.
Adding / removing backends on the fly
Step 1. To maximum utilize the load balanced environment, there should be some technique by which backend servers can be added or removed on fly in the pool of perlbal as per the load. The topic of measuring load of backend servers and then making right decision is beyond the scope of this post. I developed some perl scripts to achieve the same in Amazon EC2[aws.amazon.com/ec2] environment, where we can create/remove servers on fly. If you are also on EC2, just post a comment or send mail to me and I will happily give scripts to you. Updating perlbal using a perl script with the help of Net::Telnet module is very easy. Here’s sample code:
use Net::Telnet; $telnet=new Net::Telnet(Host=>$loadbalancerIP,Port=>60000,Timeout=>20, Errmode=>'Die'); $telnet->print("pool my_apaches add $newserverIP"); $telnet->waitfor('/OK/i'); $telnet->close;
Its Done. Play with it. One of the noticed minus point (as of ver 1.60), perlbal is not so efficient/capable while handling https connections. In case you have website which doesnt require https connections, perlbal should be given preferrence.
An alternate to perlbal is haproxy load balancer, I’ve covered it here, here and here as well.
You may also like to read:
* Install and configure haproxy load balancer, lightweight and fast alternative of perlbal/apache proxy.
* Enable or fix logging for Haproxy or perlbal load balancer.
* Install and setup haproxy load balancer for content switching.
* 5 steps to secure your Linux Server
* Ensuring secure access to production Linux Servers
* Bash script to backup essential log files in Linux
Hi. Do you still have these scripts for adding and removing in an ec2 setup? And are them still relevant to Eucalyptus?
sic
Every posting I have checked out is very nicely composed and to the point. I might moreover want to express, not simply tend to be your articles or blog posts well written, but that structure of your web site is great. It was straightforward to find the way from content to write-up and identify exactly what I was seeking for with ease. Hold up the excellent job you are working on, and I will be return quite a few times in the near future.
Thanks Leonardo, appreciate your comment. I’m glad you find articles here helpful for you.
Jagbir
this is very useful.thank you.can you help me with the dynamic load balancing in amzons ec2? I am currently working on dynamic load balancing for webserice in ec2. thanks in advance…
I checked out something very much the same to this post over at google news… I was interested and then began searching around, then somehow landed at this page… at any rate, I believe that I mostly agree with what you cover here. However I am going to go see what additional information I can look up too.
;;” I am really thankful to this topic because it really gives up to date information `..
hi Jagbir
really useful article, especially for those who don’t want to use Amazon’s ELB. If you still have those scripts I’d really appreciate a copy?
thanks
M
Hi mbconn, unfortunately it has been long time around, I need to check where scripts are and will surely forward you in case I found them.
btw, you can also have a look at haproxy here: http://linuxadminzone.com/install-and-configure-haproxy-the-software-based-loadbalancer-in-ubuntu/
which I found better than perlbal. I’m very comfortable in haproxy so you can divert query if any.
Regards
Jagbir