lvs集群

nat模式

实验前提

除了测试主机,其他三台主机都需要添加或修改网卡,修改如以下显示

lvs主机

server1主机和server2主机

创建实验环境

lvs主机

复制代码
[root@lvs ~]# vmset.sh eth0 172.25.250.100 lvs
[root@lvs ~]# vmset.sh eth1 192.168.0.100 lvs

###########eth1仅主机网卡
[root@lvs ~]# vim /etc/NetworkManager/system-connections/eth1.nmconnection 
[root@lvs ~]# cat /etc/NetworkManager/system-connections/eth1.nmconnection 
[connection]
id=eth1
type=ethernet
interface-name=eth1

[ipv4]
address1=192.168.0.100/24       
method=manual

##########eth0 NAT网卡
[root@lvs ~]# cat /etc/NetworkManager/system-connections/eth0.nmconnection 
[connection]
id=eth0
type=ethernet
interface-name=eth0

[ipv4]
address1=172.25.250.100/24,172.25.250.2
method=manual


[root@lvs ~]# nmcli connection reload 
[root@lvs ~]# nmcli connection up eth0

[root@lvs ~]# nmcli connection up eth1


################打开内核路由,使eth1和eth0通信
[root@lvs ~]# sysctl -a | grep ip_forward
net.ipv4.ip_forward = 0
net.ipv4.ip_forward_update_priority = 1
net.ipv4.ip_forward_use_pmtu = 0

[root@lvs ~]# echo net.ipv4.ip_forward = 1 > /etc/sysctl.conf 
[root@lvs ~]# sysctl -p     //使其生效
net.ipv4.ip_forward = 1

server1主机

复制代码
[root@server1 ~]# vmset.sh eth0 192.168.0.10 server1
[root@server1 ~]# cat /etc/NetworkManager/system-connections/eth0.nmconnection 
[connection]
id=eth0
type=ethernet
interface-name=eth0

[ipv4]
address1=192.168.0.10/24,192.168.0.100    //server1主机网关指向lvs主机
method=manual

[root@server1 ~]# nmcli connection reload 
[root@server1 ~]# nmcli connection up eth0 

server2主机

复制代码
[root@server2 ~]# vmset.sh eth0 192.168.0.20 server1
[root@server2 ~]# cat /etc/NetworkManager/system-connections/eth0.nmconnection 
[connection]
id=eth0
type=ethernet
interface-name=eth0

[ipv4]
address1=192.168.0.20/24,192.168.0.100    //server2主机网关指向lvs主机
method=manual

[root@server2 ~]# nmcli connection reload 
[root@server2 ~]# nmcli connection up eth0 

开始实验(基于httpd)

lvs主机下载安装ipvsadm

复制代码
[root@lvs ~]# yum install ipvsadm -y
[root@lvs ~]# ipvsadm -Ln               //查看策略
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn

布置策略

复制代码
[root@lvs ~]# ipvsadm -A -t 172.25.250.100:80  -s rr     
// -A 添加    // -t 指定TCP协议    //-s 指定算法   rr // 轮寻(你一个我一个....静态)


[root@lvs ~]# ipvsadm -a -t 172.25.250.100:80 -r 192.168.0.10:80 -m 
[root@lvs ~]# ipvsadm -a -t 172.25.250.100:80 -r 192.168.0.20:80 -m 
[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  172.25.250.100:80 rr
  -> 192.168.0.10:80              Masq    1      0          0         
  -> 192.168.0.20:80              Masq    1      0          0        

server1,server2主机

复制代码
[root@server1 ~]# yum install httpd -y  //安装httpd

[root@server1 ~]# echo this is server1 > /var/www/html/index.html

[root@server2 ~]# echo this is server2 > /var/www/html/index.html

测试

复制代码
[root@rhel9 ~]# for i in {1..10}
> do
> curl 172.25.250.100
> done
this is server1
this is server2
this is server1
this is server2
this is server1
this is server2
this is server1
this is server2
this is server1
this is server2

DR模式

创建环境

client主机

复制代码
[root@client ~]# vmset.sh 172.25.250.200 client
[root@client ~]# cat /etc/NetworkManager/system-connections/eth0.nmconnection 
[connection]
id=eth0
type=ethernet
interface-name=eth0

[ipv4]
address1=172.25.250.200/24,172.25.250.100
method=manual
dns=114.114.114.114;

router主机

复制代码
[root@router ~]# vmset.sh 172.25.250.100 router
[root@router ~]# vmset.sh 192.168.0.100 router
[root@router ~]# cat /etc/NetworkManager/system-connections/eth0.nmconnection 
[connection]
id=eth0
type=ethernet
interface-name=eth0

[ipv4]
address1=172.25.250.100/24,172.25.250.2
method=manual
dns=114.114.114.114;
[root@router ~]# cat /etc/NetworkManager/system-connections/eth1.nmconnection 
[connection]
id=eth1
type=ethernet
interface-name=eth1

[ipv4]
address1=192.168.0.100/24
method=manual
dns=114.114.114.114;


################打开内核路由,使eth1和eth0通信
[root@router ~]# sysctl -a | grep ip_forward
net.ipv4.ip_forward = 0
net.ipv4.ip_forward_update_priority = 1
net.ipv4.ip_forward_use_pmtu = 0

[root@router ~]# echo net.ipv4.ip_forward = 1 > /etc/sysctl.conf 
[root@router ~]# sysctl -p     //使其生效
net.ipv4.ip_forward = 1

lvs主机

复制代码
[root@lvs ~]# nmcli connection delete eth0  //删掉eth0网卡ip,我们用lo环回来做一次性ip

[root@lvs ~]# vmset.sh eth1 192.168.0.200 lvs
[root@lvs ~]# cat /etc/NetworkManager/system-connections/eth1.nmconnection 
[connection]
id=eth1
type=ethernet
interface-name=eth1

[ipv4]
address1=192.168.0.200/24,192.168.0.100           //网关指向路由
method=manual
dns=114.114.114.114;

[root@lvs ~]# ip a a 192.168.0.50/32 dev lo     //环回添加一次性ip

server1、server2主机

复制代码
#############server1主机

[root@server1 ~]#  vmset.sh eth0 192.168.0.10 server1
[root@server1 ~]# cat /etc/NetworkManager/system-connections/eth0.nmconnection 
[connection]
id=eth0
type=ethernet
interface-name=eth0

[ipv4]
address1=192.168.0.10/24,192.168.0.100    // //网关指向路由
method=manual


[root@server1 ~]#  echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore 
[root@server1 ~]#  echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce 
[root@server1 ~]#  echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce 
[root@server1 ~]#  echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore

[root@server1 ~]# ip a a 192.168.0.50/32 dev lo    //临时ip


############server2主机
[root@server2 ~]#  vmset.sh eth0 192.168.0.20 server2

[root@server2 ~]# cat /etc/NetworkManager/system-connections/eth0.nmconnection 
[connection]
id=eth0
type=ethernet
interface-name=eth0

[ipv4]
address1=192.168.0.20/24,192.168.0.100    //网关指向路由
method=manual

[root@server2 ~]#  echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore 
[root@server2 ~]#  echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce 
[root@server2 ~]#  echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce 
[root@server2 ~]#  echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore

[root@server2 ~]# ip a a 192.168.0.50/32 dev lo   //临时ip

开始实验

lvs主机上安装ipvsadm

复制代码
[root@lvs ~]# yum install ipvsadm -y
[root@lvs ~]# ipvsadm -Ln               //查看策略
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn

布置策略

复制代码
[root@lvs ~]# ipvsadm -A -t 192.168.0.50:80 -s wrr  
[root@lvs ~]# ipvsadm -a -t 192.168.0.50:80 -r 192.168.0.10:80 -g -w 2  //权重2
[root@lvs ~]# ipvsadm -a -t 192.168.0.50:80 -r 192.168.0.20:80 -g -w 1  //权重1
[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.0.50:80 wrr
  -> 192.168.0.10:80              Route   2      0          0         
  -> 192.168.0.20:80              Route   1      0          0         

测试

复制代码
[root@client ~]# for i in {1..10}
> do
> curl 192.168.0.50
> done
this is server1
this is server1
this is server2
this is server1
this is server1
this is server2
this is server1
this is server1
this is server2
this is server1
相关推荐
极小狐11 分钟前
极狐GitLab 项目功能和权限解读
运维·git·安全·gitlab·极狐gitlab
宁酱醇12 分钟前
GitLab_密钥生成(SSH-key)
运维·ssh·gitlab
秋风起,再归来~18 分钟前
【Linux庖丁解牛】—进程优先级!
linux·运维·服务器
cosX+sinY37 分钟前
ubuntu 20.04 编译运行lio-sam,并保存为pcd
linux·ubuntu·机器人
Lalolander1 小时前
设备制造行业如何避免项目管理混乱?
运维·制造·工程项目管理·四算一控·epc·环保设备工程·设备制造
LucianaiB1 小时前
【金仓数据库征文】_AI 赋能数据库运维:金仓KES的智能化未来
运维·数据库·人工智能·金仓数据库 2025 征文·数据库平替用金仓
prinrf('千寻)1 小时前
nacos设置权重进行负载均衡不生效
运维·负载均衡
Lary_Rock2 小时前
Android 编译问题 prebuilts/clang/host/linux-x86
android·linux·运维
熬夜学编程的小王2 小时前
【Linux篇】理解信号:如何通过信号让程序听从操作系统的指令
linux·信号产生·软件条件产生信号
子非衣2 小时前
Windows云主机远程连接提示“出现了内部错误”
服务器·windows