keepalived的多节点配置可分为两种方案:
1.抢占式方案
2.非抢占式方案
抢占式方案:
本人配置了3个节点,其中1台master,2台backup:
IP | 状态 | 权重 | x.x.x.117 | master | 100 | x.x.x.118 | backup | 90 | x.x.x.123 | backup | 70 | x.x.x.124 | 虚拟IP | |
效果为:如果117挂了,系统会自动从两个backup中找出权重较高的118选举为master,然后118挂了,则最后一台backup 123为master继续提供服务。恢复118,master切换118(因为其?权重较高),再回复第117,master切换为117.
117配置文件信息:
[root@gfmc-web-tomcat1 ~]# cd /etc/keepalived/
[root@gfmc-web-tomcat1 keepalived]# ll
total 12
-rwxr-xr-x. 1 root root 561 Oct 15 2020 check_mycat.sh
-rw-r--r--. 1 root root 709 Sep 11 12:52 keepalived.conf
-rw-r--r--. 1 root root 595 Sep 10 16:52 keepalived.conf_bk20210910
[root@gfmc-web-tomcat1 keepalived]#
[root@gfmc-web-tomcat1 keepalived]#
[root@gfmc-web-tomcat1 keepalived]# cat keepalived.conf
! Configuration File for keepalived
global_defs {
router_id LVS_DEVEL
}
#此模块为 检测脚本,可以不需要
vrrp_script chk_mycat {
script "/etc/keepalived/check_mycat.sh" ## 检测 nginx 状态的脚本路径
interval 2 ## 检测时间间隔
weight -20 ## 如果条件成立,权重-20
}
vrrp_instance VI_1 {
state MASTER
interface ens192 #自己的网卡
virtual_router_id 55
priority 100
advert_int 1
unicast_peer{
10.xx.xxx.118
10.xx.xxx.123
}
authentication {
auth_type PASS
auth_pass 1111
}
#此模块为 检测脚本,可以不需要
track_script {
chk_mycat ## 执行 Nginx 监控的服务
}
virtual_ipaddress {
10.xx.xxx.124
}
}
118配置文件:
! Configuration File for keepalived
global_defs {
router_id LVS_DEVEL_BAC
}
#此模块为监控脚本,可以不需要
vrrp_script chk_mycat {
script "/etc/keepalived/check_mycat.sh" ## 检测 nginx 状态的脚本路径
interval 2 ## 检测时间间隔
weight -20 ## 如果条件成立,权重-20
}
vrrp_instance VI_1 {
state BACKUP
interface ens192
virtual_router_id 55
priority 90 #权重
advert_int 1
unicast_peer{
10.xx.xxx.117
10.xx.xxx.123
}
authentication {
auth_type PASS
auth_pass 1111
}
#此模块为监控脚本,可以不需要
track_script {
chk_mycat ## 执行 Nginx 监控的服务
}
virtual_ipaddress {
10.xx.xxx.124
}
}
123配置文件:
! Configuration File for keepalived
global_defs {
router_id LVS_DEVEL_BAC
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 55
priority 70 #权重
advert_int 1
unicast_peer{
10.xx.xxx.117
10.xx.xxx.118
}
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.xx.xxx.124
}
}
重启三个节点的keepalived:service?keepalived?restart
结束。
非抢占式方案:
IP | 状态 | 权重 | x.x.x.117 | backup | 100 | x.x.x.118 | backup | 90 | x.x.x.123 | backup | 70 | x.x.x.124 | 虚拟IP | |
非抢占式方案,所有节点?state?都是?backup,默认 是谁先开启vip 运行在哪台机器上的。如果发生故障转移,再比较priority。
3个节点在正常运行的情况下(117是master,118和123是backup),117挂了,118变成master,118也挂了,123变成master继续服务,然后恢复118和117,则123还是master。
117配置文件:
! Configuration File for keepalived
global_defs {
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state BACKUP
interface ens192
virtual_router_id 55
priority 100
nopreempt #非抢占式标识,必须添加
advert_int 1
unicast_peer{
10.xx.xxx.118
10.xx.xxx.123
}
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.xx.xxx.124
}
}
118配置文件:
! Configuration File for keepalived
global_defs {
router_id LVS_DEVEL_BAC
}
vrrp_instance VI_1 {
state BACKUP
interface ens192
virtual_router_id 55
priority 90
nopreempt
advert_int 1
unicast_peer{
10.xx.xxx.117
10.xx.xxx.123
}
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.xx.xxx.124
}
}
123配置文件:
! Configuration File for keepalived
global_defs {
router_id LVS_DEVEL_BAC
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 55
priority 70
nopreempt
advert_int 1
unicast_peer{
10.xx.xxx.117
10.xx.xxx.118
}
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.xx.xxx.124
}
}
|