在之前的博文中,我搭建了一个1台HAproxy+2台Apache的WEB负载均衡测试环境,然而1台HAproxy终归存在单点故障的风险,如果能使用2台HAproxy做高可用,形成主备群集,对外提供一个VIP在两台HAproxy之间浮动,一旦主的故障,备机立马接管VIP,保持HAproxy仍旧可用,那就比较完美了,其实很简单,Keepalived就能实现这样的功能,且在RedHat 7的安装光盘中自带了keepalived的RPM包,通过yum安装非常方便。通过在两台HAproxy上分别安装Keepalived,形成一主一备,使用VRRP协议检测心跳信号,VIP可以在两台HAproxy之间浮动,实现HAproxy的高可用群集架构。架构示意图如下:

wKioL1jCUX2RSM2jAACakBTEc3s647.png

关于Apache和HAproxy的安装配置可以参考本人之前的博文:

《RedHat 7配置HAProxy实现Web负载均衡》

http://jiangjianlong.blog.51cto.com/3735273/1887279

《RedHat 7 安装配置Apache 2.4》

http://jiangjianlong.blog.51cto.com/3735273/1858650


一、测试环境

计算机名

操作系统

IP地址

VIP

安装的软件

RH7-HAproxy01

Redhat  7.2

192.168.10.19


192.168.10.18

Haproxy1.5.14keepalived 1.2.13

RH7-HAproxy02

Redhat  7.2

192.168.10.20

Haproxy1.5.14keepalived 1.2.13

RH7-Apache01

Redhat  7.2

192.168.10.21

         ——

Apache  httpd 2.4

RH7-Apache02

Redhat  7.2

192.168.10.22

         ——

Apache  httpd 2.4


二、安装配置keepalived

1、在两台HAproxy上分别使用yum install keepalived命令安装keepalived

[root@RH7-HAproxy01 ~]# yum install keepalived

wKiom1jCDZvDtSLsAAHJk3zqQgA259.jpg

[root@RH7-HAproxy02 ~]# yum install keepalived

wKiom1jCDZyh3bWTAAHZxgwtf34918.jpg


2、编辑RH7-HAproxy01的keepalived配置文件,将其配置成MASTER

[root@RH7-HAproxy01~]# cd /etc/keepalived/

[root@RH7-HAproxy01keepalived]# ls

keepalived.conf

[root@RH7-HAproxy01keepalived]# cp keepalived.conf keepalived.conf.BAK

[root@RH7-HAproxy01keepalived]# ls

keepalived.conf  keepalived.conf.BAK

[root@RH7-HAproxy01keepalived]# vi keepalived.conf

! Configuration Filefor keepalived
 
global_defs {
   notification_email {
     acassen@firewall.loc
     failover@firewall.loc
     sysadmin@firewall.loc
   }
   notification_email_fromAlexandre.Cassen@firewall.loc
   smtp_server 192.168.200.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}
 
#监测haproxy进程状态,每2秒执行一次
vrrp_scriptchk_haproxy {
    script"/etc/keepalived/chk_haproxy.sh"
    interval 2
    weight 2
    fall 3
}
 
vrrp_instance VI_1 {
    state MASTER  #将HR7-HAproxy01标示为MASTER
    interface eno50332208 #指定网络接口
    virtual_router_id 51
    priority 100  #MASTER权重要高于BACKUP
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.10.18   #VIP
    }
    track_script {
        chk_haproxy #监测haproxy进程状态
}
}

wKioL1jCDZyCib__AAINqdqDT4M096.jpg


3、编辑RH7-HAproxy02的keepalived配置文件,将其配置成BACKUP

[root@RH7-HAproxy02 ~]# cd /etc/keepalived/

[root@RH7-HAproxy02 keepalived]# ls

keepalived.conf

[root@RH7-HAproxy02 keepalived]# cp keepalived.confkeepalived.conf.BAK

[root@RH7-HAproxy02 keepalived]# ls

keepalived.conf  keepalived.conf.BAK

[root@RH7-HAproxy01keepalived]# vi keepalived.conf

[root@RH7-HAproxy02~]# vi /etc/keepalived/keepalived.conf

global_defs {
   notification_email {
     acassen@firewall.loc
     failover@firewall.loc
     sysadmin@firewall.loc
   }
   notification_email_fromAlexandre.Cassen@firewall.loc
   smtp_server 192.168.200.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}
 
#监测haproxy进程状态,每2秒执行一次
vrrp_scriptchk_haproxy {
    script"/etc/keepalived/chk_haproxy.sh"
    interval 2
    weight 2
}
 
 
vrrp_instance VI_1 {
    state BACKUP  #将HR7-HAproxy02标识为BACKUP
    interface eno50332208 #指定网络接口
    virtual_router_id 51
    priority 99  #MASTER权重要高于BACKUP
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
 
    virtual_ipaddress {
        192.168.10.18   #VIP
    }
    track_script {
        chk_haproxy #监测haproxy进程状态
}
}

wKiom1jCI3-DZHxXAAGwuH1YfRA173.jpg

4、在2台HAproxy上创建chk_haproxy.sh脚本,内容如下:

 vi /etc/keepalived/chk_haproxy.sh

#!/bin/bash
status=$(ps aux|grephaproxy | grep -v grep | grep -v bash | wc -l)
if [ "${status}" = "0" ]; then  #判断haproxy是否已经启动
   systemctl start haproxy #如果没有启动,则启动haproxy程序
 
sleep 3  #睡眠3秒钟以等待haproxy完全启动
 
status2=$(psaux|grep haproxy | grep -v grep | grep -v bash |wc -l)
    if [ "${status2}" ="0"  ]; then
    systemctl stop keepalived
   #如果haproxy还是没有启动,则将keepalived停掉,这样VIP会自动漂移到另外一台haproxy
    fi
fi

wKiom1jCJHaCHDBiAADfxZkVN1I503.png

5、对chk_haproxy.sh脚本赋予执行权限

wKioL1jCDaGjLSKEAAFCuYeRPLw208.jpg


wKiom1jCDaGT-obmAAE6SV0_sP8447.jpg


6、重启RH7-HAproxy01的keepalived服务,并查看IP,确认VIP成功绑定

wKiom1jCDZ6BOD1jAAE4xB5aWoM260.jpg


7、重启RH7-HAproxy02的keepalived服务,并查看IP,确认无VIP绑定

wKiom1jCDZ6S2u1uAAEHpSCCa8s991.jpg


8、设置keepalived服务开机自动启动

wKioL1jCDZ-CsQWWAABd6r_Ijmg579.jpg


wKiom1jCDZ-SQs1uAABKFkha9P0897.jpg


9、使用浏览器访问VIP确认能正常访问到后端的WEB页面,负载均衡功能也正常

wKioL1jCDaHxbuSgAABdnu_q_XA812.jpg

wKiom1jCDaWzTHnrAAByUy-ikSs612.png

本文出自江健龙的技术博客http://jiangjianlong.blog.51cto.com/3735273/1905149


三、keepalived主备切换测试

1、停止haproxy服务测试chk_haproxy.sh脚本,从日志可以看出一停止haproxy服务马上就自动启动起来了,chk_haproxy.sh脚本成功生效

root@RH7-HAproxy01 ~]# systemctl stop haproxy

[root@RH7-HAproxy01 ~]# tail -f /var/log/messages

wKiom1jCDaLyHiJQAADLZcj56W0003.jpg


2、停止keepalived服务测试主备切换,通过日志可以看到RH7-HAproxy01的keepalived服务停止后释放了VIP,而RH7-HAproxy02马上进入MASTER状态并绑定了VIP,通过浏览器进行访问VIP发现仍然正常访问

wKiom1jCDaLRu8RIAABEMHWONdk266.jpg

[root@RH7-HAproxy01 ~]# tail -f /var/log/messages

wKioL1jCQIuTv3-SAACvP7MB2ZU994.png

[root@RH7-HAproxy02 ~]# tail -f /var/log/messages

wKiom1jCRsLy8rhGAAC58mwGJ6s278.png

wKioL1jCR4nDRETjAAE2YE5TGRk992.png

wKioL1jCDaHxbuSgAABdnu_q_XA812.jpg

wKiom1jCDaWzTHnrAAByUy-ikSs612.png

3、将RH7-HAproxy01的keepalived服务启动,由于它原本是配置成MASTER,一旦服务起来后,马上接管VIP,我们观察日志也可以很直观地看出来

wKioL1jCDaXxc2EIAABXF2Sed1c380.png

[root@RH7-HAproxy01 ~]# tail -f /var/log/messages

wKiom1jCDafxrQudAANZeIICTiY688.png

[root@RH7-HAproxy02 ~]# tail -f /var/log/messages

wKioL1jCDaeQTKbYAAEIOxdFq_k650.png


wKiom1jCSHvCa35RAAFAV-ebnPU940.pngwKiom1jCDamzWqUTAABuayV2g0A630.png

wKiom1jCDaWzTHnrAAByUy-ikSs612.png

4、直接将RH7-HAproxy01关机,观察RH7-HAproxy02的日志也可以看出VIP的转移过程

[root@RH7-HAproxy01 ~]#poweroff

[root@RH7-HAproxy02 ~]# tail -f /var/log/messages

wKiom1jCDaqDiiaHAACuJ_bVfj0930.png

wKioL1jCDavCNWpVAAE_EAl4lwQ679.png

wKiom1jCDamzWqUTAABuayV2g0A630.png

wKiom1jCDaujO3umAAByUy-ikSs390.png

本文出自江健龙的技术博客http://jiangjianlong.blog.51cto.com/3735273/1905149

关于Apache和HAproxy的安装配置可以参考本人之前的博文:

《RedHat 7配置HAProxy实现Web负载均衡》

http://jiangjianlong.blog.51cto.com/3735273/1887279

《RedHat 7 安装配置Apache 2.4》

http://jiangjianlong.blog.51cto.com/3735273/1858650