keepalive的简单应用——管理VIP的飘动

VIP的飘动可以为我们解决很多问题,以前我试过使用ifup/ifdown的方式控制网卡的up/down来实现,这种方式有个小问题,就是每次VIP飘动之后都要等上几十秒才能生效,感觉时间比较长,而且还要配合一些逻辑脚本才能很好地工作,有没有更好的方法呢?当然有,这就是本文的主角——keepalived。

      安装很简单:

1
2
3
4
5
tar zxvf keepalived-1.1.20.tar.gz 
cd keepalived-1.1.20
./configure --prefix=/
make
make install

      修改一下 /etc/keepalived/keepalived.conf 这个配置文件就可以用了,以下是我的环境,192.168.10.141和192.168.10.142是两个VIP,可以在两台服务器之间飘动:

image

      主机的配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
global_defs {
    notification_email {
      failover@firewall.loc
    }
    notification_email_from Alexandre.Cassen@firewall.loc
    smtp_server 192.168.0.48
    smtp_connect_timeout 10
    router_id nginx
}
 
vrrp_instance VI_141 {
     state BACKUP
     interface eth0
     virtual_router_id 141
     priority 50
     advert_int 1
     authentication {
         auth_type PASS
         auth_pass 141
     }
     virtual_ipaddress {
         192.168.10.141/26 dev eth0
     }
}
 
vrrp_instance VI_142 {
     state BACKUP
     interface eth0
     virtual_router_id 142
     priority 100
     advert_int 1
     authentication {
         auth_type PASS
         auth_pass 142
     }
     virtual_ipaddress {
         192.168.10.142/26 dev eth0
     }
}

      备机的配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
global_defs {
    notification_email {
      failover@firewall.loc
    }
    notification_email_from Alexandre.Cassen@firewall.loc
    smtp_server 10.168.0.48
    smtp_connect_timeout 10
    router_id nginx
}
 
vrrp_instance VI_141 {
     state BACKUP
     interface eth0
     virtual_router_id 141
     priority 100
     advert_int 1
     authentication {
         auth_type PASS
         auth_pass 141
     }
     virtual_ipaddress {
         192.168.10.141/26 dev eth0
     }
}
 
vrrp_instance VI_142 {
     state BACKUP
     interface eth0
     virtual_router_id 142
     priority 50
     advert_int 1
     authentication {
         auth_type PASS
         auth_pass 142
     }
     virtual_ipaddress {
         192.168.10.142/26 dev eth0
     }
}

      乍一看,主机和备机的配置文件是一样的,仔细看一下priority的值,使用以下命令即可将keepalived加入linux的服务中:

1
chkconfig --add keepalived ;

      通过启、停keepalived这个服务即可观察到VIP的飘动,至于为什么VIP飘动后可以很快地生效,还有待研究。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值