Keepalived 高可用配置

  • Keepalived 是一种高性能的服务器高可用或热备解决方案,Keepalived 可以用来防止服务器单点故障的发生,通过配合 Nginx 可以实现 Web 前端服务的高可用

1. 安装 Keepalived

1
2
3
4
5
6
7
8
9
$ sudo apt install keepalived

$ tar -zxvf keepalived-2.0.18.tar.gz
$ cd keepalived-2.0.18
$ ./configure --prefix=/usr/local/keepalived
$ make && make install
$ cp keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
$ cp keepalived/etc/init.d/keepalived /etc/init.d/
$ cp keepalived/etc/sysconfig/keepalived /etc/sysconfig/

2. 准备检查脚本

(1) check_nginx.sh

1
2
3
4
5
6
7
8
9
10
$ sudo vi /etc/keepalived/check_nginx.sh
#!/bin/bash
COUNT=`ps -C nginx --no-header |wc -l`
if [ $COUNT -eq 0 ]; then
echo 'nginx server is died'
sudo systemctl stop keepalived.service # 需要停止keepalived服务才能漂移到其他节点
#killall keepalived
fi

$ sudo chmod +x /etc/keepalived/check_nginx.sh

(2) check_nacos.sh

1
2
3
4
5
6
7
8
9
$ sudo vi /etc/keepalived/check_nacos.sh
#!/bin/bash
COUNT=$(ps -ef | grep nacos |grep -v "check_nacos" | grep -v "grep" | wc -l )
if [ $COUNT -eq 0 ]; then
echo 'nacos server is died'
sudo systemctl stop keepalived.service
fi

$ sudo chmod +x /etc/keepalived/check_nacos.sh

3. 配置 Keepalived

  • MASTER
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
44
45
46
47
48
49
50
51
52
53
$ sudo vi /etc/keepalived/keepalived.conf
global_defs {
router_id debian101
}

vrrp_script check_nginx_alive {
script "/etc/keepalived/check_nginx.sh"
interval 3
weight -10
}

vrrp_instance VI_1 {
state MASTER
interface ens32
mcast_src_ip 10.4.7.101
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1643
}
virtual_ipaddress {
10.4.7.100
}
track_script {
check_nginx_alive
}
}

vrrp_script check_nacos_alive {
script "/etc/keepalived/check_nacos.sh"
interval 2
}

vrrp_instance VI_2 {
state MASTER
interface ens32
mcast_src_ip 10.4.7.101
virtual_router_id 52
priority 103
advert_int 1
authentication {
auth_type PASS
auth_pass 16434
}
track_script {
check_nacos_alive
}
virtual_ipaddress {
10.4.7.100
}
}
  • BACKUP
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
44
45
46
47
48
49
50
51
52
53
54
55
$ sudo vi /etc/keepalived/keepalived.conf
vrrp_script check_nginx_alive {
script "/etc/keepalived/check_nginx.sh"
interval 3
weight -10
}

global_defs {
router_id debian102
}

vrrp_instance VI_1 {
state BACKUP
interface ens32
mcast_src_ip 10.4.7.102
unicast_src_ip 10.4.7.102
virtual_router_id 51
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 1643
}
virtual_ipaddress {
10.4.7.100
}
track_script {
check_nginx_alive
}
}

vrrp_script check_nacos_alive {
script "/etc/keepalived/check_nacos.sh"
interval 2
}

vrrp_instance VI_2 {
state BACKUP
interface ens32
mcast_src_ip 10.4.7.102
unicast_src_ip 10.4.7.102
virtual_router_id 52
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 16434
}
virtual_ipaddress {
10.4.7.100
}
track_script {
check_nacos_alive
}
}
  • 重启服务
1
2
$ sudo systemctl restart keepalived
$ sudo systemctl enable keepalived

4. 配置文件详解

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
44
45
46
47
48
49
50
51
52
53
54
55
56
global_defs {
# keepalived自带的邮件提醒需要开启sendmail服务。建议用独立的监控或第三方SMTP
notification_email {
xxx@xxx.xxx
}
   notification_email_from xxx@xxx.xxx
smtp_server 127.0.0.1
smtp_connect_timeout 30
# 标识本节点的字条串,通常为hostname
router_id LVS_DEVEL
}

# keepalived会定时执行脚本并对执行结果进行分析,并动态调整vrrp_instance的优先级priority
# 如果脚本执行结果为0,并且weight配置的值大于0,则优先级相应的增加
# 如果脚本执行结果非0,并且weight配置的值小于0,则优先级相应的减少
# 其他情况,维持原本配置的优先级,即配置文件中priority对应的值
vrrp_script check_nginx_alive {
# 检测服务状态的脚本路径
script "/etc/keepalived/check_nginx.sh"
# 检测时间间隔
interval 3
# 如果条件成立,权重-10
weight -10
}

# 定义虚拟路由,VI_1为虚拟路由的标示符,可自己定义
vrrp_instance VI_1 {
# 主节点为MASTER,对应的备份节点为BACKUP
state MASTER
# 绑定虚拟IP的网络接口,与本机IP地址所在的网络接口相同
interface ens32
# 本机IP地址
mcast_src_ip 10.4.7.101
# 虚拟路由的ID号,多个节点设置必须一样,可选IP最后一段使用。相同的VRID为一个组,它将决定多播的MAC地址
virtual_router_id 51
# 节点优先级,值范围0-254,MASTER要比BACKUP高
priority 100
# 组播信息发送间隔,多个节点设置必须一样,默认1s
advert_int 1
# 设置验证信息,多个节点必须一致
authentication {
auth_type PASS
# 生产环境注意强度
auth_pass 1643
}
# 虚拟IP池,两个节点设置必须一样
virtual_ipaddress {
# 虚拟ip,可以定义多个
10.4.7.100
}
# 将track_script块加入instance配置块
track_script {
# 执行监控的服务
check_nginx_alive
}
}

参考