引言
在集群中,LVS主机为后端真实主机进行流量调度等功能。LVS是基于四层的调度,可以根据IP、MAC地址和端口对后端真实主机进行调用,但LVS不能对后端真实主机的健康状态进行检测。所以在普通LVS的基础上部署了 HAProxy 。HAProxy 能够实现对后端真实服务器的健康状态进行检测,并且支持七层模型,能够对客户端发过来的报文对数据进行分析,通过 acl 的手段达到动静分离,让服务和资源彼此映射和调用,充分利用资源,提高服务效率。如果 LVS 主机挂掉,会影响整个集群的服务,所以还需要另外的主机对 LVS 进行主从备份。本篇文章讲解通过在 LVS 主从机上部署 Keepalived 来实现即使主从机通信的 vip 漂移,也不会影响客户机访问后端真实服务器的连续性和稳定性等一系列在高可用集群中的基本配置。
目录
3.1 实现 master/slave 的 Keepalived 单主架构
3.5 实现 master/master 的 Keepalived 双主架构
3.6.1.2 virtual server (虚拟服务器)的定义格式
1. 集群介绍
vip 的通信要稳定运行,需要借助高可用集群,实现高可用集群的最常用的方法就是 Keepalived 软件。
1.1 集群类型
- LB:Load Balance 负载均衡
LVS 和 realserver 共同组成的集群叫负载均衡。流量到达 LVS 后,LVS 通过调度算法把流量调度到负载最小的主机上。
- HA:High Availability 高可用集群
通过 LVS 主备机使集群在整个生产环境中在线率达到99.99%及以上,在线时间越长,高可用性能越好。高可用集群能够解决单点故障问题(SPoF: Single Point of Failure)
数据库、Redis
- HPC:High Performance Computing 高性能集群
解决单个主机运算能力达不到想要的需求,把多个主机组合到一起。
1.2 系统可用性
- SLA
Service-Level Agreement 服务等级协议。提供服务的企业与客户之间就服务的品质、水准、性能 等方面所达成的双方共同认可的协议或契约。
- 高可用使用量的计算方法
公式:A = MTBF / (MTBF+MTTR)。A = 在线时间(在线时间+平均故障处理时间)。A的值越大说明高可用性能越好。
示例:99.95%:(60*24*30)*(1-0.9995)=21.6分钟 #一般按一个月停机时间统计
指标 :99.9%, 99.99%, 99.999%,99.9999%
1.3 系统故障
- 出现故障的原因
硬件故障:设计缺陷、wear out(损耗)、非人为不可抗拒因素。
软件故障:设计缺陷 bug。
1.4 实现高可用
- 提升系统高用性的解决方案
降低MTTR- Mean Time To Repair(平均故障时间)。
- 解决方案
建立冗余机制。HEARTBEAT心跳,检测主从机或双主机健康状态的机制,如果检测到其中一方主机挂掉,就把服务迁移到存活的另一台主机上。
- active/passive 主/备
- active/active 双主
- active --> HEARTBEAT --> passive
- active <--> HEARTBEAT <--> active
1.5 VRRP
HEARTBEAT 依赖虚拟路由冗余协议(Virtual Router Redundancy Protocol)。如下图,两台路由器主机不断在网络中发送组播,组播地址收到它俩的信息以后,会判定它俩之间哪个信息是好的,这样的做法就叫做 VRRP 协议。
虚拟路由冗余协议能够解决路由器(静态网关)单点风险问题。VRRP 协议用在下图内部四台虚拟路由器上,主要管理流量出去。运用在Keepalived 上面,主要解决客户机通过访问201.1.1.3把流量放进来访问内部网络的问题。
虚拟路由冗余协议涉及到物理层(路由器、三层交换机)和软件层(Keepalived )。
上图中,IP地址201.1.1.3是路由器组真正上网的IP。假如201.1.1.3一开始在201.1.1.1路由器主机上,201.1.1.1出现问题,201.1.1.3就会迁移到201.1.1.2主机上。通过HEARTBEAT心跳,即VRRP协议判定两台路由器主机之间谁出了故障。
1.5.1 VRRP 相关术语
- Virtual Router
虚拟路由器。
- VRID(0-255)
虚拟路由器标识,唯一标识虚拟路由器。
- VIP
Virtual IP ,虚拟IP
- VMAC
Virutal MAC (00-00-5e-00-01-VRID),虚拟IP的MAC地址。
- 物理路由器
- master:主设备
- backup:备用设备
- priority:优先级
根据优先级来判定主机的主备关系,优先级较大的为主,较小的为从。VIP的漂移取决于哪台路由器主机优先级高。
1.5.2 VRRP 相关技术
- 通告
路由器默认通过组播向组播地址发送自己的信息,告知自己处于存活状态。通过心跳实现VIP的迁移,保证存活时间。
- 工作方式
- 抢占式:如上图,201.1.1.1和201.1.1.2都部署了 Keepalived,一开始默认 VIP 在201.1.1.1主机上,如果201.1.1.1挂掉,VIP则会因为 Keeplived 的部署迁移到201.1.1.2主机上。如果201.1.1.1主机设置的抢占模式,主机修复好后会把 VIP 抢占回来。前提是201.1.1.1的优先级高于201.1.1.2。
- 非抢占式:如上图,201.1.1.1和201.1.1.2都部署了 Keepalived,一开始默认 VIP 在201.1.1.1主机上,如果201.1.1.1挂掉,VIP则会因为 Keeplived 的部署迁移到201.1.1.2主机上。如果201.1.1.1主机设置的非抢占模式,主机修复好后不会把 VIP 抢占回来,VIP 仍然在201.1.1.2主机上。前提是201.1.1.1的优先级高于201.1.1.2。
- 延迟抢占:如上图,201.1.1.1和201.1.1.2都部署了 Keepalived,一开始默认 VIP 在201.1.1.1主机上,如果201.1.1.1挂掉,VIP则会因为 Keeplived 的部署迁移到201.1.1.2主机上。如果201.1.1.1主机设置的延迟抢占模式,主机修复好后会在达到延迟抢占时间后把 VIP 抢占回来。前提是201.1.1.1的优先级高于201.1.1.2。
- 安全认证
路由器主机通过通告来证明自己的存活性,通告的时候要把自己的信息发送到组播地址,发送过程中可以设置安全认证来选择是否对自己的信息进行加密。
认证方式:无认证;简单字符认证:预共享密钥;MD5。
- 工作模式
主/备:单虚拟路由器。存在资源浪费问题。
主/主:主/备(虚拟路由器1),备/主(虚拟路由器2)。互为主备。
2. Keepalived 部署
2.1 Keepalived 简介
Keepalived是vrrp 协议的软件实现,原生设计目的为了高可用 ipvs服务。
官网地址: http://keepalived.org/
功能:
- 基于vrrp协议完成地址流动,即 VIP 的漂移。
- 为vip地址所在的节点生成ipvs规则(在配置文件中预先定义),即根据执行情况自动生成 LVS 策略。
- 为ipvs集群的各RS做健康状态检测,跟 HAProxy 的检测方法不一样。
- 基于脚本调用接口完成脚本中定义的功能,进而影响集群事务,以此支持nginx、haproxy等服务。
2.2 Keepalived 架构
- 官方资料文档
https://keepalived.org/doc/
http://keepalived.org/documentation.html
- 架构图
- 用户空间核心组件
- VRRP stack:做心跳,VIP消息通告。
- checkers:监测real server 进行健康检测,可以检测 TCP、HTTP、SSL、MISC等协议。
- system call:实现 vrrp 协议状态转换时调用脚本的功能。
- SMTP:邮件组件,需要邮件服务器。
- IPVS wrapper:生成IPVS规则,为 LVS 准备的一个套件。比如 checkers 检测到有一台 realserver 挂掉了,这时使用 IPVS wrapper 对当前主机的 IPVS 规则进行操作,把挂掉的服务器主机从策略中清除。
- Netlink Reflector:网络接口。
- WatchDog:监控进程,对服务器的健康状况进行检测,检测出问题立即进行调整。
- 控制组件
提供 Keepalived.conf 的解析器,让 Keepalived 正常运行,完成对 Keepalived 配置的读取等操作。
- IO复用器
针对网络目的而优化的自己的线程抽象,负责 IO 调度。
- 内存管理组件
对于 Keeplived 使用过程当中对内存的使用和调度。为某些通用的内存管理功能(例如分配,重新分配,发布等)提供访问权限。
2.3 Keepalived 实验环境配置
整个集群服务基于四台 RedHat7 的虚拟机来实现。
注意事项:
- 各节点时间必须同步:ntp, chrony
- 关闭防火墙及SELinux
- 各节点之间可通过主机名互相通信:非必须
- 建议使用/etc/hosts文件实现:非必须
- 各节点之间的root用户可以基于密钥认证的ssh服务完成互相通信:非必须
- RHEL7中可能会遇到的bug
systemctl restart keepalived #新配置可能无法生效
systemctl stop keepalived;systemctl start keepalived #无法停止进程,需要 kill 停 止
- 创建母盘虚拟机
创建好一台 RedHat 虚拟机之后,开启虚拟机,查看 DHCP 默认分配的IP地址。
用默认分配的 IP 地址连接 xshell 。
[C:\~]$ ssh root@172.25.254.131
Connecting to 172.25.254.131:22...
Connection established.
To escape to local shell, press 'Ctrl+Alt+]'.WARNING! The remote SSH server rejected X11 forwarding request.
Last login: Mon Aug 12 10:56:12 2024
配置本地仓库。
[root@localhost ~]# vi /etc/yum.repos.d/rpm.repo
[BaseOS]
name=BaseOS
baseurl=file:///mnt
gpgcheck=0
挂载。
[root@localhost ~]# mount /dev/sr0 /mnt/
mount: /dev/sr0 is write-protected, mounting read-only
下载基本软件测试仓库是否配置成功。
[root@localhost ~]# yum install bash-com* net-tools lrz* vim -y
重置网卡信息。
[root@localhost ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.25.254.131 netmask 255.255.255.0 broadcast 172.25.254.255
inet6 fe80::9c18:abfe:4a44:947e prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:a0:d4:52 txqueuelen 1000 (Ethernet)
RX packets 1435 bytes 107634 (105.1 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 1410 bytes 1180897 (1.1 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0lo: 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 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0[root@localhost ~]# grubby --update-kernel ALL --args net.ifnames=0
[root@localhost ~]# reboot
虚拟机里登录,配置网卡信息。
建立快照后创建四台链接交换机配置实验环境。
- 链接虚拟机——Keepalived1
改主机名
[C:\~]$ ssh root@172.25.254.132
Connecting to 172.25.254.132:22...
Connection established.
To escape to local shell, press 'Ctrl+Alt+]'.WARNING! The remote SSH server rejected X11 forwarding request.
Last login: Mon Aug 12 12:05:51 2024
[root@localhost ~]# hostnamectl set-hostname kp1.timinglee.org
[root@localhost ~]# exit
logoutConnection closed.
Disconnected from remote host(172.25.254.132:22) at 12:14:34.
Type `help' to learn how to use Xshell prompt.
[C:\~]$ ssh root@172.25.254.132
Connecting to 172.25.254.132:22...
Connection established.
To escape to local shell, press 'Ctrl+Alt+]'.WARNING! The remote SSH server rejected X11 forwarding request.
Last login: Mon Aug 12 12:08:37 2024 from 172.25.254.1[root@kp1 ~]# hostnamectl
Static hostname: kp1.timinglee.org
Icon name: computer-vm
Chassis: vm
Machine ID: 2dcb62aa5c5644e982163a59d116ee42
Boot ID: ad3a1103d1424ca8afd594759f09e112
Virtualization: vmware
Operating System: Red Hat Enterprise Linux Server 7.9 (Maipo)
CPE OS Name: cpe:/o:redhat:enterprise_linux:7.9:GA:server
Kernel: Linux 3.10.0-1160.el7.x86_64
Architecture: x86-64
配置静态IP。
[root@kp1 ~]# nmcli connection modify eth0 ipv4.addresses 172.25.254.10/24 ipv4.method manual connection.autoconnect yes
[root@kp1 ~]# nmcli connection up eth0
配置 DNS 和网关。
[root@kp1 ~]# nmcli connection modify eth0 ipv4.dns 172.25.254.2 ipv4.gateway 172.25.254.2 ipv4.method manual connection.autoconnect yes
[root@kp1 ~]# nmcli connection up eth0
[C:\~]$ ssh root@172.25.254.10
Connecting to 172.25.254.10:22...
Connection established.
To escape to local shell, press 'Ctrl+Alt+]'.WARNING! The remote SSH server rejected X11 forwarding request.
Last login: Mon Aug 12 12:14:40 2024 from 172.25.254.1
[root@kp1 ~]#
[root@kp1 ~]# mount /dev/sr0 /mnt/
mount: /dev/sr0 is write-protected, mounting read-only
[root@kp1 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 898M 0 898M 0% /dev
tmpfs 910M 0 910M 0% /dev/shm
tmpfs 910M 9.6M 901M 2% /run
tmpfs 910M 0 910M 0% /sys/fs/cgroup
/dev/mapper/rhel-root 50G 1.4G 49G 3% /
/dev/sda1 1014M 150M 865M 15% /boot
/dev/mapper/rhel-home 47G 33M 47G 1% /home
tmpfs 182M 0 182M 0% /run/user/0
/dev/sr0 4.3G 4.3G 0 100% /mnt
[root@kp1 ~]# systemctl stop firewalld.service
[root@kp1 ~]# setenforce 0
- 链接虚拟机——Keepalived2
Xshell 7 (Build 0164)
Copyright (c) 2020 NetSarang Computer, Inc. All rights reserved.Type `help' to learn how to use Xshell prompt.
[C:\~]$ ssh root@172.25.254.133
Connecting to 172.25.254.133:22...
Connection established.
To escape to local shell, press 'Ctrl+Alt+]'.WARNING! The remote SSH server rejected X11 forwarding request.
Last login: Mon Aug 12 12:16:46 2024
[root@localhost ~]# hostnamectl set-hostname kp2.timinglee.org
[root@localhost ~]# exit
logoutConnection closed.
Disconnected from remote host(172.25.254.133:22) at 12:19:07.
Type `help' to learn how to use Xshell prompt.
[C:\~]$ ssh root@172.25.254.133
Connecting to 172.25.254.133:22...
Connection established.
To escape to local shell, press 'Ctrl+Alt+]'.WARNING! The remote SSH server rejected X11 forwarding request.
Last login: Mon Aug 12 12:18:32 2024 from 172.25.254.1
[root@kp2 ~]# hostnamectl
Static hostname: kp2.timinglee.org
Icon name: computer-vm
Chassis: vm
Machine ID: 2dcb62aa5c5644e982163a59d116ee42
Boot ID: 99b04509b09d41788e8b07ab91267e92
Virtualization: vmware
Operating System: Red Hat Enterprise Linux Server 7.9 (Maipo)
CPE OS Name: cpe:/o:redhat:enterprise_linux:7.9:GA:server
Kernel: Linux 3.10.0-1160.el7.x86_64
Architecture: x86-64
[root@kp2 ~]#
[root@kp2 ~]# nmcli connection modify eth0 ipv4.addresses 172.25.254.20/24 ipv4.method manual connection.autoconnect yes
[root@kp2 ~]# nmcli connection up eth0
[root@kp2 ~]# nmcli connection modify eth0 ipv4.dns 172.25.254.2 ipv4.gateway 172.25.254.2 ipv4.method manual connection.autoconnect yes
[root@kp2 ~]# nmcli connection up eth0
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/3)
[C:\~]$ ssh root@172.25.254.20
Connecting to 172.25.254.20:22...
Connection established.
To escape to local shell, press 'Ctrl+Alt+]'.WARNING! The remote SSH server rejected X11 forwarding request.
Last login: Mon Aug 12 12:19:12 2024 from 172.25.254.1
[root@kp2 ~]#
[root@kp2 ~]# mount /dev/sr0 /mnt/
mount: /dev/sr0 is write-protected, mounting read-only
[root@kp2 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 898M 0 898M 0% /dev
tmpfs 910M 0 910M 0% /dev/shm
tmpfs 910M 9.6M 901M 2% /run
tmpfs 910M 0 910M 0% /sys/fs/cgroup
/dev/mapper/rhel-root 50G 1.4G 49G 3% /
/dev/mapper/rhel-home 47G 33M 47G 1% /home
/dev/sda1 1014M 150M 865M 15% /boot
tmpfs 182M 0 182M 0% /run/user/0
/dev/sr0 4.3G 4.3G 0 100% /mnt
[root@kp2 ~]# systemctl stop firewalld.service
[root@kp2 ~]# setenforce 0
- 链接虚拟机——realserver1
Xshell 7 (Build 0164)
Copyright (c) 2020 NetSarang Computer, Inc. All rights reserved.Type `help' to learn how to use Xshell prompt.
[C:\~]$ ssh root@172.25.254.134
Connecting to 172.25.254.134:22...
Connection established.
To escape to local shell, press 'Ctrl+Alt+]'.WARNING! The remote SSH server rejected X11 forwarding request.
Last login: Mon Aug 12 12:19:52 2024
[root@localhost ~]# hostnamectl set-hostname realserver1.timinglee.org
[root@localhost ~]# exit
logoutConnection closed.
Disconnected from remote host(172.25.254.134:22) at 12:21:31.
Type `help' to learn how to use Xshell prompt.
[C:\~]$ ssh root@172.25.254.134
Connecting to 172.25.254.134:22...
Connection established.
To escape to local shell, press 'Ctrl+Alt+]'.WARNING! The remote SSH server rejected X11 forwarding request.
Last login: Mon Aug 12 12:20:59 2024 from 172.25.254.1
[root@realserver1 ~]# hostnamectl
Static hostname: realserver1.timinglee.org
Icon name: computer-vm
Chassis: vm
Machine ID: 2dcb62aa5c5644e982163a59d116ee42
Boot ID: 14fec4bba5f34b7bbe6eb87ba915622a
Virtualization: vmware
Operating System: Red Hat Enterprise Linux Server 7.9 (Maipo)
CPE OS Name: cpe:/o:redhat:enterprise_linux:7.9:GA:server
Kernel: Linux 3.10.0-1160.el7.x86_64
Architecture: x86-64
[root@realserver1 ~]#
[root@realserver1 ~]# nmcli connection modify eth0 ipv4.addresses 172.25.254.110/24 ipv4.method manual
[root@realserver1 ~]# nmcli connection up eth0
[C:\~]$ ssh root@172.25.254.110
Connecting to 172.25.254.110:22...
Connection established.
To escape to local shell, press 'Ctrl+Alt+]'.WARNING! The remote SSH server rejected X11 forwarding request.
Last login: Mon Aug 12 12:21:35 2024 from 172.25.254.1
[root@realserver1 ~]#
[root@realserver1 ~]# mount /dev/sr0 /mnt/
mount: /dev/sr0 is write-protected, mounting read-only
[root@realserver1 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 898M 0 898M 0% /dev
tmpfs 910M 0 910M 0% /dev/shm
tmpfs 910M 9.6M 901M 2% /run
tmpfs 910M 0 910M 0% /sys/fs/cgroup
/dev/mapper/rhel-root 50G 1.4G 49G 3% /
/dev/mapper/rhel-home 47G 33M 47G 1% /home
/dev/sda1 1014M 150M 865M 15% /boot
tmpfs 182M 0 182M 0% /run/user/0
/dev/sr0 4.3G 4.3G 0 100% /mnt
[root@realserver1 ~]# systemctl stop firewalld.service
[root@realserver1 ~]# setenforce 0
- 链接虚拟机——realserver2
Xshell 7 (Build 0164)
Copyright (c) 2020 NetSarang Computer, Inc. All rights reserved.Type `help' to learn how to use Xshell prompt.
[C:\~]$ ssh root@172.25.254.135
Connecting to 172.25.254.135:22...
Connection established.
To escape to local shell, press 'Ctrl+Alt+]'.WARNING! The remote SSH server rejected X11 forwarding request.
Last login: Mon Aug 12 12:22:06 2024
[root@localhost ~]# hostnamectl set-hostname realserver2.timinglee.org
[root@localhost ~]# exit
logoutConnection closed.
Disconnected from remote host(172.25.254.135:22) at 12:23:51.
Type `help' to learn how to use Xshell prompt.
[C:\~]$ ssh root@172.25.254.135
Connecting to 172.25.254.135:22...
Connection established.
To escape to local shell, press 'Ctrl+Alt+]'.WARNING! The remote SSH server rejected X11 forwarding request.
Last login: Mon Aug 12 12:23:16 2024 from 172.25.254.1
[root@realserver2 ~]# hostnamectl
Static hostname: realserver2.timinglee.org
Icon name: computer-vm
Chassis: vm
Machine ID: 2dcb62aa5c5644e982163a59d116ee42
Boot ID: 2ad23fc0685947269f7d8b6dba4f30fd
Virtualization: vmware
Operating System: Red Hat Enterprise Linux Server 7.9 (Maipo)
CPE OS Name: cpe:/o:redhat:enterprise_linux:7.9:GA:server
Kernel: Linux 3.10.0-1160.el7.x86_64
Architecture: x86-64
[root@realserver2 ~]#
[root@realserver2 ~]# nmcli connection modify eth0 ipv4.addresses 172.25.254.120/24 ipv4.method manual
[root@realserver2 ~]# nmcli connection up eth0
[C:\~]$ ssh root@172.25.254.120
Connecting to 172.25.254.120:22...
Connection established.
To escape to local shell, press 'Ctrl+Alt+]'.WARNING! The remote SSH server rejected X11 forwarding request.
Last login: Mon Aug 12 12:23:55 2024 from 172.25.254.1
[root@realserver2 ~]#
[root@realserver2 ~]# mount /dev/sr0 /mnt/
mount: /dev/sr0 is write-protected, mounting read-only
[root@realserver2 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 898M 0 898M 0% /dev
tmpfs 910M 0 910M 0% /dev/shm
tmpfs 910M 9.6M 901M 2% /run
tmpfs 910M 0 910M 0% /sys/fs/cgroup
/dev/mapper/rhel-root 50G 1.4G 49G 3% /
/dev/mapper/rhel-home 47G 33M 47G 1% /home
/dev/sda1 1014M 150M 865M 15% /boot
tmpfs 182M 0 182M 0% /run/user/0
/dev/sr0 4.3G 4.3G 0 100% /mnt
[root@realserver2 ~]# systemctl stop firewalld.service
[root@realserver2 ~]# setenforce 0
-
环境测试
[root@realserver1 ~]# yum install httpd -y
[root@realserver1 ~]# systemctl start httpd.service
[root@realserver1 ~]# echo 172.25.254.110 > /var/www/html/index.html
[root@realserver1 ~]# systemctl enable --now httpd.service
Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.
[root@realserver2 ~]# yum install httpd -y
[root@realserver2 ~]# systemctl start httpd.service
[root@realserver2 ~]# echo 172.25.254.120 > /var/www/html/index.html
[root@realserver2 ~]# systemctl enable --now httpd.service
Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.
[root@kp1 ~]# curl 172.25.254.110
172.25.254.110
[root@kp1 ~]# curl 172.25.254.120
172.25.254.120
2.4 Keepalived 相关文件
- 软件包名:keepalived
- 主程序文件:/usr/sbin/keepalived
- 主配置文件:/etc/keepalived/keepalived.conf
- 配置文件示例:/usr/share/doc/keepalived/
- Unit File:/lib/systemd/system/keepalived.service
- Unit File的环境配置文件:/etc/sysconfig/keepalived
2.5 Keepalived 安装
dnf install keepalived -y
systemctl start keepalived
ps axf | grep keepalived
2.6 KeepAlived 配置说明
2.6.1 配置文件组成部分
- 配置文件
/etc/keepalived/keepalived.conf
- 配置文件组成
Keepalived 的配置分为以下三步:
- GLOBAL CONFIGURATION 全局配置。Global definitions:定义邮件配置,route_id,vrrp配置,多播地址等。
- VRRP CONFIGURATION 虚拟路由配置。VRRP instance(s):定义每个vrrp虚拟路由器
- LVS CONFIGURATION 最终访问内容的配置。Virtual server group(s) ;Virtual server(s):LVS集群的VS和RS
2.6.2 配置语法说明
查看帮助文档。
man keepalived.conf
2.6.2.1 全局配置
! 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地址范围:
}
2.6.2.2 配置虚拟路由器
- 配置讲解:
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
}
}
- 实验示例:
安装 Keepalived。
[root@kp1