软件版本:Keepalived v1.3.4 (02/18,2017),Apache/2.2.15 

系统版本:centos 6.6


master:10.10.24.21    backup:10.10.4.11

vip:10.10.24.253


因为主要是介绍keepalived,所以apache采用yum版本安装

keepalive采用的是源码安装,安装过程如下

keepalived下载地址是:http://www.keepalived.org/download.html

tar -zxf keepalived-1.3.4.tar.gz 

cd keepalived-1.3.4

./configure

make && make install

其实就安装配置挺简单

下面是本地脚本,一个是服务检测脚本,一个是mail报警邮件

[root@VIDEt-tes01 keepalived]# cat check_httpd.sh 
#!/bin/bashwc=`ss -ln |grep ":80 " |wc -l` #判断本地的80端口是否开启
if [ $wc -eq 1 ]
then
exit 0   #如果存在就发出0的信号
else
exit 1  #如果不存在就发出1的信号
fi
#其实这个可以根据自己的情况自定义,就是keepalived是根据你脚本返回的状态码,来判断服务是否正常
[root@VIDEt-tes01 keepalived]# cat mail.sh  #邮件报警脚本
#!/bin/bash
[email protected] #发件人地址
[email protected] #收件人地址
smail=mail.xxxxxxxx.com.cn  #邮件服务器地址,smtp
ip=`ifconfig eth0 |awk -F "[ :]+" 'NR==2{print $4}'`
/usr/local/bin/sendEmail -f $fmail -t $tmail -s $smail -u "keepalive-$ip" -o message-charset=utf8 -m "$ip 目前是否正在提供服务: $1"

我没有使用keepalived自带的发邮件功能,用的是sendEmail,平常工作中都用的sendEmail


master这边的配置项:

[root@wam keepalived]# cat keepalived.conf
! Configuration File for keepalived

global_defs {
   notification_email {
     root@localhost
   }
   notification_email_from root@localhost
   smtp_server 127.0.0.1

  smtp_connect_timeout 30
   router_id LVS_DEVEL
}
vrrp_script chk_httpd {
    script "/etc/keepalived/check_httpd.sh"
    interval 5
    weight 2
    fall 4
    rise 4

}

vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 99
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass a23c7f32dfb519d6a5dc67a4b2ff8f5e

    }
  track_interface {

   eth0
}
    virtual_ipaddress {
    10.10.4.253 dev eth0 label eth0:0
    }
track_script {
    chk_httpd

}
notify_backup "/etc/keepalived/mail.sh NO"
notify_master "/etc/keepalived/mail.sh YES"
}
启动服务:
keepalived -f /etc/keepalived/keepalived.conf


slave这边的配置项:

[root@VIDEt-tes01 keepalived]# vim keepalived.conf 

  smtp_connect_timeout 30
   router_id LVS_DEVEL
}
vrrp_script chk_httpd {
    script "/etc/keepalived/check_httpd.sh"
    interval 1
    weight 2

}

vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 51
    priority 98
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass a23c7f32dfb519d6a5dc67a4b2ff8f5e

    }
  track_interface {
   eth0
}
    virtual_ipaddress {
    10.10.4.253 dev eth0 label eth0:0
    }
track_script {
    chk_httpd

}
notify_backup "/etc/keepalived/mail.sh NO"
notify_master "/etc/keepalived/mail.sh YES"
}
启动服务:
keepalived -f /etc/keepalived/keepalived.conf

试验效果:

关闭master上面的服务:

[root@wam keepalived]# /etc/init.d/httpd stop

Stopping httpd:                                            [  OK  ]

[root@wam keepalived]#

日志:

###因为我设置的是fall 4,是失败4次之后才任务失败

Mar  3 14:22:26 wam Keepalived_vrrp[40562]: pid 53178 exited with status 1

Mar  3 14:22:31 wam Keepalived_vrrp[40562]: pid 53188 exited with status 1

Mar  3 14:22:36 wam Keepalived_vrrp[40562]: pid 53196 exited with status 1

Mar  3 14:22:41 wam Keepalived_vrrp[40562]: pid 53206 exited with status 1

Mar  3 14:22:41 wam Keepalived_vrrp[40562]: VRRP_Script(chk_httpd) failed

Mar  3 14:22:42 wam Keepalived_vrrp[40562]: VRRP_Instance(VI_1) Changing effective priority from 101 to 99

Mar  3 14:22:43 wam Keepalived_vrrp[40562]: VRRP_Instance(VI_1) Received advert with higher priority 100, ours 99

Mar  3 14:22:43 wam Keepalived_vrrp[40562]: VRRP_Instance(VI_1) Entering BACKUP STATE

Mar  3 14:22:45 wam ntpd[5879]: Deleting interface #15 eth0:0, 10.10.4.253#123, interface stats: received=0, sent=0, dropped=0, active_time=5681 secs


slave上面的效果:

2: eth0: mtu 1500 qdisc mq state UP qlen 1000

    link/ether 00:50:56:97:70:3e brd ff:ff:ff:ff:ff:ff

    inet 10.10.4.11/24 brd 10.10.4.255 scope global eth0

    inet 10.10.4.253/32 scope global eth0:0

    inet6 fe80::250:56ff:fe97:703e/64 scope link 

       valid_lft forever preferred_lft forever