


Interpretation of Linux system Keepalived health check mechanism
在进行负载均衡时,一般都会部署一个健康检查工具,确保后端real server是正常的,可以提供服务的,避免出现后端real server 已经宕机或服务不可用时,负载均衡器扔将请求分发到real server,影响整体业务访问。健康检查的方式有很多,可以自行部署脚本,当然当前用的比较多的就是keepalived服务了。keepalived的健康检查方式有三种,tcp_check、http_check、misc_check。
keepalived配置简述
keepalived主要有三个模块,分别是core、check和vrrp。core模块为keepalived的核心,负责主进程的启动、维护以及全局配置文件的加载和解析。check负责健康检查,包括常见的各种检查方式。vrrp模块是来实现VRRP协议的
keepalived只有一个配置文件keepalived.conf,里面主要包括以下几个配置区域,分别是global_defs、static_ipaddress、static_routes、vrrp_script、vrrp_instance和virtual_server。
yum install keepalived -y
配置文件:
! Configuration File for keepalived # vi /etc/keepalived/keepalived.conf # 全局配置 主要是配置故障发生时的通知对象以及机器标识 global_defs { notification_email { r_xl@xl.com# 设置报警邮件接收地址,需要开启 sendmail 服务 } notification_email_from s_xl@xl.com# 设置邮件的发送地址 smtp_server 192.168.2.241# 设置通知的 SMTP Server 地址 smtp_connect_timeout 30# 设置通知的 SMTP Server 的超时时间 router_id LVS_DEVEL_1# 路由ID,标识本节点的字符串,邮件通知时会用到 } # 自定义VRRP实例健康检查脚本 keepalived只能做到对自身问题和网络故障的监控,Script可以增加其他的监控来判定是否需要切换主备 vrrp_script chk_sshd { script "killall -0 sshd"# 示例为检查sshd服务是否运行中 interval 2 # 检查间隔时间 weight -4# 检查失败降低的权重 } # VRRP实例 定义对外提供服务的VIP区域及其相关属性 vrrp_instance VI_1 { state MASTER # 状态只有 MASTER 和 BACKUP 两种,并且要大写,MASTER 为工作状态,BACKUP 是备用状态 interface eth0 # 节点固有IP(非VIP)的网卡,用来发VRRP包 virtual_router_id 51 # 虚拟路由标识,同一个 vrrp_instance 的 MASTER 和 BACKUP 的 vitrual_router_id 需要一致 priority 100 # 优先级,同一个 vrrp_instance 的 MASTER 优先级必须比 BACKUP 高 advert_int 1 # MASTER 与 BACKUP 负载均衡器之间同步检查的时间间隔,单位为秒 authentication { # 设置认证 auth_type PASS # 认证方式,支持 PASS 和 HA auth_pass 1111 # 证密码为明文,同一 vrrp 实例 MASTER 与 BACKUP 使用相同的密码才能正常通信 } virtual_ipaddress {# 虚拟IP地址(VIP),可以有多个地址,每个地址占一行 192.168.12.200 } track_script { # 自定义健康检查脚本 chk_sshd # 配置上面自定义的vrrp脚本调用名 } } # 设置虚拟服务器 virtual_server 192.168.12.200 6500 { # 指定虚拟IP地址和服务端口 delay_loop 6 # 服务健康检查周期,6秒 lb_algo rr # 负载均衡调度算法,一般用wrr、rr、wlc lb_kind DR # 负载均衡转发规则。一般包括DR,NAT,TUN 3种 persistence_timeout 5# 会话保持时间。把用户请求请求间隔在未超过保持时间时,一直分发到某个服务节点 protocol TCP # 转发协议 有TCP和UDP两种 # 配置真实服务器 real_server 192.168.2.222 6500 {#指定IP和端口 weight 1 # 权重,数值越大,权重越高 # 健康检查方式 常见有 TCP_CHECK, HTTP_GET, SSL_GET, MISC_CHECK(自定义脚本) TCP_CHECK { # 通过TcpCheck方式判断RealServer的健康状态 connect_timeout 10# 连接超时时间 nb_get_retry 3# 重连次数 delay_before_retry 3# 重连时间间隔 connect_port 6500 # 检测端口 } } # 配置真实服务器 real_server 192.168.2.222 6500 {#指定IP和端口 weight 1# 权重,数值越大,权重越高 # 健康检查方式 常见有 TCP_CHECK, HTTP_GET, SSL_GET, MISC_CHECK(自定义脚本) TCP_CHECK { # 通过TcpCheck判断RealServer的健康状态 connect_timeout 10# 连接超时时间 nb_get_retry 3# 重连次数 delay_before_retry 3# 重连时间间隔 connect_port 6500 # 检测端口 } } }
健康检查类型
TCP_CHECK TCP_CHECK { # 通过TcpCheck判断RealServer的健康状态 connect_timeout 10# 连接超时时间 nb_get_retry 3# 重连次数 delay_before_retry 3# 重连时间间隔 connect_port 6500 # 检测端口 }
2.HTTP_GET
HTTP_GET { url { path check/200.jsp# 检查的uri地址 digest 1362a91278f0806aa1d33e1e26d67763 # 用keepalived自带的genhash生成,/usr/bin/genhash -s rsIP -p port -u uri } connect_timeout 3 # 链接超时时间 nb_get_retry 3 # 重连次数 delay_before_retry 3# 重连时间间隔 connect_port 6500# 检测端口 }
3.MISC_CHECK
keepalived.conf配置:
MISC_CHECK { misc_path "/etc/keepalived/misc_check.sh http://192.168.2.222:6500/check/200.jsp"# 外部程序或者脚本的路径和参数 misc_timeout 10 # 脚本执行的超时时间 misc_dynamic#动态权重标志。脚本返回0则检测成功权重不变,返回1表示失败权重设置为0 }
脚本示例:
#!/bin/bash # ./misc_check.sh http://192.168.2.222:6500/check/200.jsp if [ $# -ne 1 ]; then echo "Warning: command param error." exit 1 else CHECK_URL=$1 CMD=`/usr/bin/curl -I ${CHECK_URL} 2>/dev/null | grep "200 OK" | wc -l` if [ ${CMD} -eq 1 ]; then echo "Succ: Check proxy ${CHECK_URL} is succeed." exit 0 else echo "Fail: check proxy ${CHECK_URL} is failed." exit 1 fi fi
The above is the detailed content of Interpretation of Linux system Keepalived health check mechanism. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Article discusses editing Windows Registry, precautions, backup methods, and potential issues from incorrect edits. Main issue: risks of system instability and data loss from improper changes.

What does the drive health warning in Windows Settings mean and what should you do when you receive the disk warning? Read this php.cn tutorial to get step-by-step instructions to cope with this situation.

Article discusses managing Windows services for system health, including starting, stopping, restarting services, and best practices for stability.

The article explains how to use the Group Policy Editor (gpedit.msc) in Windows for managing system settings, highlighting common configurations and troubleshooting methods. It notes that gpedit.msc is unavailable in Windows Home editions, suggesting

You may see the “A connection to the Windows Metadata and Internet Services (WMIS) could not be established.” error on Event Viewer. This post from php.cn introduces how to remove the Windows Metadata and Internet Services problem.

Article discusses changing default apps for file types on Windows, including reverting and bulk changes. Main issue: no built-in bulk change option.

Are you questioned about an issue that MSConfig keeps reverting to selective startup on your Windows? How to switch to normal startup if you require it? Try the methods explained in this php.cn post to find one that works for you.

KB5035942 update issues - crashing system commonly happens to users. Inflicted people hope to find a way out of the kind of trouble, such as crashing system, installation, or sound issues. Targeting these situations, this post published by php.cn wil
