NFS客户端mount挂载优化

本人花费半年的时间总结的《Java面试指南》已拿腾讯等大厂offer,已开源在github ,欢迎star!

本文GitHub https://github.com/OUYANGSIHAI/JavaInterview 已收录,这是我花了6个月总结的一线大厂Java面试总结,本人已拿大厂offer,欢迎star

原文链接:blog.ouyangsihai.cn >> NFS客户端mount挂载优化

NFS客户端mount挂载优化

NFS作用

NFS服务可以让不同的客户端挂载使用同一个上目录,作为共享存储使用,这样可以保证不同节点的客户端数据的一致性,在集群环境中经常用到

NFS的优点

1、简单——容易上手,学习

2、方便——安装快速,后期维护简单

3、可靠——从软件层面上看,数据可靠性高

NFS服务安装成功并能使用之后,客户端挂载时也会加上一些参数进行优化

NFS挂载优化参数

1、有关系统安全的挂载优化参数

[root@localhost ~]# **cat /etc/redhat-release **

CentOS release 6.5 (Final)

检查测试环境

[root@localhost ~]# showmount -e 192.168.1.2

Export list for 192.168.1.2:

/data/bbs 192.168.1.3

[root@localhost ~]# **mount -t nfs -o rw,sync,nosuid,noexec 192.168.1.2:/data/bbs /mnt                                               **

[root@localhost ~]# df -h

Filesystem                    Size  Used Avail Use% Mounted on

/dev/mapper/VolGroup-lv_root   18G  3.3G   14G  20% /

tmpfs                         491M     0  491M   0% /dev/shm

/dev/sda1                     485M   35M  426M   8% /boot

192.168.1.2:/data/bbs          50G  3.5G   44G   8% /mnt

[root@localhost mnt]# echo ‘echo pwd‘/mnt/text.sh

[root@localhost mnt]# sh /mnt/text.sh

/mnt

[root@localhost mnt]#** ./text.sh      **

-bash: ./text.sh: Permission denied

经测试发现noexec参数只能禁止二进制程序执行,shell程序依然可执行

2、性能优化参数

首先卸载原来的挂载

[root@localhost ~]# umount /mnt

[root@localhost ~]# df -h

Filesystem                    Size  Used Avail Use% Mounted on

/dev/mapper/VolGroup-lv_root   18G  3.3G   14G  20% /

tmpfs                         491M     0  491M   0% /dev/shm

/dev/sda1                     485M   35M  426M   8% /boot

[root@localhost ~]#** grep mnt /proc/mounts **

192.168.1.2:/data/bbs /mnt nfs4 rw,nosuid,noexec,relatime,vers=4,rsize=262144,wsize=262144,namlen=255,hard,proto=tcp,port=0,timeo=600,retrans=2,sec=sys,clientaddr=192.168.1.3,minorversion=0,local_lock=none,addr=192.168.1.2 0 0

可以看出一些系统默认的参数

[root@localhost ~]# mount -t nfs -o rw,sync,nosuid,noexec,**noatime,rsize=65536,wsize=65536 **192.168.1.2:/data/bbs /mnt 

** 不同步更新文件系统访问inode时间,提高I/O性能,最大读写       **

[root@localhost ~]# df -h

Filesystem                    Size  Used Avail Use% Mounted on

/dev/mapper/VolGroup-lv_root   18G  3.3G   14G  20% /

tmpfs                         491M     0  491M   0% /dev/shm

/dev/sda1                     485M   35M  426M   8% /boot

192.168.1.2:/data/bbs          50G  3.5G   44G   8% /mnt

下面用一个脚本来测试参数的效果

[root@localhost ~]# ls /bin/touch -lsh

52K -rwxr-xr-x. 1 root root 52K Nov 22  2013 /bin/touch

测试批量拷贝/bin/touch这个文件到远程服务器端

[root@Centos bbs]# ls|wc -l

4999

文件拷贝成功

[root@localhost mnt]#** time for((i=1;i5000;i++));do /bin/cp /bin/touch /mnt/touch$i;done  **

real    1m39.245s

user    0m2.570s

sys     0m11.889s

经过测试拷贝/bin/touch这个文件到远程服务器端用时1m39.245s

接下来我们测试下不加参数的拷贝时间

[root@localhost ~]# umount /mnt

[root@localhost ~]# **mount -t nfs -o rw,sync,nosuid,noexec,rsize=65536,wsize=65536 192.168.1.2:/data/bbs /mnt              **

[root@localhost ~]# df -h

Filesystem                    Size  Used Avail Use% Mounted on

/dev/mapper/VolGroup-lv_root   18G  3.3G   14G  20% /

tmpfs                         491M     0  491M   0% /dev/shm

/dev/sda1                     485M   35M  426M   8% /boot

192.168.1.2:/data/bbs          50G  3.5G   44G   8% /mnt

[root@localhost ~]# time for((i=1;i5000;i++));do /bin/cp /bin/touch /mnt/toucx$i;done

real    0m45.965s

user    0m2.435s

sys     0m6.907s

经过测试拷贝/bin/touch这个文件到远程服务器端用时0m45.965s,不加参数反而更快了,上面所有测试数据由于虚拟机的硬件及其它因素可能会与实际环境有所差别,如需真正的数据需到生产环境测试

3、自动挂载autofs

此参数经常用于内部测试环境中,客户不需挂载目录到本地,一旦客户访问服务端,它才会自动挂载到本地,一定时间后会自动断开

首先看下服务端的配置

[root@Centos bbs]#** cat /etc/exports **

#####config for nfs-server 2016-8-21

/data/bbs       192.168.1.3(rw,sync,all_squash,anonuid=3000,anongid=3000)

客户端的配置如下

首先需要开启autofs服务

[root@localhost ~]# **umount /mnt  **        先卸载掉原来的挂载

[root@localhost ~]# df -h

Filesystem                    Size  Used Avail Use% Mounted on

/dev/mapper/VolGroup-lv_root   18G  3.3G   14G  20% /

tmpfs                         491M     0  491M   0% /dev/shm

/dev/sda1                     485M   35M  426M   8% /boot

[root@localhost ~]# /etc/init.d/autofs start

Starting automount: automount: program is already running.   [  OK  ]

autofs两个组件:/etc/auto.master   /etc/auto.misc

[root@localhost ~]#** vi /etc/auto.master **

Sample auto.master file

This is an automounter map and it has the following format

key [ -mount-options-separated-by-comma ] location

For details of the format look at autofs(5).

/misc   /etc/auto.misc

/mnt    /etc/auto.misc –timeout 60

NOTE: mounts done from a hosts map will be mounted with the

“nosuid” and “nodev” options unless the “suid” and “dev”

options are explicitly given.

[root@localhost ~]# vi /etc/auto.misc

This is an automounter map and it has the following format

key [ -mount-options-separated-by-comma ] location

Details may be found in the autofs(5) manpage

the following entries are samples to pique your imagination

#linux          -ro,soft,intr           ftp.example.org:/pub/linux

#boot           -fstype=ext2            :/dev/hda1

#floppy         -fstype=auto            :/dev/fd0

#floppy         -fstype=ext2            :/dev/fd0

#e2floppy       -fstype=ext2            :/dev/fd0

#jaz            -fstype=ext2            :/dev/sdc1

#removable      -fstype=ext2            :/dev/hdd

nfsfile          -fstype=nfs             192.168.1.2:/data/bbs

在/mnt下新建一个nfsfile文件夹,-fstype=nfs         192.168.1.2:/data/bbs

[root@localhost ~]# /etc/init.d/autofs restart

Stopping automount:                                        [  OK  ]

Starting automount:                                        [  OK  ]

[root@localhost ~]# /etc/init.d/autofs status

automount (pid  12307) is running…

下面测试自动挂载情况

[root@localhost ~]# df -h

Filesystem                    Size  Used Avail Use% Mounted on

/dev/mapper/VolGroup-lv_root   18G  3.3G   14G  20% /

tmpfs                         491M     0  491M   0% /dev/shm

/dev/sda1                     485M   35M  426M   8% /boot

[root@localhost ~]# cd /mnt

[root@localhost mnt]# ls

[root@localhost mnt]# cd nfsfile

[root@localhost nfsfile]#** ls**

[root@localhost nfsfile]# touch 123.txt

[root@localhost nfsfile]# df -h

Filesystem                    Size  Used Avail Use% Mounted on

/dev/mapper/VolGroup-lv_root   18G  3.3G   14G  20% /

tmpfs                         491M     0  491M   0% /dev/shm

/dev/sda1                     485M   35M  426M   8% /boot

192.168.1.2:/data/bbs          50G  3.5G   44G   8% /mnt/nfsfile

客户访问NFS共享目录时,才会自动挂载

[root@Centos bbs]# **ls -ll    **          服务端共享目录

total 0

-rw-r–r–. 1 nfsuser nfsuser 0 Aug 26 06:57 123.txt

到一定时间后就会自动断开(60S)

[root@localhost ~]# date

Sat Aug 27 14:01:53 SYOT 2016

[root@localhost ~]# date

Sat Aug 27 14:03:00 SYOT 2016

[root@localhost ~]# df -h

Filesystem                    Size  Used Avail Use% Mounted on

/dev/mapper/VolGroup-lv_root   18G  3.3G   14G  20% /

tmpfs                         491M     0  491M   0% /dev/shm

/dev/sda1                     485M   35M  426M   8% /boot

客户挂载自动断开

补充说明

真正的企业生产环境的存储服务器都是内网环境,都无需防火墙,因此可以不配置,如果需要配置的话,有以下两种方法
1、允许IP段访问

iptables -A INPUT -s 192.168.1.0/24 -j ACCEPT

2、允许IP段加端口访问

iptables -A INPUT -i eth0 -p tcp -s 192.168.1.0/24 –dprot 111 -j ACCEPT

iptables -A INPUT -i eth0 -p udp -s 192.168.1.0/24 –dprot 111 -j ACCEPT

iptables -A INPUT -i eth0 -p udp -s 192.168.1.0/24 –dprot 2049 -j ACCEPT  

iptables -A INPUT -i eth0 -p udp -s 192.168.1.0/24  -j ACCEPT                                                         NFS客户端mount挂载优化

本人花费半年的时间总结的《Java面试指南》已拿腾讯等大厂offer,已开源在github ,欢迎star!

本文GitHub https://github.com/OUYANGSIHAI/JavaInterview 已收录,这是我花了6个月总结的一线大厂Java面试总结,本人已拿大厂offer,欢迎star

原文链接:blog.ouyangsihai.cn >> NFS客户端mount挂载优化


 上一篇
Linux系统开机自动挂载文件fstab介绍 Linux系统开机自动挂载文件fstab介绍
Linux系统开机自动挂载文件fstab介绍 **一、fstab简介 ** fstab文件中包含了各种各样的文件系统描述信息,它可以被特殊的工具修改,fstab中每一个文件系统描述占一行,每一行是TAB或空格分隔。 二、fstab文件内容介
2021-04-05
下一篇 
Linux系统SSH服务之 sshkey密钥认证实战 Linux系统SSH服务之 sshkey密钥认证实战
Linux系统SSH服务之 sshkey密钥认证实战 前面介绍了SSH服务一些特性及其简单配置,在实际的生产环境中,经常会用到sshkey密钥认证实行数据分发数据等操作,还可以批量操作内网服务器,实行免密认证进行推送分发数据 实际生产结构拓
2021-04-05