Keepalived实现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
|
# master
# /etc/keepalived/master/keepalived.conf
vrrp_script chk_nginx {
script "/etc/keepalived/master/check_local.sh"
interval 5
}
vrrp_instance VI_1 {
interface em2
state MASTER
#state BACKUP
#nopreempt
virtual_router_id 50
priority 100
advert_int 1
track_interface {
em1
}
authentication {
auth_type PASS
auth_pass sss23234d
}
track_script {
chk_nginx
}
virtual_ipaddress {
210.14.xxx.xxx/32 dev em1 label em1:1
10.10.xxx.xxx/16 dev em2 label em2:1
}
notify_master "/etc/keepalived/master/notify_master.sh"
notify_backup "/etc/keepalived/master/notify_backup.sh"
notify_fault "/etc/keepalived/master/notify_backup.sh"
notify_stop "/etc/keepalived/master/notify_backup.sh"
}
|
从的配置:
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
|
# slave
# /etc/keepalived/slave/keepalived.conf
vrrp_script chk_nginx {
script "/etc/keepalived/slave/check_local.sh"
interval 5
}
vrrp_instance VI_1 {
interface eth1
state BACKUP
#state BACKUP
#nopreempt
virtual_router_id 50
priority 99
advert_int 1
track_interface {
eth0
eth1
}
authentication {
auth_type PASS
auth_pass sss23234d
}
track_script {
chk_nginx
}
virtual_ipaddress {
210.14.xxx.xxx/32 dev eth0 label eth0:1
10.10.xxx.xxx/16 dev eth1 label eth1:1
}
notify_master "/etc/keepalived/slave/notify_master.sh"
notify_backup "/etc/keepalived/slave/notify_backup.sh"
notify_fault "/etc/keepalived/slave/notify_backup.sh"
notify_stop "/etc/keepalived/slave/notify_backup.sh"
}
|
说明:
上面如果将主从都写成下面的配置:
1
2
3
4
|
...
state BACKUP
nopreempt
...
|
这样的配置意味着没有主从之分了,谁先接管服务,只要服务不挂,另外一台起来了也没用,不会发生VIP的转移切换。
check_local.sh
1
2
3
4
5
6
7
8
9
10
11
12
|
#!/bin/bash
RET=`ps awx|grep "nginx_api/nc_api.conf" |grep -v grep`
if [ -z "$RET" ];then
sh /home/bmc/website/nginx_api/restart_nginx_api.sh
#sleep 1
#RET2=`ps awx|grep "nginx_api/nc_api_and_web" |grep -v grep`
#if [ -z "$RET2" ];then
# killall keepalived
#fi
fi
exit 0
|
restart_keepalived.sh
1
2
3
4
|
#!/bin/bash
killall keepalived
keepalived -D -f /etc/keepalived/master/keepalived.conf
|
几个需要注意的问题
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
40
41
42
43
|
# 有下面的日志
Opening file '/etc/keepalived/master/keepalived.conf'.
NOTICE: setting config option max_auto_priority should result in better keepalived performance
# 新版的参考
global_defs {
router_id fundtxjslave
script_user root
enable_script_security
max_auto_priority 1 # 禁止上面的NOTICE->max_auto_priority
}
vrrp_script chk_script {
script "/etc/keepalived/slave/check_local.sh"
interval 6
fall 3
rise 1
weight -5
}
vrrp_instance VI_1 {
interface eno1
state BACKUP
nopreempt
virtual_router_id 59
priority 99
advert_int 2 # 心跳间隔的秒数
track_interface {
eno2
}
authentication {
auth_type PASS
auth_pass wxtl232
}
track_script {
chk_script
}
virtual_ipaddress {
211.19.x.x/27 dev eno1 label eno1:174
10.10.x.x/24 dev eno2 label eno2:174
}
notify_master "/etc/keepalived/slave/notify_master.sh"
notify_backup "/etc/keepalived/slave/notify_backup.sh"
}
|
Keepalived脑裂问题
keepalived是软件级别的浮动IP方案,在使用过程中存在两台主机同时得到VIP的情况,此时请求访问那台服务器是未知的。如果应用是无状态的服务接口还好,但如果服务器上部署的是有状态的数据库服务器,将可能导致数据错乱,两台服务器数据不一致等严重的问题。
要想实现这种系统的高可用方案,可以考虑硬件级别的HA方案;或者在没那个严苛的场合,自己写脚本监控Keepalived软件,防止两台服务器同时获得VIP。
(完)