使用ldirectord实现LVS健康检测

发布于:2024-05-01 ⋅ 阅读:(39) ⋅ 点赞:(0)

注意:在LVS/DR模式环境下,具体配置见上篇LVS/DR配置

1.1 ldirectord介绍

  • LVS监控: ldirectord 主要设计用于监控Linux虚拟服务器(LVS)架构中的服务器池状态。LVS是一种负载均衡解决方案,用于将网络流量和请求分发到多个服务器上。

  • 运行环境: ldirectord 运行在IPVS节点上,即负载均衡器所在的服务器上。

  • 守护进程: 它作为一个守护进程运行,这意味着它会在后台持续运行,监控服务器池中服务器的状态。

  • 健康检查: ldirectord 通过向服务器池中的每个真实服务器发送请求来执行健康检查。这些请求可以是TCP连接、HTTP请求或其他类型的网络请求,具体取决于配置。

  • 自动管理: 如果某个服务器没有响应 ldirectord 发送的请求,ldirectord 会认为该服务器不可用,并使用 ipvsadm 工具从IPVS表中删除该服务器的条目。ipvsadm 是一个用于管理IP虚拟服务器的命令行工具。

  • 恢复处理: 当不可用的服务器再次变得可用并能够响应健康检查时,ldirectord 同样会使用 ipvsadm 将该服务器重新添加到IPVS表中。

  • 高可用性: 通过自动添加和删除服务器,ldirectord 有助于维护LVS架构的高可用性,确保流量始终被分发到健康的服务器上。

  • 配置灵活: ldirectord 允许管理员自定义健康检查的频率、类型和超时设置,以适应不同的监控需求。

  • 日志和监控: 它可能还提供日志记录功能,帮助系统管理员跟踪服务器状态的变化和 ldirectord 的操作。

  • 开源社区: 作为一个开源工具,ldirectord 可能会有一个活跃的社区,提供支持和分享最佳实践。

1.1.1 模拟一台Nginx宕机

[root@LVS-RS2 ~]# systemctl stop nginx.service 
​
[root@LVS ~]# ipvsadm -Ln   #并没有将RS2删除
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  192.168.110.10:80 rr
  -> 192.168.110.32:80            Route   1      0          10        
  -> 192.168.110.33:80            Route   1      5          5       
​
[root@Client ~]# for ((i=1;i<=10;i++)); do curl http://192.168.110.10; done
curl: (7) Failed to connect to 192.168.110.10 port 80: Connection refused
This is LVS test IP=192.168.110.32  Host=LVS-RS1
curl: (7) Failed to connect to 192.168.110.10 port 80: Connection refused
This is LVS test IP=192.168.110.32  Host=LVS-RS1
curl: (7) Failed to connect to 192.168.110.10 port 80: Connection refused
This is LVS test IP=192.168.110.32  Host=LVS-RS1
curl: (7) Failed to connect to 192.168.110.10 port 80: Connection refused
This is LVS test IP=192.168.110.32  Host=LVS-RS1
curl: (7) Failed to connect to 192.168.110.10 port 80: Connection refused
This is LVS test IP=192.168.110.32  Host=LVS-RS1

1.2 使用ldirectord实现LVS/DR的高可用

机器名称 IP地址 子网掩码 说明
LVS 192.168.110.41 255.255.255.0 负载均衡器
RS1 192.168.110.32 255.255.255.0 真实服务器1
RS2 192.168.110.33 255.255.255.0 真实服务器2
Client 192.168.110.34 255.255.255.0 客户端

1.2.1 安装部署

[root@LVS ~]# yum install ldirectord -y    #Rocky 8 仓库没有这个包

1.2.2 配置

[root@LVS ~]# cp /usr/share/doc/ldirectord/ldirectord.cf /etc/ha.d/ldirectord.cf
[root@LVS ~]# vim /etc/ha.d/ldirectord.cf   #修改配置如下
# Global Directives
checktimeout=3
checkinterval=1
#fallback=127.0.0.1:80
#fallback6=[::1]:80
autoreload=yes
#logfile="/var/log/ldirectord.log"
#logfile="local0"
#emailalert="admin@x.y.z"
#emailalertfreq=3600
#emailalertstatus=all
quiescent=no
​
# Sample for an http virtual service
virtual=192.168.110.10:80
        real=192.168.110.32:80 gate
        real=192.168.110.33:80 gate
        fallback=127.0.0.1:80 gate
        service=http
        scheduler=rr
        protocol=tcp
        checktype=negotiate
        checkport=80
        
[root@LVS ~]# systemctl start ldirectord.service

1.2.1 测试

[root@LVS ~]# watch -n 5 ipvsadm -Ln  #可以实时监控
​
[root@LVS ~]# ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  192.168.110.10:80 rr
  -> 192.168.110.32:80            Route   1      0          0         
  -> 192.168.110.33:80            Route   1      0          0   
  
[root@LVS-RS2 ~]# systemctl stop nginx.service  #模拟宕机
​
[root@LVS ~]# ipvsadm -Ln   #自动移除
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  192.168.110.10:80 rr
  -> 192.168.110.32:80            Route   1      0          0      
  
[root@Client ~]# for ((i=1;i<=10;i++)); do curl http://192.168.110.10; done  #请求全部转到RS1
This is LVS test IP=192.168.110.32  Host=LVS-RS1
This is LVS test IP=192.168.110.32  Host=LVS-RS1
This is LVS test IP=192.168.110.32  Host=LVS-RS1
This is LVS test IP=192.168.110.32  Host=LVS-RS1
This is LVS test IP=192.168.110.32  Host=LVS-RS1
This is LVS test IP=192.168.110.32  Host=LVS-RS1
This is LVS test IP=192.168.110.32  Host=LVS-RS1
This is LVS test IP=192.168.110.32  Host=LVS-RS1
This is LVS test IP=192.168.110.32  Host=LVS-RS1
This is LVS test IP=192.168.110.32  Host=LVS-RS1
​
[root@LVS-RS2 ~]# systemctl start nginx.service 
​
[root@LVS ~]# ipvsadm -Ln    #启动后回复
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  192.168.110.10:80 rr
  -> 192.168.110.32:80            Route   1      0          10        
  -> 192.168.110.33:80            Route   1      0          0        

网站公告

今日签到

点亮在社区的每一天
去签到