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

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 —
mysql server 2 —
apache server 1 server 1 (site 1)–
apache server 2 server 1 (site 2)–
apache server 1 (site 2) —
apache server 2 (site 2) —

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

## Load balance Mysql
/usr/sbin/xr --verbose --server tcp:0:3306 --backend --backend 2>&1 >> /var/log/xr-web.log &
## Load balance APACHE
/usr/sbin/xr --verbose -S --server http:0:80 --host-match --backend --backend \
--host-match --backend --backend 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 --backend --backend --backend 2>&1 >> /var/log/xr-web.log &

Now make the script to start crossroads executable

chmod +x /opt/

Create symlink in /usr/bin:

ln -s /opt/ /usr/bin/

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

#Start crossroads load balancer

Start the load balancer:


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 | because code should be free

3 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

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>