* on 192.168.3.7 | 软件 | 版本 | 备注 | | --- | --- |--- | | centos| 7.5 | | | keepalived| | | * 安装 ``` cd ~ ls wget http://www.keepalived.org/software/keepalived-1.1.19.tar.gz ls ln -s /usr/src/kernels/`uname -r`/ /usr/src/linux ls tar -zxvf keepalived-1.1.19.tar.gz ls rm -rf keepalived-1.1.19.tar.gz ls cd keepalived-1.1.19/ ls yum install libnl\* popt\* -y ./configure make && make install echo $? ls cp /usr/local/etc/rc.d/init.d/keepalived /etc/init.d/ cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/ mkdir /etc/keepalived -p cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/ cp /usr/local/sbin/keepalived /usr/sbin/ mkdir -p /etc/keepalived/logs/ ``` * 配置文件keepalived.conf ``` [root@rabbitmq01 keepalived]# vi keepalived.conf ! Configuration File for keepalived global_defs { router_id mq01 } vrrp_script chk_haproxy { script "/etc/keepalived/haproxy_check.sh" interval 2 weight 2 } vrrp_instance VI_1 { state MASTER interface ens33 virtual_router_id 51 priority 150 advert_int 1 authentication { auth_type PASS auth_pass 1111 } track_script { chk_haproxy } notify_master "/etc/keepalived/haproxy_master.sh" virtual_ipaddress { 192.168.3.6 } } ``` * 脚本haproxy_master.sh ``` [root@rabbitmq01 keepalived]# cat /etc/keepalived/haproxy_master.sh #!/bin/bash LOGFILE="/etc/keepalived/logs/keepalived-haproxy-state.log" echo "Being Master ..." >> $LOGFILE ``` * 脚本haproxy_check.sh ``` [root@rabbitmq01 keepalived]# cat /etc/keepalived/haproxy_check.sh #!/bin/bash LOGFILE="/etc/keepalived/logs/keepalived-haproxy-state.log" date >> $LOGFILE if [ `ps -C haproxy --no-header |wc -l` -eq 0 ];then echo "fail: check_haproxy status" >> $LOGFILE exit 1 else echo "success: check_haproxy status" >> $LOGFILE exit 0 fi ``` 启动 ``` /etc/init.d/keepalived start ``` * on 192.168.3.8 | 软件 | 版本 | 备注 | | --- | --- |--- | | centos| 7.5 | | | keepalived| | | * 安装 ``` cd ~ ls wget http://www.keepalived.org/software/keepalived-1.1.19.tar.gz ls ln -s /usr/src/kernels/`uname -r`/ /usr/src/linux ls tar -zxvf keepalived-1.1.19.tar.gz ls rm -rf keepalived-1.1.19.tar.gz ls cd keepalived-1.1.19/ ls yum install libnl\* popt\* -y ./configure make && make install echo $? ls cp /usr/local/etc/rc.d/init.d/keepalived /etc/init.d/ cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/ mkdir /etc/keepalived -p cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/ cp /usr/local/sbin/keepalived /usr/sbin/ mkdir -p /etc/keepalived/logs/ ``` * 配置文件keepalived.conf ``` [root@rabbitmq02 keepalived]# cat keepalived.conf ! Configuration File for keepalived global_defs { router_id mq02 } vrrp_script chk_haproxy { script "/etc/keepalived/haproxy_check.sh" interval 2 weight 2 } vrrp_instance VI_1 { state BACKUP interface ens33 virtual_router_id 51 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } track_script { chk_haproxy } notify_master "/etc/keepalived/haproxy_master.sh" virtual_ipaddress { 192.168.3.6 } } ``` * 脚本haproxy_master.sh ``` [root@rabbitmq01 keepalived]# cat /etc/keepalived/haproxy_master.sh #!/bin/bash LOGFILE="/etc/keepalived/logs/keepalived-haproxy-state.log" echo "Being Master ..." >> $LOGFILE ``` * 脚本haproxy_check.sh ``` [root@rabbitmq01 keepalived]# cat /etc/keepalived/haproxy_check.sh #!/bin/bash LOGFILE="/etc/keepalived/logs/keepalived-haproxy-state.log" date >> $LOGFILE if [ `ps -C haproxy --no-header |wc -l` -eq 0 ];then echo "fail: check_haproxy status" >> $LOGFILE exit 1 else echo "success: check_haproxy status" >> $LOGFILE exit 0 fi ``` 启动 ``` /etc/init.d/keepalived start ``` ![](https://img.kancloud.cn/ae/06/ae06ecb7908846dc8286053851214cc6_1910x623.png)