Load Balancing with HAProxy, Full Tutorial

Load Balancing using HaProxy.

Writeen by Kendy Hikaru - Nam Habach
kendyhikaru.blogspot.com

Step 1: Basic config:

#cat /etc/hosts

# Do not remove the following line, or various programs
# that require network functionality will fail.
127.0.0.1 localhost.localdomain localhost
::1 localhost6.localdomain6 localhost6

##### IP's of HaProxy #####
192.168.44.130 server.hbn.local server

##### IP's of Web Servers #####
192.168.44.131 www1.hbn.local www1
192.168.44.132 www2.hbn.local www2

##### IP's of MySQL DB servers #####
192.168.44.135 db.hbn.local db

save and quit

#scp /etc/hosts www1:/etc/

#scp /etc/hosts www2:/etc/

#scp /etc/hosts db:/etc/

####################################################################################################
Step 2: Configurations

On 2 Web Servers:

#yum install -y httpd php php-mysql php-gd

#service httpd start

#chkconfig --level 35 httpd on
web1:

#echo "

www1.demo.com

" > /var/www/html/index.html

web2:

#echo "

www2.demo.com

" > /var/www/html/index.html

On LB:

#rpm -Uvh haproxy-1.3.14.6-1.el5.i386.rpm

#vim /etc/haproxy/haproxy.cfg

global
log 127.0.0.1 local0
log 127.0.0.1 local1 notice

#log loghost local0 info
maxconn 4096

#debug

#quiet
user haproxy
group haproxy

defaults
log global
mode http
option httplog
option dontlognull
retries 3
redispatch
maxconn 2000
contimeout 5000
clitimeout 50000
srvtimeout 50000

listen webfarm 192.168.44.130:80
mode http
stats enable
stats auth admin:123456
balance roundrobin
cookie JSESSIONID prefix
option httpclose
option forwardfor
option httpchk HEAD /check.txt HTTP/1.0
server sv1 192.168.44.131:80 cookie A weight 50 check
server sv2 192.168.44.132:80 cookie B weight 50 check

save and quit

#service haproxy start

#chkconfig --level 35 haproxy on

Admin:
192.168.1.100/haproxy?stats
Check (down)
On web1, web2:

#echo "OK" > /var/www/html/check.txt
Check again ( up)

On DB:

#yum install -y mysql-server

#vim my.cf

bind-address = 192.168.44.135

#mysqladmin -u root password 123456

#mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.0.77 Source distribution

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql> create database joomla;
Query OK, 1 row affected (0.02 sec)

mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| joomla |
| mysql |
| test |
+--------------------+
4 rows in set (0.01 sec)

mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'192.168.44.131' IDENTIFIED BY '123456';
Query OK, 0 rows affected (0.00 sec)
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'192.168.44.132' IDENTIFIED BY '123456';
Query OK, 0 rows affected (0.00 sec)
mysql> exit;
Bye

Test on 2 web server:

#mysql -u root -p -h 192.168.44.150
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.0.77 Source distribution

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| joomla |
| mysql |
| test |
+--------------------+
4 rows in set (0.00 sec)
mysql> exit;
Bye

####################################################################################################
Step 4: Install Joomla
Copy joomla code, extract it

#unzip Joomla_1.5.22-Stable-Full_Package.zip

#mkdir -p /var/www/html/web

#cp -r * /var/www/html/web/

#cd /var/www/html

#chown -R apache:apache web/

Install web:

Then:

#cd /var/www/html/web
# scp -r * www2:/var/www/html/web
or:

#cd /var/www/html/

#tar cvf web.tar web/

#scp web.tar www2:/var/www/html/
On web 2:

#tar xvf web.tar

#chown -R apache:apache web/
Finish
Videos Demo:

Part 1:

Part 2:

Or:
Part 1: http://www.mediafire.com/?34ca3o150d845gz
Part 2: http://www.mediafire.com/?gy6815vunyz3kia

------------------------------------------------------------
Thanks for reading
--------------------------------------------------------------------------
All my Lab:
Linux Lab -- window and Cisco Lab
to be continued - I will update more.

Nam Habach