试验环境:
五台服务器系统:centos 8.1
vip:172.168.0.100/32
client(一台):192.168.126.81/24 GW:192.168.126.82
router(一台):
eth1: 192.168.126.82/24
eth0: 10.0.0.82/24
eth0:1: 172.168.0.200/24
LVS(一台):
lo:1 172.168.0.100/32 VIP
eth0: 10.0.0.85/24 GW:10.0.0.82
RS(两台):
RS1: lo:1 172.168.0.100/32 VIP
eth0: 10.0.0.83/24 GW:10.0.0.82
RS2: lo:1 172.168.0.100/32 VIP
eth0: 10.0.0.84/24 GW:10.0.0.82
1、client网络配置
[root@client ~]#hostname -I
192.168.126.81
[root@client ~]#cat /etc/sysconfig/network-scripts/ifcfg-eth0
NAME=eth0
DEVICE=eth0
BOOTPROTO=static
IPADDR=192.168.126.81
PREFIX=24
GATEWAY=192.168.126.82
[root@client ~]#route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.126.82 0.0.0.0 UG 100 0 0 eth0
192.168.126.0 0.0.0.0 255.255.255.0 U 100 0 0 eth0
2、Router配置
#开启内核数据转发功能
[root@router ~]#echo 'net.ipv4.ip_forward=1' >> /etc/sysctl.conf
[root@router ~]#sysctl -p
#网络配置,确保路由器有网络到三个IP地址分别和client、RS、VIP在同一网段。
[root@router ~]#cat /etc/sysconfig/network-scripts/ifcfg-eth0 #rs网关
BOOTPROTO="static"
NAME=eth0
DEVICE=eth0
IPADDR=10.0.0.82
PREFIX=24
ONBOOT=yes
[root@router ~]#cat /etc/sysconfig/network-scripts/ifcfg-eth1 #客户端网关
BOOTPROTO="static"
NAME=eth1
DEVICE=eth1
IPADDR=192.168.126.82
PREFIX=24
[root@router ~]#ifconfig eth0:1 172.168.0.200/24 #和vip网段相同
[root@router ~]#hostname -I
10.0.0.82 172.168.0.200 192.168.126.82
3、LVS网络配置
[root@LVS ~]#cat /etc/sysconfig/network-scripts/ifcfg-eth0
BOOTPROTO="static"
NAME=eth0
DEVICE=eth0
IPADDR=10.0.0.85
PREFIX=24
GATEWAY=10.0.0.82
ONBOOT=yes
#配置VIP地址和路由器eth0:1的IP网段相同
[root@LVS ~]#ifconfig lo:1 172.168.0.100/32
[root@LVS ~]#ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
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 172.168.0.100/0 scope global lo:1
valid_lft forever preferred_lft forever
[root@LVS ~]#hostname -I
10.0.0.85
#路由
[root@LVS ~]#route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 10.0.0.82 0.0.0.0 UG 100 0 0 eth0
10.0.0.0 0.0.0.0 255.255.255.0 U 100 0 0 eth0
4、RS1配置
#配置web服务
[root@RS1 ~]#yum install -y nginx
[root@RS1 ~]#echo "RS1 10.0.0.83" > /usr/share/nginx/html/index.html
[root@RS1 ~]#systemctl enable --now nginx
[root@RS1 nginx]#ss -ntl
[root@RS1 nginx]#ps aux|grep nginx
root 4149 0.0 0.1 117764 2164 ? Ss 19:48 0:00 nginx: master process /usr/sbin/nginx
nginx 4150 0.0 0.4 149292 8140 ? S 19:48 0:00 nginx: worker process
nginx 4151 0.0 0.4 149292 8140 ? S 19:48 0:00 nginx: worker process
nginx 4152 0.0 0.4 149292 8140 ? S 19:48 0:00 nginx: worker process
nginx 4153 0.0 0.4 149292 8140 ? S 19:48 0:00 nginx: worker process
root 4161 0.0 0.0 12112 972 pts/0 S+ 19:51 0:00 grep --color=auto nginx
#配置网络
[root@RS1 ~]#vim /etc/sysconfig/network-scripts/ifcfg-eth0
BOOTPROTO="static"
NAME=eth0
UUID=0f357cf0-c66c-4395-a1aa-c2c5ffd88a18
DEVICE=eth0
IPADDR=10.0.0.83
PREFIX=24
GATEWAY=10.0.0.82
ONBOOT=yes
#配置VIP
[root@RS1 ~]#ifconfig lo:1 172.168.0.100/32
[root@RS1 ~]#nmcli c r
[root@RS1 ~]#nmcli c u eth0
[root@RS1 ~]#ip a
[root@RS1 ~]#route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 10.0.0.82 0.0.0.0 UG 100 0 0 eth0
10.0.0.0 0.0.0.0 255.255.255.0 U 100 0 0 eth0
5、RS2配置
#配置web服务器
[root@RS2 ~]#dnf install -y nginx
[root@RS2 ~]#echo "RS2 10.0.0.84" > /usr/share/nginx/html/index.html
[root@RS2 ~]#systemctl enable --now nginx
[root@RS2 ~]#ss -ntl
[root@RS2 ~]#ps aux|grep nginx
root 3771 0.0 0.1 117764 2168 ? Ss 19:43 0:00 nginx: master process /usr/sbin/nginx
nginx 3772 0.0 0.4 149292 8124 ? S 19:43 0:00 nginx: worker process
nginx 3773 0.0 0.4 149292 8124 ? S 19:43 0:00 nginx: worker process
nginx 3774 0.0 0.4 149292 8124 ? S 19:43 0:00 nginx: worker process
nginx 3775 0.0 0.4 149292 8124 ? S 19:43 0:00 nginx: worker process
#配置网络
[root@RS2 ~]#vim /etc/sysconfig/network-scripts/ifcfg-eth0
BOOTPROTO="static"
NAME=eth0
UUID=0f357cf0-c66c-4395-a1aa-c2c5ffd88a18
DEVICE=eth0
IPADDR=10.0.0.84
PREFIX=24
GATEWAY=10.0.0.82
ONBOOT=yes
#配置VIP地址
[root@RS2 ~]#ifconfig lo:1 172.168.0.100/32
[root@RS2 ~]#nmcli c r
[root@RS2 ~]#nmcli c u eth0
[root@RS2 ~]#ip a
[root@RS2 ~]#route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 10.0.0.82 0.0.0.0 UG 100 0 0 eth0
10.0.0.0 0.0.0.0 255.255.255.0 U 100 0 0 eth0
6、LVS服务配置
[root@LVS ~]#dnf -y install ipvsadm
[root@LVS ~]#ipvsadm -A -t 172.168.0.100:80 -s rr
[root@LVS ~]#ipvsadm -a -t 172.168.0.100:80 -r 10.0.0.83 -g
[root@LVS ~]#ipvsadm -a -t 172.168.0.100:80 -r 10.0.0.84 -g
[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.168.0.100:80 rr
-> 10.0.0.83:80 Route 1 0 0
-> 10.0.0.84:80 Route 1 0 0
7、client测试
#首先保证client和后端服务器是相通
[root@client ~]#ping 10.0.0.83 -c1
PING 10.0.0.83 (10.0.0.83) 56(84) bytes of data.
64 bytes from 10.0.0.83: icmp_seq=1 ttl=63 time=1.48 ms
[root@client ~]#ping 10.0.0.84 -c1
PING 10.0.0.84 (10.0.0.84) 56(84) bytes of data.
64 bytes from 10.0.0.84: icmp_seq=1 ttl=63 time=1.19 ms
#测试web服务
[root@client ~]#curl 10.0.0.83
RS1 10.0.0.83
[root@client ~]#curl 10.0.0.84
RS2 10.0.0.84
#测试负载均衡
[root@client ~]#while true;do curl 172.168.100;sleep 0.5s;done
RS2 10.0.0.84
RS1 10.0.0.83
RS2 10.0.0.84
RS1 10.0.0.83
RS2 10.0.0.84
RS1 10.0.0.83