CentOS 7 DR模式LVS搭建

时间:2023-01-15 21:53:33

调度器LB : 192.168.94.11

真实web服务器1 : 192.168.94.22

真实web服务器2 : 192.168.94.33

VIP : 192.168.94.111

脚本如下 : 

[root@lb ~]# vim /usr/local/sbin/lvs_dr.sh
#!/bin/bash echo 1 > /proc/sys/net/ipv4/ip_forward ipv=/usr/sbin/ipvsadm vip=192.168.94.111 rs1=192.168.94.22 rs2=192.168.94.33 #注意这里的网卡名字 ifconfig ens33:2 $vip broadcast $vip netmask 255.255.255.255 up route add -host $vip dev ens33:2 $ipv -C $ipv -A -t $vip:80 -s wrr $ipv -a -t $vip:80 -r $rs1:80 -g -w 1 $ipv -a -t $vip:80 -r $rs2:80 -g -w 1
[root@web1 ~]# vim /usr/local/sbin/lvs_dr_rs.sh
 #/bin/bash                                                                               
 vip=192.168.94.111
 #把vip绑定在lo上,是为了实现rs直接把结果返回给客户端
 ifconfig lo:0 $vip broadcast $vip netmask 255.255.255.255 up
 route add -host $vip lo:0
 #以下操作为更改arp内核参数,目的是为了让rs顺利发送mac地址给客户端
 echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore
 echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce
 echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore
 echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce
#把脚本拷贝到另一台web服务器
[root@web1 ~]# cd /usr/local/sbin/
[root@web1 sbin]# scp lvs_dr_rs.sh root@192.168.94.33:/usr/local/sbin/
The authenticity of host '192.168.94.33 (192.168.94.33)' can't be established.
ECDSA key fingerprint is SHA256:gNmF8nTFjdlS2FazjHUxSiqtm+FQojwv6ABTbLqjKrI.
ECDSA key fingerprint is MD5:d9:c1:8e:a2:38:ef:30:2f:42:85:31:aa:6c:62:b1:21.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.94.33' (ECDSA) to the list of known hosts.
root@192.168.94.33's password: 
lvs_dr_rs.sh 100% 484 419.0KB/s 00:00

分别在三台服务器上执行各自的脚本

[root@lb ~]# bash /usr/local/sbin/lvs_dr.sh
[root@web1 ~]# bash /usr/local/sbin/lvs_dr_rs.sh
[root@web2 ~]# bash /usr/local/sbin/lvs_dr_rs.sh

测试

要注意 , 不能在LB上用curl命令测试 , 因为VIP在每一台服务器上都有设置 , 直接curl去访问VIP的话不可能成功 , 所以用浏览器测试

CentOS 7 DR模式LVS搭建