L7 LB with HAProxy

HaProxy is a free and reliable software solution for high availability and load-balancing for TCP and HTTP applications. HaProxy has been used many Fortune companies for a long time. It has proved its reliability. In this post I will show you how to create Layer 7 Load-Balancing. You can see below diagram on the Figure-1.

 

 

 

 

 

 

 

 

 

 

Configuration of the HaProxy(CentOS6):

Creating a Virtual Interface:

cp /etc/sysconfig/network-scripts/ifcfg-eth0 /etc/sysconfig/network-scripts/ifcfg-eth0:0

Configure /etc/sysconfig/network-scripts/ifcfg-eth0:0

#[root@centos6 ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0:0
DEVICE=eth0:0
TYPE=Ethernet
ONBOOT=yes
NM_CONTROLLED=yes
BOOTPROTO=none
IPADDR=192.168.59.65
PREFIX=24
GATEWAY=192.168.59.2
DNS1=192.168.59.2
DEFROUTE=yes
IPV4_FAILURE_FATAL=yes
IPV6INIT=no
NAME="System eth0:0"

Restart network service

[root@centos6 haproxy]# service network restart
eth0      Link encap:Ethernet  HWaddr 00:0C:29:26:13:4F
          inet addr:192.168.59.60  Bcast:192.168.59.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fe26:134f/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:41596 errors:0 dropped:0 overruns:0 frame:0
          TX packets:81978 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:11250635 (10.7 MiB)  TX bytes:9759307 (9.3 MiB)
eth0:0    Link encap:Ethernet  HWaddr 00:0C:29:26:13:4F
          inet addr:192.168.59.65  Bcast:192.168.59.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

Install HaProxy:

[root@centos6 ~]# yum install -y haproxy

Configure HaProxy: /etc/haproxy/haproxy.cfg

Copy below configuration at the end of the /etc/haproxy/haproxy.cfg file.

frontend front_myapp
        bind 192.168.59.65:80
        mode http
        acl url_blog1 path_beg /blog1
        acl url_blog2  path_beg /blog2
        use_backend back_blog1 if url_blog1
        use_backend back_blog2 if url_blog2
        default_backend back_default

backend back_blog1
        mode http
        server lubuntu 192.168.59.137:80 check

backend back_blog2
        mode http
        server lubuntu2 192.168.59.138:80 check

backend back_default
        mode http
        balance roundrobin
        server lubuntu 192.168.59.137:80 check
        server lubuntu2 192.168.59.138:80 check

listen stats  # Listen on localhost:9000
        bind 192.168.59.65:8080
        mode http
        stats enable  # Enable stats page
        stats hide-version  # Hide HAProxy version
        stats realm Haproxy\ Statistics  # Title text for popup window
        stats uri /hastats  # Stats URI
        #stats auth Username:Password  # Authentication

Check  configuration for syntax:

[root@centos6 haproxy]# haproxy -c -f haproxy.cfg
Configuration file is valid

Restart Service:

[root@centos6 haproxy]# service haproxy restart

For Statistics:

http://192.168.59.65:8080/hastats

 

 

 

 

 

Experiments:

Each request by client to the example.com will be redirected  lubuntu and  lubuntu2 (roundrobin) by the HaProxy.

 

 

 

 

 

 

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.