#! /bin/bash
echo 1 > /proc/sys/net/ipv4/ip_forward
ipv=/usr/sbin/ipvsadm
vip=192.168.133.200
rs1=192.168.133.132
rs2=192.168.133.133
#注意这里的网卡名字
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
#/bin/bash
vip=192.168.133.200
#把vip绑定在lo上,是为了实现rs直接把结果返回给客户端
ifconfig lo:0 $vip broadcast $vip netmask 255.255.255.255 up
route add -host $vip lo:0
#以下操作为更改arp内核参数,目的是为了让rs顺利发送mac地址给客户端
#参考文档www.cnblogs.com/lgfeng/archive/2012/10/16/2726308.html
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
[root@hanfeng ~]# vim /usr/local/sbin/lvs_dr.sh
#! /bin/bash
echo 1 > /proc/sys/net/ipv4/ip_forward
#打开端口转发
ipv=/usr/sbin/ipvsadm
#这是一个变量
vip=192.168.202.200
rs1=192.168.202.131
rs2=192.168.202.133
#注意这里的网卡名字
ifdown ens37
ifup ens37
ifconfig ens37:2 $vip broadcast $vip netmask 255.255.255.255 up
#//给一个网卡设置一个虚拟网卡
route add -host $vip dev ens37:2
$ipv -C
$ipv -A -t $vip:80 -s wrr
$ipv -a -t $vip:80 -r $rs1:80 -g -w 1
#这里-g 指定DR模式
$ipv -a -t $vip:80 -r $rs2:80 -g -w 1
保存退出
[root@hanfeng ~]# sh /usr/local/sbin/lvs_dr.sh
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/8)
[root@hanfeng ~]#
B机器
[root@hf-02 ~]# vi /usr/local/sbin/lvs_rs.sh
#/bin/bash
vip=192.168.202.200
#把vip绑定在lo上,是为了实现rs直接把结果返回给客户端
ifdown lo
ifup lo
ifconfig lo:0 $vip broadcast $vip netmask 255.255.255.255 up
route add -host $vip lo:0
#以下操作为更改arp内核参数,目的是为了让rs顺利发送mac地址给客户端
#参考文档www.cnblogs.com/lgfeng/archive/2012/10/16/2726308.html
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
保存退出
C机器同上
sh /usr/local/sbin/lvs_rs.sh
B机器
[root@hf-02 ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.202.2 0.0.0.0 UG 100 0 0 ens32
192.168.202.0 0.0.0.0 255.255.255.0 U 100 0 0 ens32
192.168.202.200 0.0.0.0 255.255.255.255 UH 0 0 0 lo
[root@hf-02 ~]#
C机器同上
B机器
[root@hf-02 ~]# ip add
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet 192.168.202.200/32 brd 192.168.202.200 scope global lo:0
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens32: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:e3:68:95 brd ff:ff:ff:ff:ff:ff
inet 192.168.202.133/24 brd 192.168.202.255 scope global ens32
valid_lft forever preferred_lft forever
inet6 fe80::babb:1944:3d05:ff65/64 scope link
valid_lft forever preferred_lft forever
[root@hf-02 ~]#
C机器同上
A机器
[root@hanfeng ~]# ip add
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eno16777736: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:ff:45:8f brd ff:ff:ff:ff:ff:ff
inet 192.168.202.130/24 brd 192.168.202.255 scope global eno16777736
valid_lft forever preferred_lft forever
inet 192.168.202.150/24 brd 192.168.202.255 scope global secondary eno16777736:0
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:feff:458f/64 scope link
valid_lft forever preferred_lft forever
3: ens37: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:ff:45:99 brd ff:ff:ff:ff:ff:ff
inet 192.168.80.100/24 brd 192.168.80.255 scope global ens37
valid_lft forever preferred_lft forever
inet 192.168.202.200/32 brd 192.168.202.200 scope global ens37:2
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:feff:4599/64 scope link
valid_lft forever preferred_lft forever
[root@hanfeng ~]#
A机器
[root@hanfeng ~]# 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.202.200:80 wrr
-> 192.168.202.131:80 Route 1 0 1
-> 192.168.202.133:80 Route 1 1 0
[root@hanfeng ~]# 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.202.200:80 wrr
-> 192.168.202.131:80 Route 1 1 0
-> 192.168.202.133:80 Route 1 2 0
[root@hanfeng ~]# 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.202.200:80 wrr
-> 192.168.202.131:80 Route 1 1 1
-> 192.168.202.133:80 Route 1 0 2
[root@hanfeng ~]#