in 未分类

高可用haproxy+keepalived(二)

上一篇写了haproxy+keepalived,这边文章将写keepalived的高可用

首先,创建一个脚本 checkha.sh

# cat /tmp/checkha.sh
#!/bin/bash
count=`ps aux | grep -v grep | grep haproxy | wc -l`
echo $count
if [ $count -eq 1 ]; then
   echo "is work"
else
    echo "is not work"
    /etc/init.d/keepalived stop
fi

keepalived判断haproxy进程,如果进程不在,就自动把keepalived服务停掉,这样Vip就会漂移到另外一台机器上. 也就是从90漂移到91

同时,keepalived.conf也需要修改

keepalived.conf

! Configuration File for keepalived

global_defs {
   notification_email {
   jack.zou@jingle.cn
   }
   notification_email_from jackzou19@163.com
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}

vrrp_script checkhaproxy {
    script "/tmp/checkha.sh"
    interval 2
    weight  -10
}

vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
    192.168.1.136
    }
    track_script {
        checkhaproxy
    }
}

首先是添加了
vrrp_script checkhaproxy {
script “/tmp/checkha.sh”
interval 2
weight -10
}

其次在instance里添加了
track_script {
checkhaproxy
}

这样,就达到了效果.当你把haproxy停掉后,keepalived会自动的停掉,这样,Vip漂移到91上面.
当90的haproxy和keepalived起来后,Vip会漂移回90

我觉得在这种情况下,91是不需要做这个判断的. 因为默认的master会接管Vip
除非你将state都设置为Backup,然后在90那台设置参数nopreempt.这样,当90恢复后,是不会自动接管的.这个就有必要在91也设置一个检查脚本,当发现异常的时候,将keepavlied停掉,然后90才会接管Vip

Write a Comment

Comment