基于 openEuler 构建 LVS-NAT 集群和ldirectord监控RS

1、环境准备

准备好下面四台台服务器:

|-------------|----------------------------------------|--------------|
| 主机名 | IP | 角色 |
| open-Euler1 | 192.168.121.150(公网)、192.168.16.150(内网) | LVS |
| openEuler-2 | 192.168.121.151 | Real Server1 |
| openEuler-3 | 192.168.121.152 | Real Server2 |
| Rocky8 | 192.168.121.160 | Client |

2、Web服务器配置

在两台RS上安装并配置nginx服务:

bash 复制代码
open-Euler2
[root@open-Euler2 ~]# echo "This is test page `hostname -I`" > /var/www/html/index.html
[root@open-Euler2 ~]# systemctl restart httpd
[root@open-Euler2 ~]# curl 192.168.16.151
This is test page 192.168.16.151 


open-Euler3
[root@open-Euler3 ~]# echo "This is test page `hostname -I`" > /var/www/html/index.html
[root@open-Euler3 ~]# systemctl restart httpd
[root@open-Euler3 ~]# curl 192.168.16.152
This is test page 192.168.16.152 

3、LVS开启路由转发

在lvs上:

bash 复制代码
临时开启:
1、echo1>/proc/sys/net/ipv4/ip_forward
2、sysctl -w net.ipv4.ip_forward=1
永久开启:
修改文件 /etc/sysctl.conf
net.ipv4.ip forward=1
使生效:sysctl -p

4、LVS配置

在lvs上:

bash 复制代码
[root@opem-Euler1 yum.repos.d]# yum install ipvsadm #下载ipvsadm管理工具
[root@localhost yum.repos.d]# ipvsadm -At 192.168.121.150:80 -s rr
[root@localhost yum.repos.d]# ipvsadm -at 192.168.121.150:80 -r 192.168.16.151:80 -m
[root@localhost yum.repos.d]# ipvsadm -at 192.168.121.150:80 -r 192.168.16.152:80 -m
[root@openEuler-1 ~]# 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.121.150:80 rr
  -> 192.168.16.151:80            Masq    1      0          0
  -> 192.168.16.152:80            Masq    1      0          0

5、LVS-NAT 集群测试

在Client上:

bash 复制代码
[root@Rocky8 ~]# for ((i=0;i<=9;i++)); do curl 192.168.121.150:80; done
This is test page 192.168.16.152 
This is test page 192.168.16.151 
This is test page 192.168.16.152 
This is test page 192.168.16.151 
This is test page 192.168.16.152 
This is test page 192.168.16.151 
This is test page 192.168.16.152 
This is test page 192.168.16.151 
This is test page 192.168.16.152 
This is test page 192.168.16.151 

6.ldirectord工具

1.简介

ldirectord是专门为LVS监控而编写的,用来监控lvs架构中服务器池(server pool) 的服务器状态。 ldirectord 运行在 IPVS 节点上, ldirectord作为一个守护进程启动后会对服务器池中的每个真实服务器 发送请求进行监控,如果 服务器没有响应 lldirectord 的请求,那么ldirectord 认为该服务器不可用, ldirectord 会运行 ipvsadm 对 IPVS表中该服务器进行删除,如果等下次再次检测有相应则通过ipvsadm 进行添加

ldirectord:监控和控制LVS守护进程,可管理LVS规则

2.工具下载地址

http://rpm.pbone.net/index.php3/stat/4/idpl/23860919/dir/centos_6/com/ldirectord-3.9.5- 3.1.x86_64.rpm.html

3.配置

创建 /etc/ha.d/ldirectord.cf 文件在文件里面添加以下内容:

bash 复制代码
# 健康检查超时时间(秒)
checktimeout = 3
# 健康检查间隔时间(秒)
checkinterval = 1
# 是否在配置文件更改时自动重新加载
autoreload = yes
# ldirectord日志文件路径
logfile = "/var/log/ldirectord.log"
# 是否在健康检查失败时将节点设置为静默状态
quiescent = no

# 虚拟服务的IP地址和端口
virtual = 192.168.121.150:80
# 第一个真实服务器的IP地址、端口和转发模式(masq表示NAT)
real = 192.168.16.151:80 masq
# 第二个真实服务器的IP地址、端口和转发模式(masq表示NAT)
real = 192.168.16.152:80 masq
# 所有真实服务器都失败时的备用服务器
fallback = 127.0.0.1:80
# 服务类型(HTTP)
service = http
# 调度算法(轮询)
scheduler = rr
# 会话持久性超时时间
#persistent = 600
# 控制持久性的网络掩码
#netmask = 255.255.255.255
# 使用的协议(TCP)
protocol = tcp
# 健康检查类型(协商)
checktype = negotiate
# 健康检查的端口
checkport = 80

注:这个配置文件里的内容跟之前ipvsadm配置的内容重合了,我们可以用该命令删掉之前的ipvsadm配置

ipvsadm -C

4.重启服务

bash 复制代码
systemctl status ldirectord.service

这里重新生成了虚拟服务器配置

5.测试

相关推荐
未济3 天前
linux 配置环境变量
linux
傲世仙尊3 天前
目录即文件-Ext文件系统收尾篇
linux·c语言
虎头金猫3 天前
4K 视频总卡在公网带宽?用 N1 + OpenList 把网盘播放链路重新理顺
运维·服务器·网络·python·容器·beautifulsoup·pandas
_艾伦 耶格尔.3 天前
进程间通信
linux
AI职业加油站3 天前
AI智能体应用工程师证书:政策红利下的职业新风口
大数据·运维·人工智能·学习·职场发展
Liuqy-053 天前
Linux IO编程——静态库、动态库
linux
此冬歌咏3 天前
K8s 节点故障实战:优雅驱逐 31 秒,硬故障 331 秒,以及那个永远 Pending 的 Pod
运维·k8s
彧azz3 天前
Linux 环境下 Redis 学习总结:数据类型、持久化、锁、事务、主从与缓存问题
linux·redis·笔记·学习·面试
-梅3 天前
linux(8) 软硬链接
linux·运维·服务器
AIgorithmGEEK3 天前
[Linux]线程三部曲(上):一个执行流的诞生——从操作系统一路拆到 pthread_create
linux·线程·pid