Thursday, March 14, 2013

CentOS: Configure Piranha as Load Balancer (Direct Routing Method)

I am currently working on a web cluster project using CentOS. In this project, I have 2 web servers running on Apache and mounted the same document root to serve the HTTP content. I also have 2 servers in front of it to become the load balancer and failover to increase high availability of the two-node web server cluster. The virtual IP will be hold by load balancer #1 with auto failover to load balancer #2.
You may refer to diagram below to get clearer picture:
I am using following variables:
All servers’ OS: CentOS 6.2 64bit
Web server #1:
Web server #2:
Load balancer #1:
Load balancer #2:
Virtual IP:

Load Balancer Server

1. All steps should be done in both servers unless specified. We will install Piranha and other required packages using yum:
$ yum install piranha ipvsadm -y
2. Open firewall ports as below:
  • Piranha: 3636
  • HTTP: 80
  • Hearbeat: 539
3. Start all required services and make sure they will auto start if server reboot:
$ service piranha-gui start
$ chkconfig piranha-gui on
$ chkconfig pulse on
4. Run following command to set password for user piranha. This will be used when accessing the web-based configuration tools:
$ piranha-passwd
5. Turn on IP forwarding. Open /etc/sysctl.conf and make sure following line has value 1:
net.ipv4.ip_forward = 1
And run following command to activate it:
$ sysctl -p

Load Balancer #1

1. Open Piranha web-based configuration tools at and login as piranha with respective password. We start with configuring Global Settings as below:
2. Then, go to the Redundancy tab and enter the secondary server IP. In this case, we will put load balancer #2 IP as the redundant server in case load balancer #1 is down:
3. Under Virtual Servers tab, click Add and enter required information as below:
4. Now we need to configure the virtual IP and virtual HTTP server to map into the real HTTP server. Go toVirtual Servers > Real Server and add into the list as below:
Make sure you activate the real server once the adding completed by clicking the (DE)ACTIVATE button.
5.  Now copy the configuration file to load balancer #2 to as below:
$ scp /etc/sysconfig/ha/lvs.conf
6. Restart Pulse service to apply the new configuration:
$ service pulse restart
You can monitor what is happening with Pulse by tailing the /var/log/message output as below:
$ tail -f /var/log/message

Load Balancer #2

No need to configure anything in this server. We just need to restart Pulse service to get affected with the new configuration changes which being copied over from LB1.
$ service pulse restart
If you see the /var/log/message, pulse in this server will report that it will run on BACKUP mode.

Web Servers

1. Since we are using direct-routing method, regards to your Apache installation, we also need to install another package called arptables_jf. Here is some quote from RedHat documentation page:
Using the arptables_jf method, applications may bind to each individual VIP or port that the real server is servicing. For example, the arptables_jf method allows multiple instances of Apache HTTP Server to be running bound explicitly to different VIPs on the system. There are also significant performance advantages to usingarptables_jf over the IPTables option.
However, using the arptables_jf method, VIPs can not be configured to start on boot using standard Red Hat Enterprise Linux system configuration tools.
We will instsall using yum:
$ yum install arptables_jf -y
2. Configure arptables_jf by executing following command:
In web server #1:
$ arptables -A IN -d -j DROP
$ arptables -A IN -d -j mangle --mangle-ip-s
In web server #2:
$ arptables -A IN -d -j DROP
$ arptables -A IN -d -j mangle --mangle-ip-s
3.  Save the arptables rules and make sure the service is started on boot:
$ service arptables_jf save
$ chkconfig arptables_jf on
4.  Add the virtual IP address in the servers:
$ ip addr add dev eth0
5. Since the IP cannot be started during sysinit (boot time), we can automatically start the IP after sysinit complete. Open /etc/rc.local using text editor:
$ vim /etc/rc.local
And add following line:
/sbin/ip addr add dev eth0
Warning: Every time you restart your network service, please make sure to run step #4 to bring up the virtual IP in real server.
Done. You can now point your website to the virtual IP and you will see that the load balancer #1 will report as below:
$ ipvsadm -L
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP lblc
> Route 1 0 34
> Route 1 0 19


Post a Comment