Friday, March 24, 2017

Apache High Availibility Load Balancer

   
           HAProxy is a Load-Balancer, this is fact. It is used to route traffic to servers to primarily ensure applications reliability.  HAPRoxy, which stands for High Availability Proxy, is popular open source software TCP/HTTP Load Balancer and proxying solution which can be run on Linux, Solaris and FreeBSD.


 Keepalived project is to provide simple and robust facilites for loadbalancing and high availability to Linus systems . Load Balancing framework depends on Linux Virtual Server ( IPVS) kernel module which provides Layer4 loadbalacing.

We need to configure Keepalived and HAProxy to achieve this. Our Scnerio is as below
If these are on same network

SCENARIO

1) Node 1 : Web server : 192.168.85.87 . It has content "This is NODE01 Test Server".
2) Node 2 : Web server : 192.168.85.88. It has content "This is NODE02 Test Server".
3) Load1 :  Load Balancer : 192.168.85.97.
4) Load2 : Load Balacner : 192.168.85.99
5) One Virtual IP : 192.168.85.55


Our main Objective is to access website without any downtime. Loadbalancer should have responsibility to send access request to both nodes which are hosting web server. In case of any Web Server down and it should detect that and send next request to another server.

Also loadbalancer should not be single point of failure . So set up loadbalancer in Masters and Backup mode . In case any Loadbalancer get down, other loadbalancer  will get start to work automatically and forward request to Web Server nodes.



[root@load1 ~]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived

global_defs {
   notification_email {
     admin@example.com
   }
   notification_email_from load1@example.com
   smtp_server localhost
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}

vrrp_instance VI_1 {
    state MASTER
    interface eth1
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass password123
    }
    virtual_ipaddress {
        192.168.85.55/24
    }
}

[root@load1 ~]#

[root@load1 ~] service keepalived restart


HAPROXY CONFIGURATION




/etc/haproxy/haproxy.cfg

frontend http_front
   bind 192.168.85.55:80
   stats uri /haproxy?stats
   default_backend http_back

backend http_back
   balance roundrobin
   server node1 192.168.85.194:8000 check
   server node2 192.168.86.70:80 check

Check Configuration file if it has some syntax error.
haproxy -f /etc/haproxy/haproxy.cfg –c

In some cases we need to add below entry in sysctl.conf file to get routing work.
vi /etc/sysctl.conf
Add this line
net.ipv4.ip_nonlocal_bind=1
sysctl –p

service haproxy restart


TESTING

[root@load1 ~]# while true; do curl http://192.168.85.55; sleep 1; done
This is NODE01 Test Server
This is NODE02 Test Server
This is NODE01 Test Server
This is NODE02 Test Server
This is NODE01 Test Server
This is NODE02 Test Server
This is NODE01 Test Server
This is NODE02 Test Server
This is NODE01 Test Server
This is NODE02 Test Server


[root@load1 ~]# curl http://192.168.85.55 -D /dev/stdout
HTTP/1.1 200 OK
Date: Fri, 24 Mar 2017 07:28:19 GMT
Server: Apache/2.2.15 (Red Hat)
Last-Modified: Wed, 22 Mar 2017 06:16:01 GMT
ETag: "2c0f91-1b-54b4bb1d7ed3c"
Accept-Ranges: bytes
Content-Length: 27
Content-Type: text/html; charset=UTF-8
Set-Cookie: WEBSVR=1; path=/

This is NODE01 Test Server
[root@load1 ~]#
[root@load1 ~]# curl http://192.168.85.55 -D /dev/stdout
HTTP/1.1 200 OK
Date: Fri, 24 Mar 2017 07:28:23 GMT
Server: Apache/2.2.15 (Red Hat)
Last-Modified: Wed, 22 Mar 2017 06:16:25 GMT
ETag: "2c0f91-1b-54b4bb33dde1e"
Accept-Ranges: bytes
Content-Length: 27
Content-Type: text/html; charset=UTF-8
Set-Cookie: WEBSVR=2; path=/

This is NODE02 Test Server
[root@load1 ~]#



[root@load1 ~]# while true; do curl http://192.168.85.55 --cookie "WEBSVR=2"; sleep 1; done
This is NODE02 Test Server
This is NODE02 Test Server
This is NODE02 Test Server
This is NODE02 Test Server
This is NODE02 Test Server
This is NODE02 Test Server
This is NODE02 Test Server
This is NODE02 Test Server

No comments: