Keepalived详解

news2024/11/25 3:06:19

目录

1、高可用集群keepalived

高可用集群

VRRP 相关概念

keepalived 简介

2、基础实验环境搭建

 3、keepalived的虚拟路由管理

全局配置

​编辑 配置虚拟路由器

 4、虚拟路由的通讯设置

 5、keepalived的日志分离

6、实现独立子配置文件 

 7、keepalived的抢占模式和非抢占模式

 非抢占模式

 抢占延迟模式

8、VIP单播配置

9、邮件通知

10、实现 master/master 的 Keepalived 双主架构

11、实现IPVS的高可用性

       实现双主的 LVS-DR 模式

       编辑利用脚本实现主从角色切换 

        实现haproxy高可用


1、高可用集群keepalived

高可用集群

集群类型:

  • 负载均衡(LB):包括 LVS、HAProxy、nginx(http/upstream, stream/upstream),用于分配网络流量。
  • 高可用集群(HA):例如数据库、Redis 等,确保服务持续可用。 高性能集群(HPC):专注于提供强大的计算能力。

系统可用性:通过服务等级协议(SLA)来定义,可用性指标用公式 A = MTBF / (MTBF + MTTR)计算,常见指标有 99.9%、99.99%、99.999%、99.9999%。

系统故障:分为硬件故障(如设计缺陷、损耗、非人为不可抗拒因素)和软件故障(设计缺陷 bug)。

实现高可用:提升系统高可用性的关键在于降低平均故障修复时间(MTTR),可通过建立冗余机制实现,包括主/备(active/passive)、双主(active/active)等模式,以及通过心跳机制(HEARTBEAT)进行状态监测和切换。

虚拟路由冗余协议(VRRP):用于解决静态网关单点风险,可在物理层的路由器、三层交换机和软件层的 keepalived 中应用。

VRRP 相关概念

虚拟路由器: Virtual Router
虚拟路由器标识: VRID(0-255) ,唯一标识虚拟路由器
VIP Virtual IP
VMAC Virutal MAC (00-00-5e-00-01-VRID)
物理路由器:
master :主设备
backup :备用设备
priority :优先级
通告相关: 包括心跳和优先级等,具有周期性特点。
安全认证方式: 无认证或简单字符认证或采用预共享密钥以及MD5 认证。
工作模式:
主/备:单虚拟路由器模式。
主/主:分为主/备(虚拟路由器 1)和备/主(虚拟路由器 2)两种情况。

keepalived 简介

        负载均衡是一种在真实集群之间分配 IP 流量的方法 服务器,提供一个或多个高可用性虚拟服务。在设计负载均衡拓扑时,必须考虑负载均衡器本身以及后面的真实服务器的可用性。

        Keepalived 为负载均衡和高可用性提供了框架。 负载均衡框架依赖于众所周知且广泛使用的 Linux 虚拟服务器 (IPVS) 内核模块,提供第 4 层负载均衡。 Keepalived 实现了一组健康检查器,以动态和自适应的方式 根据服务器池的运行状况维护和管理负载均衡的服务器池。 高可用性是通过虚拟冗余路由协议实现的 (VRRP)。VRRP是路由器故障切换的基础砖块。keepalived也 实现了一组到 VRRP 有限状态机的钩子 提供低级和高速协议交互。每个Keepalived 框架可以单独使用,也可以一起使用,以提供弹性基础设施。

简而言之,Keepalived 提供了两个主要功能:

  • LVS系统的健康检查
  • 实施 VRRPv2 堆栈以处理负载均衡器故障切换

2、基础实验环境搭建

克隆4台主机:realserver1、realserver2、KAT1、KAT2

注意修改一下虚拟机内存和处理器大小

各节点时间必须同步: ntp, chrony
关闭防火墙及 SELinux
各节点之间可通过主机名互相通信:非必须
建议使用 /etc/hosts 文件实现:非必须
各节点之间的 root 用户可以基于密钥认证的 ssh 服务完成互相通信:非必须

①为各个主机添加网卡信息

[root@localhost ~]# vmset.sh eth0 172.25.254.110 realserver1.zf.org
连接已成功激活(D-Bus 活动路径:/org/freedesktop/NetworkManager/ActiveConnection/4)

[root@localhost ~]# vmset.sh eth0 172.25.254.120 realserver2.zf.org
连接已成功激活(D-Bus 活动路径:/org/freedesktop/NetworkManager/ActiveConnection/4)

[root@localhost ~]# vmset.sh eth0 172.25.254.10 KAT1.zf.org
连接已成功激活(D-Bus 活动路径:/org/freedesktop/NetworkManager/ActiveConnection/4)

[root@localhost ~]# vmset.sh eth0 172.25.254.20 KAT2.zf.org
连接已成功激活(D-Bus 活动路径:/org/freedesktop/NetworkManager/ActiveConnection/4)

②server上安装软件httpd,并做重定向配置

[root@realserver1 ~]# yum install httpd -y
[root@realserver2 ~]# yum install httpd -y
[root@realserver1 ~]# echo 172.25.254.110 > /var/www/html/index.html
[root@realserver2 ~]# echo 172.25.254.120 > /var/www/html/index.html
[root@realserver1 ~]# systemctl enable --now httpd
[root@realserver2 ~]# systemctl enable --now httpd

基础环境测试

 3、keepalived的虚拟路由管理

全局配置

! Configuration File for keepalived
global_defs {
notification_email {
594233887@qq.com #keepalived 发生故障切换时邮件发送的目标邮箱,可以按行区
分写多个
timiniglee-zln@163.com
}
notification_email_from keepalived@KA1.timinglee.org #发邮件的地址
smtp_server 127.0.0.1 #邮件服务器地址
smtp_connect_timeout 30 #邮件服务器连接timeout
router_id KA1.timinglee.org #每个keepalived主机唯一标识
#建议使用当前主机名,但多节点
重名不影响
vrrp_skip_check_adv_addr #对所有通告报文都检查,会比较消耗性能
#启用此配置后,如果收到的通告报文和上一
个报文是同一 #个路由器,则跳过检查,默认
值为全检查
vrrp_strict #严格遵循vrrp协议
#启用此项后以下状况将无法启动服务:
#1.无VIP地址
#2.配置了单播邻居
#3.在VRRP版本2中有IPv6地址
#建议不加此项配置
vrrp_garp_interval 0 #报文发送延迟,0表示不延迟
vrrp_gna_interval 0 #消息发送延迟
vrrp_mcast_group4 224.0.0.18 #指定组播IP地址范围:
}

在KAT1和KAT2中安装keepalived安装包

[root@kat1 ~]# yum install keepalived -y
[root@kat2 ~]# yum install keepalived -y

编辑配置文件

[root@kat1 ~]# vim /etc/keepalived/keepalived.conf

 配置虚拟路由器

vrrp_instance VI_1 {
state MASTER
interface eth0 #绑定为当前虚拟路由器使用的物理接口,如:eth0,可以和VIP不在一
个网卡
virtual_router_id 51 #每个虚拟路由器惟一标识,范围:0-255,每个虚拟路由器此值必须唯一
#否则服务无法启动
#同属一个虚拟路由器的多个keepalived节点必须相同
#务必要确认在同一网络中此值必须唯一
priority 100 #当前物理节点在此虚拟路由器的优先级,范围:1-254
#值越大优先级越高,每个keepalived主机节点此值不同
advert_int 1 #vrrp通告的时间间隔,默认1s
authentication { #认证机制
auth_type AH|PASS #AH为IPSEC认证(不推荐),PASS为简单密码(建议使用)
uth_pass 1111 #预共享密钥,仅前8位有效
#同一个虚拟路由器的多个keepalived节点必须一样
}
virtual_ipaddress { #虚拟IP,生产环境可能指定上百个IP地址
<IPADDR>/<MASK> brd <IPADDR> dev <STRING> scope <SCOPE> label <LABEL>
172.25.254.100 #指定VIP,不指定网卡,默认为eth0,注意:不指定/prefix,默认32
172.25.254.101/24 dev eth1
172.25.254.102/24 dev eth2 label eth2:1
 }
}

 编辑配置文件

[root@kat1 ~]# vim /etc/keepalived/keepalived.conf
[root@kat1 ~]# systemctl enable --now keepalived.service
Created symlink from /etc/systemd/system/multi-user.target.wants/keepalived.service to /usr/lib/systemd/system/keepalived.service.
[root@kat1 ~]# systemctl restart keepalived.service 
[root@kat1 ~]# scp /etc/keepalived/keepalived.conf  root@172.25.254.20:/etc/keepalived/keepalived.conf 
The authenticity of host '172.25.254.20 (172.25.254.20)' can't be established.
ECDSA key fingerprint is SHA256:p8+SUh5ckDQItOAIxbzYL28fpdswAsYDOXJUm6sD/6k.
ECDSA key fingerprint is MD5:30:56:50:67:5e:d4:ca:37:33:ff:e0:ca:c3:71:cc:be.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '172.25.254.20' (ECDSA) to the list of known hosts.
root@172.25.254.20's password: 
keepalived.conf                                               100% 3542     2.4MB/s   00:00

 设定KAT1的router-id和VIP的地址

查看KAT1的VIP

 修改KAT2的优先级

[root@kat2 ~]# vim /etc/keepalived/keepalived.conf 
[root@kat2 ~]# systemctl restart keepalived.service
[root@kat2 ~]# systemctl enable --now keepalived.service 
Created symlink from /etc/systemd/system/multi-user.target.wants/keepalived.service to /usr/lib/systemd/system/keepalived.service.

 由于优先级低于kat1,KAT2没有VIP

抓包测试:在realserver1上远程登录kat1,关闭keepalived服务,模拟故障,在kat1上利用tcpdump命令检测发现有20的vip,重新启动keepalived服务,此时kat1上利用tcpdump命令检测发现有10的vip,kat2无vip

 

 

 4、虚拟路由的通讯设置

DROP参数限制了虚拟路由的通讯

编辑kat1和kat2的配置文件:vim /etc/keepalived/keepalived.conf
                       systemctl restart keepalived.service

 realserver测试:

 或者kat1和kat2注释掉2个全局参数

测试:

 

 5、keepalived的日志分离

[root@kat1 ~]# vim /etc/sysconfig/keepalived 
[root@kat1 ~]# systemctl restart keepalived.service

[root@kat1 ~]# vim /etc/rsyslog.conf
[root@kat1 ~]# systemctl restart rsyslog.service
[root@kat1 ~]# systemctl restart keepalived.service

 查看独立出的日志信息

[root@kat1 ~]# ll /var/log/keepalived.log 
-rw------- 1 root root 8268 Aug 12 18:19 /var/log/keepalived.log
[root@kat1 ~]# cat /var/log/keepalived.log 
Aug 12 18:19:14 kat1 Keepalived[5702]: Stopping
Aug 12 18:19:14 kat1 Keepalived_vrrp[5704]: VRRP_Instance(VI_1) sent 0 priority
Aug 12 18:19:14 kat1 Keepalived_vrrp[5704]: VRRP_Instance(VI_1) removing protocol VIPs.
Aug 12 18:19:14 kat1 Keepalived_healthcheckers[5703]: Stopped
Aug 12 18:19:15 kat1 Keepalived_vrrp[5704]: Stopped
Aug 12 18:19:15 kat1 Keepalived[5702]: Stopped Keepalived v1.3.5 (03/19,2017), git commit v1.3.5-6-g6fa32f2
Aug 12 18:19:15 kat1 Keepalived[5822]: Starting Keepalived v1.3.5 (03/19,2017), git commit v1.3.5-6-g6fa32f2
Aug 12 18:19:15 kat1 Keepalived[5822]: Opening file '/etc/keepalived/keepalived.conf'.
Aug 12 18:19:15 kat1 Keepalived[5823]: Starting Healthcheck child process, pid=5824
Aug 12 18:19:15 kat1 Keepalived[5823]: Starting VRRP child process, pid=5825
Aug 12 18:19:15 kat1 Keepalived_healthcheckers[5824]: Initializing ipvs
Aug 12 18:19:15 kat1 Keepalived_healthcheckers[5824]: Opening file '/etc/keepalived/keepalived.conf'.
Aug 12 18:19:15 kat1 Keepalived_healthcheckers[5824]: Activating healthchecker for service [192.168.200.100]:443
Aug 12 18:19:15 kat1 Keepalived_healthcheckers[5824]: Activating healthchecker for service [10.10.10.2]:1358
Aug 12 18:19:15 kat1 Keepalived_healthcheckers[5824]: Activating healthchecker for service [10.10.10.2]:1358
Aug 12 18:19:15 kat1 Keepalived_healthcheckers[5824]: Activating healthchecker for service [10.10.10.3]:1358
Aug 12 18:19:15 kat1 Keepalived_healthcheckers[5824]: Activating healthchecker for service [10.10.10.3]:1358
Aug 12 18:19:15 kat1 Keepalived_vrrp[5825]: Registering Kernel netlink reflector
Aug 12 18:19:15 kat1 Keepalived_vrrp[5825]: Registering Kernel netlink command channel
Aug 12 18:19:15 kat1 Keepalived_vrrp[5825]: Registering gratuitous ARP shared channel
Aug 12 18:19:15 kat1 Keepalived_vrrp[5825]: Opening file '/etc/keepalived/keepalived.conf'.
Aug 12 18:19:15 kat1 Keepalived_vrrp[5825]: VRRP_Instance(VI_1) removing protocol VIPs.
Aug 12 18:19:15 kat1 Keepalived_vrrp[5825]: Using LinkWatch kernel netlink reflector...
Aug 12 18:19:15 kat1 Keepalived_vrrp[5825]: VRRP sockpool: [ifindex(2), proto(112), unicast(0), fd(10,11)]
Aug 12 18:19:16 kat1 Keepalived_vrrp[5825]: VRRP_Instance(VI_1) Transition to MASTER STATE
Aug 12 18:19:17 kat1 Keepalived_vrrp[5825]: VRRP_Instance(VI_1) Entering MASTER STATE
Aug 12 18:19:17 kat1 Keepalived_vrrp[5825]: VRRP_Instance(VI_1) setting protocol VIPs.
Aug 12 18:19:17 kat1 Keepalived_vrrp[5825]: Sending gratuitous ARP on eth0 for 172.25.254.100
Aug 12 18:19:17 kat1 Keepalived_vrrp[5825]: VRRP_Instance(VI_1) Sending/queueing gratuitous ARPs on eth0 for 172.25.254.100
Aug 12 18:19:17 kat1 Keepalived_vrrp[5825]: Sending gratuitous ARP on eth0 for 172.25.254.100
Aug 12 18:19:17 kat1 Keepalived_vrrp[5825]: Sending gratuitous ARP on eth0 for 172.25.254.100
Aug 12 18:19:17 kat1 Keepalived_vrrp[5825]: Sending gratuitous ARP on eth0 for 172.25.254.100
Aug 12 18:19:17 kat1 Keepalived_vrrp[5825]: Sending gratuitous ARP on eth0 for 172.25.254.100
Aug 12 18:19:21 kat1 Keepalived_healthcheckers[5824]: Timeout connecting server [192.168.200.2]:1358.
Aug 12 18:19:21 kat1 Keepalived_healthcheckers[5824]: Timeout connecting server [192.168.200.4]:1358.
Aug 12 18:19:22 kat1 Keepalived_vrrp[5825]: Sending gratuitous ARP on eth0 for 172.25.254.100
Aug 12 18:19:22 kat1 Keepalived_vrrp[5825]: VRRP_Instance(VI_1) Sending/queueing gratuitous ARPs on eth0 for 172.25.254.100
Aug 12 18:19:22 kat1 Keepalived_vrrp[5825]: Sending gratuitous ARP on eth0 for 172.25.254.100
Aug 12 18:19:22 kat1 Keepalived_vrrp[5825]: Sending gratuitous ARP on eth0 for 172.25.254.100
Aug 12 18:19:22 kat1 Keepalived_vrrp[5825]: Sending gratuitous ARP on eth0 for 172.25.254.100
Aug 12 18:19:22 kat1 Keepalived_vrrp[5825]: Sending gratuitous ARP on eth0 for 172.25.254.100
Aug 12 18:19:22 kat1 Keepalived_healthcheckers[5824]: Timeout connecting server [192.168.200.5]:1358.
Aug 12 18:19:24 kat1 Keepalived_healthcheckers[5824]: Timeout connecting server [192.168.200.3]:1358.
Aug 12 18:19:24 kat1 Keepalived_healthcheckers[5824]: Timeout connecting server [192.168.201.100]:443.
Aug 12 18:19:27 kat1 Keepalived_healthcheckers[5824]: Timeout connecting server [192.168.200.2]:1358.
Aug 12 18:19:27 kat1 Keepalived_healthcheckers[5824]: Timeout connecting server [192.168.200.4]:1358.
Aug 12 18:19:28 kat1 Keepalived_healthcheckers[5824]: Timeout connecting server [192.168.200.5]:1358.
Aug 12 18:19:30 kat1 Keepalived_healthcheckers[5824]: Timeout connecting server [192.168.200.3]:1358.
Aug 12 18:19:30 kat1 Keepalived_healthcheckers[5824]: Timeout connecting server [192.168.201.100]:443.
Aug 12 18:19:33 kat1 Keepalived_healthcheckers[5824]: Timeout connecting server [192.168.200.2]:1358.
Aug 12 18:19:33 kat1 Keepalived_healthcheckers[5824]: Timeout connecting server [192.168.200.4]:1358.
Aug 12 18:19:34 kat1 Keepalived_healthcheckers[5824]: Timeout connecting server [192.168.200.5]:1358.
Aug 12 18:19:36 kat1 Keepalived_healthcheckers[5824]: Timeout connecting server [192.168.200.3]:1358.
Aug 12 18:19:36 kat1 Keepalived_healthcheckers[5824]: Timeout connecting server [192.168.201.100]:443.
Aug 12 18:19:39 kat1 Keepalived_healthcheckers[5824]: Timeout connecting server [192.168.200.2]:1358.
Aug 12 18:19:39 kat1 Keepalived_healthcheckers[5824]: Check on service [192.168.200.2]:1358 failed after 3 retry.
Aug 12 18:19:39 kat1 Keepalived_healthcheckers[5824]: Removing service [192.168.200.2]:1358 from VS [10.10.10.2]:1358
Aug 12 18:19:39 kat1 Keepalived_healthcheckers[5824]: Remote SMTP server [127.0.0.1]:25 connected.
Aug 12 18:19:39 kat1 Keepalived_healthcheckers[5824]: Timeout connecting server [192.168.200.4]:1358.
Aug 12 18:19:39 kat1 Keepalived_healthcheckers[5824]: Check on service [192.168.200.4]:1358 failed after 3 retry.
Aug 12 18:19:39 kat1 Keepalived_healthcheckers[5824]: Removing service [192.168.200.4]:1358 from VS [10.10.10.3]:1358
Aug 12 18:19:39 kat1 Keepalived_healthcheckers[5824]: Remote SMTP server [127.0.0.1]:25 connected.
Aug 12 18:19:39 kat1 Keepalived_healthcheckers[5824]: SMTP alert successfully sent.
Aug 12 18:19:39 kat1 Keepalived_healthcheckers[5824]: SMTP alert successfully sent.
Aug 12 18:19:40 kat1 Keepalived_healthcheckers[5824]: Timeout connecting server [192.168.200.5]:1358.
Aug 12 18:19:40 kat1 Keepalived_healthcheckers[5824]: Check on service [192.168.200.5]:1358 failed after 3 retry.
Aug 12 18:19:40 kat1 Keepalived_healthcheckers[5824]: Removing service [192.168.200.5]:1358 from VS [10.10.10.3]:1358
Aug 12 18:19:40 kat1 Keepalived_healthcheckers[5824]: Lost quorum 1-0=1 > 0 for VS [10.10.10.3]:1358
Aug 12 18:19:40 kat1 Keepalived_healthcheckers[5824]: Remote SMTP server [127.0.0.1]:25 connected.
Aug 12 18:19:40 kat1 Keepalived_healthcheckers[5824]: SMTP alert successfully sent.
Aug 12 18:19:42 kat1 Keepalived_healthcheckers[5824]: Timeout connecting server [192.168.200.3]:1358.
Aug 12 18:19:42 kat1 Keepalived_healthcheckers[5824]: Check on service [192.168.200.3]:1358 failed after 3 retry.
Aug 12 18:19:42 kat1 Keepalived_healthcheckers[5824]: Removing service [192.168.200.3]:1358 from VS [10.10.10.2]:1358
Aug 12 18:19:42 kat1 Keepalived_healthcheckers[5824]: Lost quorum 1-0=1 > 0 for VS [10.10.10.2]:1358
Aug 12 18:19:42 kat1 Keepalived_healthcheckers[5824]: Adding sorry server [192.168.200.200]:1358 to VS [10.10.10.2]:1358
Aug 12 18:19:42 kat1 Keepalived_healthcheckers[5824]: Removing alive servers from the pool for VS [10.10.10.2]:1358
Aug 12 18:19:42 kat1 Keepalived_healthcheckers[5824]: Remote SMTP server [127.0.0.1]:25 connected.
Aug 12 18:19:42 kat1 Keepalived_healthcheckers[5824]: SMTP alert successfully sent.
Aug 12 18:19:42 kat1 Keepalived_healthcheckers[5824]: Timeout connecting server [192.168.201.100]:443.
Aug 12 18:19:42 kat1 Keepalived_healthcheckers[5824]: Check on service [192.168.201.100]:443 failed after 3 retry.
Aug 12 18:19:42 kat1 Keepalived_healthcheckers[5824]: Removing service [192.168.201.100]:443 from VS [192.168.200.100]:443
Aug 12 18:19:42 kat1 Keepalived_healthcheckers[5824]: Lost quorum 1-0=1 > 0 for VS [192.168.200.100]:443
Aug 12 18:19:42 kat1 Keepalived_healthcheckers[5824]: Remote SMTP server [127.0.0.1]:25 connected.
Aug 12 18:19:42 kat1 Keepalived_healthcheckers[5824]: SMTP alert successfully sent.

6、实现独立子配置文件 

[root@kat1 ~]# vim /etc/keepalived/keepalived.conf
[root@kat1 ~]# systemctl restart keepalived.service
Job for keepalived.service failed because the control process exited with error code. See "systemctl status keepalived.service" and "journalctl -xe" for details.

[root@kat1 ~]# mkdir -p /etc/keepalived/conf.d #建立子目录
[root@kat1 ~]# vim /etc/keepalived/conf.d/172.25.254.100.conf
[root@kat1 ~]# systemctl restart keepalived.service

测试:

 7、keepalived的抢占模式和非抢占模式

    默认为抢占模式 preempt ,即当高优先级的主机恢复在线后,会抢占低先级的主机的 master 角色,这样会使vip KA 主机中来回漂移,造成网络抖动,建议设置为非抢占模式 nopreempt ,即高优先级主机恢复后,并不会抢占低优先级主机的 master 角色。
    非抢占模块下 , 如果原主机 down , VIP 迁移至的新主机 , 后续也发生 down , 仍会将 VIP 迁移回原主机。

 非抢占模式

参数:nopreempt #非抢占模式

修改kat1和kat2主配置文件

[root@kat1 ~]# vim /etc/keepalived/keepalived.conf 
[root@kat1 ~]# systemctl restart keepalived.service
[root@kat2 ~]# vim /etc/keepalived/keepalived.conf 
[root@kat2 ~]# systemctl restart keepalived.service

 测试:

kat2关闭keepalived服务,测试

开启kat2的keepalived服务 ,再次测试

 抢占延迟模式

     抢占延迟模式,即优先级高的主机恢复后,不会立即抢回 VIP ,而是延迟一段时间(默认 300s)再抢回VIP,参数:preempt_delay  # 指定抢占延迟时间为 #s ,默认延迟 300s
为了实验效果,设定抢占延迟时间为5s
[root@kat1 ~]# vim /etc/keepalived/keepalived.conf
[root@kat1 ~]# systemctl restart keepalived.service

[root@kat2 ~]# vim /etc/keepalived/keepalived.conf 
[root@kat2 ~]# systemctl restart keepalived.service

测试:先关闭kat1的服务,kat2有VIP

[root@kat1 ~]# systemctl stop keepalived.service 

 再次打开kat1的服务,5s后,kat1有VIP

[root@kat1 ~]# systemctl start keepalived.service 

 注意:建议做完实验后,注释掉延迟抢占的参数,恢复到默认的抢占模式。

8、VIP单播配置

       默认keepalived主机之间利用多播相互通告消息,会造成网络拥塞,可以替换成单播,减少网络流量。注意:启用 vrrp_strict 时,不能启用单播 , 否则服务无法启动 , 并在 messages 文件中记录下面信息 。
[root@kat1 ~]# vim /etc/keepalived/keepalived.conf
[root@kat1 ~]# systemctl restart keepalived.service
[root@kat2 ~]# vim /etc/keepalived/keepalived.conf
[root@kat2 ~]# systemctl restart keepalived.service

抓包测试:

 

此时关闭kat1的服务,再次抓包

开启kat1的服务

9、邮件通知

安装邮件发送工具
[root@kat1 ~]# yum install mailx -y
[root@kat2 ~]# yum install mailx -y

在QQ邮箱页面设定POP3/IMAP/SMTP/Exchange/CardDAV 服务,查看授权码

[root@kat1 ~]# vim /etc/mail.rc
[root@kat2 ~]# vim /etc/mail.rc

[root@kat1 ~]# echo hello world | mail -s test 1373771818@qq.com
[root@kat2 ~]# echo test | mail -s test 1373771818@qq.com

到QQ邮箱查看

 通知脚本配置

[root@kat1 ~]# vim /etc/keepalived/mail.sh
[root@kat1 ~]# chmod +x /etc/keepalived/mail.sh
[root@kat2 ~]# vim /etc/keepalived/mail.sh
[root@kat2 ~]# chmod +x /etc/keepalived/mail.sh

编辑主配置文件 

[root@kat1 ~]# vim /etc/keepalived/keepalived.conf
[root@kat1 ~]# systemctl restart keepalived.service

[root@kat2 ~]# vim /etc/keepalived/keepalived.conf 
[root@kat2 ~]# systemctl restart keepalived.service

 查看邮件:

 

 测试:


[root@kat1 ~]# systemctl stop  keepalived.service

[root@kat1 ~]# systemctl start  keepalived.service

10、实现 master/master Keepalived 双主架构

[root@kat1 ~]# vim /etc/keepalived/keepalived.conf
[root@kat1 ~]# systemctl start  keepalived.service 

[root@kat2 ~]# vim /etc/keepalived/keepalived.conf 
[root@kat2 ~]# systemctl restart keepalived.service

测试:

11、实现IPVS的高可用性

   实现双主的 LVS-DR 模式

[root@realserver1 ~]# ip a a 172.25.254.100/32 dev lo
[root@realserver1 ~]# cd /etc/sysconfig/network-scripts/
[root@realserver1 network-scripts]# ls
ifcfg-ens33  ifdown-bnep  ifdown-isdn    ifdown-Team      ifup-bnep  ifup-isdn   ifup-routes    ifup-wireless
ifcfg-eth0   ifdown-eth   ifdown-post    ifdown-TeamPort  ifup-eth   ifup-plip   ifup-sit       init.ipv6-global
ifcfg-eth1   ifdown-ib    ifdown-ppp     ifdown-tunnel    ifup-ib    ifup-plusb  ifup-Team      network-functions
ifcfg-lo     ifdown-ippp  ifdown-routes  ifup             ifup-ippp  ifup-post   ifup-TeamPort  network-functions-ipv6
ifdown       ifdown-ipv6  ifdown-sit     ifup-aliases     ifup-ipv6  ifup-ppp    ifup-tunnel
[root@realserver1 network-scripts]# rm -rf ifcfg-ens33
[root@realserver1 network-scripts]# rm -rf ifcfg-eth1
[root@realserver1 network-scripts]# systemctl restart network


[root@realserver2 ~]# ip a a 172.25.254.100/32 dev lo
[root@realserver2 ~]# vim /etc/sysctl.d/arp.conf
[root@realserver2 ~]# scp /etc/sysctl.d/arp.conf root@172.25.254.110:/etc/sysctl.d/arp.conf
The authenticity of host '172.25.254.110 (172.25.254.110)' can't be established.
ECDSA key fingerprint is SHA256:p8+SUh5ckDQItOAIxbzYL28fpdswAsYDOXJUm6sD/6k.
ECDSA key fingerprint is MD5:30:56:50:67:5e:d4:ca:37:33:ff:e0:ca:c3:71:cc:be.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '172.25.254.110' (ECDSA) to the list of known hosts.
root@172.25.254.110's password: 
arp.conf                                                   100%  126   217.5KB/s   00:00                                                                                

开启内核路由 

[root@kat1 ~]# yum install ipvsadm -y
[root@kat2 ~]# yum install ipvsadm -y

[root@kat1 ~]# vim /etc/keepalived/keepalived.conf
[root@kat1 ~]# systemctl restart keepalived.service
[root@kat2 ~]# vim /etc/keepalived/keepalived.conf
[root@kat2 ~]# systemctl restart keepalived.service

 ​​​​​​虚拟服务器配置、应用层检测及TCP监测相关参数详解

virtual_server IP port { #VIP和PORT
delay_loop <INT> #检查后端服务器的时间间隔
lb_algo rr|wrr|lc|wlc|lblc|sh|dh #定义调度方法
lb_kind NAT|DR|TUN #集群的类型,注意要大写
persistence_timeout <INT> #持久连接时长
protocol TCP|UDP|SCTP #指定服务协议,一般为TCP
sorry_server <IPADDR> <PORT> #所有RS故障时,备用服务器地址
real_server <IPADDR> <PORT> { #RS的IP和PORT
weight <INT> #RS权重
notify_up <STRING>|<QUOTED-STRING> #RS上线通知脚本
notify_down <STRING>|<QUOTED-STRING> #RS下线通知脚本
HTTP_GET|SSL_GET|TCP_CHECK|SMTP_CHECK|MISC_CHECK { ... } #定义当前主机健康状态检测方法

HTTP_GET|SSL_GET {
url {
path <URL_PATH> #定义要监控的URL
status_code <INT> #判断上述检测机制为健康状态的响应码,一般为 200
}
connect_timeout <INTEGER> #客户端请求的超时时长, 相当于haproxy的timeout server
nb_get_retry <INT> #重试次数
delay_before_retry <INT> #重试之前的延迟时长
connect_ip <IP ADDRESS> #向当前RS哪个IP地址发起健康状态检测请求
connect_port <PORT> #向当前RS的哪个PORT发起健康状态检测请求
bindto <IP ADDRESS> #向当前RS发出健康状态检测请求时使用的源地址
bind_port <PORT> #向当前RS发出健康状态检测请求时使用的源端口
}
TCP_CHECK {
connect_ip <IP ADDRESS> #向当前RS的哪个IP地址发起健康状态检测请求
connect_port <PORT> #向当前RS的哪个PORT发起健康状态检测请求
bindto <IP ADDRESS> #发出健康状态检测请求时使用的源地址
bind_port <PORT> #发出健康状态检测请求时使用的源端口
connect_timeout <INTEGER> #客户端请求的超时时长
#等于haproxy的timeout server
}

 查看策略结果

 测试:

[root@realserver1 ~]# systemctl stop httpd.service

 

再次开启realserver1的httpd服务

利用脚本实现主从角色切换 

      定义脚本

  • vrrp_script:自定义资源监控脚本,vrrp实例根据脚本返回值,公共定义,可被多个实例调用,定义在vrrp实例之外的独立配置块,一般放在global_defs设置块之后。
  • 通常此脚本用于监控指定应用的状态。一旦发现应用的状态异常,则触发对MASTER节点的权重减至低于SLAVE节点,从而实现 VIP 切换到 SLAVE 节点。
  • vrrp_script <SCRIPT_NAME> { #定义一个检测脚本,在global_defs 之外配置
    script <STRING>|<QUOTED-STRING> #shell命令或脚本路径
    interval <INTEGER> #间隔时间,单位为秒,默认1秒
    timeout <INTEGER> #超时时间
    weight <INTEGER:-254..254> #默认为0,如果设置此值为负数,
    #当上面脚本返回值为非0时
    #会将此值与本节点权重相加可以降低本节点权重,
    #即表示fall.
    #如果是正数,当脚本返回值为0,
    #会将此值与本节点权重相加可以提高本节点权重
    #即表示 rise.通常使用负值
    fall <INTEGER> #执行脚本连续几次都失败,则转换为失败,建议设为2以上
    rise <INTEGER> #执行脚本连续几次都成功,把服务器从失败标记为成功
    user USERNAME [GROUPNAME] #执行监测脚本的用户或组
    init_fail #设置默认标记为失败状态,监测成功之后再转换为成功状态
    }

    调用脚本

  • track_script:调用vrrp_script定义的脚本去监控资源,定义在VRRP实例之内,调用事先定义的 vrrp_script。
  • vrrp_instance test {
    ... ...
    track_script {
    check_down
      }
    }

具体配置如下: 

[root@kat1 ~]# vim /etc/keepalived/test.sh
[root@kat1 ~]# sh /etc/keepalived/test.sh
[root@kat1 ~]# echo $?
0

[root@kat1 ~]# vim /etc/keepalived/test.sh
[root@kat1 ~]# chmod +x /etc/keepalived/test.sh

 查看脚本返回值的真假

[root@kat1 ~]# sh /etc/keepalived/test.sh
0

删除echo $?后,测试:

① 定义 VRRP script

[root@kat1 ~]# vim /etc/keepalived/keepalived.conf
[root@kat1 ~]# ls /mnt/zf
/mnt/zf
[root@kat1 ~]# systemctl restart keepalived.service

②调用 VRRP script  

 测试:

实现haproxy高可用

安装软件包

[root@kat1 ~]# yum install haproxy -y
[root@kat2 ~]# yum install haproxy -y

在两个kat1kat2两个节点启用内核参数 

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

在两个 kat1 kat2 实现 haproxy 的配置
[root@kat1 ~]# vim /etc/haproxy/haproxy.cfg
[root@kat1 ~]# systemctl enable --now haproxy.service
Created symlink from /etc/systemd/system/multi-user.target.wants/haproxy.service to /usr/lib/systemd/system/haproxy.service.
[root@kat2 ~]# vim /etc/haproxy/haproxy.cfg

重启realserver上的网卡服务 

 改回原先的配置环境

[root@realserver1 ~]# vim /etc/sysctl.d/arp.conf
[root@realserver1 ~]# sysctl --system
* Applying /usr/lib/sysctl.d/00-system.conf ...
* Applying /usr/lib/sysctl.d/10-default-yama-scope.conf ...
kernel.yama.ptrace_scope = 0
* Applying /usr/lib/sysctl.d/50-default.conf ...
kernel.sysrq = 16
kernel.core_uses_pid = 1
kernel.kptr_restrict = 1
net.ipv4.conf.default.rp_filter = 1
net.ipv4.conf.all.rp_filter = 1
net.ipv4.conf.default.accept_source_route = 0
net.ipv4.conf.all.accept_source_route = 0
net.ipv4.conf.default.promote_secondaries = 1
net.ipv4.conf.all.promote_secondaries = 1
fs.protected_hardlinks = 1
fs.protected_symlinks = 1
* Applying /usr/lib/sysctl.d/60-libvirtd.conf ...
fs.aio-max-nr = 1048576
* Applying /etc/sysctl.d/99-sysctl.conf ...
* Applying /etc/sysctl.d/arp.conf ...
net.ipv4.conf.all.arp_ignore = 0
net.ipv4.conf.all.arp_announce = 0
net.ipv4.conf.lo.arp_ignore = 0
net.ipv4.conf.lo.arp_announce = 0
* Applying /etc/sysctl.conf ...


[root@realserver2 ~]# vim /etc/sysctl.d/arp.conf
[root@realserver2 ~]# sysctl --system
* Applying /usr/lib/sysctl.d/00-system.conf ...
* Applying /usr/lib/sysctl.d/10-default-yama-scope.conf ...
kernel.yama.ptrace_scope = 0
* Applying /usr/lib/sysctl.d/50-default.conf ...
kernel.sysrq = 16
kernel.core_uses_pid = 1
kernel.kptr_restrict = 1
net.ipv4.conf.default.rp_filter = 1
net.ipv4.conf.all.rp_filter = 1
net.ipv4.conf.default.accept_source_route = 0
net.ipv4.conf.all.accept_source_route = 0
net.ipv4.conf.default.promote_secondaries = 1
net.ipv4.conf.all.promote_secondaries = 1
fs.protected_hardlinks = 1
fs.protected_symlinks = 1
* Applying /usr/lib/sysctl.d/60-libvirtd.conf ...
fs.aio-max-nr = 1048576
* Applying /etc/sysctl.d/99-sysctl.conf ...
* Applying /etc/sysctl.d/arp.conf ...
net.ipv4.conf.all.arp_ignore = 0
net.ipv4.conf.all.arp_announce = 0
net.ipv4.conf.lo.arp_ignore = 0
net.ipv4.conf.lo.arp_announce = 0
* Applying /etc/sysctl.conf ...

 测试一下

[root@kat1 ~]# curl 172.25.254.110
172.25.254.110
[root@kat1 ~]# curl 172.25.254.120
172.25.254.120

 修改配置文件

[root@kat1 ~]# vim /etc/haproxy/haproxy.cfg 
[root@kat1 ~]# systemctl restart keepalived.service 
[root@kat1 ~]# systemctl restart haproxy.service
[root@kat2 ~]# vim /etc/haproxy/haproxy.cfg 
[root@kat2 ~]# systemctl enable --now haproxy.service 
Created symlink from /etc/systemd/system/multi-user.target.wants/haproxy.service to /usr/lib/systemd/system/haproxy.service.

 测试:

 haproxy服务不能与keepalived服务同时存在

[root@kat1 ~]# vim /etc/keepalived/keepalived.conf
[root@kat1 ~]# systemctl restart keepalived.service
[root@kat2 ~]# vim /etc/keepalived/keepalived.conf
[root@kat2 ~]# systemctl restart keepalived.service

 修改haproxy的配置文件

[root@kat1 ~]# vim /etc/haproxy/haproxy.cfg 
[root@kat1 ~]# systemctl restart haproxy.service 
[root@kat1 ~]# systemctl restart keepalived.service 

[root@kat2 ~]# vim /etc/haproxy/haproxy.cfg 
[root@kat2 ~]# systemctl restart keepalived.service 
[root@kat2 ~]# systemctl restart haproxy.service 

 访问测试:

[root@kat1 ~]# curl 172.25.254.100
172.25.254.110
[root@kat1 ~]# curl 172.25.254.100
172.25.254.120

 检测命令:

[root@kat1 ~]# vim /etc/keepalived/test.sh
[root@kat2 ~]# vim /etc/keepalived/test.sh
[root@kat2 ~]# cat /etc/keepalived/test.sh
#!/bin/bash
killall -0 haproxy

[root@kat1 ~]# vim /etc/keepalived/keepalived.conf
[root@kat1 ~]# systemctl restart keepalived.service 
[root@kat1 ~]# systemctl start haproxy.service
[root@kat2 ~]# vim /etc/keepalived/keepalived.conf
[root@kat2 ~]# systemctl restart keepalived.service 

最终测试

[C:\~]$ while true
> do
> curl 172.25.254.100;sleep 0.5
>done
172.25.254.120
172.25.254.110
172.25.254.120
172.25.254.110

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2036044.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

ubuntu 20 安装mysql workbench 过程

ubuntu 20 安装mysql workbench 过程_ubuntu 安装mysql workbench-CSDN博客 How To Install And Use MySQL Workbench On Ubuntu 18.04 or 20.04 MySQL :: Begin Your Download sudo apt install ./mysql-apt-config_0.8.32-1_all.deb snap install mysql-workbench-communi…

【鸿蒙学习】HarmonyOS应用开发者基础 - 从简单的页面开始

学完时间&#xff1a;2024年8月13日 我的纸飞机呀&#xff01;飞呀飞&#xff01;飞到了代码中&#xff01;&#xff01;&#xff01; 一、前言叨叨 今天是HarmonyOS学习教学课第一课的第五课内容了&#xff0c;课后练习的人数已经降到了4150人了&#xff0c;预测下到 ”Harmon…

Win10 去掉桌面右上角 了解有关此图片的信息

1. 进入注册表 Win R运行regedit 2. 找到以下路径 计算机\HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\HideDesktopIcons\NewStartPanel 3. 新建 DWORD&#xff08;32位&#xff09;值&#xff08;D&#xff09; 右击 NewStartPanel新建 DWORD…

前端技术day01-HTML入门

一、前端介绍 技术描述HTML用于构建网站的基础结构的CSS用于美化页面的&#xff0c;作用和化妆或者整容作用一样JS实现网页和用户的交互Vue主要用于将数据填充到html页面上的Element主要提供了一些非常美观的组件 二、工具软件 VsCode 在前端领域&#xff0c;有一个公认好用…

C语言问答进阶--6、函数(1)

A&#xff1a;现在我们将研究函数这个概念。 其实这个概念很简单&#xff0c;和中学的时候学的yf(x)来对照着看&#xff0c;对于一个参数x&#xff0c;会得到一个值y&#xff0c;就会发现它和C中的函数是一个道理。 只是C函数中可以包含不传入任何参数的函数。 A&#xff1a;函…

自学嵌入式第十九天高级编程篇 文件2

标准IO的返回值判断 feof&#xff1a;判断文件流指针是否到达结尾。 feof(src) //如果文件流指针到达末尾&#xff0c;函数返回值为真 ferror&#xff1a;判断文件流指针是否出错。搭配clearerr使用可以跳过报错的地方&#xff0c;读取后面的数据 ferror(src) //如…

短视频SDK解决方案,良好的二次开发可扩展性

短视频已成为当代社交与内容消费的重要载体&#xff0c;其影响力与日俱增&#xff0c;面对这一蓬勃发展的市场&#xff0c;如何高效、创新地打造短视频应用&#xff0c;成为众多开发者和企业的核心关切。在此背景下&#xff0c;美摄科技凭借其深厚的技术积累与前瞻性的市场洞察…

奇异值分解(SVD)

1 奇异值分解(SVD)简介 Beltrami 和 Jordan 被认为是奇异值分解&#xff08;Singular Value Decomposition&#xff0c;SVD&#xff09;的共同开创者&#xff0c;二人于19世纪70年代相继提出了相关理论。奇异值分解主要解决的问题是数据降维。在高维度的数据中&#xff0c;数据…

什么是流批一体?怎样理解流批一体?

目录 一、流式处理与批量处理概述 1.流式处理 2.批量处理 3.流批一体的定义 二、流批一体的关键特点 三、流批一体的技术实现 四、应用场景 五、实施流批一体的考虑因素 流批一体听起来很简单&#xff0c;但内涵却十分复杂。它包含了计算语义、编程模型、API、调度、执行、shuf…

Halcon玩转机器视觉专栏特殊声明

欢迎来到 PaQiuQiu 的空间 本文为【Halcon玩转机器视觉专栏特殊声明】&#xff0c;方便大家更合理的订阅! &#x1f4e2; ~特殊声明~ 鉴于很多童鞋在订阅专栏过程中&#xff0c;对于专栏中涉及到的资料&#xff08;比如中文学习手册&#xff09;和源码&#xff08;C#联合Halco…

背包九讲(动态规划)

文章目录 01背包问题题目描述解题思路&#xff1a;上代码&#xff1a;思路2&#xff1a;二维代码&#xff1a;优化代码&#xff1a; 完全背包问题题目描述&#xff1a;解题思路&#xff1a;二维代码&#xff1a;优化代码&#xff1a; 多重背包问题题目描述&#xff1a;解题思路…

有趣的rce漏洞复现分析(1)

目录 eval长度限制突破 第一种方法 第二种方法 无字母数字webshell之命令执行 php7 php5 eval长度限制突破 php eval函数参数限制在16个字符的情况下&#xff0c;如何拿到webshell呢 首先&#xff0c;我们还是先把环境搭好&#xff08;此次的所有漏洞环境我都部署在Ubu…

MySQL的索引事务和JDBC编程

目录 索引 查看索引 创建索引 删除索引 底层数据结构&#xff08;这个很重要哦&#xff0c;面试容易问&#xff09; 事务 事务的使用 事务的基本特性 并发执行事务可能产生的问题 MySQL提供的四种事务隔离级别 JDBC编程 JDBC的来源&#xff08;一定要了解&#xff…

[WUSTCTF2020]颜值成绩查询

打开题目 输入1 输出 输入1会提示学号不存在 输入1/**/or/**/11#,过滤了空格。 1/**/order/**/by/**/3# 存在 1/**/order/**/by/**/4# 不存在 绕过 爆破表名 -1/**/Union/**/Select/**/1,2,group_concat(table_name)/**/from/**/information_schema.tables/**/where/**/tabl…

8.1.数据库基础技术-数据库基本概念

数据库基本概念 数据库系统概述三级模式两级映射概念模式内模式外模式三级模式两级映像练习题 数据库设计练习题 数据库系统概述 数据&#xff1a;是数据库中存储的基本对象&#xff0c;是描述事物的符号记录。 数据的分类&#xff1a;文本、图形、图像、音频、视频。 数据库…

Unity动画模块 之 2D IK(反向动力学)

本文仅作笔记学习和分享&#xff0c;不用做任何商业用途 本文包括但不限于unity官方手册&#xff0c;unity唐老狮等教程知识&#xff0c;如有不足还请斧正​ 1.什么是IK 反向动力学 IK&#xff08;Inverse Kinematics&#xff09;是一种方法&#xff0c;可以根据某些子关节的最…

C++初阶:内存管理详解

✨✨小新课堂开课了&#xff0c;欢迎欢迎~✨✨ &#x1f388;&#x1f388;养成好习惯&#xff0c;先赞后看哦~&#x1f388;&#x1f388; 所属专栏&#xff1a;C&#xff1a;由浅入深篇 小新的主页&#xff1a;编程版小新-CSDN博客 1.C/C内存分布 我们先来看下面一段代码和相…

Haproxy简介及配置详解

一、Haproxy简介 官网&#xff1a; 企业版网站: https://www.haproxy.com 社区版网站: http://www.haproxy.org github: https://github.com/haprox Haproxy是法国人Willy Tarreaus开发的一款开源软件&#xff0c;能够提供高性能、负载均衡以及基于HTTP和TCP应用个代理&…

微信自动回复的设置

如何在微信上高效回复客户&#xff0c;提供良好的用户体验是很重要的。 但常常因为一人管理太多号&#xff0c;消息回复不过来&#xff1b;同时太多客户咨询&#xff0c;手忙脚乱&#xff1b;回复的话术让人感到不专业。 没关系&#xff0c;小编有办法。给大家分享几个小技巧…

【聚类算法】

聚类算法是一种无监督学习方法&#xff0c;用于将数据集中的数据点自动分组到不同的类别中&#xff0c;这些类别也称为“簇”或“群”。聚类的目标是让同一簇内的数据点尽可能相似&#xff0c;而不同簇之间的数据点尽可能不相似。聚类算法广泛应用于多种领域&#xff0c;如数据…