一,keepalived配置文件修改
1,master 上配置keepalived
cat /etc/keepalived/keepalived.conf! Configuration File for keepalivedglobal_defs { router_id LVS_1}vrrp_script chk_ngx { script "/data/shell/health_chk.sh" interval 2 weight 2}vrrp_instance VI_1 { state MASTER interface eth1 virtual_router_id 51 priority 100 advert_int 1 nopreempt #设置 nopreempt 防止抢占资源,只生效BACKUP节点 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.20.22 } track_script { chk_ngx }}
2,backup 上配置keepalived
cat /etc/keepalived/keepalived.conf! Configuration File for keepalivedglobal_defs { router_id LVS_2}vrrp_script chk_ngx { script "/data/shell/health_chk.sh" interval 2 weight 2}vrrp_instance VI_1 { state BACKUP interface eth1 virtual_router_id 51 nopreempt priority 99 advert_int 1 nopreempt #设置 nopreempt 防止抢占资源,只生效BACKUP节点 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.20.22 } track_script { chk_ngx }}
3,健康检测脚本
cat health_chk.sh#!/bin/bashA=`ps -C nginx --no-header |wc -l`B=`ps aux|grep -v grep |grep 'zookeeper.properties' |wc -l`C=`ps aux|grep -v grep |grep 'server.properties' |wc -l`if [ $A -eq 0 ];then /usr/sbin/nginx sleep 3 if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then /etc/init.d/keepalived stop fifiif [ $B -eq 0 ];then /etc/init.d/zookeeper start sleep 3 if [ `ps aux|grep -v grep |grep 'zookeeper.properties' |wc -l` -eq 0 ];then /etc/init.d/keepalived stop fifiif [ $C -eq 0 ];then /etc/init.d/kafka start sleep 3 if [ `ps aux|grep -v grep |grep 'server.properties' |wc -l` -eq 0 ];then /etc/init.d/keepalived stop fifi
nginx、zk、kafka都是可以正常启动的应用。可自行更改。
此文可结合进行测试。