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
{ } notification_email_from
[email protected] 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
{ } notification_email_from
[email protected] 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飄動後可以很快地生效,還有待研究。