[High Availability] Using Keepalived (failover) in combination with haproxy (load balancing).

As in the previous post, we used load balancing to increase the processing speed of the web server. The Round Robin algorithm seems like a backup connection in case one web server dies, another web server will be used. However, the main purpose of load balancing is still to ensure the stability of the web server. In this article, we will set up a backup connection in case one web server dies and will use another web server to perform.

Here’s how to set up redundant connections combined with load balancing.

Preparation required:

  • Web Server 1
    OS: CentOS 7 + Nginx + haproxy + keepalived
    IP web server: 192.168.1.129:8080
    IP haproxy: 192.168.1.129:80
  • Web Server 2
    OS: CentOS 6+ Nginx + haproxy + keepalived
    IP web server: 192.168.1.128:8080
    IP haproxy: 192.168.1.128:80

Install keepalived with the command on both webservers: yum install keepalived

The Keepalived service will help us create a Virtual IP to use for the server, roughly speaking, the server will use the IP we define by ourselves with Keepalived, not the IP on the server’s interface (granted to the server). by a certain DHCP or assigned by us.). To do this, we need to go to file /etc/sysctl.conf and add or edit the following line in file sysctl.confnet.ipv4.ip_nonlocal_bind=1

On Web Server 1, we configure the file /etc/keepalived/keepalived.conf như sau:


global_defs {
router_id test1 #declare the route_id of keepalived
}
vrrp_script chk_haproxy {
script "killall -0 haproxy"
interval 2
weight 2
}
vrrp_instance VI_1 {
virtual_router_id 51
advert_int 1
priority 100
state MASTER
interface ens33 #information about the server's interface name, use the `ifconfig` command to view and fill in correctly
virtual_ipaddress {
192.168.1.69 dev ens33 #Declare Virtual IP for the corresponding interface
}
authentication {
auth_type PASS
auth_pass 123456 #This password must be declared the same between keepalived servers
}
track_script {
chk_haproxy
}
}

 

On Web Server 2 we configure the file /etc/keepalived/keepalived.conf như sau:


global_defs {
router_id test2 #khai báo route_id của keepalived
}
vrrp_script chk_haproxy {
script "killall -0 haproxy"
interval 2
weight 2
}
vrrp_instance VI_1 {
virtual_router_id 51
advert_int 1
priority 99
state BACKUP
interface eth0 #information about the server's interface name, use the `ifconfig` command to view and fill in correctly
virtual_ipaddress {
192.168.1.69 dev eth0 #Declare Virtual IP for the corresponding interface
}
authentication {
auth_type PASS
auth_pass 123456 #This password must be declared the same between keepalived servers
}
track_script {
chk_haproxy
}
}

In the above Keepalived configuration file of both test1 and test2 machines, there is track_script – which means that both servers will run the script to check the status of the service process ID (PID) declared in the script (here declared check service haproxy). Assuming the haproxy service on test1 (initialized with state MASTER and priotiry 100) for some reason doesn’t work, Keepalived will subtract the weight (priority 100-2=98) on machine test1. At this time, the priority of test1 will be 98 and less than the priority=99 originally declared in test2 , so Keepalived will change the state of test2 from BACKUP to MASTER and test2 will keep the declared VIP.

Config haproxy Web Server 1:

global
daemon
maxconn 256

defaults
 mode http
 timeout connect 5000ms
 timeout client 50000ms
 timeout server 50000ms

 stats enable
 stats hide-version
 stats refresh 30s
 stats show-node
 stats auth admin:123456
 stats uri /haproxy?stats
frontend http-in
 bind *:80
 default_backend servers

backend static
 balance roundrobin
 server static 192.168.1.69:80

backend servers
 balance roundrobin
 server webserver1 192.168.1.128:8080 check
 server webserver2 192.168.1.129:8080 check

Config haproxy Web Server 2:

global
daemon
maxconn 256

defaults
 mode http
 timeout connect 5000ms
 timeout client 50000ms
 timeout server 50000ms

 stats enable
 stats hide-version
 stats refresh 30s
 stats show-node
 stats auth admin:123456
 stats uri /haproxy?stats
frontend http-in
 bind *:80
 default_backend servers

backend static
 balance roundrobin
 server static 192.168.1.69:80

backend servers
 balance roundrobin
 server webserver1 192.168.1.128:8080 check
 server webserver2 192.168.1.129:8080 check

Run command service keepalived start for CentOS 6, systemctl start keepalived for CentOS 7.
Turn off haproxy one by one to see the results.

Bài viết liên quan