创建5台主机(一些配置是基于实验一的基础):
- 客户端client 172.25.254.200
- 路由器route 172.25.254.100 192.168.0.100 (需要eth0、eth1两个网关)
- LVS 192.168.0.50
- webserver1 192.168.0.10
- webserver2 192.168.0.20
1.LVS主机:
vim /etc/NetworkManager/system-connection/eth1.nmconnection
[connection]
id=eth1
type=ethernet
interface-name=eth1[ipv4]
address1=192.168.0.50/24.192.168.0.100
method=manual
nmcli connection reload
nmcli connection up eth1
(如果主机有多余网卡记得清空,只留eth1)
ip a a 192.168.0.200/32 dev lo #在lvs中添加vip,为本地回环接口(lo
)添加一个 IPv4 地址 192.168.0.200
,子网掩码为 255.255.255.255
ipvsadm -Ln
ipvsadm -A -t 192.168.0.200:80 -s wrr #w :权重 ;权重值等于多少轮询调度的时候就调度几次才调度下一个
ipvsadm -a -t 192.168.0.200:80 -r 192.168.0.10:80 -g -w 1 #轮询调度一次就调度192.168.0.20
ipvsadm -a -t 192.168.0.200:80 -r 192.168.0.20:80 -g -w 2 #轮询调度两次才停止或调度下一个
2.路由器route :
vim /etc/NetworkManager/system-connections/eth0.nmconnection #NAT模式的eth0
[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;
vim /etc/NetworkManager/system-connections/eth1.nmconnection #仅主机模式的eth1
[connection]
id=eth1
type=ethernet
interface-name=eth1[ipv4]
address1=192.168.0.100/24
method=manual
nmcli connection reload
nmcli connection up eth0
nmcli connection up eth1
sysctl -a | grep ip_forward #配置内核
vim /etc/sysctl.conf
net.ipv4.ip_forward=1 #添加这一行并将值改为1,保存退出
sysctl -p
3.RS--webserver1 :
实验一配好网关、IP等基础上:
echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore
echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce
#使vip不对外响应
ip a a 192.168.0.200/32 dev lo #添加vip
4.RS--webserver2 :
实验一配好网关、IP等基础上:
echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore
echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce
#使vip不对外响应
ip a a 192.168.0.200/32 dev lo #添加vip
5.客户端client :
vim /etc/NetworkManager/system-connections/eth0.nmconnection #NAT模式的eth0
[connection]
id=eth0
type=ethernet
interface-name=eth0[ipv4]
address1=172.25.254.200/24,172.25.254.2
method=manual
nmcli connection reload
nmcli connection up eth0
结果测试:
for i in {1..10} #省略号只能有两点 ..
do
curl 192.168.0.200
done