LVS-DR模式单网段实战

 

LVS-DR模式单网段实战_第1张图片

 

LVS-DR模式单网段实战_第2张图片

环境:五台主机

一台:client

eth0:仅主机 192.168.140.10/24  GW:192.168.140.100

一台:route

eth0: 仅主机 192.168.140.100

eth1:  NAT 10.0.0.100.24

启用 ip_forward

一台:lvs

eth0: NAT:DIP:10.0.0.11/24 GW: 10.0.0.100

两台RS:

RS1:eth0:NAT:10.0.0.22/24 GW: 10.0.0.100

RS2:eth0:NAT:10.0.0.33/24 GW: 10.0.0.100

1、LVS的网络配置

#所有主机禁用iptables和selinux

#client主机环境

root@client:~# hostname -I
192.168.140.10 

root@client:~# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.140.100 0.0.0.0         UG    0      0        0 ens33
192.168.140.0   0.0.0.0         255.255.255.0   U     0      0        0 ens33

root@client:~# ping 10.0.0.100
64 bytes from 10.0.0.100: icmp_seq=2 ttl=64 time=0.440 ms

root@client:~# ping 10.0.0.11
PING 10.0.0.11 (10.0.0.11) 56(84) bytes of data.


#route环境配置

[root@Route ~]# hostname -I
10.0.0.100 192.168.140.100 
[root@Route ~]# 

[root@Route ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
10.0.0.0        0.0.0.0         255.255.255.0   U     101    0        0 ens33
192.168.140.0   0.0.0.0         255.255.255.0   U     100    0        0 ens37
[root@Route ~]# 

#开启路由转发
[root@Route ~]# echo 'net.ipv4.ip_forward=1' >> /etc/sysctl.conf 
[root@Route ~]# sysctl -p
net.ipv4.ip_forward = 1

#LVS的网络配置
[root@LVS ~]# hostname -I
10.0.0.11 
[root@LVS ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         10.0.0.100      0.0.0.0         UG    100    0        0 ens33
10.0.0.0        0.0.0.0         255.255.255.0   U     100    0        0 ens33
[root@LVS ~]# 


#RS1的网络配置

[root@RS1 ~]# hostname -I
10.0.0.22 
[root@RS1 ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         10.0.0.100      0.0.0.0         UG    100    0        0 ens33
10.0.0.0        0.0.0.0         255.255.255.0   U     100    0        0 ens33

[root@RS1 ~]# yum install -y httpd
[root@RS1 ~]# systemctl enable --now httpd
[root@RS1 ~]# echo RS1 > /var/www/html/index.html 
[root@RS1 ~]# hostname -I >> /var/www/html/index.html
[root@RS1 ~]# cat /var/www/html/index.html 
RS1
10.0.0.22 
[root@RS1 ~]# 


#RS2的网络配置

[root@RS2 ~]# hostname -I
10.0.0.33 
[root@RS2 ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         10.0.0.100      0.0.0.0         UG    100    0        0 ens33
10.0.0.0        0.0.0.0         255.255.255.0   U     100    0        0 ens33

[root@RS2 ~]# yum install -y httpd
root@RS2 ~]# systemctl enable --now httpd
[root@RS2 ~]# echo RS2 > /var/www/html/index.html
[root@RS2 ~]# hostname -I >> /var/www/html/index.html
[root@RS2 ~]# cat /var/www/html/index.html 
RS2
10.0.0.33 
[root@RS2 ~]# curl 10.0.0.22
RS1
10.0.0.22 
[root@RS2 ~]# 

2、后端RS的IPVS的配置

#RS1的IPVS配置

[root@RS1 ~]# echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore 
[root@RS1 ~]# echo 2 > /proc/sys/net/ipv4/conf/lo/arp_ignore 
[root@RS1 ~]# echo 1 > /proc/sys/net/ipv4/conf/all/arp_announce 
[root@RS1 ~]# echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce 
[root@RS1 ~]# ifconfig lo:1 10.0.0.200/32
[root@RS1 ~]# ip a 
1: lo:  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 10.0.0.200/0 scope global lo:1
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: ens33:  mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 00:0c:29:a9:11:6e brd ff:ff:ff:ff:ff:ff
    inet 10.0.0.22/24 brd 10.0.0.255 scope global noprefixroute ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::fbf3:9a70:f1bb:88ed/64 scope link dadfailed tentative noprefixroute 
       valid_lft forever preferred_lft forever
    inet6 fe80::63d5:8547:bef8:27bc/64 scope link dadfailed tentative noprefixroute 
       valid_lft forever preferred_lft forever
    inet6 fe80::2097:51de:521a:1cdb/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever
[root@RS1 ~]# 

#RS2的IPVS配置

[root@RS2 ~]# echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore 
[root@RS2 ~]# echo 2 > /proc/sys/net/ipv4/conf/lo/arp_ignore 
[root@RS2 ~]# echo 1 > /proc/sys/net/ipv4/conf/all/arp_announce
[root@RS2 ~]# echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce 
[root@RS2 ~]# ifconfig lo:1 10.0.0.200/32
[root@RS2 ~]# ip a 
1: lo:  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 10.0.0.200/0 scope global lo:1
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: ens33:  mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 00:0c:29:ff:d7:7d brd ff:ff:ff:ff:ff:ff
    inet 10.0.0.33/24 brd 10.0.0.255 scope global noprefixroute ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::63d5:8547:bef8:27bc/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever
[root@RS2 ~]# 

3、LVS主机的配置

#在LVS上添加VIP
[root@LVS ~]# ifconfig lo:1 10.0.0.200/32
[root@LVS ~]# ip a 
1: lo:  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 10.0.0.200/0 scope global lo:1
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: ens33:  mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 00:0c:29:92:c1:38 brd ff:ff:ff:ff:ff:ff
    inet 10.0.0.11/24 brd 10.0.0.255 scope global noprefixroute ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::63d5:8547:bef8:27bc/64 scope link dadfailed tentative noprefixroute 
       valid_lft forever preferred_lft forever
    inet6 fe80::fbf3:9a70:f1bb:88ed/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever
[root@LVS ~]# 


#实现LVS规则
[root@LVS ~]# yum install -y ipvsadm
[root@LVS ~]# ipvsadm -A -t 10.0.0.200:80 -s rr
[root@LVS ~]# ipvsadm -a -t 10.0.0.200:80 -r 10.0.0.22:80 -g
[root@LVS ~]# ipvsadm -a -t 10.0.0.200:80 -r 10.0.0.33:80 -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  10.0.0.200:80 rr
  -> 10.0.0.22:80                 Route   1      0          0         
  -> 10.0.0.33:80                 Route   1      0          0         
[root@LVS ~]#

4、测试访问

root@client:~# curl 10.0.0.22
RS1
10.0.0.22 
root@client:~# curl 10.0.0.33
RS2
10.0.0.33 
root@client:~# 
root@client:~# curl 10.0.0.200
RS1
10.0.0.22 
root@client:~# curl 10.0.0.200
RS2
10.0.0.33 
root@client:~# curl 10.0.0.200
RS1
10.0.0.22 
root@client:~# curl 10.0.0.200
RS2
10.0.0.33 

[root@RS1 ~]# tail -f /var/log/httpd/access_log -n0
192.168.140.10 - - [05/Jun/2021:15:23:50 +0800] "GET / HTTP/1.1" 200 15 "-" "curl/7.58.0"
192.168.140.10 - - [05/Jun/2021:15:23:57 +0800] "GET / HTTP/1.1" 200 15 "-" "curl/7.58.0"

 

你可能感兴趣的:(LVS)