Linux运维:测试HAProxy+Keepalived高可用负载均衡集群

Posted 计算机与网络安全

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linux运维:测试HAProxy+Keepalived高可用负载均衡集群相关的知识,希望对你有一定的参考价值。

一次性付费进群,长期免费索取教程,没有付费教程。

 教程列表  见微信公众号底部菜单 |   本文底部有推荐书籍 

ID:Computer-network


高可用的HAProxy系统能够实现HAProxy的高可用性、特性和故障自动切换特性。本文介绍高可用性和两个特性的简单测试。


1、测试Keepalived的高可用功能


高可用性是通过HAProxy的两个HAProxy Server完成的。为了模拟故障,先将主haproxy-server上面的HAProxy服务停止,接着观察haproxy-server上Keepalived的运行日志,信息如下:


Jul 24 16:04:13 haproxy-server Keepalived_vrrp[25735]: VRRP_Script(check_haproxy) failed

Jul 24 16:04:14 haproxy-server Keepalived_vrrp[25735]: VRRP_Instance(HAProxy_HA) Received higher prio advert

Jul 24 16:04:14 haproxy-server Keepalived_vrrp[25735]: VRRP_Instance(HAProxy_HA) Entering BACKUP STATE

Jul 24 16:04:14 haproxy-server Keepalived_vrrp[25735]: VRRP_Instance(HAProxy_HA) removing protocol VIPs.

Jul 24 16:04:14 haproxy-server Keepalived_healthcheckers[25734]: Netlink reflector reports IP 10.0.0.40 removed


这段日志显示了check_haproxy检测失败后,haproxy-server自动进入了BACKUP状态,同时释放了虚拟IP。由于执行了角色切换,此时mail_notify.py脚本应该会自动执行并发送状态切换邮件,类似的邮件信息如图1所示。

图1  Keepalived状态切换时的报警邮件


然后观察备机backup-haproxy上Keepalived的运行日志,信息如下:


Jul 24 16:04:14 backup-haproxy Keepalived_vrrp[25243]: VRRP_Instance(HAProxy_HA) forcing a new MASTER election

Jul 24 16:04:14 data-1-2 Keepalived_vrrp[25243]: VRRP_Instance(HAProxy_HA) forcing a new MASTER election

Jul 24 16:04:16 data-1-2 Keepalived_vrrp[25243]: VRRP_Instance(HAProxy_HA) Transition to MASTER STATE

Jul 24 16:04:18 data-1-2 Keepalived_vrrp[25243]: VRRP_Instance(HAProxy_HA) Entering MASTER STATE

Jul 24 16:04:18 data-1-2 Keepalived_vrrp[25243]: VRRP_Instance(HAProxy_HA) setting protocol VIPs.

Jul 24 16:04:18 data-1-2 Keepalived_healthcheckers[25242]: Netlink reflector reports IP 192.168.66.10 added

Jul 24 16:04:18 data-1-2 Keepalived_vrrp[25243]: VRRP_Instance(HAProxy_HA) Sending gratuitous ARPs on eth0 for 192.168.66.10

Jul 24 16:04:23 data-1-2 Keepalived_vrrp[25243]: VRRP_Instance(HAProxy_HA) Sending gratuitous ARPs on eth0 for 192.168.66.10


从日志中可以看出,出现故障后,backup-haproxy立刻检测到,此时backup-haproxy变为Master角色,并且接管了的虚拟IP资源,最后将虚拟IP绑定在eth0设备上。


接着,重新启动主haproxy-server上的Keepalived服务,然后观察haproxy-server上的日志状态:


Jul 24 18:22:16 localhost haproxy[30896]: Proxy www started.

Jul 24 18:22:17 haproxy-server Keepalived_vrrp[30896]: VRRP_Script(check_haproxy) succeeded


从日志输出可知,在HAProxy服务启动后,Keepalived监控程序vrrp_script检测到HAProxy已经正常运行,但是并没有执行切换操作,这是由于在Keepalived集群中设置了不抢占模式的缘故。


2、测试负载均衡功能


将www.zb.com、static.zb.com、video.zb.com这三个域名解析到192.168.66.10这个虚拟IP上,然后依次访问网站,如果HAProxy运行正常,并且ACL规则设置正确,这三个网站应该都能正常访问,如果出现错误,可通过查看HAProxy的运行日志判断哪里出了问题。


ID:Computer-network


【推荐书籍】