Linux作业(8)——实现NFS文件共享及LVS-DR模式负载
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的均衡负载,提高了主机的利用率。简而言之就是“ 能者多劳 ”模式。
-
关闭防火墙及selinux
-
配置各主机
- CLIENT
...
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
- ROUTER
##网卡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
- LVS
...
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
- RS1
DR模式下各主机都需要配置一个相同的VIP,所以会造成地址冲突。解决地址冲突的方式有三种:
(1) 在前端网关做静态绑定
(2) 在各RS使用arptables
(3) 在各RS修改内核参数,来限制arp响应和通告的级别
这里我们直接修改内核参数,抑制各主机的ARP报文响应以及向网络中通告。即当网络中的相同IP的主机不发送也不回应ARP广播报文
- arp_ignore
- 0:默认值,表示可使用本地任意接口上配置的任意地址进行响应
- 1:仅在请求的目标IP配置在本地主机的接收到请求报文的接口上时,才给予响应
- arp_announce
- 0:默认值,把本机所有接口的所有信息向每个接口的网络进行通告
- 1:尽量避免将接口信息向非直接连接网络进行通告
- 2:必须避免将接口信息向非本网络进行通告
...
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
- RS2
...
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
- NFS-server
[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文件,待会客户端验证
-
NFS-client1
[root@c7_109 ~]# yum install -y nfs-utils httpd [root@c7_109 ~]# systemctl start httpd [root@c7_109 ~]# mount -t nfs 10.0.0.108:/share/html /var/www/html##远程挂载nfs共享目录 [root@c7_109 ~]# curl 10.0.0.109##挂载后验证本机的web服务网页,可以看到显示的是刚才nfs服务器创 建的index文件 10.0.0.108 [root@c7_109 ~]# cd /var/www/html/ [root@c7_109 html]# ls index.html [root@c7_109 html]# cat index.html 10.0.0.108 [root@c7_109 html]# touch "file`hostname -I`"##创建文件,验证目录权限 [root@c7_109 html]# ls file10.0.0.109 index.html ###可以看到只有两个文件,现在去NFS-client2创建一个文件 [root@c7_110 html]# touch "file`hostname -I`" [root@c7_110 html]# ls file10.0.0.109 file10.0.0.110 index.html ###回到client1可以看到新文件 [root@c7_109 html]# ls file10.0.0.109 file10.0.0.110 index.html
- 实现自动挂载
[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修改。
- NFS-server
[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)
- NFS-client1
[root@c7_109 /]# yum install -y autofs [root@c7_109 /]# mkdir -pv /share/html mkdir: created directory '/share' mkdir: created directory '/share/html' [root@c7_109 /]# vim /etc/auto.master ###绝对路径法配置 ... /- /etc/auto.html ... [root@c7_109 /]# vim /etc/auto.html /share/html -fstype=nfs,rw 10.0.0.108:/share/html ###验证 [root@c7_109 /]# df Filesystem 1K-blocks Used Available Use% Mounted on devtmpfs 919488 0 919488 0% /dev tmpfs 931520 0 931520 0% /dev/shm tmpfs 931520 9796 921724 2% /run tmpfs 931520 0 931520 0% /sys/fs/cgroup /dev/mapper/centos-root 104806400 1893752 102912648 2% / /dev/mapper/centos-data 98509828 32992 98476836 1% /data /dev/sda1 2086912 153520 1933392 8% /boot tmpfs 186304 0 186304 0% /run/user/0 ###当前处于根目录下,共享文件目录并没有在使用,所以看不到挂载信息 [root@c7_109 /]# cd /share/html/ [root@c7_109 html]# df Filesystem 1K-blocks Used Available Use% Mounted on devtmpfs 919488 0 919488 0% /dev tmpfs 931520 0 931520 0% /dev/shm tmpfs 931520 9796 921724 2% /run tmpfs 931520 0 931520 0% /sys/fs/cgroup /dev/mapper/centos-root 104806400 1893732 102912668 2% / /dev/mapper/centos-data 98509828 32992 98476836 1% /data /dev/sda1 2086912 153520 1933392 8% /boot tmpfs 186304 0 186304 0% /run/user/0 10.0.0.108:/share/html 104806400 1487360 103319040 2% /share/html ###进入挂载点,可以看到远程目录被挂载在了客户端的/share/html目录 [root@c7_109 html]# ls 123 1232 file10.0.0.109 file10.0.0.110 index.html