设置vip:
ifconfig eth0:ha1 10.11.157.63 broadcast 10.11.157.225 netmask 255.255.255.0 up
ifconfig eth0:ha1 10.11.157.63 broadcast 10.11.157.225 netmask 255.255.255.0 down
文档有两种方法,一种为一台服务运行,另一台是备机,然后做虚拟ip,运行shell脚本当主机不能用时 则ip漂移到备机。也可以用keepalive。
第二种方法是两台服务器都运行,都做虚拟ip,当一台坏了的时候把所有ip都漂移到另一台服务器上,写两个脚本 各自检测对方机器是否坏了,然后脚本重做虚拟ip,这种是通过前端dns轮询做出来的 我觉得会出现session共享的问题
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,可以在两台服务器之间飘动:
主机的配置:
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的飘动