haproxy+keepalived配置

来源:互联网 发布:暗黑2 1.13 完美 mac 编辑:程序博客网 时间:2024/06/05 04:45
haproxy+keepalived配置
1. cat keepalived.conf

! Configuration File for keepalived

global_defs {
   notification_email {
     1041126478@qq.com
   }
   notification_email_from jzyc5@jzyc.com
   smtp_server 192.168.44.27
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}

vrrp_script chk_state_down {
    script "[[ -f /etc/keepalived/down ]] && exit 1 || exit 0"
    interval 2
    weight 3
}

vrrp_script chk_haproxy {
  script "/etc/keepalived/chk_haproxy.sh" #
  interval 2 # script
  weight 2
}

vrrp_instance VI_1 {
    state MASTER
    interface em2
    virtual_router_id 27
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.44.102
    }

    track_script {
      chk_haproxy  #
      chk_state_down
    }
    
    notify_master "/etc/keepalived/notify.sh master 192.168.44.102"
    notify_backup "/etc/keepalived/notify.sh backup 192.168.44.102"
    notify_fault  "/etc/keepalived/notify.sh fault 192.168.44.102"
}



2. cat chk_haproxy.sh
#!/bin/bash
#
# author: weizhifeng
# description:
# ?¨?2?haproxyˇ·委???1?????ˉhaproxy£?
# ?1??°?????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



3. cat notify.sh
# desccription: An example of notify script
#
contact='jzyc5@jzyc.com'
notify() {
mailsubject="`hostname` to be $1: $2 floating"
mailbody="`date '+%F %H:%M:%S'`: vrrp transition, `hostname` changed to be $1"
echo $mailbody | mail -s "$mailsubject" $contact
}

case "$1" in
master)
notify master
/etc/rc.d/init.d/haproxy start
exit 0
;;
backup)
notify backup
/etc/rc.d/init.d/haproxy stop
exit 0
;;
fault)
notify fault
/etc/rc.d/init.d/haproxy stop
exit 0
;;
*)
echo 'Usage: `basename $0` {master|backup|fault}'
exit 1
;;
esac












原创粉丝点击