Linux作业(8)——实现NFS文件共享及LVS-DR模式负载

2020-11-08  本文已影响0人  羰基生物

1、DR模式下vip不在同一网段上实现过程



实际虚拟机环境略微不同:

主机名 IP 角色
CIP 192.168.10.6/24 Bridge CLIENT
ROUTER Internet:192.168.10.200/24 Bridge模式;Intranet:10.0.0.106/24 NAT模式 路由器
LVS 10.0.0.108/24 NAT LVS服务器
RS1 10.0.0.109/24 NAT Real Server 1
RS2 10.0.0.110/24 NAT Real Server 2
Virtual IP 172.16.0.100/32 LVS集群虚拟IP

LVS(Linux Virtual Server),根据request报文的destination IP 和目标协议及端口,选择对应调度算法把用户请求调度至各个real server,实现负载均衡。这里使用加权轮询算法(Weighted Round-Robin Scheduling WRR),带权重的轮询调度方式,能更好的考虑到不同服务器的处理能力,所以这种均衡算法能确保高性能的服务器得到更多的使用率,避免低性能的服务器负载过重。所以,在实际应用中比较常见。例如上面RS1主机配置较好,所以在配置权重的时候可以考虑设置一个较大的值,例如2,而RS1则设置一个小值为1,实现2:1的均衡负载,提高了主机的利用率。简而言之就是“ 能者多劳 ”模式。

...
IPADDR=192.168.10.6
NETMASK=255.255.255.0
GATEWAY=192.168.10.200
...

[root@client ~]# hostname -I
192.168.10.6 

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

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

[root@client ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.10.200  0.0.0.0         UG    100    0        0 ens33
192.168.10.0    0.0.0.0         255.255.255.0   U     100    0        0 ens33
##网卡1
...
IPADDR=10.0.0.106
NETMASK=255.255.255.0
GATEWAY=10.0.0.2
...
##网卡2
...
IPADDR=192.168.10.200
NETMASK=255.255.255.0
...

[root@router ~]# hostname -I
10.0.0.106 192.168.10.200 

[root@router ~]# ping 10.0.0.106 
PING 10.0.0.106 (10.0.0.106) 56(84) bytes of data.
64 bytes from 10.0.0.106: icmp_seq=1 ttl=64 time=0.014 ms

[root@router ~]# ping 192.168.10.200
PING 192.168.10.200 (192.168.10.200) 56(84) bytes of data.
64 bytes from 192.168.10.200: icmp_seq=1 ttl=64 time=0.021 ms

[root@router ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         0.0.0.0         0.0.0.0         U     0      0        0 ens33
0.0.0.0         10.0.0.2        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
192.168.10.0    0.0.0.0         255.255.255.0   U     101    0        0 ens37
...
IPADDR=10.0.0.108
NETMASK=255.255.255.0
GATEWAY=10.0.0.106
...

[root@lvs ~]# hostname -I
10.0.0.108 

[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=0.703 ms

[root@lvs ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         10.0.0.106      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 ~]# ifconfig lo:1 172.16.0.100/32
[root@lvs ~]# yum install -y ipvsadm
[root@lvs ~]# iptables -F
[root@lvs ~]# ipvsadm -A -t 172.16.0.100:80 -s wrr
[root@lvs ~]# ipvsadm -a -t 172.16.0.100:80 -r 10.0.0.109 -w 1
[root@lvs ~]# ipvsadm -a -t 172.16.0.100:80 -r 10.0.0.110 -w 1
[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.16.0.100:80 wrr
  -> 10.0.0.109:80                Route   1      0          0         
  -> 10.0.0.110:80                Route   1      0          0   

DR模式下各主机都需要配置一个相同的VIP,所以会造成地址冲突。解决地址冲突的方式有三种:
(1) 在前端网关做静态绑定
(2) 在各RS使用arptables
(3) 在各RS修改内核参数,来限制arp响应和通告的级别

这里我们直接修改内核参数,抑制各主机的ARP报文响应以及向网络中通告。即当网络中的相同IP的主机不发送也不回应ARP广播报文

...
IPADDR=10.0.0.109
NETMASK=255.255.255.0
GATEWAY=10.0.0.106
...

[root@rs1 ~]# hostname -I
10.0.0.109 

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

[root@rs1 ~]# echo 1 >   /proc/sys/net/ipv4/conf/all/arp_ignore
[root@rs1 ~]# echo 1 >   /proc/sys/net/ipv4/conf/lo/arp_ignore
[root@rs1 ~]# echo 2 >   /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 172.16.0.100/32
[root@rs1 ~]# yum install -y httpd
[root@rs1 ~]# echo "`hostname -I` RS1" >> /var/www/html/index.html
[root@rs1 ~]# systemctl start httpd

...
IPADDR=10.0.0.110
NETMASK=255.255.255.0
GATEWAY=10.0.0.106
...

[root@rs2 ~]# hostname -I
10.0.0.110 

[root@rs2 ~]# 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=0.387 ms

[root@rs2 ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         10.0.0.106      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 ~]# echo 1 >   /proc/sys/net/ipv4/conf/all/arp_ignore
[root@rs2 ~]# echo 1 >   /proc/sys/net/ipv4/conf/lo/arp_ignore
[root@rs2 ~]# echo 2 >   /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 172.16.0.100/32
[root@rs2 ~]# yum install -y httpd
[root@rs2 ~]# echo "`hostname -I` RS1" >> /var/www/html/index.html
[root@rs2 ~]# systemctl start httpd
###这是将权重设为1:1的测试结果,可以看到client的请求被LVS服务器调度到了RS1和RS2,得到了不同的结果,且比例是1:1。
[root@client~]# while :;do curl 172.16.0.100;sleep 1;done
10.0.0.109  RS1
10.0.0.110  RS2
10.0.0.109  RS1
10.0.0.110  RS2
10.0.0.109  RS1
10.0.0.110  RS2
###修改权重为3:1
[root@lvs ~]# ipvsadm -e -t 172.16.0.100:80 -r 10.0.0.109 -w 3
[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.16.0.100:80 wrr
  -> 10.0.0.109:80                Route   3      0          0         
  -> 10.0.0.110:80                Route   1      0          0      
[root@client~]# while :;do curl 172.16.0.100;sleep 1;done
10.0.0.110  RS2
10.0.0.109  RS1
10.0.0.109  RS1
10.0.0.109  RS1
10.0.0.110  RS2
10.0.0.109  RS1
10.0.0.109  RS1
10.0.0.109  RS1
###这是将权重设为3:1的测试结果,可以看到不同的结果,且比例是3:1。

2、CentOS7.6 中 nfs 客户端使用 /etc/fatab 实现开机自动挂载

虚拟机环境:

NFS-server:10.0.0.108 Centos7
NFS-client1:10.0.0.109 Centos7
NFS-client2:10.0.0.110 Centos7

[root@c7_108 ~]# yum install rpcbind nfs-utils
[root@c7_108 ~]# mkdir -p /share/html
[root@c7_108 ~]# chmod -R 755 /share/html
[root@c7_108 ~]# vim /etc/exports

/share/html 10.0.0.0/24(rw,no_root_squash)
[root@c7_108 ~]# systemctl start nfs
[root@c7_108 ~]# exportfs -v 
/share/html       10.0.0.0/24(sync,wdelay,hide,no_subtree_check,sec=sys,rw,secure,no_root_squash,no_all_squash)

[root@c7_108 ~]# echo "`hostname -I` >> /share/html/index.html###将主机地址写入index文件,待会客户端验证
 [root@c7_110 html]# vim /etc/fstab 
 10.0.0.108:/share/html /var/www/html nfs defaults 0 0
 [root@c7_110 html]# reboot
 ...
 ...
 ...
 Connecting to 10.0.0.110:22...
 Connection established.
 To escape to local shell, press Ctrl+Alt+].
 
 WARNING! The remote SSH server rejected X11 forwarding request.
 Last login: Sun Nov  8 17:22:31 2020 from 10.0.0.1
 [root@c7_110 html]# cd /var/www/html/
 [root@c7_110 html]# ls
 file10.0.0.109   file10.0.0.110   index.html

3、CentOS7.6 中 nfs 客户端使用 autofs 实现使用 nfs 时实时挂载

实时挂载,即需要的时候自动挂载,在空闲五分钟后又自动卸载。可以在/etc/autofs.conf修改。

 [root@c7_108 ~]# yum install rpcbind nfs-utils
 [root@c7_108 ~]# mkdir -p /share/html
 [root@c7_108 ~]# chmod -R 755 /share/html
 [root@c7_108 ~]# vim /etc/exports
 
 /share/html 10.0.0.0/24(rw,no_root_squash)
 [root@c7_108 ~]# systemctl start nfs
 [root@c7_108 ~]# exportfs -v 
 /share/html       10.0.0.0/24(sync,wdelay,hide,no_subtree_check,sec=sys,rw,secure,no_root_squash,no_all_squash)
上一篇 下一篇

猜你喜欢

热点阅读