keepalived原理

一、Keepalived简介

Keepalived是Linux下一个轻量级别的高可用解决方案。高可用:广义来讲,是指整个系统的高可用行;狭义的来讲就是主机的冗余和接管。
它与HeartBeat实现类似的功能,都可以实现服务或者网络的高可用,但是又有差别,HeartBeat是一个专业的、功能完善的高可用软件,它提供HA软件所需的基本功能,比如:心跳检测、资源接管,检测集群中的服务,在集群节点转移共享IP地址的所有者等等。HeartBeat功能强大,但是部署和使用相对比较麻烦,与HeartBeat相比,Keepalived主要是通过虚拟路由冗余来实现高可用功能,虽然它没有HeartBeat功能强大,但是Keepalived部署和使用非常的简单,所有配置只需要一个配置文件即可以完成。

Keepalived起初是为LVS设计的,专门用来监控集群系统中各个服务节点的状态,它根据TCP/IP参考模型的第三、第四层、第五层交换机制检测每个服务节点的状态,如果某个服务器节点出现异常,或者工作出现故障,Keepalived将检测到,并将出现的故障的服务器节点从集群系统中剔除,这些工作全部是自动完成的,不需要人工干涉,需要人工完成的只是修复出现故障的服务节点。

后来Keepalived又加入了VRRP的功能,VRRP(Vritrual Router Redundancy Protocol,虚拟路由冗余协议)出现的目的是解决静态路由出现的单点故障问题,通过VRRP可以实现网络不间断稳定运行,因此Keepalvied一方面具有服务器状态检测和故障隔离功能,另外一方面也有HAcluster功能。

健康检查和失败切换是keepalived的两大核心功能。所谓的健康检查,就是采用tcp三次握手,icmp请求,http请求,udp echo请求等方式对负载均衡器后面的实际的服务器进行探测;而失败切换主要是应用于配置了主备模式的负载均衡器,利用VRRP维持主备负载均衡器的心跳,当主负载均衡器出现问题时,由备负载均衡器承载对应的业务,从而在最大限度上减少流量损失,并提供服务的稳定性。

二、VRRP协议与工作原理

在现实的网络环境中。主机之间的通信都是通过配置静态路由或者(默认网关)来完成的,而主机之间的路由器一旦发生故障,通信就会失效,因此这种通信模式当中,路由器就成了一个单点瓶颈,为了解决这个问题,就引入了VRRP协议。

VRRP协议是一种容错的主备模式的协议,保证当主机的下一跳路由出现故障时,由另一台路由器来代替出现故障的路由器进行工作,通过VRRP可以在网络发生故障时透明的进行设备切换而不影响主机之间的数据通信。
在这里插入图片描述

虚拟路由器:虚拟路由器是VRRP备份组中所有路由器的集合,它是一个逻辑概念,并不是正真存在的。从备份组外面看备份组中的路由器,感觉组中的所有路由器就像一个 一样,可以理解为在一个组中: 主路由器+所有备份路由器=虚拟路由器。虚拟路由器有一个虚拟的IP地址和MAC地址。主机将虚拟路由器当作默认网关。虚拟MAC地址的格式为00-00-5E-00-01-{VRID}。通常情况下,虚拟路由器回应ARP请求使用的是虚拟MAC地址,只有虚拟路由器做特殊配置的时候,才回应接口的真实MAC地址。

主路由器(MASTER):虚拟路由器通过虚拟IP对外提供服务,而在虚拟路由器内部同一时间只有一台物理路由器对外提供服务,这台提供服务的物理路由器被称为主路由器。一般情况下Master是由选举算法产生,它拥有对外服务的虚拟IP,提供各种网络功能,如:ARP请求,ICMP数据转发等。

备份路由器(BACKUP):虚拟路由器中的其他物理路由器不拥有对外的虚拟IP,也不对外提供网络功能,仅接受MASTER的VRRP状态通告信息,这些路由器被称为备份路由器。当主路由器失败时,处于BACKUP角色的备份路由器将重新进行选举,产生一个新的主路由器进入MASTER角色,继续提供对外服务,整个切换对用户来说是完全透明的。

三、VRRP选举机制

VRRP路由器在运行过程中有三种状态:

  1. Initialize状态: 系统启动后就进入Initialize,此状态下路由器不对VRRP报文做任何处理;
  2. Master状态;
  3. Backup状态;
    一般主路由器处于Master状态,备份路由器处于Backup状态。

VRRP使用选举机制来确定路由器的状态,优先级选举:
1.VRRP组中IP拥有者。如果虚拟IP地址与VRRP组中的某台VRRP路由器IP地址相同,则此路由器为IP地址拥有者,这台路由器将被定位主路由器。
2.比较优先级。如果没有IP地址拥有者,则比较路由器的优先级,优先级的范围是0~255,优先级大的作为主路由器
3.比较IP地址。在没有Ip地址拥有者和优先级相同的情况下,IP地址大的作为主路由器。

如下图所示,虚拟IP为10.1.1.254,在VRRP组中没有IP地址拥有者,则比较优先级,很明显RB和RA的优先级要大于RC,则比较RA和RB的IP地址,RB的IP地址大。所以RB为组中的主路由器。
在这里插入图片描述

四、工作过程

路由器使用VRRP 功能后,会根据优先级确定自己在备份组中的角色。优先级高的路由器成为Master 路由器,优先级低的成为Backup 路由器。Master 拥有对外服务的虚拟IP,提供各种网络功能,并定期发送VRRP 报文,通知备份组内的其他设备自己工作正常;Backup 路由器只接收Master 发来的报文信息,用来监控Master 的运行状态。当Master 失效时,Backup 路由器进行选举,优先级高的Backup 将成为新的Master 。

在抢占方式下,当Backup 路由器收到VRRP 报文后,会将自己的优先级与报文中的优先级进行比较。如果大于通告报文中的优先级,则成为Master 路由器;否则将保持Backup状态;

在非抢占方式下,只要Master 路由器没有出现故障,备份组中的路由器始终保持Master 或Backup 状态,Backup 路由器即使随后被配置了更高的优先级也不会成为Master 路由器;

如果Backup 路由器的定时器超时后仍未收到Master 路由器发送来的VRRP报文,则认为Master 路由器已经无法正常工作,此时Backup 路由器会认为自己是Master 路由器,并对外发送VRRP报文。备份组内的路由器根据优先级选举出Master 路由 器,承担报文的转发功能。

五、Keepalvied的工作原理

Keepalived对服务器运行状态和故障隔离的工作原理:
Keepalived工作在TCP/IP参考模型的三层、四层、五层(物理层,链路层):
网络层(3):Keepalived通过ICMP协议向服务器集群中的每一个节点发送一个ICMP数据包(有点类似与Ping的功能),如果某个节点没有返回响应数据包,那么认为该节点发生了故障,Keepalived将报告这个节点失效,并从服务器集群中剔除故障节点。

传输层(4):Keepalived在传输层里利用了TCP协议的端口连接和扫描技术来判断集群节点的端口是否正常,比如对于常见的WEB服务器80端口。或者SSH服务22端口,Keepalived一旦在传输层探测到这些端口号没有数据响应和数据返回,就认为这些端口发生异常,然后强制将这些端口所对应的节点从服务器集群中剔除掉。

应用层(5):,Keepalived的运行方式也更加全面化和复杂化,用户可以通过自定义Keepalived工作方式,例如:可以通过编写程序或者脚本来运行Keepalived,而Keepalived将根据用户的设定参数检测各种程序或者服务是否允许正常,如果Keepalived的检测结果和用户设定的不一致时,Keepalived将把对应的服务器从服务器集群中剔除。

六、Keepalived体系结构

Keepalived起初是为LVS设计的,由于Keeplalived可以实现对集群节点的状态检测,而IPVS可以实现负载均衡功能,因此,Keepalived借助于第三方模块IPVS就可以很方便地搭建一套负载均衡系统。在Keepalived当中IPVS模块是可配置的,如果需要负载均衡功能,可以在编译Keepalived时开打负载均衡功能,也可以通过编译参数关闭。
在这里插入图片描述
SchedulerI/OMultiplexer是一个I/O复用分发调度器,它负载安排Keepalived所有内部的任务请求;

Memory Mngt是一个内存管理机制,这个框架提供了访问内存的一些通用方法;

Control Plane 是keepalived的控制版面,可以实现对配置文件编译和解析;

Core componets 这部分主要包含了5个部分;

1、Watchdog:是计算机可靠领域中极为简单又非常有效的检测工具,Keepalived正是通过它监控Checkers和VRRP进程的。
2、Checkers:这是Keepalived最基础的功能,也是最主要的功能,可以实现对服务器运行状态检测和故障隔离。
3、VRRP Stack:这是keepalived后来引用VRRP功能,可以实现HA集群中失败切换功能。负责负载均衡器之间的失败切换FailOver;
4、IPVS wrapper:这个是IPVS功能的一个实现,IPVSwarrper模块将可以设置好的IPVS规则发送的内核空间并且提供给IPVS模块,最终实现IPVS模块的负载功能。
5、Netlink Reflector:用来实现高可用集群Failover时虚拟IP(VIP)的设置和切换,
keepalived运行时,会启动3个进程,分别为:core(核心进程),check和vrrp

  • core:负责主进程的启动,维护和全局配置文件的加载;
  • check:负责健康检查
  • vrrp:用来实现vrrp协议

七、Keepalived高可用实验

实验环境:
两台centos主机:192.168.239.128(主) 192.168.239.130(备)

单主模型
工作在抢占方式下
1、在两台主机上安装keepalived

[root@centos7 ~]# yum install keepalived -y

2、修改/etc/keepalived/keepalived.conf配置文件,修改之前最好做一下备份。
备份配置文件

[root@centos7 keepalived]# cp keepalived.conf{,.bak}

在主节点上

[root@centos7 keepalived]# vim keepalived.conf
! Configuration File for keepalived

global_defs {
   notification_email {
	root@localhost
   }
   notification_email_from keepalived@localhost
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id node128
   vrrp_mcast_group4 224.0.100.19
}

vrrp_instance VI_1 {
    state MASTER
    interface ens33
    virtual_router_id 51
    priority 100
    advert_int 2
    authentication {
        auth_type PASS
        auth_pass crFNzHqV
    }
    virtual_ipaddress {
        192.168.239.98/24 dev ens33 label ens33:1
    }
}

备节点
在备节点上vrrp_instance实例中virtual_router_id,authentication,virtual_ipaddress要与主节点的一致。

[root@centos7 keepalived]# vim keepalived.conf
! Configuration File for keepalived

global_defs {
   notification_email {
	root@localhost
   }
   notification_email_from keepalived@localhost
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id node130
   vrrp_mcast_group4 224.0.100.19
}

vrrp_instance VI_1 {
    state BACKUP
    interface ens33
    virtual_router_id 51
    priority 95
    advert_int 2
    authentication {
        auth_type PASS
        auth_pass cafNzHqV
    }
    virtual_ipaddress {
        192.168.239.98/24 dev ens33 label ens33:1
    }
}

3、启动服务
先启动备节点

[root@centos7 keepalived]# systemctl start keepalived

查看keepalived的状态信息,发现VRRP_Instance(VI_1)在ens33上为192.168.239.98发送免费ARP。

[root@centos7 keepalived]# systemctl status keepalived.service
● keepalived.service - LVS and VRRP High Availability Monitor
   Loaded: loaded (/usr/lib/systemd/system/keepalived.service; disabled; vendor preset: disabled)
   Active: active (running) since 六 2019-11-02 16:53:47 CST; 2min 13s ago
  Process: 9207 ExecStart=/usr/sbin/keepalived $KEEPALIVED_OPTIONS (code=exited, status=0/SUCCESS)
 Main PID: 9210 (keepalived)
   CGroup: /system.slice/keepalived.service
           ├─9210 /usr/sbin/keepalived -D
           ├─9211 /usr/sbin/keepalived -D
           └─9212 /usr/sbin/keepalived -D

11月 02 16:53:56 centos7.cwj.com Keepalived_vrrp[9212]: Sending gratuitous ARP on ens33 for 192.168.239.98
11月 02 16:53:56 centos7.cwj.com Keepalived_vrrp[9212]: Sending gratuitous ARP on ens33 for 192.168.239.98
11月 02 16:53:56 centos7.cwj.com Keepalived_vrrp[9212]: Sending gratuitous ARP on ens33 for 192.168.239.98
11月 02 16:53:56 centos7.cwj.com Keepalived_vrrp[9212]: Sending gratuitous ARP on ens33 for 192.168.239.98
11月 02 16:54:01 centos7.cwj.com Keepalived_vrrp[9212]: Sending gratuitous ARP on ens33 for 192.168.239.98
11月 02 16:54:01 centos7.cwj.com Keepalived_vrrp[9212]: VRRP_Instance(VI_1) Sending/queueing gratuitous ARPs on ens33 for 192.168.239.98
11月 02 16:54:01 centos7.cwj.com Keepalived_vrrp[9212]: Sending gratuitous ARP on ens33 for 192.168.239.98
11月 02 16:54:01 centos7.cwj.com Keepalived_vrrp[9212]: Sending gratuitous ARP on ens33 for 192.168.239.98
11月 02 16:54:01 centos7.cwj.com Keepalived_vrrp[9212]: Sending gratuitous ARP on ens33 for 192.168.239.98
11月 02 16:54:01 centos7.cwj.com Keepalived_vrrp[9212]: Sending gratuitous ARP on ens33 for 192.168.239.98

查看网卡信息,发现地址是附着在备节点上

[root@centos7 keepalived]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.239.130  netmask 255.255.255.0  broadcast 192.168.239.255
        inet6 fe80::20c:29ff:fe65:a88f  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:65:a8:8f  txqueuelen 1000  (Ethernet)
        RX packets 4923  bytes 436498 (426.2 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 2771  bytes 343790 (335.7 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

ens33:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.239.98  netmask 255.255.255.0  broadcast 0.0.0.0
        ether 00:0c:29:65:a8:8f  txqueuelen 1000  (Ethernet)

在备节点上抓取数据包,发现备节点一直在向组播地址传输心跳包,可以看到备节点的优先级为95。

[root@centos7 keepalived]# tcpdump  -i ens33  -nn host  224.0.100.19
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on ens33, link-type EN10MB (Ethernet), capture size 262144 bytes
16:57:38.555952 IP 192.168.239.130 > 224.0.100.19: VRRPv2, Advertisement, vrid 51, prio 95, authtype simple, intvl 2s, length 20
16:57:40.558172 IP 192.168.239.130 > 224.0.100.19: VRRPv2, Advertisement, vrid 51, prio 95, authtype simple, intvl 2s, length 20
16:57:42.560515 IP 192.168.239.130 > 224.0.100.19: VRRPv2, Advertisement, vrid 51, prio 95, authtype simple, intvl 2s, length 20
16:57:44.561900 IP 192.168.239.130 > 224.0.100.19: VRRPv2, Advertisement, vrid 51, prio 95, authtype simple, intvl 2s, length 20
16:57:46.563482 IP 192.168.239.130 > 224.0.100.19: VRRPv2, Advertisement, vrid 51, prio 95, authtype simple, intvl 2s, length 20
16:57:48.565145 IP 192.168.239.130 > 224.0.100.19: VRRPv2, Advertisement, vrid 51, prio 95, authtype simple, intvl 2s, length 20
16:57:50.566776 IP 192.168.239.130 > 224.0.100.19: VRRPv2, Advertisement, vrid 51, prio 95, authtype simple, intvl 2s, length 20
16:57:52.568622 IP 192.168.239.130 > 224.0.100.19: VRRPv2, Advertisement, vrid 51, prio 95, authtype simple, intvl 2s, length 20

启动主节点

[root@centos7 keepalived]# systemctl restart keepalived

查看keepalived状态信息,发现主节点启动后将进入主节点状态。

[root@centos7 keepalived]# systemctl status keepalived
● keepalived.service - LVS and VRRP High Availability Monitor
   Loaded: loaded (/usr/lib/systemd/system/keepalived.service; disabled; vendor preset: disabled)
   Active: active (running) since 六 2019-11-02 17:11:55 CST; 2s ago
  Process: 14074 ExecStart=/usr/sbin/keepalived $KEEPALIVED_OPTIONS (code=exited, status=0/SUCCESS)
 Main PID: 14075 (keepalived)
   CGroup: /system.slice/keepalived.service
           ├─14075 /usr/sbin/keepalived -D
           ├─14076 /usr/sbin/keepalived -D
           └─14077 /usr/sbin/keepalived -D

11月 02 17:11:55 centos7.cwj.com Keepalived_healthcheckers[14076]: Initializing ipvs
11月 02 17:11:55 centos7.cwj.com Keepalived_healthcheckers[14076]: Opening file '/etc/keepalived/keepalived.conf'.
11月 02 17:11:55 centos7.cwj.com Keepalived_vrrp[14077]: Registering Kernel netlink reflector
11月 02 17:11:55 centos7.cwj.com Keepalived_vrrp[14077]: Registering Kernel netlink command channel
11月 02 17:11:55 centos7.cwj.com Keepalived_vrrp[14077]: Registering gratuitous ARP shared channel
11月 02 17:11:55 centos7.cwj.com Keepalived_vrrp[14077]: Opening file '/etc/keepalived/keepalived.conf'.
11月 02 17:11:55 centos7.cwj.com Keepalived_vrrp[14077]: VRRP_Instance(VI_1) removing protocol VIPs.
11月 02 17:11:55 centos7.cwj.com Keepalived_vrrp[14077]: Using LinkWatch kernel netlink reflector...
11月 02 17:11:55 centos7.cwj.com Keepalived_vrrp[14077]: VRRP sockpool: [ifindex(2), proto(112), unicast(0), fd(10,11)]
11月 02 17:11:56 centos7.cwj.com Keepalived_vrrp[14077]: VRRP_Instance(VI_1) Transition to MASTER STATE

这时查看网卡信息,发现地址已经转移到主节点上了

[root@centos7 keepalived]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.239.128  netmask 255.255.255.0  broadcast 192.168.239.255
        inet6 fe80::20c:29ff:fe60:4537  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:60:45:37  txqueuelen 1000  (Ethernet)
        RX packets 7612  bytes 723622 (706.6 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 5394  bytes 813368 (794.3 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

ens33:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.239.98  netmask 255.255.255.0  broadcast 0.0.0.0
        ether 00:0c:29:60:45:37  txqueuelen 1000  (Ethernet)

在备节点上抓取数据包,这时发现是主节点的地址一直在向组播地址传输心跳包,同时可以看到主节点的优先级为100

[root@centos7 keepalived]# tcpdump  -i ens33  -nn host  224.0.100.19
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on ens33, link-type EN10MB (Ethernet), capture size 262144 bytes
17:16:04.294426 IP 192.168.239.128 > 224.0.100.19: VRRPv2, Advertisement, vrid 51, prio 100, authtype simple, intvl 2s, length 20
17:16:06.296387 IP 192.168.239.128 > 224.0.100.19: VRRPv2, Advertisement, vrid 51, prio 100, authtype simple, intvl 2s, length 20
17:16:08.297939 IP 192.168.239.128 > 224.0.100.19: VRRPv2, Advertisement, vrid 51, prio 100, authtype simple, intvl 2s, length 20
17:16:10.300131 IP 192.168.239.128 > 224.0.100.19: VRRPv2, Advertisement, vrid 51, prio 100, authtype simple, intvl 2s, length 20
17:16:12.301214 IP 192.168.239.128 > 224.0.100.19: VRRPv2, Advertisement, vrid 51, prio 100, authtype simple, intvl 2s, length 20
17:16:14.302847 IP 192.168.239.128 > 224.0.100.19: VRRPv2, Advertisement, vrid 51, prio 100, authtype simple, intvl 2s, length 20
17:16:16.305257 IP 192.168.239.128 > 224.0.100.19: VRRPv2, Advertisement, vrid 51, prio 100, authtype simple, intvl 2s, length 20
17:16:18.306566 IP 192.168.239.128 > 224.0.100.19: VRRPv2, Advertisement, vrid 51, prio 100, authtype simple, intvl 2s, length 20

双主模型
使用双主模型得在配置文件中配置两个vrrp_instance实例,其中一个vrrp_instance配置为主节点,另一个配置为备用节点。
1、编辑配置文件
node1节点的配置

[root@centos7 keepalived]# cat keepalived.conf
! Configuration File for keepalived

global_defs {
   notification_email {
	root@localhost
   }
   notification_email_from keepalived@localhost
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id node128
   vrrp_mcast_group4 224.0.100.19
}

vrrp_instance VI_1 {
    state MASTER
    interface ens33
    virtual_router_id 51
    priority 100
    advert_int 2
    authentication {
        auth_type PASS
        auth_pass cafNzHqV
    }
    virtual_ipaddress {
        192.168.239.98/24 dev ens33 label ens33:1
    }
}
vrrp_instance VI_2 {
    state BACKUP
    interface ens33
    virtual_router_id 15
    priority 95
    advert_int 2
    authentication {
	auth_type PASS
	auth_pass 578f07b2
    }
    virtual_ipaddress {
	192.168.239.99/24 dev ens33 label ens33:2
    }
}

node2节点的配置

[root@centos7 keepalived]# cat keepalived.conf
! Configuration File for keepalived

global_defs {
   notification_email {
	root@localhost
   }
   notification_email_from keepalived@localhost
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id node130
   vrrp_mcast_group4 224.0.100.19
}

vrrp_instance VI_1 {
    state BACKUP
    interface ens33
    virtual_router_id 51
    priority 95
    advert_int 2
    authentication {
        auth_type PASS
        auth_pass cafNzHqV
    }
    virtual_ipaddress {
        192.168.239.98/24 dev ens33 label ens33:1
    }
}
vrrp_instance VI_2 {
    state MASTER
    interface ens33
    virtual_router_id 15
    priority 100
    advert_int 2
    authentication {
        auth_type PASS
        auth_pass 578f07b2
    }
    virtual_ipaddress {
        192.168.239.99/24 dev ens33 label ens33:2
    }
}

2、先启动node2节点
查看网卡信息,发现两个地址都在node2节点上

[root@centos7 keepalived]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.239.130  netmask 255.255.255.0  broadcast 192.168.239.255
        inet6 fe80::20c:29ff:fe65:a88f  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:65:a8:8f  txqueuelen 1000  (Ethernet)
        RX packets 7069  bytes 633752 (618.8 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 10957  bytes 909675 (888.3 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

ens33:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.239.98  netmask 255.255.255.0  broadcast 0.0.0.0
        ether 00:0c:29:65:a8:8f  txqueuelen 1000  (Ethernet)

ens33:2: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.239.99  netmask 255.255.255.0  broadcast 0.0.0.0
        ether 00:0c:29:65:a8:8f  txqueuelen 1000  (Ethernet)

抓取数据包

[root@centos7 keepalived]# tcpdump  -i ens33  -nn host  224.0.100.19
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on ens33, link-type EN10MB (Ethernet), capture size 262144 bytes
23:48:45.105576 IP 192.168.239.130 > 224.0.100.19: VRRPv2, Advertisement, vrid 15, prio 100, authtype simple, intvl 2s, length 20
23:48:47.107655 IP 192.168.239.130 > 224.0.100.19: VRRPv2, Advertisement, vrid 15, prio 100, authtype simple, intvl 2s, length 20
23:48:47.731820 IP 192.168.239.130 > 224.0.100.19: VRRPv2, Advertisement, vrid 51, prio 95, authtype simple, intvl 2s, length 20
23:48:49.108729 IP 192.168.239.130 > 224.0.100.19: VRRPv2, Advertisement, vrid 15, prio 100, authtype simple, intvl 2s, length 20
23:48:49.734413 IP 192.168.239.130 > 224.0.100.19: VRRPv2, Advertisement, vrid 51, prio 95, authtype simple, intvl 2s, length 20
23:48:51.109241 IP 192.168.239.130 > 224.0.100.19: VRRPv2, Advertisement, vrid 15, prio 100, authtype simple, intvl 2s, length 20
23:48:51.736300 IP 192.168.239.130 > 224.0.100.19: VRRPv2, Advertisement, vrid 51, prio 95, authtype simple, intvl 2s, length 20
23:48:53.110677 IP 192.168.239.130 > 224.0.100.19: VRRPv2, Advertisement, vrid 15, prio 100, authtype simple, intvl 2s, length 20
23:48:53.737638 IP 192.168.239.130 > 224.0.100.19: VRRPv2, Advertisement, vrid 51, prio 95, authtype simple, intvl 2s, length 20
23:48:55.112465 IP 192.168.239.130 > 224.0.100.19: VRRPv2, Advertisement, vrid 15, prio 100, authtype simple, intvl 2s, length 20
23:48:55.739794 IP 192.168.239.130 > 224.0.100.19: VRRPv2, Advertisement, vrid 51, prio 95, authtype simple, intvl 2s, length 20

3、这时启动node1节点
查看网卡信息,发现192.168.239.98这个地址转移过来了。

[root@centos7 keepalived]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.239.128  netmask 255.255.255.0  broadcast 192.168.239.255
        inet6 fe80::20c:29ff:fe60:4537  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:60:45:37  txqueuelen 1000  (Ethernet)
        RX packets 10019  bytes 943483 (921.3 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 7334  bytes 1038441 (1014.1 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

ens33:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.239.98  netmask 255.255.255.0  broadcast 0.0.0.0
        ether 00:0c:29:60:45:37  txqueuelen 1000  (Ethernet)


再去查看node2节点的状态信息,这时会发现VRRP_Instance(VI_1)进入了备用状态。

[root@centos7 keepalived]# systemctl status keepalived.service 
● keepalived.service - LVS and VRRP High Availability Monitor
   Loaded: loaded (/usr/lib/systemd/system/keepalived.service; disabled; vendor preset: disabled)
   Active: active (running) since 六 2019-11-02 23:48:41 CST; 2min 26s ago
  Process: 11574 ExecStart=/usr/sbin/keepalived $KEEPALIVED_OPTIONS (code=exited, status=0/SUCCESS)
 Main PID: 11575 (keepalived)
   CGroup: /system.slice/keepalived.service
           ├─11575 /usr/sbin/keepalived -D
           ├─11576 /usr/sbin/keepalived -D
           └─11577 /usr/sbin/keepalived -D

11月 02 23:48:50 centos7.cwj.com Keepalived_vrrp[11577]: Sending gratuitous ARP on ens33 for 192.168.239.99
11月 02 23:48:54 centos7.cwj.com Keepalived_vrrp[11577]: Sending gratuitous ARP on ens33 for 192.168.239.98
11月 02 23:48:54 centos7.cwj.com Keepalived_vrrp[11577]: VRRP_Instance(VI_1) Sending/queueing gratuitous ARPs on ens33 for 192.168.239.98
11月 02 23:48:54 centos7.cwj.com Keepalived_vrrp[11577]: Sending gratuitous ARP on ens33 for 192.168.239.98
11月 02 23:48:54 centos7.cwj.com Keepalived_vrrp[11577]: Sending gratuitous ARP on ens33 for 192.168.239.98
11月 02 23:48:54 centos7.cwj.com Keepalived_vrrp[11577]: Sending gratuitous ARP on ens33 for 192.168.239.98
11月 02 23:48:54 centos7.cwj.com Keepalived_vrrp[11577]: Sending gratuitous ARP on ens33 for 192.168.239.98
11月 02 23:50:41 centos7.cwj.com Keepalived_vrrp[11577]: VRRP_Instance(VI_1) Received advert with higher priority 100, ours 95
11月 02 23:50:41 centos7.cwj.com Keepalived_vrrp[11577]: VRRP_Instance(VI_1) Entering BACKUP STATE
11月 02 23:50:41 centos7.cwj.com Keepalived_vrrp[11577]: VRRP_Instance(VI_1) removing protocol VIPs

再去node2节点抓取数据包,发现两个节点都在向组播地址传输心跳包,同时可以看到优先级都为100。

[root@centos7 keepalived]# tcpdump  -i ens33  -nn host  224.0.100.19
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on ens33, link-type EN10MB (Ethernet), capture size 262144 bytes
23:53:36.022337 IP 192.168.239.128 > 224.0.100.19: VRRPv2, Advertisement, vrid 51, prio 100, authtype simple, intvl 2s, length 20
23:53:37.293328 IP 192.168.239.130 > 224.0.100.19: VRRPv2, Advertisement, vrid 15, prio 100, authtype simple, intvl 2s, length 20
23:53:38.023824 IP 192.168.239.128 > 224.0.100.19: VRRPv2, Advertisement, vrid 51, prio 100, authtype simple, intvl 2s, length 20
23:53:39.294652 IP 192.168.239.130 > 224.0.100.19: VRRPv2, Advertisement, vrid 15, prio 100, authtype simple, intvl 2s, length 20
23:53:40.025126 IP 192.168.239.128 > 224.0.100.19: VRRPv2, Advertisement, vrid 51, prio 100, authtype simple, intvl 2s, length 20
23:53:41.296202 IP 192.168.239.130 > 224.0.100.19: VRRPv2, Advertisement, vrid 15, prio 100, authtype simple, intvl 2s, length 20
23:53:42.026760 IP 192.168.239.128 > 224.0.100.19: VRRPv2, Advertisement, vrid 51, prio 100, authtype simple, intvl 2s, length 20
23:53:43.297751 IP 192.168.239.130 > 224.0.100.19: VRRPv2, Advertisement, vrid 15, prio 100, authtype simple, intvl 2s, length 20
23:53:44.028592 IP 192.168.239.128 > 224.0.100.19: VRRPv2, Advertisement, vrid 51, prio 100, authtype simple, intvl 2s, length 20
23:53:45.298683 IP 192.168.239.130 > 224.0.100.19: VRRPv2, Advertisement, vrid 15, prio 100, authtype simple, intvl 2s, length 20
23:53:46.030749 IP 192.168.239.128 > 224.0.100.19: VRRPv2, Advertisement, vrid 51, prio 100, authtype simple, intvl 2s, length 20
23:53:47.299758 IP 192.168.239.130 > 224.0.100.19: VRRPv2, Advertisement, vrid 15, prio 100, authtype simple, intvl 2s, length 20

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值