Thursday, May 12, 2016

Install Configure - HAProxy and Keepalived

Install Configure - HAProxy and Keepalived

HAProxy will be used as load-balancing software.
Keepalived as high availability solution and apache as software to load-balance.

Server OS: CentOS 7 - 64 bits

VIP = Virtual IP

vip,   IP:  124.124.124.200
ha01, IP: 124.124.124.201
ha02, IP: 124.124.124.202
web01,IP: 124.124.124.203
web02,IP: 124.124.124.204

Diagram,


1. Install the HAProxy on Server, ha01 and ha02
yum install -y haproxy

Edit file: vi /etc/haproxy/haproxy.cfg
Change line "frontend  main *:5000" to "frontend  main *:80"

And comment out the line "use_backend static if url_static".

Go to the end of file and remove the lines starting with “server app” and replace them with the following lines:
server httpd1 124.124.124.203:80 check
server httpd2 124.124.124.204:80 check
Enable and Start service haproxy
systemctl enable haproxy
systemctl start haproxy

Create /etc/firewalld/services/haproxy.xml file and paste the following lines:

HAProxy
HAProxy load-balancer

Next we need to assign correct SELinux context and file permissions to the haproxy.xml file:
cd /etc/firewalld/services
restorecon haproxy.xml
chmod 640 haproxy.xml

Update the firewall configuration:
firewall-cmd --permanent --add-service=haproxy
firewall-cmd --reload

2. Install Keepalived on Server, ha01 and ha02
yum install -y keepalived

Create a new /etc/keepalived/keepalived.conf file and paste the following lines:

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 eno16777736 # interface to monitor
  state MASTER # MASTER on haproxy1, BACKUP on haproxy2
  virtual_router_id 51
  priority 201  #The smaller number will be become the primary
  virtual_ipaddress {
    124.124.124.200 # virtual ip address
  }
  track_script {
    chk_haproxy
  }
}

Issue following commands to Enable keepalived service on system boot up:
systemctl enable keepalived
systemctl start keepalived

Now we need to Check VIP 124.124.124.200 on the ha01 server. on ha02 also have this IP.

3. Install Apache on web01 and web02

To install Apache, enter the following command
yum install httpd -y

Start the Apache service and make it to start automatically on every reboot:
systemctl start httpd
systemctl enable httpd

If we want to access it from the remote systems.
firewall-cmd --permanent --add-service=http
systemctl restart firewalld

Test Apache to web01 and web02 make sure it's work.
http://124.124.124.203
http://124.124.124.204

Access to web service via our VIP.
Just refresh the page you will got into web02

This is just a demo, In reality your web01 and web02 should deliver the same content page. 

Test shutdown one of your HA server and one of the Web Server you still be able to get the content. 

Done, 



No comments:

Post a Comment