调度器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 > /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: $vip broadcast $vip netmask 255.255.255.255 up
route add -host $vip dev ens33:
$ipv -C
$ipv -A -t $vip: -s wrr
$ipv -a -t $vip: -r $rs1: -g -w
$ipv -a -t $vip: -r $rs2: -g -w
[root@web1 ~]# vim /usr/local/sbin/lvs_dr_rs.sh
#/bin/bash
vip=192.168.94.111
#把vip绑定在lo上,是为了实现rs直接把结果返回给客户端
ifconfig lo: $vip broadcast $vip netmask 255.255.255.255 up
route add -host $vip lo:
#以下操作为更改arp内核参数,目的是为了让rs顺利发送mac地址给客户端
echo "" >/proc/sys/net/ipv4/conf/lo/arp_ignore
echo "" >/proc/sys/net/ipv4/conf/lo/arp_announce
echo "" >/proc/sys/net/ipv4/conf/all/arp_ignore
echo "" >/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::ef::2f::::aa:6c::b1:.
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 % .0KB/s :
分别在三台服务器上执行各自的脚本
[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的话不可能成功 , 所以用浏览器测试