一、环境准备
主机名 | IP地址 |
router | eth0:172.25.254.100 eth1:192.168.0.100 |
client | eth0:172.25.254.200 |
lvs | eth1:192.168.0.50 |
web1 | |
web2 |
1、client配置
[root@client ~]# cat /etc/NetworkManager/system-connections/eth0.nmconnection
[connection]
id=eth0
type=ethernet
interface-name=eth0
[ipv4]
address1=172.25.254.200/24,172.25.254.100
method=manual
[root@client ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 172.25.254.100 0.0.0.0 UG 100 0 0 eth0
172.25.254.0 0.0.0.0 255.255.255.0 U 100 0 0 eth0
2、router配置
打开路由转发
[root@router ~]# vim /etc/sysctl.conf
[root@router ~]#
[root@router ~]# sysctl -p
net.ipv4.ip_forward = 1
网卡配置
[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
[root@router ~]# cat /etc/NetworkManager/system-connections/eth0.nmconnection
[connection]
id=eth0
type=ethernet
interface-name=eth0
[ipv4]
address1=172.25.254.100/24,172.25.254.2
method=manual
dns=114.114.114.114;
3、lvs服务器配置
lvs服务器网卡及网关配置
[root@lvs ~]# cat /etc/NetworkManager/system-connections/eth1.nmconnection
[connection]
id=eth1
type=ethernet
interface-name=eth1
[ipv4]
address1=192.168.0.50/24,192.168.0.100
method=manual
[root@lvs ~]# ip a a 192.168.0.200/32 dev lo
4、web1配置
[root@web1 ~]# 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
dns=114.114.114.114;
[root@web1 ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.0.100 0.0.0.0 UG 100 0 0 eth0
192.168.0.0 0.0.0.0 255.255.255.0 U 100 0 0 eth0
rs主机中使vip不对外响应
[root@web1 ~]# echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
[root@web1 ~]# echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
[root@web1 ~]# echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce
[root@web1 ~]# echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore
[root@web1 ~]# ip a a 192.168.0.200/32 dev lo
5、web2配置
[root@web2 ~]# echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
[root@web2 ~]# echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
[root@web2 ~]# echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce
[root@web2 ~]# echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore
[root@web2 ~]# ip a a 192.168.0.200/32 dev lo
二、在lvs服务器中配置策略
[root@lvs ~]# ipvsadm -A -t 192.168.0.200:80 -s wrr
[root@lvs ~]# ipvsadm -a -t 192.168.0.200:80 -r 192.168.0.10 -g -w 1
[root@lvs ~]# ipvsadm -a -t 192.168.0.200:80 -r 192.168.0.20 -g -w 2
[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.0.254:80 rr
TCP 172.25.254.100:80 rr
-> 192.168.0.10:80 Masq 1 0 0
-> 192.168.0.20:80 Masq 1 0 0
TCP 192.168.0.200:80 wrr
-> 192.168.0.10:80 Route 1 0 0
-> 192.168.0.20:80 Route 2 0 0
三、检测
1.clien
[root@client ~]# ping 192.168.0.200
PING 192.168.0.200 (192.168.0.200) 56(84) 比特的数据。
64 比特,来自 192.168.0.200: icmp_seq=1 ttl=63 时间=0.847 毫秒
64 比特,来自 192.168.0.200: icmp_seq=2 ttl=63 时间=0.802 毫秒
^C
--- 192.168.0.200 ping 统计 ---
已发送 2 个包, 已接收 2 个包, 0% packet loss, time 1050ms
rtt min/avg/max/mdev = 0.802/0.824/0.847/0.022 ms
[root@client ~]# ping 192.168.0.100
PING 192.168.0.100 (192.168.0.100) 56(84) 比特的数据。
64 比特,来自 192.168.0.100: icmp_seq=1 ttl=64 时间=0.279 毫秒
64 比特,来自 192.168.0.100: icmp_seq=2 ttl=64 时间=0.204 毫秒
64 比特,来自 192.168.0.100: icmp_seq=3 ttl=64 时间=0.260 毫秒
^C
--- 192.168.0.100 ping 统计 ---
已发送 3 个包, 已接收 3 个包, 0% packet loss, time 2055ms
rtt min/avg/max/mdev = 0.204/0.247/0.279/0.031 ms
[root@client ~]# for i in {1..10}
> do
> curl 192.168.0.200
> done
web2 - 192.168.0.20
web1 - 192.168.0.10
web2 - 192.168.0.20
web2 - 192.168.0.20
web1 - 192.168.0.10
web2 - 192.168.0.20
web2 - 192.168.0.20
web1 - 192.168.0.10
web2 - 192.168.0.20
web2 - 192.168.0.20
成功!!!