目录
一、资源清单
主机 |
操作系统 |
IP地址 |
lb01 |
OpenEuler24.03 |
192.168.16.142 |
lb02 |
OpenEuler24.03 |
192.168.16.143 |
web1 |
OpenEuler24.03 |
192.168.16.144 |
web2 |
OpenEuler24.03 |
192.168.16.145 |
二、修改主机名
hostnamectl set-hostname lb01
hostnamectl set-hostname lb02
hostnamectl set-hostname web1
hostnamectl set-hostname web2
三、配置调度器
1.加载模块(lb01、lb02)
# 加载 ip_vs 模块
modprobe ip_vs
# 查看 ip_vs 版本信息
cat /proc/net/ip_vs
2.安装服务(lb01、lb02)
dnf install -y ipvsadm keepalived
3.修改配置文件(lb01)
vi /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id lb01
}
vrrp_instance VI_1 {
state MASTER # 两个 DS,一个为 MASTER 一个为 BACKUP
interface ens33 # 当前 IP 对应的网络接口,通过 ifconfig 查询
virtual_router_id 62 # 虚拟路由 ID(0-255),在一个 VRRP 实例中主备服务器 ID 必须一样
priority 100 # 优先级值设定:MASTER 要比 BACKUP 的值大
advert_int 1 # 通告时间间隔:单位秒,主备要一致
authentication { # 认证机制,主从节点保持一致即可
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.16.100 # VIP,可配置多个
}
}
# web 配置
virtual_server 192.168.16.100 80 {
delay_loop 3 # 设置健康状态检查时间
lb_algo rr # 调度算法,这里用了 rr 轮询算法
lb_kind DR # 这里测试用了 Direct Route 模式
#persistence_timeout 50 # 持久连接超时时间,注意添加此项配置客户端连续请求时,请求到同一节点
protocol TCP
real_server 192.168.16.144 80 {
weight 1
TCP_CHECK {
connect_timeout 10
retry 3 # 旧版本为 nb_get_retry
delay_before_retry 3 # 重试间隔3秒
connect_port 80
}
}
real_server 192.168.16.145 80 {
weight 1
TCP_CHECK {
connect_timeout 10
retry 3
delay_before_retry 3
connect_port 80
}
}
}
4.修改配置文件(lb02)
vi /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id lb02
}
vrrp_instance VI_1 {
state BACKUP # 两个 DS,一个为 MASTER 一个为 BACKUP
interface ens33 # 当前 IP 对应的网络接口,通过 ifconfig 查询
virtual_router_id 62 # 虚拟路由 ID(0-255),在一个 VRRP 实例中主备服务器 ID 必须一样
priority 90 # 优先级值设定:MASTER 要比 BACKUP 的值大
advert_int 1 # 通告时间间隔:单位秒,主备要一致
authentication { # 认证机制,主从节点保持一致即可
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.16.100 # VIP,可配置多个
}
}
# web 配置
virtual_server 192.168.16.100 80 {
delay_loop 3 # 设置健康状态检查时间
lb_algo rr # 调度算法,这里用了 rr 轮询算法
lb_kind DR # 这里测试用了 Direct Route 模式
#persistence_timeout 50 # 持久连接超时时间,注意添加此项配置客户端连续请求时,请求到同一节点
protocol TCP
real_server 192.168.16.144 80 {
weight 1
TCP_CHECK {
connect_timeout 10
retry 3 # 旧版本为 nb_get_retry
delay_before_retry 3 # 重试间隔3秒
connect_port 80
}
}
real_server 192.168.16.145 80 {
weight 1
TCP_CHECK {
connect_timeout 10
retry 3
delay_before_retry 3
connect_port 80
}
}
}
5.重启Keeplived服务(lb01、lb02)
echo 'net.ipv4.conf.all.send_redirects = 0' >> /etc/sysctl.conf
echo 'net.ipv4.conf.default.send_redirects = 0' >> /etc/sysctl.conf
echo 'net.ipv4.conf.ens33.send_redirects = 0' >> /etc/sysctl.conf
sysctl -p
# 启动keepalived
systemctl start keepalived
四、配置Web节点服务器(web1、web2)
1.安装服务
dnf install -y httpd
2.启动服务
systemctl start httpd --now
3.测试网页
#web1
echo "This is web1" > /var/www/html/index.html
#web2
echo "This is web2" > /var/www/html/index.html
4.编写脚本
vi start.sh
#!/bin/bash
# 修改为自己的VIP
vip='192.168.16.100'
# 临时添加 VIP(重启失效)
sudo ip addr add ${vip}/32 dev lo label lo:0
# 临时添加路由(重启失效)
sudo ip route add local ${vip}/32 dev lo
# 永久生效(通过 rc.local 或 NetworkManager 脚本)
echo "ip addr add ${vip}/32 dev lo label lo:0" | sudo tee -a /etc/rc.local
echo "ip route add local ${vip}/32 dev lo" | sudo tee -a /etc/rc.local
sudo chmod +x /etc/rc.local
sh start.sh
5.添加环境参数
vi /etc/sysctl.conf
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
net.ipv4.conf.default.arp_ignore = 1
net.ipv4.conf.default.arp_announce = 2
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.lo.arp_announce = 2
sysctl -p
五、测试负载均衡
C:\Users\Y>curl 192.168.16.100
This is web1
C:\Users\Y>curl 192.168.16.100
This is web2
C:\Users\Y>curl 192.168.16.100
This is web2
C:\Users\Y>curl 192.168.16.100
This is web2
六、测试LVS+Keepalived高可用群集
#添加持续访问脚本
for i in $(seq 1 100);do curl 192.168.16.100 ;sleep 1;done
#关闭主调度器
systemctl stop keepalived
#可发现客户端依然可以访问
for i in $(seq 1 100);do curl 192.168.16.100 ;sleep 1;done
This is web1
This is web2
This is web1
This is web2
This is web2