Thursday, March 12, 2015

HAProxy &Keepalived

HAProxy &Keepalived
       For configuring HA-Load balancer, You need 2 virtual/physical servers for the load-balancers and 2 virtual/physical servers to load-balance. In addition to the 4 IP addresses needed by the servers themselves, a fifth virtual IP address (VIP) is necessary. The two load-balancers and the VIP need to be in the same network segment.
HAProxy and keepalived. So, HAProxy will be used as load-balancing software, keepalived as high availability solution and apache as software to load-balance.

Load Balancer 1: haproxy1.psi.com, IP address: 192.168.0.25
Load Balancer 2: haproxy2.psi.com, IP address: 192.168.0.26
MGMT Server 1: mgmt1.psi.com, IP address: 192.168.0.21
MGMT Server 2: mgmt1.psi.com, IP address: 192.168.0.22
We also need a virtual IP address that floats between lb1 and lb2: 192.168.0.31
Here's a little diagram that shows our setup:

    shared IP=192.168.0.31
   192.168.0.25     192.168.0.26        192.168.0.21            192.168.0.22
 -------+--------- ---+--------------+-----------+-----------------+--------- ---+--------------+
    |                          |                      |       |                       |             |                        |
     +--+--+ +--+--+ +----+----+ +----+----+ +----+----+ +----+----+ +----+----+ +----+--
     | lb1 |                    | lb2 |                 |  http1  |                           |  http2  |
     +-----++-----+ +---------+ +---------+ +---------+ +---------+ +---------+ +---------+
     haproxy              haproxy                                  2 MGMT servers (Apache)
     keepalived        keepalived





HAProxy Configuration on server 1
HaProxy configuration-
   #Yum install haproxy
Copy the below configuration file below defaults and change the portnumber,name of the server& server ip by requirements
vi /etc/haproxy/haproxy.conf
listen cloudstack_ui_443(Port https-443,Http-8080)
bind *:443
mode http     
 balance source
server mgmt1.psi.com 192.168.0.21:443 cookie A check(Server name&IP address)
server mgmt2.psi.com 192.168.0.22:443 cookie B check(Server name&IP address)
      listen cloudstack_systemvm_8250(System VM-Mgmt server port port)
            bind *:8250
            mode tcp
            balance source
server mgmt1.psi.com 192.168.0.21:8250 maxconn 32 check (Servername&IP address)
server mgmt2.psi.com 192.168.0.22:8250 maxconn 32 check (Servername&IP address) 

Keepalived configuration on server 1(used for virtual ip creation)
        #Yum install keepalived
•         cp /etc/keepalived/keepalived.conf  /etc/keepalived/keepalived.conf.orig(Copy original file and keep it as a backup)
•         Open vi editor and copy below content in to configuration file(192.168.0.31 virtual ip)
vi /etc/keepalived/keepalived.conf
  vrrp_script chk_haproxy {
    script "killall -0 haproxy" # check the haproxy process
    interval 2 # every 2 seconds
    weight 2 # add 2 points if OK
 }
 vrrp_instance VI_1 {
     interface eth0 # interface to monitor
     state MASTER # MASTER on haproxy1, BACKUP on haproxy2
     virtual_router_id 77 
     priority 200 # 101 on haproxy1, 100 on haproxy2 
  virtual_ipaddress {     
    192.168.0.31 #dev eth0 # virtual ip address 
  }  
  track_script   {
      chk_haproxy 
  }
} 

HAProxy Configuration on server 2
HaProxy configuration-
   #Yum install haproxy
Copy the below configuration file below defaults and change the portnumber,name of the server& server ip by requirements
vi /etc/haproxy/haproxy.conf
listen cloudstack_ui_443(Port https-443,Http-8080)
bind *:443
mode http     
 balance source
server mgmt1.psi.com 192.168.0.21:443 cookie A check(Server name&IP address)
server mgmt2.psi.com 192.168.0.22:443 cookie B check(Server name&IP address)
      listen cloudstack_systemvm_8250(System VM-Mgmt server port port)
            bind *:8250
            mode tcp
            balance source
server mgmt1.psi.com 192.168.0.21:8250 maxconn 32 check (Servername&IP address)
server mgmt2.psi.com 192.168.0.22:8250 maxconn 32 check (Servername&IP address)

To check keep alive status: ip addr | grep eth0




Keepalived configuration on server 2(used for virtual ip creation)
        #Yum install keepalived
•         cp /etc/keepalived/keepalived.conf  /etc/keepalived/keepalived.conf.orig(Copy original file and keep it as a backup)
•         Open vi editor and copy below content in to configuration file(192.168.0.31 virtual ip)
vi /etc/keepalived/keepalived.conf
  vrrp_script chk_haproxy {
    script "killall -0 haproxy" # check the haproxy process
    interval 2 # every 2 seconds
    weight 2 # add 2 points if OK
 }
 vrrp_instance VI_1 {
     interface eth0 # interface to monitor
    state MASTER # MASTER on haproxy1, BACKUP on haproxy2
     virtual_router_id 77 
     priority 111 # 101 on haproxy1, 100 on haproxy2 
  virtual_ipaddress {     
    192.168.0.31 #dev eth0 # virtual ip address 
  }  
  track_script   {
      chk_haproxy 
  }
}

 To check keep alive status: ip addr | grep eth0


No comments:

Post a Comment