LVS DR模式單網段案例

igcllq發表於2020-11-06

在這裡插入圖片描述

準備五臺虛擬機器
提前在RS1.2上安裝上httpd
在LVS上安裝ipvsadm

1、客戶端 eth0 :僅主機 192.168.10.6/24 GW:192.168.10.200

2、router (路由器)
ech0:NAT 10.0.0.200/24
eth1:僅主機 192.168.10.200/24
開啟路由轉發 IP_FORWARD

3、:LVS
echo :NAT:DIP:10.0.0.8/24 GW:10.0.0.200

4 5 RS:
RS1: ech0:NAT:10.0.0.7/24 GW:10.0.0.200
RS2:ech0: NAT:10.0.0.17/24 GW:10.0.0.200

關閉所有主機的iptables和selinux

在客戶端操作

#配置網路卡 在router上開起埠轉發
[root@igcllq ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0 
DEVICE=eth0
NAME=eth0
IPADDR=192.168.10.6
PREFIX=24
GATEWAY=192.168.10.200
BOOTPROTS=static

[root@igcllq ~]# ping 10.0.0.7
PING 10.0.0.7 (10.0.0.7) 56(84) bytes of data.
64 bytes from 10.0.0.7: icmp_seq=1 ttl=63 time=3.88 ms

root@igcllq ~]# ping 10.0.0.17
PING 10.0.0.17 (10.0.0.17) 56(84) bytes of data.
64 bytes from 10.0.0.17: icmp_seq=1 ttl=63 time=0.934 ms

在router上操作

關閉ip-forward
echo 'net.ipv4.ip_forward=1' >> /etc/sysctl.conf
sysctl -p

配置網路卡
[root@localhost ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
name=eth0
IPADDR=10.0.0.200
PREFIX=24
BOOTPROTS=static
ONBOOT=yes
[root@localhost ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth1
DEVICE=eth1
name=eth1
IPADDR=192.168.10.200
PREFIX=24
BOOTPROTS=static
ONBOOT=yes

配置rs1.2

rs1配置網路卡
[root@rs1 ~]# vi /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
name=eth0
IPADDR=10.0.0.7
PREFIX=24
GATEWAY=10.0.0.200
BOOTPROTS=static
ONBOOT=yes

檢視路由
[root@rs1 ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         10.0.0.200      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

安裝httpd
[root@rs1 ~]#yum -y install httpd 
[root@rs1 ~]#systemctl enable --now httpd 
[root@rs1 ~]#hostname -I > /var/www/html/index.html
[root@rs1 ~]# ping 192.168.10.6 -c1
PING 192.168.10.6 (192.168.10.6) 56(84) bytes of data.
64 bytes from 192.168.10.6: icmp_seq=1 ttl=63 time=1.46 ms

rs2配置網路卡
[root@rs1 ~]# vi /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
name=eth0
IPADDR=10.0.0.17
PREFIX=24
GATEWAY=10.0.0.200
BOOTPROTS=static
ONBOOT=yes

檢視路由
[root@rs2 ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         10.0.0.200      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
安裝httpd
[root@rs2 ~]#yum -y install httpd 
[root@rs2 ~]#systemctl enable --now httpd 
[root@rs2 ~]#hostname -I > /var/www/html/index.html
[root@rs2 ~]# ping 192.168.10.6 -c1
PING 192.168.10.6 (192.168.10.6) 56(84) bytes of data.
64 bytes from 192.168.10.6: icmp_seq=1 ttl=63 time=1.46 ms

LVS配置

網路配置
[root@lvs ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0 
DEVICE=eth0
name=eth0
IPADDR=10.0.0.8
PREFIX=24
GATEWAY=10.0.0.200
BOOTPROTS=static

[root@lvs ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         10.0.0.200      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

[root@lvs ~]# ping 192.168.10.6
PING 192.168.10.6 (192.168.10.6) 56(84) bytes of data.
64 bytes from 192.168.10.6: icmp_seq=1 ttl=63 time=1.29 ms

後端RS的IPVS配置(相當於在rs1.2和LVS上開了一個單獨的小通道)

RS1的IPVS配置

echo 1 >   /proc/sys/net/ipv4/conf/all/arp_ignore
echo 2 >   /proc/sys/net/ipv4/conf/all/arp_announce
echo 1 >   /proc/sys/net/ipv4/conf/lo/arp_ignore 
echo 2 >   /proc/sys/net/ipv4/conf/lo/arp_announce 
ifconfig lo:1 10.0.0.100/32
ip a
#RS2的IPVS配置 
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 
ifconfig lo:1 10.0.0.100/32 
ip a

在LVS上新增VIP

ifconfig lo:1 10.0.0.100/32 
ip a

在LVS上實現LVS 規則

yum -y install ipvsadm
ipvsadm -A -t 10.0.0.100:80 -s rr
ipvsadm -a -t 10.0.0.100:80 -r 10.0.0.7:80 -g
ipvsadm -a -t 10.0.0.100:80 -r 10.0.0.17:80 -g
ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  10.0.0.100:80 rr
  -> 10.0.0.7:80                  Route   1      0          0         
  -> 10.0.0.17:80                 Route   1      0          0     

測試訪問(在客戶端測試)

[root@igcllq ~]# curl 10.0.0.100
rs2
[root@igcllq ~]# curl 10.0.0.100
rs1
[root@igcllq ~]# curl 10.0.0.100
rs2
[root@igcllq ~]# curl 10.0.0.100
rs1

相關文章