keepalived的一些。。

继续采坑。。有些坑,踩了才知道。

1.文件位置不能错。

首先是安装, 源码编译,--prefix=/usr/local/keepalive 

然后用 sbin/keepalive -f  ...conf

那就发现根本生成不了vip ,虚拟ip ,因为你的配置文件没有放在 /etc/keepalived/keepalived.conf ,不生效的。。即使你指定了-f 。

解决方案是

$ cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/keepalived
$ cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
$ cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
$ mkdir -p /etc/keepalived/
$ cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf 

或者干脆用yum 安装,centos 里面有包。能省不少麻烦。

2.virtual_router_id

如果猛报错,像这种

VRRP_Instance(VI_1) Dropping received VRRP packet...
Feb 20 23:21:52 localhost Keepalived_vrrp[1125]: ip address associated with VRID not present in received packet : 192.168.51.230
Feb 20 23:21:52 localhost Keepalived_vrrp[1125]: one or more VIP associated with VRID mismatch actual MASTER advert
Feb 20 23:21:52 localhost Keepalived_vrrp[1125]: bogus VRRP packet received on eth0 !!!

说明网段内的机器有keepalive 了。需要修改。

3.weight 

好像有好多种设置方法。实践下来,都设置成master ,然后安装优先级来抢占vip 。

vrrp_script chk_http_port {
script "/opt/chk.sh"
interval 2
weight -2
}

 

假设这台机器是优先级比较高的master ,脚本执行失败,也就是返回非0 ,就优先级-2 。然后另外的机器就抢到vip 了。自动切为backup 

然后 chk.sh里面可以这样写,比如检测 httpd 进程的

num=`pgrep httpd|wc -l `

if [ $num -eq "0" ] ;

then #没有进程,让脚本返回1

  exit 1 

else  #有进程,脚本返回0 ,正常

  exit 0

fi 

里面也可以加上 killall keepalived ,把自己杀了,就不会再切回来。

 

我的配置文件是 

! Configuration File for keepalived

global_defs {
#   notification_email {
#     acassen@firewall.loc
#     failover@firewall.loc
#     sysadmin@firewall.loc
#   }
#   notification_email_from Alexandre.Cassen@firewall.loc
#   smtp_server 192.168.200.1
#   smtp_connect_timeout 30
   router_id LVS_DEVEL
}
vrrp_script chk_http_port {
    script "/opt/chk.sh"
    interval 2
    weight -2    这里是负数。 
}

vrrp_instance VI_1 {
    state MASTER 都是master 
    interface eth0
    virtual_router_id 52  这个保持一致。
    priority 100   两者不要差太多。
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
#        192.168.200.16
#        192.168.200.17
#        192.168.200.18
	192.168.51.230   vip 
    }
track_script {  注意有个空格。中间。
    chk_http_port
}

}

  实验起来很简单,就开个httpd ,然后主页改下不同,访问vip ,关httpd ,看keepalived 能不能切到 另一台机器上去。如果能的话,网页会显示会不同。网上文章还不少!

 如果是虚拟机,virtual box 的配置环境,可以参看之前的文章。http://www.cnblogs.com/gqdw/p/3556072.html 

posted @ 2014-02-21 01:44  过去的我  阅读(2595)  评论(0编辑  收藏  举报