centos6.5 keepalived检测脚本

一,keepalived配置文件修改

1,master 上配置keepalived

cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived

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
    
    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 keepalived

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
  
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.20.22
    }
    track_script {
        chk_ngx
    }
}

3,健康检测脚本

cat health_chk.sh
#!/bin/bash

A=`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
      fi
fi
if [ $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
    fi
fi
if [ $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
    fi
fi

nginx、zk、kafka都是可以正常启动的应用。可自行更改。

此文可结合kafka单点测试进行测试。

posted @ 2018-10-16 11:03  fuhaizi  阅读(660)  评论(0编辑  收藏  举报