Interpretation of Linux system Keepalived health check mechanism

PHPz
Release: 2024-02-28 09:52:10
forward
294 people have browsed it

解读Linux 系统Keepalived健康检查机制

在进行负载均衡时,一般都会部署一个健康检查工具,确保后端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
Copy after login

配置文件:

! 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 # 检测端口

} 

} 

}
Copy after login

健康检查类型

TCP_CHECK
TCP_CHECK { # 通过TcpCheck判断RealServer的健康状态

connect_timeout 10# 连接超时时间
nb_get_retry 3# 重连次数
delay_before_retry 3# 重连时间间隔
connect_port 6500 # 检测端口
}
Copy after login

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# 检测端口

}
Copy after login

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

}
Copy after login

脚本示例:

#!/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

Copy after login

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!

Related labels:
source:mryunwei.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!