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
|
如果出错有可能缺少以下两个包
yum -y install openssl-devel
yum install popt-devel -y
复制文件到系统中
#cp /usr/local/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/
#cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/
#mkdir /etc/keepalived
#cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/
#cp /usr/local/sbin/keepalived /usr/sbin/
# chmod +x /etc/init.d/keepalived
# chkconfig --add /etc/init.d/keepalived
修改一下 /etc/keepalived/keepalived.conf 这个配置文件就可以用了,以下是我的环境,192.168.10.141和192.168.10.142是两个VIP,可以在两台服务器之间飘动:
主机的配置:
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
}
}
|
#service keepalived start