诗林 发表于 2024-8-8 20:28:11

lvs集群

nat模式

https://i-blog.csdnimg.cn/direct/53ce35abea24481a8b5681ce07512252.png
实验条件

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

https://i-blog.csdnimg.cn/direct/53c03ca82923419d8d141a20d0ccf41b.png


server1主机和server2主机

https://i-blog.csdnimg.cn/direct/01a8faeb30974ed3b037834a751c6fe2.png


创建实验环境

lvs主机

# vmset.sh eth0 172.25.250.100 lvs
# vmset.sh eth1 192.168.0.100 lvs

###########eth1仅主机网卡
# vim /etc/NetworkManager/system-connections/eth1.nmconnection
# cat /etc/NetworkManager/system-connections/eth1.nmconnection

id=eth1
type=ethernet
interface-name=eth1


address1=192.168.0.100/24      
method=manual

##########eth0 NAT网卡
# cat /etc/NetworkManager/system-connections/eth0.nmconnection

id=eth0
type=ethernet
interface-name=eth0


address1=172.25.250.100/24,172.25.250.2
method=manual


# nmcli connection reload
# nmcli connection up eth0

# nmcli connection up eth1


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

# echo net.ipv4.ip_forward = 1 > /etc/sysctl.conf
# sysctl -p   //使其生效
net.ipv4.ip_forward = 1


server1主机


# vmset.sh eth0 192.168.0.10 server1
# cat /etc/NetworkManager/system-connections/eth0.nmconnection

id=eth0
type=ethernet
interface-name=eth0


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

# nmcli connection reload
# nmcli connection up eth0
server2主机

# vmset.sh eth0 192.168.0.20 server1
# cat /etc/NetworkManager/system-connections/eth0.nmconnection

id=eth0
type=ethernet
interface-name=eth0


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

# nmcli connection reload
# nmcli connection up eth0 开始实验(基于httpd)

lvs主机下载安装ipvsadm


# yum install ipvsadm -y
# ipvsadm -Ln               //查看策略
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port         Forward Weight ActiveConn InActConn
布置策略

# ipvsadm -A -t 172.25.250.100:80-s rr   
// -A 添加    // -t 指定TCP协议    //-s 指定算法   rr // 轮寻(你一个我一个....静态)


# ipvsadm -a -t 172.25.250.100:80 -r 192.168.0.10:80 -m
# ipvsadm -a -t 172.25.250.100:80 -r 192.168.0.20:80 -m
# ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port         Forward Weight ActiveConn InActConn
TCP172.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主机

# yum install httpd -y//安装httpd

# echo this is server1 > /var/www/html/index.html

# echo this is server2 > /var/www/html/index.html

测试

# 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模式

https://i-blog.csdnimg.cn/direct/bc65c73e6348449ba55131793a497e85.png
创建环境

client主机

# vmset.sh 172.25.250.200 client
# cat /etc/NetworkManager/system-connections/eth0.nmconnection

id=eth0
type=ethernet
interface-name=eth0


address1=172.25.250.200/24,172.25.250.100
method=manual
dns=114.114.114.114;
router主机

# vmset.sh 172.25.250.100 router
# vmset.sh 192.168.0.100 router
# cat /etc/NetworkManager/system-connections/eth0.nmconnection

id=eth0
type=ethernet
interface-name=eth0


address1=172.25.250.100/24,172.25.250.2
method=manual
dns=114.114.114.114;
# cat /etc/NetworkManager/system-connections/eth1.nmconnection

id=eth1
type=ethernet
interface-name=eth1


address1=192.168.0.100/24
method=manual
dns=114.114.114.114;


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

# echo net.ipv4.ip_forward = 1 > /etc/sysctl.conf
# sysctl -p   //使其生效
net.ipv4.ip_forward = 1 lvs主机

# nmcli connection delete eth0//删掉eth0网卡ip,我们用lo环回来做一次性ip

# vmset.sh eth1 192.168.0.200 lvs
# cat /etc/NetworkManager/system-connections/eth1.nmconnection

id=eth1
type=ethernet
interface-name=eth1


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

# ip a a 192.168.0.50/32 dev lo   //环回添加一次性ip
server1、server2主机

#############server1主机

#vmset.sh eth0 192.168.0.10 server1
# cat /etc/NetworkManager/system-connections/eth0.nmconnection

id=eth0
type=ethernet
interface-name=eth0


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


#echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
#echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
#echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce
#echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore

# ip a a 192.168.0.50/32 dev lo    //临时ip


############server2主机
#vmset.sh eth0 192.168.0.20 server2

# cat /etc/NetworkManager/system-connections/eth0.nmconnection

id=eth0
type=ethernet
interface-name=eth0


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

#echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
#echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
#echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce
#echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore

# ip a a 192.168.0.50/32 dev lo   //临时ip
开始实验

lvs主机上安装ipvsadm


# yum install ipvsadm -y
# ipvsadm -Ln               //查看策略
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port         Forward Weight ActiveConn InActConn
布置策略

# ipvsadm -A -t 192.168.0.50:80 -s wrr
# ipvsadm -a -t 192.168.0.50:80 -r 192.168.0.10:80 -g -w 2//权重2
# ipvsadm -a -t 192.168.0.50:80 -r 192.168.0.20:80 -g -w 1//权重1
# ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port         Forward Weight ActiveConn InActConn
TCP192.168.0.50:80 wrr
-> 192.168.0.10:80            Route   2      0          0         
-> 192.168.0.20:80            Route   1      0          0         
测试

# 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
https://i-blog.csdnimg.cn/direct/bf7b32d3b0f5463fbc108eb1ca2baff5.png

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页: [1]
查看完整版本: lvs集群