How To Setup A Load Balancer

I’ve finally gotten to writting a new tutorial after a while without posting any new content. Also, in case you haven’t noticed, I have re-designed the site, so hopefully you’ll be able to enjoy it even more. In this tutorial, I’ll show you how to setup a load balancer running on linux, with CentOS as the operating system of choice for this tutorial.
A load balancer distributes requests for services accross multiple servers, and in this tutorial I’ll be walking you through setting up crossroads load balancer.

Crossroads is an open source load balancer and fail over utility for TCP based services. Crossroads can be downloaded from here

First things first, you’ll need to install the required dependencies:

yum install gcc make
yum groupinstall "Development Tools"
yum install yum-utils

After all dependencies have been installed, reboot.

Now lets create a folder where we’ll download crossroads sources:

mkdir /opt/crossroads
cd /opt/crossroads
wget http://crossroads.e-tunity.com/downloads/crossroads-stable.tar.gz

Unpack the archive and create a symlink in sources directory, e.g., /usr/local/src/

tar xzvf crossroads-stable.tar.gz
ln -s /opt/crossroads/crossroads-2.74 /usr/local/src/crossroads

Go to the created directory /usr/local/src/crossroads/

cd /usr/local/src/crossroads/

Type make install, this compiles crossroads and installs it into /usr/sbin/

make install

Crossroads is now installed. For this tutorial I’ll setup crossroads to manage traffic for mysql and apache, but you can add as many servers and services you’d like. Here’s our sample information:

mysql server 1 — 192.168.1.10
mysql server 2 — 192.168.1.20
apache server 1 server 1 (site 1)– 192.168.1.30
apache server 2 server 1 (site 2)– 192.168.1.40
apache server 1 (site 2) — 192.168.2.30
apache server 2 (site 2) — 192.168.2.40

Create a startup script in /opt/startCrossroads.sh and make it executable:

#!/bin/bash
## Load balance Mysql
/usr/sbin/xr --verbose --server tcp:0:3306 --backend 192.168.1.10:3306 --backend 192.168.1.20:3306 2>&1 >> /var/log/xr-web.log &
## Load balance APACHE
/usr/sbin/xr --verbose -S --server http:0:80 --host-match www.site1.com --backend 192.168.1.30:80 --backend 192.168.1.40:80 \
--host-match www.site2.com --backend 192.168.2.30:80 --backend 192.168.2.40:80 2>&1 >> /var/log/xr-web.log &

Alternatively, you can also manage your http traffic using tcp (same way as we did mysql), this is how it would look like:

/usr/sbin/xr --verbose --server tcp:0:80 --backend 192.168.1.30:80 --backend 192.168.1.40:80 --backend 192.168.2.30:80 --backend 192.168.2.40:80 2>&1 >> /var/log/xr-web.log &

Now make the script to start crossroads executable

chmod +x /opt/startCrossroads.sh

Create symlink in /usr/bin:

ln -s /opt/startCrossroads.sh /usr/bin/startCrossroads.sh

Add the line below to /etc/rc.local so it’ll start at boot time

#Start crossroads load balancer
/usr/bin/startCrossroads.sh

Start the load balancer:

/usr/bin/startCrossroads.sh

Open port 3306 and 80 on firewall and whichever other services will be forwarded to the load balancer, then forward incoming traffic to port 80 and 3306 (any other services crossroads is handling) to the crossroads ip address.

Congratulations, you have now setup your very own load balancer. Thanks for stopping by once again and remember, please share with others, after all, code should be free.foscode.com | because code should be free

4 thoughts on “How To Setup A Load Balancer

  1. @tquangbut Thanks for pointing it out, unfortunately wordpress does this sometimes, hopefully it’s all correct now.

  2. You can click the view source button, to the right of the code window and it will open up on a new window, without any strange formatting or meta errors

  3. Pingback: How To Setup Linux High Availability - foscode

Leave a Reply

Your email address will not be published. Required fields are marked *

*


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>