HAProxy+KeepAlived构建高可用的反向代理

发布时间:2020-04-18编辑:脚本学堂
本文介绍下,使用haproxy与keepalived配置高可用的反向代理系统的方法,有需要的朋友,可以参考下,相当不错 一篇文章。
Bakcup服务器上的配置 /etc/keepalived/keepalived.conf
 

复制代码 代码示例:

global_defs {
   notification_email {
   user@example.com
   }

   notification_email_from mail@example.org
   smtp_server 192.168.x.x
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}

#监测haproxy进程状态,每2秒执行一次
vrrp_script chk_haproxy {
    script "/usr/local/keepalived/chk_haproxy.sh"
    interval 2
    weight 2
}

vrrp_instance VI_1 {
    state BACKUP #状态为BACKUP
    interface eth0
    virtual_router_id 51
    priority 100  #权重要低于MASTER
    advert_int 1
    mcast_src_ip 192.168.1.222 #Backup服务器的IP

    authentication {
        auth_type PASS
        auth_pass 1111
    }

    track_script {
        chk_haproxy #监测haproxy进程状态
    }

    #VIP
    virtual_ipaddress {
        192.168.1.99 #虚拟IP
    }
}
chk_haproxy.sh内容

#!/bin/bash
#
# author: weizhifeng
# description:
# 定时查看haproxy是否存在,如果不存在则启动haproxy,
# 如果启动失败,则停止keepalived
#
status=$(ps aux|grep haproxy | grep -v grep | grep -v bash | wc -l)
if [ "${status}" = "0" ]; then
    /etc/init.d/haproxy start

    status2=$(ps aux|grep haproxy | grep -v grep | grep -v bash |wc -l)

    if [ "${status2}" = "0"  ]; then
            /etc/init.d/keepalived stop
    fi
fi
 

#--高可用测试
在Master上停止keepalived,查看系统日志,发现MASTER释放了VIP。
 

复制代码 代码示例:
 $ /etc/init.d/keepalived stop
 $ tail -f /var/log/message
 Keepalived: Terminating on signal Keepalived: Stopping Keepalived v1.2.2 (11/03,2011)
 Keepalived_vrrp: Terminating VRRP child process on signal
 Keepalived_vrrp: VRRP_Instance(VI_1) removing protocol VIPs.
 

在Backup上查看系统日志,发现Backup已经进入MASTER角色,并且绑定了VIP 192.168.1.99。
 

复制代码 代码示例:
 $ tail -f /var/log/message
 Keepalived_vrrp: VRRP_Instance(VI_1) Entering MASTER STATE
 Keepalived_vrrp: VRRP_Instance(VI_1) setting protocol VIPs
 Keepalived_vrrp: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.1.99 #在Backup上查看VIP是否已经绑定
 

在Master上重新启动keepalived,查看系统日志,发现重新获得MASTER角色,并且绑定VIP 192.168.1.99。
 

复制代码 代码示例:
 $ /etc/init.d/keepalived start
 $ tail -f /var/log/message
 Keepalived_vrrp: VRRP_Instance(VI_1) Transition to MASTER STATE
 Keepalived_vrrp: VRRP_Instance(VI_1) Entering MASTER STATE
 Keepalived_vrrp: VRRP_Instance(VI_1) setting protocol VIPs.
 Keepalived_vrrp: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.1.99
 

在Backup上查看系统日志,发现其重新回到BACKUP角色,并且释放VIP。
 

复制代码 代码示例:
 $ tail -f /var/log/message
 Keepalived_vrrp: VRRP_Instance(VI_1) Received higher prio advert
 Keepalived_vrrp: VRRP_Instance(VI_1) Entering BACKUP STATE
 Keepalived_vrrp: VRRP_Instance(VI_1) removing protocol VIPs.
 

#----并发测试
我们使用webbench来对HAProxy进行并发测试。
 

复制代码 代码示例:
$ yum install ctags
$ wget http://home.tiscali.cz/~cz210552/distfiles/webbench-1.5.tar.gz
$ tar -zxvf webbench-1.5.tar.gz
$ cd webbench-1.5
$ make
$ mkdir -p /usr/local/man && make install
 

测试环境:
CPU:Intel 双核 x86_64 主频3191MHZ
Mem:2G
修改php-fpm.conf,设置PHP-FPM spawn的进程数量为100:
pm.start_servers = 100
pm.max_spare_servers = 100
测试方法:
 

复制代码 代码示例:
$ webbench -c 100 -t 3000 http://192.168.1.99/check.txt
$ webbench -c 100 -t 3000 http://192.168.1.99/test.php
 

测试结果:
并发访问txt文件,HAProxy的session数量为10000左右,这说明HAProxy能够hold住10000个并发连接;
并发访问php文件,HAProxy的session峰值为200左右,接近于后端PHP的并发处理能力(100x2)。