当前位置:网站首页>一次 Keepalived 高可用的事故,让我重学了一遍它
一次 Keepalived 高可用的事故,让我重学了一遍它
2022-07-04 12:46:00 【InfoQ】
- Keepalived 如何提供数据流量转发。
- Keepalived 选举的原理。
- Keepalived 的负载均衡算法。
- Keepalived 的路由规则。
- Keepalived 如何监控服务的。
- Keepalived 如何进行故障切换。
- Keepalived 的架构剖析。
- Keepalived 配置详解
- Keepalived 实战部署
一、Keepalived 和 LVS 概述
1.1 Keepalived 概述
1.2 LVS 概述
1.3 LVS 基本原理

- 基于 DNS 域名轮流解析方案。
- 基于客户端调度访问方案。
- 基于应用层系统的调度方案。
- 基于 IP 地址的调度方案。
二、Keepalived 流量转发原理

三、Keepalived 如何进行选主的
3.1 VRRP 协议
- 虚拟路由器和虚拟 IP。
- Master 广播 ARP 报文。
- Backup 选举新的 Master。


3.2 vrrp_instance 配置
- state:可选值为 MASTER、BACKUP。
- priority:节点的优先级,可选值为 [1-255]。
- nopreempt:不抢占模式,如果配置,则当优先级高时,会将自己设置为 Master。
vrrp_instance VI_1 {
# 节点为 BACKUP
state BACKUP
# 优先级为 100
priority 100
# 不抢占模式
nopreempt
}
3.2 vrrp_script 配置
vrrp_script restart_mysql {
# 监测和重启 mysql 容器,如果 MySQL 服务正常或 MySQL 失败
script "/usr/local/keepalived/restart_mysql.sh"
interval 5
weight -20
}
当 weight 为正数

- 如果 MASTER 节点的 vrrp_script 脚本检测失败时,如果 MASTER 节点的 priority 值小于 BACKUP 节点 weight + priority,则发生主备切换。
- 如果 MASTER 节点的 vrrp_script 脚本检测成功时,如果 MASTER 节点的 priority 值大于 BACKUP 节点 weight + priority,则不发生主备切换。
当 weight 为负数

- 如果 MASTER 节点的 vrrp_script 脚本检测失败时,如果 MASTER 节点的 priority - |weight| 值小于 BACKUP 节点 priority 值,则发生主备切换。
- 如果 MASTER 节点的 vrrp_script 脚本检测成功时,如果 MASTER 节点的 priority 值大于 BACKUP 节点 priority 值,则不发生主备切换。


四、Keepalived 的负载均衡机制
4.1 转发机制

- Keepalived 是运行在用户空间的 LVS 路由(LVS Router)进程,作为 MASTER 角色 Keepalived 称为 Active Router,BACKUP 角色的 Keepalived 称为 SLAVE Router。只有 Active Router 是工作的,其他 Router 是 Stand By (待机状态)。
- Active Router 和 Backup Router 之间是通过 VRRP 协议进行主备切换的。
- Active Router 会启动内核中 LVS 服务以创建虚拟服务器,虚拟服务器有一个虚拟 IP(VIP),比如下图中的 VIP 为 192.168.56.88。
- Active Router 还会设置 IPVS TABLES(服务器列表),记录了后端服务器的地址及服务运行状态。负载均衡就从服务器列表选择一个可用的服务进行转发。
- 这些后端服务是配置在 Keepalived 的 virtual_server 配置项里面的,如下所示,配置了三个 real_server,分别对应了三台后端服务器。
virtual_server 192.168.56.88 80 {
delay_loop 6
lb_algo rr
lb kind NAT
protocol tcp
# 服务器 1
real_server 192.168.56.11 80 {
TCP_CHECK {
connect timeout 10
}
# 服务器 2
real_server 192.168.56.12 80 {
TCP_CHECK {
connect timeout 10
}
# 服务器 3
real_server 192.168.56.13 80 {
TCP_CHECK {
connect timeout 10
}
4.2 负载调度算法
- rr,就是 Round-Robin,轮询算法, 每个服务器平等的,依次被调度。
- wrr,就是 Weighted Round-Robin,加权轮询调度算法,加权值较大的,会被转发更多的请求。比如有的服务器硬件能力较弱,则可以将加权值配置得低一点。
- lc,就是 Least-Connection,最少连接算法。请求被转发到活动连接较少的服务器上。连接数是通过 IPVS Table 来动态跟踪的。
- wlc,加权最少连接。根据权重 + 连接数 分配请求。
- sh,目标地址哈希算法,通过在静态 Hash 表中查询目的 IP 地址来确定请求要转发的服务器,这类算法主要用于缓存代理服务器中。
- dh,源地址哈希算法,通过在静态 Hash 表中查询源 IP 地址来确定请求要转发的服务器,这类算法主要用于防火墙的 LVS Router 中。
五、总结
边栏推荐
- Using nsproxy to forward messages
- 诸神黄昏时代的对比学习
- Practice: fabric user certificate revocation operation process
- Comprehensive evaluation of modular note taking software: craft, notation, flowus
- 数据库公共字段自动填充
- Flet教程之 03 FilledButton基础入门(教程含源码)(教程含源码)
- Scrapy 框架学习
- Introduction to XML III
- MySQL three-level distribution agent relationship storage
- WPF double slider control and forced capture of mouse event focus
猜你喜欢

实时云交互如何助力教育行业发展

Dgraph: large scale dynamic graph dataset

基于链表管理的单片机轮询程序框架

CANN算子:利用迭代器高效实现Tensor数据切割分块处理
![[AI system frontier dynamics, issue 40] Hinton: my deep learning career and research mind method; Google refutes rumors and gives up tensorflow; The apotheosis framework is officially open source](/img/2c/b1d6277c1b23a6a77f90d5b2874759.png)
[AI system frontier dynamics, issue 40] Hinton: my deep learning career and research mind method; Google refutes rumors and gives up tensorflow; The apotheosis framework is officially open source

Zhongang Mining: in order to ensure sufficient supply of fluorite, it is imperative to open source and save flow

CVPR 2022 | transfusion: Lidar camera fusion for 3D target detection with transformer

Go 语言入门很简单:Go 实现凯撒密码

Talk about the design and implementation logic of payment process

7 月数据库排行榜:MongoDB 和 Oracle 分数下降最多
随机推荐
室外LED屏幕防水吗?
Getting started with microservices
mysql三级分销代理关系存储
JVM series - stack and heap, method area day1-2
C语言中学生成绩管理系统
C#/VB. Net to add text / image watermarks to PDF documents
7 月数据库排行榜:MongoDB 和 Oracle 分数下降最多
HAProxy高可用解决方案
How real-time cloud interaction helps the development of education industry
The only core indicator of high-quality software architecture
从0到1建设智能灰度数据体系:以vivo游戏中心为例
"Pre training weekly" issue 52: shielding visual pre training and goal-oriented dialogue
Using nsproxy to forward messages
iptables基础及Samba配置举例
C#基础补充
Rsyslog configuration and use tutorial
.Net之延迟队列
Fs4056 800mA charging IC domestic fast charging power IC
2022年中国移动阅读市场年度综合分析
"Tips" to slim down Seurat objects