linux基于 openEuler 构建 LVS-DR 群集--一、用命令行完成 二、使用脚本完成

目录

一·、用命令行完成

1、在nginx上(两台都是一样的配置)

[2、 在LVS上](#2、 在LVS上)

[1.)绑定VIP (与nginx上一致)](#1.)绑定VIP (与nginx上一致))

2)安装ipvsadm

3)配置LVS-DR

3、在CLINT上

1)验证 (验证成功如下)

​编辑

2)故障排查步骤:

二、使用脚本完成

1、在nginx上(与一一样)

2、在lvs上用脚本(前提安装好ipvsadm)

3、验证(与一一样)


IP地址如图:

一·、用命令行完成

主要思路如下

在nginx上:1、安装nginx 2、绑定VIP 3、arp抑制

在LVS上:1.、绑定VIP 2、安装ipvsadm 3、配置ipvsadm

在CLINT上 :1、验证

1、在nginx上(两台都是一样的配置)

1)安装nginx

#下载nginx
[root@openeuler ~]# yum install nginx -y

#列出 Nginx 默认网页目录的内容
[root@openeuler ~]# ls /usr/share/nginx/html/
404.html  50x.html  index.html  nginx-logo.png

#这个命令的目的是创建一个新的首页文件,内容包含当前主机的 IP 地址
[root@openeuler ~]# echo "web test page,ip is `hostname -I`." > /usr/share/nginx/html/

#启用并启动 Nginx 服务
[root@openeuler ~]# systemctl enable --now nginx

#访问本地 Nginx 服务
[root@openeuler ~]# curl localhost
web test page,ip is 192.168.131.19 .#出现这个代表访问成功

2)绑定VIP

#网络需增加 VIP 的相关配置
[root@openeuler ~]# nmcli connection add type dummy ifname dummy2 ipv4.method manual ipv4.addresses 192.168.131.9/32

3)arp的抑制

#完成网络配置后,修改相应的 arp 内核配置,具体命令如下
[root@openeuler ~]# cat >> /etc/sysctl.conf << EOF
> net.ipv4.conf.all.arp_ignore = 1
> net.ipv4.conf.all.arp_announce = 2
> net.ipv4.conf.dummy2.arp_ignore = 1
> net.ipv4.conf.dummy2.arp_announce = 2
> EOF
#配置完成后,使用命令 sysctl -p 使其生效
[root@openeuler ~]# sysctl -p | tail -4
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
net.ipv4.conf.dummy2.arp_ignore = 1
net.ipv4.conf.dummy2.arp_announce = 2

nginx上配置完成!

2、 在LVS上

1.)绑定VIP (与nginx上一致)
2)安装ipvsadm
[root@client ~]# yum install ipvsadm -y
3)配置LVS-DR

在没有配置前可以先查看(如下是为空的结果)

[root@client ~]# ipvsadm -L
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn

#添加配置vip与nginx两个IP
[root@client ~]# ipvsadm -A -t 192.168.131.9:80 -s rr
[root@client ~]# ipvsadm -a -t 192.168.131.9:80 -r 192.168.131.19
[root@client ~]# ipvsadm -a -t 192.168.131.9:80 -r 192.168.131.20
#查看具体配置
[root@client ~]# ipvsadm -Ln

3、在CLINT上

1)验证 (验证成功如下)

如果不成功如下

2)故障排查步骤:

1、 检查网络连通性

2、检查防火墙:是否有防火墙规则阻止了 80 端口的流量,也可以直接关闭防火墙(四台操作机都要关防火墙)

2、检查服务状态:确保nginx正在运行,并且正在监听 80 端口。

二、使用脚本完成

1、在nginx上(与一一样)

2、在lvs上用脚本(前提安装好ipvsadm)

在代码里的VIP\RIP1\RIP2改

#!/bin/sh
#
# Startup script handle the initialisation of LVS
# chkconfig: - 28 72
# description: Initialise the Linux Virtual Server for DR
#
### BEGIN INIT INFO
# Provides: ipvsadm
# Required-Start: $local_fs $network $named
# Required-Stop: $local_fs $remote_fs $network
# Short-Description: Initialise the Linux Virtual Server
# Description: The Linux Virtual Server is a highly scalable and highly
# available server built on a cluster of real servers, with the load
# balancer running on Linux.
# description: start LVS of DR
LOCK=/var/lock/ipvsadm.lock
VIP=192.168.131.9
RIP1=192.168.131.19
RIP2=192.168.131.20
DipName=ens33
. /etc/rc.d/init.d/functions
start() {
PID=`ipvsadm -Ln | grep ${VIP} | wc -l`
if [ $PID -gt 0 ];
then
echo "The LVS-DR Server is already running !"
else
#Set the Virtual IP Address
/sbin/ifconfig ${DipName}:10 $VIP broadcast $VIP netmask
255.255.255.255 up
/sbin/route add -host $VIP dev ${DipName}:10
#Clear IPVS Table
/sbin/ipvsadm -C
#Set Lvs
/sbin/ipvsadm -At $VIP:80 -s rr
/sbin/ipvsadm -at $VIP:80 -r $RIP1:80 -g
/sbin/ipvsadm -at $VIP:80 -r $RIP2:80 -g
/bin/touch $LOCK
#Run Lvs
echo "starting LVS-DR Server is ok !"
fi
}
stop() {
#clear Lvs and vip
/sbin/ipvsadm -C
/sbin/route del -host $VIP dev ${DipName}:10
/sbin/ifconfig ${DipName}:10 down >/dev/null
rm -rf $LOCK
echo "stopping LVS-DR server is ok !"
}
status() {
if [ -e $LOCK ];
then
echo "The LVS-DR Server is already running !"
else
echo "The LVS-DR Server is not running !"
fi
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
status)
status
;;
*)
echo "Usage: $1 {start|stop|restart|status}"
exit 1
esac
exit 0


[root@openeuler ~]# vim /etc/init.d/lvs_dr
#赋予脚本可执行权限
[root@openeuler ~]# chmod +x /etc/init.d/lvs_dr
#将服务添加到 chkconfig 管理
[root@openeuler ~]# chkconfig --add lvs dr
#设置服务开机自启动
[root@openeuler ~]# chkconfig lvs_dr on
# 启动服务
[root@openeuler ~]# systemctl start lvs_dr
#查看服务状态
[root@openeuler ~]# systemctl status lvs_dr

3、验证(与一一样)

相关推荐
冷白白26 分钟前
【Qt】信号和槽机制
服务器·c++·qt
irisart30 分钟前
3.2 > Bash
linux·bash
上海运维Q先生1 小时前
Rhel Centos环境开关机自动脚本
linux·运维·centos
魔法小匠1 小时前
Go 模块化开发入门(go mod 深度解析)
服务器·golang·go·模块化·go mod
丁总学Java1 小时前
在Mac arm架构终端中运行 corepack enable yarn 命令,安装yarn
linux·编辑器·vim
F-2H2 小时前
数据结构:队列
linux·c语言·开发语言·数据结构·算法·链表
小馒头学python2 小时前
【算法学习】蓝耘云智算|利用DeepSeek R1模型提升数据结构与算法学习效率
服务器·数据结构·python·学习·算法
大时代11053 小时前
Reactor
linux
幻想趾于现实5 小时前
C#中的序列化和反序列化
服务器·c#