首页 > 代码库 > RedHat 7配置keepalived实现HAproxy群集
RedHat 7配置keepalived实现HAproxy群集
在之前的博文中,我搭建了一个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的高可用群集架构。架构示意图如下:
关于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.14、keepalived 1.2.13 |
RH7-HAproxy02 | Redhat 7.2 | 192.168.10.20 | Haproxy1.5.14、keepalived 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
[root@RH7-HAproxy02 ~]# yum install keepalived
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进程状态 } }
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进程状态 } }
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
5、对chk_haproxy.sh脚本赋予执行权限
6、重启RH7-HAproxy01的keepalived服务,并查看IP,确认VIP成功绑定
7、重启RH7-HAproxy02的keepalived服务,并查看IP,确认无VIP绑定
8、设置keepalived服务开机自动启动
9、使用浏览器访问VIP确认能正常访问到后端的WEB页面,负载均衡功能也正常
本文出自江健龙的技术博客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
2、停止keepalived服务测试主备切换,通过日志可以看到RH7-HAproxy01的keepalived服务停止后释放了VIP,而RH7-HAproxy02马上进入MASTER状态并绑定了VIP,通过浏览器进行访问VIP发现仍然正常访问
[root@RH7-HAproxy01 ~]# tail -f /var/log/messages
[root@RH7-HAproxy02 ~]# tail -f /var/log/messages
3、将RH7-HAproxy01的keepalived服务启动,由于它原本是配置成MASTER,一旦服务起来后,马上接管VIP,我们观察日志也可以很直观地看出来
[root@RH7-HAproxy01 ~]# tail -f /var/log/messages
[root@RH7-HAproxy02 ~]# tail -f /var/log/messages
4、直接将RH7-HAproxy01关机,观察RH7-HAproxy02的日志也可以看出VIP的转移过程
[root@RH7-HAproxy01 ~]#poweroff
[root@RH7-HAproxy02 ~]# tail -f /var/log/messages
本文出自江健龙的技术博客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
本文出自 “江健龙的技术博客” 博客,请务必保留此出处http://jiangjianlong.blog.51cto.com/3735273/1905149
RedHat 7配置keepalived实现HAproxy群集