欢迎光临
我们一直在努力

高可用集群KEEPALIVED

一、高可用集群

  • 集群类型

    • LB:负载均衡(LVS)

    • HA:高可用集群

    • HPC:高性能集群

  • 实现高可用

    • active/passive:主/备

    • active/active:主/主

    • active ——> HEARTBEAT —-> passive

    • active ——> HEARTBEAT —-> active

  • VRRP(虚拟路由冗余协议,解决静态网关单点风险)

    • 物理层:路由器,三层交换机

    • 软件层:keepalived

二、Keepalived实验环境设定

  • 基于vrrp协议完成地址流动

  • 为vip地址所在的节点生成ipvs规则(在配置文件中预先定义)

  • 为ipvs集群的各RS做健康状态检测

  • 基于脚本调用接口完成脚本中定义的功能,进而影响集群事务,以此支持nginx、haproxy等服务

1、环境设定图

2、环境设定

# 1、部署rs1和rs2(单网卡NAT模式)
[root@rs1 ~]# vmset.sh eth0 172.25.254.10 rs1
[root@rs1 ~]# dnf install httpd -y
[root@rs1 ~]# echo RS1 – 172.25.254.10 > /var/www/html/index.html
[root@rs1 ~]# systemctl enable –now httpd

[root@rs2 ~]# vmset.sh eth0 172.25.254.20 rs2
[root@rs2 ~]# dnf install httpd -y
[root@rs2 ~]# echo RS2 – 172.25.254.20 > /var/www/html/index.html
[root@rs2 ~]# systemctl enable –now httpd

# 2、测试:
[2026-01-28 10:47.02] ~
[莫子.mozi] ⮞ curl 172.25.254.10
RS1 – 172.25.254.10

[2026-01-28 10:47.07] ~
[莫子.mozi] ⮞ curl 172.25.254.20
RS2 – 172.25.254.20

# 3、设定ka1和ka2(单网卡NAT模式)
[root@KA1 ~]# vmset.sh eth0 172.25.254.50 KA1
[root@KA2 ~]# vmset.sh eth0 172.25.254.60 KA2

# 4、设定本地解析
[root@KA1 ~]# vim /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
172.25.254.50 KA1
172.25.254.60 KA2
172.25.254.10 rs1
172.25.254.20 rs2

[root@KA2 ~]# vim /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
172.25.254.50 KA1
172.25.254.60 KA2
172.25.254.10 rs1
172.25.254.20 rs2
# 在所有主机中查看/etc/hosts

# 5、在KA1中开启时间同步服务
[root@KA1 ~]# vim /etc/chrony.conf
allow 0.0.0.0/0
local stratum 10
[root@KA1 ~]# systemctl restart chronyd
[root@KA1 ~]# systemctl enable –now chronyd

# 6、在KA2中使用KA1的时间同步服务(^?代表同步失败,连接不成功,需要检查操作)
[root@KA2 ~]# vim /etc/chrony.conf
pool 172.25.254.50 iburst
[root@KA2 ~]# systemctl restart chronyd
[root@KA2 ~]# systemctl enable –now chronyd
[root@KA2 ~]# chronyc sources -v

.– Source mode '^' = server, '=' = peer, '#' = local clock.
/ .- Source state '*' = current best, '+' = combined, '-' = not combined,
| / 'x' = may be in error, '~' = too variable, '?' = unusable.
|| .- xxxx [ yyyy ] +/- zzzz
|| Reachability register (octal) -. | xxxx = adjusted offset,
|| Log2(Polling interval) –. | | yyyy = measured offset,
|| \\ | | zzzz = estimated error.
|| | | \\
MS Name/IP address Stratum Poll Reach LastRx Last sample
===============================================================================
^* KA1 4 6 17 13 -782ns[-7089ns] +/- 65ms

三、Keepalived虚拟路由配置

1、keepalived安装

[root@KA1 ~]# dnf install keepalived-2.2.8-4.el9.x86_64 -y
[root@KA2 ~]# dnf install keepalived-2.2.8-4.el9.x86_64 -y

主程序文件:/usr/sbin/keepalived

主配置文件:/etc/keepalived/keepalived.conf

2、配置虚拟路由

# 1、在master进行配置
[root@KA1 ~]# vim /etc/keepalived/keepalived.conf
# 全局配置
global_defs {
notification_email {
18390706086@163.com
}
notification_email_from 18390706086@163.com
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id KA1
vrrp_skip_check_adv_addr
#vrrp_strict
vrrp_garp_interval 1
vrrp_gna_interval 1
vrrp_mcast_group4 224.0.0.44
}
# 虚拟路由配置
vrrp_instance WEB_VIP {
state MASTER
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.25.254.100/24 dev eth0 label eth0:0
}
#accpet
}
[root@KA1 ~]# systemctl enable –now keepalived.service
Created symlink /etc/systemd/system/multi-user.target.wants/keepalived.service → /usr/lib/systemd/system/keepalived.service.

# 2、检测是否发生错误,没有任何信息显示,则代表没有错误
[root@KA1 ~]# keepalived -t -f /etc/keepalived/keepalived.conf

# 3、直接从KA1复制到KA2中
[root@KA1 ~]# scp /etc/keepalived/keepalived.conf root@172.25.254.60:/etc/keepalived/keepalived.conf
keepalived.conf

# 4、在KA2中查看,并修改state为backup和priority为80
[root@KA2 ~]# vim /etc/keepalived/keepalived.conf
global_defs {
notification_email {
18390706086@163.com
}
notification_email_from 18390706086@163.com
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id KA1
vrrp_skip_check_adv_addr
#vrrp_strict
vrrp_garp_interval 1
vrrp_gna_interval 1
vrrp_mcast_group4 224.0.0.44
}
# 虚拟路由配置
vrrp_instance WEB_VIP {
state BACKUP
interface eth0
virtual_router_id 51
priority 80
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.25.254.100/24 dev eth0 label eth0:0
}
#accpet
}
[root@KA2 ~]# systemctl enable –now keepalived.service
Created symlink /etc/systemd/system/multi-user.target.wants/keepalived.service → /usr/lib/systemd/system/keepalived.service.

# 5、验证
[root@KA1 ~]# tcpdump -i eth0 -nn host 224.0.0.44
dropped privs to tcpdump
tcpdump: verbose output suppressed, use -v[v]… for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), snapshot length 262144 bytes
11:38:14.143975 IP 172.25.254.50 > 224.0.0.44: VRRPv2, Advertisement, vrid 51, prio 100, authtype simple, intvl 1s, length 20
11:38:15.144325 IP 172.25.254.50 > 224.0.0.44: VRRPv2, Advertisement, vrid 51, prio 100, authtype simple, intvl 1s, length 20
[root@KA1 ~]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.25.254.50 netmask 255.255.255.0 broadcast 172.25.254.255
inet6 fe80::8127:6803:fad1:925f prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:1a:23:1f txqueuelen 1000 (Ethernet)
RX packets 8793 bytes 660277 (644.8 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 14303 bytes 1603200 (1.5 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

eth0:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.25.254.100 netmask 255.255.255.0 broadcast 0.0.0.0
ether 00:0c:29:1a:23:1f 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 22 bytes 1556 (1.5 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 22 bytes 1556 (1.5 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

# 6、检测通信,没有就没问题
[root@KA1 ~]# nft list ruleset

# 7、检测故障
# 在独立的shell中执行
[root@rs1 ~]# tcpdump -i eth0 -nn host 224.0.0.44
11:53:11.663919 IP 172.25.254.50 > 224.0.0.44: VRRPv2, Advertisement, vrid 51, prio 100, authtype simple, intvl 1s, length 20
11:53:12.664860 IP 172.25.254.50 > 224.0.0.44: VRRPv2, Advertisement, vrid 51, prio 100, authtype simple, intvl 1s, length 20

# 8、在ka1中模拟故障
[root@KA1 ~]# systemctl stop keepalived.service
# 停止之后就会迁移到172.25.254.60
11:53:35.518346 IP 172.25.254.50 > 224.0.0.44: VRRPv2, Advertisement, vrid 51, prio 0, authtype simple, intvl 1s, length 20
11:53:36.206888 IP 172.25.254.60 > 224.0.0.44: VRRPv2, Advertisement, vrid 51, prio 80, authtype simple, intvl 1s, length 20
11:53:37.207194 IP 172.25.254.60 > 224.0.0.44: VRRPv2, Advertisement, vrid 51, prio 80, authtype simple, intvl 1s, length 20

# 9、在KA2中看vip是否被迁移到当前主机,之后50主机中就已经看不到eth0:0
[root@KA2 ~]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.25.254.60 netmask 255.255.255.0 broadcast 172.25.254.255
inet6 fe80::d733:30ed:b212:cfa7 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:2c:be:55 txqueuelen 1000 (Ethernet)
RX packets 5540 bytes 407113 (397.5 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 7046 bytes 743818 (726.3 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

eth0:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.25.254.100 netmask 255.255.255.0 broadcast 0.0.0.0
ether 00:0c:29:2c:be:55 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 46 bytes 3768 (3.6 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 46 bytes 3768 (3.6 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

# 10、启动keepalived之后,就可以看到eth0:0又回到KA1(50主机)中
[root@KA1 ~]# systemctl start keepalived.service
[root@KA1 ~]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.25.254.50 netmask 255.255.255.0 broadcast 172.25.254.255
inet6 fe80::8127:6803:fad1:925f prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:1a:23:1f txqueuelen 1000 (Ethernet)
RX packets 12438 bytes 911875 (890.5 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 21808 bytes 2357826 (2.2 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

eth0:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.25.254.100 netmask 255.255.255.0 broadcast 0.0.0.0
ether 00:0c:29:1a:23:1f 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 32 bytes 2056 (2.0 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 32 bytes 2056 (2.0 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

四、Keepalived日志分离

默认情况下,keepalived的日志会被保存在/ver/log/messages文件中,这个文件中除了含有keepalived的日志外,还有其他服务的日志信息,这样不利于keepalived的日志进行查看

[root@KA1 ~]# vim /etc/sysconfig/keepalived
KEEPALIVED_OPTIONS="-D -S 6"
[root@KA1 ~]# systemctl restart keepalived.service
[root@KA1 ~]# vim /etc/rsyslog.conf
local6.* /var/log/keepalived.log
[root@KA1 ~]# systemctl restart rsyslog.service

# 测试:
[root@KA1 ~]# ls -l /var/log/keepalived.log
ls: 无法访问 '/var/log/keepalived.log': 没有那个文件或目录
[root@KA1 log]# ls keepalived.log
keepalived.log

五、Keepalived的子配置文件设定

在主配置文件中如果写入过多的配置不利于对于主配置文件的阅读

[root@KA1 ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived

global_defs {
notification_email {
timinglee_zln@163.com
}
notification_email_from timinglee_zln@163.com
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id KA1
vrrp_skip_check_adv_addr
#vrrp_strict
vrrp_garp_interval 1
vrrp_gna_interval 1
vrrp_mcast_group4 224.0.0.44
}

include /etc/keepalived/conf.d/.conf# 指定独立子配置文件

[root@KA1 ~]# mkdir /etc/keepalived/conf.d -p
[root@KA1 ~]# vim /etc/keepalived/conf.d/webvip.conf
vrrp_instance WEB_VIP {
state MASTER
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.25.254.100/24 dev eth0 label eth0:0
}
}

[root@KA1 ~]# keepalived -t -f /etc/keepalived/keepalived.conf
[root@KA1 ~]# systemctl restart keepalived.service
[root@KA1 ~]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.25.254.50 netmask 255.255.255.0 broadcast 172.25.254.255
inet6 fe80::3901:aeea:786a:7227 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:26:33:d9 txqueuelen 1000 (Ethernet)
RX packets 17383 bytes 1417554 (1.3 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 32593 bytes 3135052 (2.9 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

eth0:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.25.254.100 netmask 255.255.255.0 broadcast 0.0.0.0
ether 00:0c:29:26:33:d9 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 118 bytes 6828 (6.6 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 118 bytes 6828 (6.6 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

六、抢占模式

  • 抢占模式:默认的,谁优先级高就把vip放到哪里

  • 非抢占模式

  • 延迟抢占

1、非抢占模式

持有vip只要vrrp通告正常就不做vip迁移

# 1、编辑KA1中
[root@KA1 ~]# vim /etc/keepalived/keepalived.conf
vrrp_instance WEB_VIP {
state BACKUP # 非抢占模式互为BACKUP
interface eth0
virtual_router_id 51
nopreempt # 启动非抢占模式
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.25.254.100/24 dev eth0 label eth0:0
}
#accpet
}
[root@KA1 ~]# systemctl stop keepalived.service

# 2、在KA2中
[root@KA2 ~]# vim /etc/keepalived/keepalived.conf
vrrp_instance WEB_VIP {
state BACKUP
interface eth0
virtual_router_id 51
nopreempt # 开启非抢占模式
priority 80
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.25.254.100/24 dev eth0 label eth0:0
}
#accpet
}
[root@KA2 ~]# systemctl stop keepalived.service

# 3、测试:
[root@KA1 ~]# systemctl start keepalived.service
[root@KA2 ~]# systemctl start keepalived.service
[root@KA1 ~]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.25.254.50 netmask 255.255.255.0 broadcast 172.25.254.255
inet6 fe80::8127:6803:fad1:925f prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:1a:23:1f txqueuelen 1000 (Ethernet)
RX packets 11593 bytes 844530 (824.7 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 22135 bytes 2140388 (2.0 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

eth0:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.25.254.100 netmask 255.255.255.0 broadcast 0.0.0.0
ether 00:0c:29:1a:23:1f 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 60 bytes 3000 (2.9 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 60 bytes 3000 (2.9 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

[root@KA1 ~]# systemctl stop keepalived.service
[root@KA2 ~]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.25.254.60 netmask 255.255.255.0 broadcast 172.25.254.255
inet6 fe80::d733:30ed:b212:cfa7 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:2c:be:55 txqueuelen 1000 (Ethernet)
RX packets 7674 bytes 511782 (499.7 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 7396 bytes 628122 (613.4 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

eth0:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.25.254.100 netmask 255.255.255.0 broadcast 0.0.0.0
ether 00:0c:29:2c:be:55 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 30 bytes 1500 (1.4 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 30 bytes 1500 (1.4 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

# 4、开启KA1的服务ip,不会被抢占到1中
[root@KA1 ~]# systemctl start keepalived.service
[root@KA1 ~]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.25.254.50 netmask 255.255.255.0 broadcast 172.25.254.255
inet6 fe80::8127:6803:fad1:925f prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:1a:23:1f txqueuelen 1000 (Ethernet)
RX packets 12096 bytes 881288 (860.6 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 22948 bytes 2224662 (2.1 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 80 bytes 4000 (3.9 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 80 bytes 4000 (3.9 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

# 5、还在KA2中
[root@KA2 ~]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.25.254.60 netmask 255.255.255.0 broadcast 172.25.254.255
inet6 fe80::d733:30ed:b212:cfa7 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:2c:be:55 txqueuelen 1000 (Ethernet)
RX packets 8121 bytes 547210 (534.3 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 8298 bytes 721876 (704.9 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

eth0:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.25.254.100 netmask 255.255.255.0 broadcast 0.0.0.0
ether 00:0c:29:2c:be:55 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 30 bytes 1500 (1.4 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 30 bytes 1500 (1.4 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

2、延迟抢占

# 1、KA1中
[root@KA1 ~]# vim /etc/keepalived/keepalived.conf
vrrp_instance WEB_VIP {
state BACKUP
interface eth0
virtual_router_id 51
#nopreempt
priority 100
preempt_delay 10 # 启动延迟抢占,延迟10s抢占
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.25.254.100/24 dev eth0 label eth0:0
}
#accpet
}

# 2、KA2中
[root@KA2 ~]# vim /etc/keepalived/keepalived.conf
vrrp_instance WEB_VIP {
state BACKUP
interface eth0
virtual_router_id 51
#nopreempt
priority 80
preempt_delay 10 # 启动延迟抢占,延迟10s抢占
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.25.254.100/24 dev eth0 label eth0:0
}
#accpet
}

# 3、测试:
[root@KA1 ~]# systemctl start keepalived.service
[root@KA2 ~]# systemctl start keepalived.service

# 在一个独立的shell中开启监控
[root@KA1 ~]# watch -n 1 ifconfig

# 4、在KA1另外的shell中关闭开启keepalived后,过个十秒就可以看到抢占过来了
[root@KA1 ~]# systemctl stop keepalived.service
[root@KA1 ~]# systemctl start keepalived.service
[root@KA1 ~]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.25.254.50 netmask 255.255.255.0 broadcast 172.25.254.255
inet6 fe80::8127:6803:fad1:925f prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:1a:23:1f txqueuelen 1000 (Ethernet)
RX packets 14571 bytes 1054578 (1.0 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 26423 bytes 2598120 (2.4 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

eth0:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.25.254.100 netmask 255.255.255.0 broadcast 0.0.0.0
ether 00:0c:29:1a:23:1f 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 100 bytes 5000 (4.8 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 100 bytes 5000 (4.8 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

# 操作完毕后观察监控中vip的迁移延迟过程

七、Keepalived的单播模式

启用vrrp_strict时,不能启动单播

# 1、组播的就需要关闭掉
# 在KA1中
[root@KA1 ~]# vim /etc/keepalived/keepalived.conf
#vrrp_mcast_group4 224.0.0.44
}

vrrp_instance WEB_VIP{
state MASTER
interface eth0
virtual_router_id 51
#nopreempt
priority 100
advert_int 1
unicast_src_ip 172.25.254.50
unicast_peer {
172.25.254.60
}
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.25.254.100/24 dev eth0 label eth0:0
}
#accpet
}
[root@KA1 ~]# systemctl restart keepalived.service

# 在KA2中
[root@KA2 ~]# vim /etc/keepalived/keepalived.conf
#vrrp_mcast_group4 224.0.0.44
}

vrrp_instance WEB_VIP {
state BACKUP
interface eth0
virtual_router_id 51
#nopreempt
priority 80
advert_int 1
unicast_src_ip 172.25.254.60
unicast_peer{
172.25.254.50
}
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.25.254.100/24 dev eth0 label eth0:0
}
#accpet
}
[root@KA2 ~]# systemctl restart keepalived.service

# 2、抓包测试(目前的监控):
[root@KA1 ~]# tcpdump -i eth0 -nn host 172.25.254.50 and dst 172.25.254.60
dropped privs to tcpdump
tcpdump: verbose output suppressed, use -v[v]… for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), snapshot length 262144 bytes
15:35:21.832998 IP 172.25.254.50 > 172.25.254.60: VRRPv2, Advertisement, vrid 51, prio 100, authtype simple, intvl 1s, length 20
15:35:22.833680 IP 172.25.254.50 > 172.25.254.60: VRRPv2, Advertisement, vrid 51, prio 100, authtype simple, intvl 1s, length 20

# 可以另开一台shell去观测KA1和KA2的具体抓包变化
# VIP的抢占可以从ifconfig直接进行查看
# 当关掉KA1时,因为优先级VIP被KA2抢占,所以就是从50->60变成60->50
[root@KA1 ~]# systemctl stop keepalived.service
[root@KA2 ~]# tcpdump -i eth0 -nn host 172.25.254.60 and dst 172.25.254.50
dropped privs to tcpdump
tcpdump: verbose output suppressed, use -v[v]… for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), snapshot length 262144 bytes
15:39:19.606112 IP 172.25.254.60 > 172.25.254.50: VRRPv2, Advertisement, vrid 51, prio 80, authtype simple, intvl 1s, length 20
15:39:20.606656 IP 172.25.254.60 > 172.25.254.50: VRRPv2, Advertisement, vrid 51, prio 80, authtype simple, intvl 1s, length 20

# 但是我们启动KA1时,之后又因为优先级VIP被抢占到KA1上,所以就是从60->50变成50->60
[root@KA1 ~]# systemctl start keepalived.service
[root@KA1 ~]# tcpdump -i eth0 -nn host 172.25.254.50 and dst 172.25.254.60
dropped privs to tcpdump
tcpdump: verbose output suppressed, use -v[v]… for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), snapshot length 262144 bytes
15:39:41.219353 IP 172.25.254.50 > 172.25.254.60: VRRPv2, Advertisement, vrid 51, prio 100, authtype simple, intvl 1s, length 20
15:39:42.219889 IP 172.25.254.50 > 172.25.254.60: VRRPv2, Advertisement, vrid 51, prio 100, authtype simple, intvl 1s, length 20

八、Keepalived业务迁移告警

1、邮件告警环境构建

# 1、安装邮件软件
[root@KA1 ~]# dnf install s-nail sendmail -y
[root@KA2 ~]# dnf install s-nail sendmail -y

# 2、启动邮件代理
[root@KA1 ~]# systemctl start sendmail.service
[root@KA2 ~]# systemctl start sendmail.service

# 3、查看25端口是否启动(1和2中都可以查看)
[root@KA2 ~]# netstat -antlupe | grep 25
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 0 58548 7871/sendmail: acce
tcp 0 64 172.25.254.60:22 172.25.254.1:59807 ESTABLISHED 0 39638 3474/sshd: root [pr
tcp 0 0 172.25.254.60:22 172.25.254.1:59808 ESTABLISHED 0 37628 3478/sshd: root [pr
tcp 0 1 172.25.254.60:39718 192.168.200.5:1358 SYN_SENT 0 59505 5842/keepalived
tcp 0 1 172.25.254.60:56258 192.168.200.4:1358 SYN_SENT 0 59511 5842/keepalived

# 4、设定sendmail可以通过公网邮箱发送邮件

# 1、在linux主机中配置mailrc(1和2都要进行配置)
[root@KA1 ~]# vim /etc/mail.rc
set smtp=smtp.163.com
set smtp-auth=login
set smtp-auth-user=18390706086@163.com
set smtp-auth-password=EGfRt38jjUZFmRG5
set from=18390706086@163.com
set ssl-verify=ignore
[root@KA2 ~]# vim /etc/mail.rc
set smtp=smtp.163.com
set smtp-auth=login
set smtp-auth-user=18390706086@163.com
set smtp-auth-password=EGfRt38jjUZFmRG5
set from=18390706086@163.com
set ssl-verify=ignore

# 2、测试邮件,可以发送成功(使用sendmail)
[root@KA1 ~]# echo test message | mail -s test 18390706086@163.com
[root@KA2 ~]# echo test message | mail -s test 18390706086@163.com

# 3、查看邮件队列
[root@KA1 ~]# mailq
/var/spool/mqueue is empty
Total requests: 0
[root@KA2 ~]# mailq
/var/spool/mqueue is empty
Total requests: 0

# 4、查看是否又退信(No就是没有)
[root@KA1 ~]# mail
s-nail: No mail for root at /var/spool/mail/root
s-nail: /var/spool/mail/root: No such entry, file or directory
[root@KA2 ~]# mail
s-nail: No mail for root at /var/spool/mail/root
s-nail: /var/spool/mail/root: No such entry, file or directory

# 5、查看对应邮箱是否有邮件收到
下图:是收到的

# 使用postfix发送邮件,安装
[root@KA1 ~]# dnf install s-nail postfix -y
[root@KA2 ~]# dnf install s-nail postfix -y
[root@KA1 ~]# systemctl start postfix.service
[root@KA2 ~]# systemctl start postfix.service

2、设定keepalived告警脚本

[root@KA1 ~]# mkdir -p /etc/keepalived/scripts
[root@KA2 ~]# mkdir -p /etc/keepalived/scripts

# 1、编写告警脚本
[root@KA1 ~]# vim /etc/keepalived/scripts/waring.sh
#!/bin/bash
mail_dest='18390706086@163.com'

mail_send()
{
mail_subj="$HOSTNAME to be $1 vip 转移"
mail_mess="`date +%F\\ %T`: vrrp 转移,$HOSTNAME 变为 $1"
echo "$mail_mess" | mail -s "$mail_subj" $mail_dest
}
case $1 in
master)
mail_send master
;;
backup)
mail_send backup
;;
fault)
mail_send fault
;;
*)
exit 1
;;
esac

[root@KA2 ~]# vim /etc/keepalived/scripts/waring.sh
#!/bin/bash
mail_dest='18390706086@163.com'

mail_send()
{
mail_subj="$HOSTNAME to be $1 vip 转移"
mail_mess="`date +%F\\ %T`: vrrp 转移,$HOSTNAME 变为 $1"
echo "$mail_mess" | mail -s "$mail_subj" $mail_dest
}
case $1 in
master)
mail_send master
;;
backup)
mail_send backup
;;
fault)
mail_send fault
;;
*)
exit 1
;;
esac

# 2、添加权限
[root@KA1 ~]# chmod +x /etc/keepalived/scripts/waring.sh
[root@KA2 ~]# chmod +x /etc/keepalived/scripts/waring.sh

# 3、对应邮箱中会出现邮件
[root@KA1 ~]# /etc/keepalived/scripts/waring.sh master

3、配置keepalived告警

# 1、前面的一些单播配置就可以注释掉
# 在KA1和KA2中设定配置文件
[root@KA1 ~]# vim /etc/keepalived/keepalived.conf
global_defs {
notification_email {
18390706086@163.com
}
notification_email_from 18390706086@163.com
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id KA1
vrrp_skip_check_adv_addr
#vrrp_strict
vrrp_garp_interval 1
vrrp_gna_interval 1
vrrp_mcast_group4 224.0.0.44
enable_script_security
script_user root
}
vrrp_instance WEB_VIP {
state MASTER
interface eth0
virtual_router_id 51
priority 100
advert_int 1
# unicast_src_ip 172.25.254.50
# unicast_peer {
# 172.25.254.60
# }
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.25.254.100/24 dev eth0 label eth0:0
}
notify_master "/etc/keepalived/scripts/waring.sh master"
notify_backup "/etc/keepalived/scripts/waring.sh backup"
notify_fault "/etc/keepalived/scripts/waring.sh fault"
}

[root@KA2 ~]# vim /etc/keepalived/keepalived.conf
global_defs {
notification_email {
18390706086@163.com
}
notification_email_from 18390706086@163.com
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id KA2
vrrp_skip_check_adv_addr
#vrrp_strict
vrrp_garp_interval 1
vrrp_gna_interval 1
vrrp_mcast_group4 224.0.0.44
enable_script_security
script_user root
}
vrrp_instance WEB_VIP {
state BACKUP
interface eth0
virtual_router_id 51
#nopreempt
priority 80
advert_int 1
#unicast_src_ip 172.25.254.60
#unicast_peer{
# 172.25.254.50
#}
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.25.254.100/24 dev eth0 label eth0:0
}
#accpet
notify_master "/etc/keepalived/scripts/waring.sh master"
notify_backup "/etc/keepalived/scripts/waring.sh backup"
notify_fault "/etc/keepalived/scripts/waring.sh fault"
}

[root@KA1 ~]# systemctl restart keepalived.service
[root@KA2 ~]# systemctl restart keepalived.service

# 2、测试
[root@KA1 ~]# systemctl stop keepalived.service#停止服务后查看邮件
[root@KA1 ~]# systemctl start keepalived.service#开启服务后查看邮件

九、Keepalived双主模式

[root@KA1 ~]# vim /etc/keepalived/keepalived.conf
vrrp_instance WEB_VIP{ # 第一个虚拟路由,由master身份设定
state MASTER
interface eth0
virtual_router_id 51
#nopreempt
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.25.254.100/24 dev eth0 label eth0:0
}
#accpet
}
vrrp_instance DB_VIP{ # 第二个虚拟路由,由backup身份设定
state BACKUP
interface eth0
virtual_router_id 52
#nopreempt
priority 80
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.25.254.200/24 dev eth0 label eth0:1
}
#accpet
}

#

vrrp_instance WEB_VIP {
state BACKUP
interface eth0
virtual_router_id 51
#nopreempt
priority 80
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.25.254.100/24 dev eth0 label eth0:0
}
#accpet
}

vrrp_instance DB_VIP {
state MASTER
interface eth0
virtual_router_id 52
#nopreempt
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.25.254.200/24 dev eth0 label eth0:1
}
#accpet
}

[root@KA1 ~]# systemctl restart keepalived.service
[root@KA2 ~]# systemctl restart keepalived.service

# 1、测试:
[root@KA1 ~]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.25.254.50 netmask 255.255.255.0 broadcast 172.25.254.255
inet6 fe80::8127:6803:fad1:925f prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:1a:23:1f txqueuelen 1000 (Ethernet)
RX packets 11593 bytes 821636 (802.3 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 22077 bytes 2301956 (2.1 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

eth0:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.25.254.100 netmask 255.255.255.0 broadcast 0.0.0.0
ether 00:0c:29:1a:23:1f 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 40 bytes 2000 (1.9 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 40 bytes 2000 (1.9 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@KA2 ~]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.25.254.60 netmask 255.255.255.0 broadcast 172.25.254.255
inet6 fe80::d733:30ed:b212:cfa7 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:2c:be:55 txqueuelen 1000 (Ethernet)
RX packets 5934 bytes 415884 (406.1 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 6222 bytes 587926 (574.1 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

eth0:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.25.254.200 netmask 255.255.255.0 broadcast 0.0.0.0
ether 00:0c:29:2c:be:55 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 30 bytes 1500 (1.4 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 30 bytes 1500 (1.4 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

# 关闭KA1
[root@KA1 ~]# systemctl stop keepalived.service
[root@KA2 ~]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.25.254.60 netmask 255.255.255.0 broadcast 172.25.254.255
inet6 fe80::d733:30ed:b212:cfa7 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:2c:be:55 txqueuelen 1000 (Ethernet)
RX packets 6078 bytes 425868 (415.8 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 6480 bytes 613050 (598.6 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

eth0:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.25.254.100 netmask 255.255.255.0 broadcast 0.0.0.0
ether 00:0c:29:2c:be:55 txqueuelen 1000 (Ethernet)

eth0:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.25.254.200 netmask 255.255.255.0 broadcast 0.0.0.0
ether 00:0c:29:2c:be:55 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 30 bytes 1500 (1.4 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 30 bytes 1500 (1.4 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

# 关闭KA2
[root@KA1 ~]# systemctl start keepalived.service
[root@KA2 ~]# systemctl stop keepalived.service
[root@KA1 ~]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.25.254.50 netmask 255.255.255.0 broadcast 172.25.254.255
inet6 fe80::8127:6803:fad1:925f prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:1a:23:1f txqueuelen 1000 (Ethernet)
RX packets 11961 bytes 848542 (828.6 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 22593 bytes 2353124 (2.2 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

eth0:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.25.254.100 netmask 255.255.255.0 broadcast 0.0.0.0
ether 00:0c:29:1a:23:1f txqueuelen 1000 (Ethernet)

eth0:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.25.254.200 netmask 255.255.255.0 broadcast 0.0.0.0
ether 00:0c:29:1a:23:1f 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 50 bytes 2500 (2.4 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 50 bytes 2500 (2.4 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

十、实现IPVS的高性能

1、配置RS

# 1、在RS1中
[root@rs1 ~]# cd /etc/NetworkManager/system-connections/
[root@rs1 system-connections]# cp -p eth0.nmconnection lo.nmconnection
[root@rs1 system-connections]# vim lo.nmconnection
[connection]
id=lo
type=loopback
interface-name=lo

[ipv4]
method=manual
address1=127.0.0.1/8
address2=172.25.254.100/32
[root@rs1 system-connections]# nmcli connection reload
[root@rs1 system-connections]# nmcli connection up lo
连接已成功激活(D-Bus 活动路径:/org/freedesktop/NetworkManager/ActiveConnection/4)
[root@rs1 system-connections]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet 172.25.254.100/32 scope global lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
link/ether 00:0c:29:9b:9a:0f brd ff:ff:ff:ff:ff:ff
altname enp3s0
altname ens160
inet 172.25.254.10/24 brd 172.25.254.255 scope global noprefixroute eth0
valid_lft forever preferred_lft forever
inet6 fe80::fa79:a6db:ba1a:97ac/64 scope link noprefixroute
valid_lft forever preferred_lft forever

# 2、在RS2中
[root@rs2 ~]# cd /etc/NetworkManager/system-connections/
[root@rs2 system-connections]# cp -p eth0.nmconnection lo.nmconnection
[root@rs2 system-connections]# vim lo.nmconnection
[connection]
id=lo
type=loopback
interface-name=lo

[ipv4]
method=manual
address1=127.0.0.1/8
address2=172.25.254.100/32
[root@rs2 system-connections]# nmcli connection reload
[root@rs2 system-connections]# nmcli connection up lo
连接已成功激活(D-Bus 活动路径:/org/freedesktop/NetworkManager/ActiveConnection/4)
[root@rs2 system-connections]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet 172.25.254.100/32 scope global lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
link/ether 00:0c:29:b1:2f:af brd ff:ff:ff:ff:ff:ff
altname enp3s0
altname ens160
inet 172.25.254.20/24 brd 172.25.254.255 scope global noprefixroute eth0
valid_lft forever preferred_lft forever
inet6 fe80::466e:fe26:7640:7ea3/64 scope link noprefixroute
valid_lft forever preferred_lft forever

[root@rs1 system-connections]# vim /etc/sysctl.conf
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
net.ipv4.conf.lo.arp_announce = 2
net.ipv4.conf.lo.arp_ignore = 1
[root@rs1 system-connections]# sysctl -p
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
net.ipv4.conf.lo.arp_announce = 2
net.ipv4.conf.lo.arp_ignore = 1

[root@rs2 ~]# vim /etc/sysctl.conf
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
net.ipv4.conf.lo.arp_announce = 2
net.ipv4.conf.lo.arp_ignore = 1
[root@rs2 ~]# sysctl -p
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
net.ipv4.conf.lo.arp_announce = 2
net.ipv4.conf.lo.arp_ignore = 1

# 3、安装ipvsadm
[root@rs1 ~]# dnf install ipvsadm -y
[root@rs2 ~]# dnf install ipvsadm -y

2、实现方式

# 1、在keepalived的所有主机中
[root@KA1 ~]# vim /etc/keepalived/keepalived.conf
virtual_server 172.25.254.100 80 {
delay_loop 6
lb_algo rr
lb_kind DR
#persistence_timeout 50
protocol TCP

real_server 172.25.254.10 80 {
weight 1
HTTP_GET {
url {
path /
status_code 200
}
connect_timeout 1
retry 3
delay_before_retry 1
}
}

real_server 172.25.254.20 80 {
weight 1
TCP_CHECK {
connect_timeout 5
retry 3
delay_before_retry 1
connect_port 80
}
}
}

# 2、重启测试:
[root@KA1 ~]# keepalived -t -f /etc/keepalived/keepalived.conf
[root@KA1 ~]# systemctl restart keepalived.service
[root@KA2 ~]# keepalived -t -f /etc/keepalived/keepalived.conf
[root@KA2 ~]# systemctl restart keepalived.service

3、测试

#友情提示:不要再KA1和KA2中访问vip,会检测不出效果

#在ka1中开启独立的shell
[root@KA1 ~]# watch -n 1 ipvsadm -Ln

#在RS1中关闭wen服务查看lvs策略是否变化,变化了
[root@rs1 ~]# systemctl stop httpd

#把ka1中的keepalived关闭查看ka2中是否自动生成lvs策略,可以在KA2中看到
[root@KA1 ~]# systemctl stop keepalived.service
[root@KA2 ~]# watch -n 1 ipvsadm -Ln

十一、双主模式代理

1、实验环境

# 1、web服务设定在上个实验已经设定完成
#在rs中设定lo添加vip2 172.25.254.200、32
#在rs中搭建数据库
[root@rs1 ~]# dnf install mariadb-server -y
[root@rs2 ~]# dnf install mariadb-server -y

[root@rs1 ~]# systemctl enable –now mariadb
Created symlink /etc/systemd/system/mysql.service → /usr/lib/systemd/system/mariadb.service.
Created symlink /etc/systemd/system/mysqld.service → /usr/lib/systemd/system/mariadb.service.
Created symlink /etc/systemd/system/multi-user.target.wants/mariadb.service → /usr/lib/systemd/system/mariadb.service.
[root@rs2 ~]# systemctl enable –now mariadb
Created symlink /etc/systemd/system/mysql.service → /usr/lib/systemd/system/mariadb.service.
Created symlink /etc/systemd/system/mysqld.service → /usr/lib/systemd/system/mariadb.service.
Created symlink /etc/systemd/system/multi-user.target.wants/mariadb.service → /usr/lib/systemd/system/mariadb.service.

[root@rs1 ~]# mysql
Welcome to the MariaDB monitor. Commands end with ; or \\g.
Your MariaDB connection id is 3
Server version: 10.5.27-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\\h' for help. Type '\\c' to clear the current input statement.

MariaDB [(none)]> create user lee@'%' identified by 'lee';
Query OK, 0 rows affected (0.001 sec)

MariaDB [(none)]> grant all on *.* to lee@'%';
Query OK, 0 rows affected (0.000 sec)

[root@rs2 ~]# mysql
Welcome to the MariaDB monitor. Commands end with ; or \\g.
Your MariaDB connection id is 3
Server version: 10.5.27-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\\h' for help. Type '\\c' to clear the current input statement.

MariaDB [(none)]> create user lee@'%' identified by 'lee';
Query OK, 0 rows affected (0.001 sec)

MariaDB [(none)]> grant all on *.* to lee@'%';
Query OK, 0 rows affected (0.001 sec)

# 2、测试成功:
[root@rs1 ~]# mysql -ulee -plee -h172.25.254.10
Welcome to the MariaDB monitor. Commands end with ; or \\g.
Your MariaDB connection id is 4
Server version: 10.5.27-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\\h' for help. Type '\\c' to clear the current input statement.

MariaDB [(none)]>
[root@rs2 ~]# mysql -ulee -plee -h172.25.254.20
Welcome to the MariaDB monitor. Commands end with ; or \\g.
Your MariaDB connection id is 4
Server version: 10.5.27-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\\h' for help. Type '\\c' to clear the current input statement.

MariaDB [(none)]>

2、实现不同VIP代理不同业务

# 1、KA1和KA2一样的操作
[root@KA1 ~]# vim /etc/keepalived/keepalived.conf
include /etc/keepalived/conf.d/webserver.conf
include /etc/keepalived/conf.d/datebase.conf

[root@KA1 ~]# vim /etc/keepalived/conf.d/webserver.conf
virtual_server 172.25.254.100 80 {
delay_loop 6
lb_algo rr
lb_kind DR
protocol TCP

real_server 172.25.254.10 80 {
weight 1
HTTP_GET {
url {
path /
status_code 200
}
connect_timeout 1
retry 3
delay_before_retry 1
}
}

real_server 172.25.254.20 80 {
weight 1
TCP_CHECK {
connect_timeout 5
retry 3
delay_before_retry 3
connect_port 80
}
}
}

[root@KA1 ~]# vim /etc/keepalived/conf.d/datebase.conf
virtual_server 172.25.254.200 3306 {
delay_loop 6
lb_algo rr
lb_kind DR
protocol TCP

real_server 172.25.254.10 3306 {
weight 1
TCP_CHECK {
connect_timeout 5
retry 3
delay_before_retry 3
connect_port 3306
}
}

real_server 172.25.254.20 3306 {
weight 1
TCP_CHECK {
connect_timeout 5
retry 3
delay_before_retry 3
connect_port 3306
}
}
}

# 2、测试是否出现报错
[root@KA1 ~]# keepalived -t -f /etc/keepalived/keepalived.conf
[root@KA1 ~]# systemctl restart keepalived.service
[root@KA2 ~]# keepalived -t -f /etc/keepalived/keepalived.conf
[root@KA2 ~]# systemctl restart keepalived.service

3、测试

# 编写配置文件
[root@rs1 ~]# vim /etc/NetworkManager/system-connections/lo.nmconnection
[connection]
id=lo
type=loopback
interface-name=lo

[ipv4]
method=manual
address1=127.0.0.1/8
address2=172.25.254.100/32
address3=172.25.254.200/32
[root@rs1 ~]# nmcli connection reload
[root@rs1 ~]# nmcli connection up lo
连接已成功激活(D-Bus 活动路径:/org/freedesktop/NetworkManager/ActiveConnection/6)

[root@rs2 ~]# vim /etc/NetworkManager/system-connections/lo.nmconnection
[connection]
id=lo
type=loopback
interface-name=lo

[ipv4]
method=manual
address1=127.0.0.1/8
address2=172.25.254.100/32
address3=172.25.254.200/32
[root@rs2 ~]# nmcli connection reload
[root@rs2 ~]# nmcli connection up lo
连接已成功激活(D-Bus 活动路径:/org/freedesktop/NetworkManager/ActiveConnection/6)

# 检测
[root@rs1 ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet 172.25.254.100/32 scope global lo
valid_lft forever preferred_lft forever
inet 172.25.254.200/32 scope global lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
link/ether 00:0c:29:9b:9a:0f brd ff:ff:ff:ff:ff:ff
altname enp3s0
altname ens160
inet 172.25.254.10/24 brd 172.25.254.255 scope global noprefixroute eth0
valid_lft forever preferred_lft forever
inet6 fe80::fa79:a6db:ba1a:97ac/64 scope link noprefixroute
valid_lft forever preferred_lft forever

[root@rs2 ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet 172.25.254.100/32 scope global lo
valid_lft forever preferred_lft forever
inet 172.25.254.200/32 scope global lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000[root@rs2 ~]# mysql -ulee -plee -h172.25.254.200
Welcome to the MariaDB monitor. Commands end with ; or \\g.
Your MariaDB connection id is 109
Server version: 10.5.27-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\\h' for help. Type '\\c' to clear the current input statement.

MariaDB [(none)]>

link/ether 00:0c:29:b1:2f:af brd ff:ff:ff:ff:ff:ff
altname enp3s0
altname ens160
inet 172.25.254.20/24 brd 172.25.254.255 scope global noprefixroute eth0
valid_lft forever preferred_lft forever
inet6 fe80::466e:fe26:7640:7ea3/64 scope link noprefixroute
valid_lft forever preferred_lft forever

# 测试:
[root@rs2 ~]# mysql -ulee -plee -h172.25.254.200
Welcome to the MariaDB monitor. Commands end with ; or \\g.
Your MariaDB connection id is 109
Server version: 10.5.27-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\\h' for help. Type '\\c' to clear the current input statement.

MariaDB [(none)]>

[2026-01-29 12:12.23] ~
[莫子.mozi] ⮞ curl 172.25.254.100
RS2 – 172.25.254.20

[2026-01-29 12:12.31] ~
[莫子.mozi] ⮞ curl 172.25.254.100
RS1 – 172.25.254.10

十二、利用VRRP Script 实现全能高可用

1、实验环境

# 1、在KA1和KA2中安装haproxy
[root@KA1 ~]# dnf install haproxy-2.4.22-4.el9.x86_64 -y
[root@KA2 ~]# dnf install haproxy-2.4.22-4.el9.x86_64 -y

[root@KA1 ~]# vim /etc/sysctl.conf
net.ipv4.ip_nonlocal_bind=1
[root@KA1 ~]# sysctl -p
net.ipv4.ip_nonlocal_bind = 1

[root@KA2 ~]# vim /etc/sysctl.conf
net.ipv4.ip_nonlocal_bind=1
[root@KA2 ~]# sysctl -p
net.ipv4.ip_nonlocal_bind = 1

# 2、编辑haproxy配置文件
[root@KA1 ~]# vim /etc/haproxy/haproxy.cfg
listen webserver
bind 172.25.254.100:80
mode http
server web1 172.25.254.10:80 check
server web2 172.25.254.20:80 check
[root@KA1 ~]# systemctl stop httpd.service
[root@KA1 ~]# systemctl enable –now haproxy.service

[root@KA2 ~]# vim /etc/haproxy/haproxy.cfg
listen webserver
bind 172.25.254.100:80
mode http
server web1 172.25.254.10:80 check
server web2 172.25.254.20:80 check
[root@KA2 ~]# systemctl stop httpd.service
[root@KA2 ~]# systemctl enable –now haproxy.service
Created symlink /etc/systemd/system/multi-user.target.wants/haproxy.service → /usr/lib/systemd/system/haproxy.service.

# 3、测试:
[root@KA1 ~]# netstat -antlupe | grep haproxy
tcp 0 0 172.25.254.100:80 0.0.0.0:* LISTEN 0 134144 39551/haproxy
[root@KA2 ~]# netstat -antlupe | grep haproxy
tcp 0 0 172.25.254.100:80 0.0.0.0:* LISTEN 0 77294 15202/haproxy

2、利用案例理解VRRP_script

# 1、在KA1上
[root@KA1 ~]# vim /etc/keepalived/scripts/test.sh
#!/bin/bash
[ ! -f "/mnt/lee" ]
[root@KA1 ~]# echo $?
0
[root@KA1 ~]# vim /etc/keepalived/keepalived.conf
vrrp_script check_lee {
script "/etc/keepalived/scripts/test.sh"
interval 1
weight -30
fall 2
rise 2
timeout 2
user root
}
vrrp_instance WEB_VIP{
state MASTER
interface eth0
virtual_router_id 51
#nopreempt
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.25.254.100/24 dev eth0 label eth0:0
}
track_script {
check_test
}
#accpet
}
#include /etc/keepalived/conf.d/webserver.conf
#include /etc/keepalived/conf.d/datebase.conf

[root@KA1 ~]# systemctl restart keepalived.service

# 2、测试:
[root@KA1 ~]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.25.254.50 netmask 255.255.255.0 broadcast 172.25.254.255
inet6 fe80::3901:aeea:786a:7227 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:26:33:d9 txqueuelen 1000 (Ethernet)
RX packets 98198 bytes 9235557 (8.8 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 145101 bytes 12247386 (11.6 MiB)
TX errors 0 dropped 9 overruns 0 carrier 0 collisions 0

eth0:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.25.254.100 netmask 255.255.255.0 broadcast 0.0.0.0
ether 00:0c:29:26:33:d9 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 932 bytes 72195 (70.5 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 932 bytes 72195 (70.5 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

[root@KA1 ~]# touch /mnt/lee

[root@KA1 ~]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.25.254.50 netmask 255.255.255.0 broadcast 172.25.254.255
inet6 fe80::3901:aeea:786a:7227 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:26:33:d9 txqueuelen 1000 (Ethernet)
RX packets 97968 bytes 9216259 (8.7 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 144858 bytes 12219108 (11.6 MiB)
TX errors 0 dropped 9 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 932 bytes 72195 (70.5 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 932 bytes 72195 (70.5 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

[root@KA1 ~]# rm -fr /mnt/lee

[root@KA1 ~]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.25.254.50 netmask 255.255.255.0 broadcast 172.25.254.255
inet6 fe80::3901:aeea:786a:7227 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:26:33:d9 txqueuelen 1000 (Ethernet)
RX packets 98198 bytes 9235557 (8.8 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 145101 bytes 12247386 (11.6 MiB)
TX errors 0 dropped 9 overruns 0 carrier 0 collisions 0

eth0:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.25.254.100 netmask 255.255.255.0 broadcast 0.0.0.0
ether 00:0c:29:26:33:d9 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 932 bytes 72195 (70.5 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 932 bytes 72195 (70.5 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

3、keepalived + haproxy

[root@KA1 ~]# vim /etc/keepalived/scripts/haproxy_check.sh
#!/bin/bash
killall -0 haproxy &> /dev/null

[root@KA1 ~]# systemctl restart haproxy.service
[root@KA1 ~]# chmod +x /etc/keepalived/scripts/haproxy_check.sh

# 1、编写keepalived配置
[root@KA1 ~]# vim /etc/keepalived/keepalived.conf
vrrp_script haproxy_check {
script "/etc/keepalived/scripts/haproxy_check.sh"
interval 1
weight -30
fall 2
rise 2
timeout 2
user root
}
vrrp_instance WEB_VIP{
state MASTER
interface eth0
virtual_router_id 51
#nopreempt
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.25.254.100/24 dev eth0 label eth0:0
}
track_script {
haproxy_check
}
#accpet
}
[root@KA1 ~]# systemctl restart keepalived.service

# 2、测试:通过关闭和开启haproxy来观察vip是否迁移
[root@KA1 ~]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.25.254.50 netmask 255.255.255.0 broadcast 172.25.254.255
inet6 fe80::8127:6803:fad1:925f prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:1a:23:1f txqueuelen 1000 (Ethernet)
RX packets 74923 bytes 5765913 (5.4 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 114896 bytes 11135851 (10.6 MiB)
TX errors 0 dropped 2 overruns 0 carrier 0 collisions 0

eth0:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.25.254.100 netmask 255.255.255.0 broadcast 0.0.0.0
ether 00:0c:29:1a:23:1f 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 54 bytes 2700 (2.6 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 54 bytes 2700 (2.6 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@KA1 ~]# systemctl stop haproxy.service
[root@KA1 ~]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.25.254.50 netmask 255.255.255.0 broadcast 172.25.254.255
inet6 fe80::8127:6803:fad1:925f prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:1a:23:1f txqueuelen 1000 (Ethernet)
RX packets 75249 bytes 5789283 (5.5 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 115310 bytes 11181699 (10.6 MiB)
TX errors 0 dropped 2 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 54 bytes 2700 (2.6 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 54 bytes 2700 (2.6 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

赞(0)
未经允许不得转载:171主机测评 » 高可用集群KEEPALIVED
分享到: 更多 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址