keepalived的抢占与非抢占模式

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了keepalived的抢占与非抢占模式相关的知识,希望对你有一定的参考价值。

参考技术A

背景:俩节点haproxy通过keepalived实现高可用

说明:harpxy的实际运行过程中,当master发生异常,且后期恢复master正常后,存在抢占或非抢占两种情况。简单点说抢占模式就是,当master宕机后,backup 接管服务。后续当master恢复后,vip漂移到master上,master重新接管服务,多了一次多余的vip切换,而在实际生产中是不需要这样。实际生产中是,当 原先的master恢复后,状态变为backup,不接管服务,这是 非抢占模式。

接下来分4种情况说明

1)俩台都为master时,比如server1的优先级大于server2,keepalived启动后server1获得master,server2自动降级为backup。此时server1宕机的话,server2接替 服务,当server1恢复后,server1又变为master,重新接管服务,server2变为backup。属于抢占式。

2)server1为master,server2位backup,且master优先级大于backup。keepalived启动后server1获得master,server2为backup。当server1宕机后, server2接管服务。当server1恢复后,server1重新接管服务变为master,而server2变为backup。属于抢占式

3)server1为master,server2位backup,且master优先级低于backup。keepalived启动后server2获得master,server1为backup。当server2宕机后, server1接管服务。此时server2恢复后抢占服务,获得master,server1降级将为backup。属于抢占式

以上3种,只要级别高就会获取master,与state状态是无关的

4)server1和server2都为backup。我们要注意 启动server服务的启动顺序 ,先启动的升级为master,与优先级无关。。且配置 nopreempt 项

比如server1获得master权限,server2为backup。此时server1宕机后,server2接管服务升级为master。当server1恢复后权限将为backup,不会争抢 server2的master权限,server2将会继续master权限。属于非抢占式

重点:第4种非抢占式俩节点state必须为bakcup,且必须配置nopreempt

注意:这样配置后,我们要注意启动服务的顺序,优先启动的获取master权限,与优先级没有关系了

总结:抢占模式即MASTER从故障中恢复后,会将VIP从BACKUP节点中抢占过来。非抢占模式即MASTER恢复后不抢占BACKUP升级为MASTER后的VIP

keepalived 非抢占模式

keepalived 非抢占模式

抢占模式为当keepalived的某台机器挂了之后VIP漂移到了备节点,当主节点恢复后主动将VIP再次抢回,keepalived默认工作在抢占模式下,而非抢占模式则是当主节挂了再次起来后不再抢回VIP。此处需要注意非抢占模式的keepalived其工作机制必须都为BACKUP,并且开启nopreempt选项.

实现keepalived 非抢占模式

准备主机2台

server host ip
keepalived s1 172.20.27.10
keepalived s2 172.20.27.11

s1节点配置

修改配置文件

[[email protected] ~]# vim /etc/keepalived/keepalived.conf 
global_defs 
   notification_email 
        [email protected]
   
   notification_email_from [email protected]
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id s1.mylinuxops.com
   vrrp_skip_check_adv_addr
   vrrp_strict
   vrrp_iptables
   vrrp_garp_interval 0
   vrrp_gna_interval 0


vrrp_instance VI_1 
    state BACKUP
    interface ens33
    virtual_router_id 27
    priority 100
    advert_int 2
    nopreempt
    authentication 
        auth_type PASS
        auth_pass 1111
    
    virtual_ipaddress 
        172.20.27.100 dev ens33 label ens33:0
    

重启服务

[[email protected] ~]# systemctl restart keepalived

s2 节点操作

修改配置文件

[[email protected] ~]# vim /etc/keepalived/keepalived.conf 
global_defs 
   notification_email 
        [email protected]
   
   notification_email_from [email protected]
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id s2.mylinuxops.com
   vrrp_skip_check_adv_addr
   vrrp_strict
   vrrp_iptables
   vrrp_garp_interval 0
   vrrp_gna_interval 0


vrrp_instance VI_1 
    state BACKUP
    interface ens33
    virtual_router_id 27
    priority 80
    advert_int 2
    nopreempt
    authentication 
        auth_type PASS
        auth_pass 1111
    
    virtual_ipaddress 
        172.20.27.100 dev ens33 label ens33:0
    

重启服务

[[email protected] ~]# systemctl restart keepalived

测试

查看当前vip所在位置

[[email protected] ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.20.27.10  netmask 255.255.0.0  broadcast 172.20.255.255
        inet6 fe80::20c:29ff:fec5:123c  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:c5:12:3c  txqueuelen 1000  (Ethernet)
        RX packets 71851  bytes 5769380 (5.5 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 30175  bytes 7417306 (7.0 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

ens33:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.20.27.100  netmask 255.255.255.255  broadcast 0.0.0.0
        ether 00:0c:29:c5:12:3c  txqueuelen 1000  (Ethernet)

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 2  bytes 140 (140.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 2  bytes 140 (140.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

当前VIP在s1节点上,当s1节点挂了之后再次查看

[[email protected] ~]# systemctl stop keepalived

VIP已经迁移至s2节点

[[email protected] ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.20.27.11  netmask 255.255.0.0  broadcast 172.20.255.255
        inet6 fe80::20c:29ff:fe4d:1ce3  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:4d:1c:e3  txqueuelen 1000  (Ethernet)
        RX packets 61298  bytes 5145141 (4.9 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 793  bytes 69325 (67.7 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

ens33:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.20.27.100  netmask 255.255.255.255  broadcast 0.0.0.0
        ether 00:0c:29:4d:1c:e3  txqueuelen 1000  (Ethernet)

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 6  bytes 482 (482.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 6  bytes 482 (482.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

此时再次启动s1节点查看vip是否抢回

[[email protected] ~]# systemctl start keepalived
[[email protected] ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.20.27.10  netmask 255.255.0.0  broadcast 172.20.255.255
        inet6 fe80::20c:29ff:fec5:123c  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:c5:12:3c  txqueuelen 1000  (Ethernet)
        RX packets 76211  bytes 6139923 (5.8 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 30392  bytes 7436214 (7.0 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 2  bytes 140 (140.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 2  bytes 140 (140.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
#没有VIP

查看s2节点

[[email protected] ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.20.27.11  netmask 255.255.0.0  broadcast 172.20.255.255
        inet6 fe80::20c:29ff:fe4d:1ce3  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:4d:1c:e3  txqueuelen 1000  (Ethernet)
        RX packets 63311  bytes 5316040 (5.0 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 877  bytes 75815 (74.0 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

ens33:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.20.27.100  netmask 255.255.255.255  broadcast 0.0.0.0
        ether 00:0c:29:4d:1c:e3  txqueuelen 1000  (Ethernet)

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 6  bytes 482 (482.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 6  bytes 482 (482.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

vip依旧在s2节点上。

以上是关于keepalived的抢占与非抢占模式的主要内容,如果未能解决你的问题,请参考以下文章

Keepalived非抢占模式配置

Keepalived的不(非)抢占模式#yyds干货盘点#

Keepalived+Nginx实现高可用(HA)

Keepalived+Nginx实现高可用(HA)

keepalived抢占VIP(单播模式解决)

【keepalived】keepalived的非抢占模式与单播模式