Hello guys,
The purpose of this post is create a high availability load balancer for http traffic (on Red Hat)), it should be the same proccess with a few changes for other types of traffic. The following image is the structure of the
We should follow all the steps in both servers. The only difference between them is in conf file of Keepalived where we must set backup or master.
1.- Install HAproxy and Keepalive.
rpm -ivh http://ftp.astral.ro/mirrors/fedora/pub/epel/6/x86_64/epel-release-6-7.noarch.rpm
yum -y install haproxy keepalived
2.- Configure Keepalived
mv /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bak
nano /etc/keepalived/keepalived.conf
vrrp_script chk_haproxy { # Requires keepalived-1.1.13
script "killall -0 haproxy" # cheaper than pidof
interval 2 # check every 2 seconds
weight 2 # add 2 points of prio if OK
} vrrp_instance VI_1 {
interface eth0
state MASTER
virtual_router_id 51
priority 100 # 101 on master, 100 on backup
virtual_ipaddress {
ip_virtual #change for the virtual IP
}
track_script {
chk_haproxy
}
}
3.-Now we need to configure the system to allow HAProxy to access shared virtual IP addresses. First make a backup of the sysctl.conf file:
nano /etc/sysctl.conf
net.ipv4.ip_nonlocal_bind = 1
4.- Let’s check we are doing well:
service keepalived start
ip addr sh eth0
eth0: <BROADCAST,MULTICAST,UP,10000> mtu 1500 qdisc pfifo_fast qlen 1000
link/ether xx:xx:xx:xx:xx:xx brd ff:ff:ff:ff:ff:ff inet xx.xx.xx.xx/xx brd xx.xx.xx.xx scope global eth0 inet xx.xx.xx.xx/xx scope global eth0 inet6 xxxx::xxx:xxxx:xxx:xxxx/xx scope link valid_lft forever preferred_lft forever
5.- Configure HAproxy.
mv /etc/haproxy/haproxy.cfg /etc/haproxy/haproxy.cfg.bak
nano /etc/haproxy/haproxy.cfg
global
log 127.0.0.1 local0
log 127.0.0.1 local1 notice chroot /var/lib/haproxy pidfile /var/run/haproxy.pid maxconn 4096 user haproxy group haproxy daemon stats socket /var/lib/haproxy/stats defaults mode http log global option httplog option dontlognull option http-server-close option forwardfor except 127.0.0.0/8 option redispatch retries 3 timeout http-request 10s timeout queue 1m timeout connect 10s timeout client 1m timeout server 1m timeout http-keep-alive 10s timeout check 10s maxconn 3000 listen smartcloudtest.com xx.xx.xx.xx:80 mode http stats enable stats auth user:password #We should set an user and password in order to see the stats balance roundrobin cookie JSESSIONID prefix option httpclose option forwardfor option httpchk HEAD /check.txt HTTP/1.0 #We should create this field in the Rootdirectory of the web server in each web server. server webA xx.xx.xx.xx:80 cookie A check server webB xx.xx.xx.xx:80 cookie B check
6.- Set up the start-up
chkconfig haproxy onchkconfig keepalived onservice haproxy start
7.- Modify webservers conf. Comment the LogFormat line in httpd.conf and add the new one.
nano /usr/local/apache2/conf/httpd.conf
#LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined LogFormat "%{X-Forwarded-For}i %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
Comment all the CustomLog lines and the following lines, in the virtual host definition:
SetEnvIf Request_URI "^/check\.txt$" dontlog CustomLog /var/log/apache2/access.log combined env=!dontlog
Create in the rootdirectory the file check.txt
touch /usr/local/apache2/htdocs/check.txt
service httpd restart
I hope it will be usefull for you.
Source:
http://www.howtoforge.com/haproxy_loadbalancer_debian_etch

cool story bro