NFS实质便是一个共享存储,文件做事器。
一、NFS基本概述
NFS是Network File System的缩写即网络文件系统。NFS紧张功能是通过局域网络让不同的主机系统之间可以共享文件或目录。

NFS系统和Windows网络共享、网络驱动器类似, 只不过windows用于局域网, NFS用于企业集群架构中, 如果是大型网站, 会用到更繁芜的分布式文件系统glusterfs(大文件ISO镜像等),ceph,oss(阿里云的存储系统)
为什么利用NFS
1.实现多台做事器之间数据共享
2.实现多台做事器之间数据同等
二、NFS运用
1)没有NFS时:
1.A用户上传图片经由负载均衡,负载均衡将上传要求调度至WEB1做事器上。
2.B用户访问A用户上传的图片,此时B用户被负载均衡调度至WEB2上,由于WEB2上没有这张图片,以是B用户无法看到A用户传的图片。
2)如果有NFS
1.A用户上传图片无论被负载均衡调度至WEB1还是WEB2, 终极数据都被写入至共享存储
2.B用户访问A用户上传图片时,无论调度至WEB1还是WEB2,终极都会上共享存储访问对应的文件,这样就可以访问到资源了
3)NFS事情事理
1.用户访问NFS客户端,将要求转化为函数
2.NFS通过tcp/ip连接做事器
3.NFS做事端吸收要求,会先调用portmap进程进行端口映射
4.Rpc.nfsd进程用于判断NFS客户端能否连接到做事端
5.Rpc.mount进程用于判断客户端对做事真个操作权限
6.如果通过权限验证,可以对做事端进行操作,修正或读取等
三、NFS实践
1、环境准备:
主机 ip 角色
web01 172.16.1.7 NFS客户端
NFS 172.16.1.31 NFS做事端
2、做事端(172.16.1.31)
1)关闭防火墙与selinux
2)安装NFS和rpcbind
[root@nfs ~]# yum install -y nfs-utils rpcbind
把稳:
centos6须要安装rpcbind
centos7默认已经安装好了rpcbind,并且默认开机自启动
3)配置nfs
#nfs的默认配置文件是:/etc/exports#配置nfs配置文件vim /etc/exports/data 172.16.1.0/24(rw sync all_squash)
语法拆分:
/data #nfs做事真个共享目录
172.16.1.0/24 nfs许可连接的客户端ip
(rw sync all_squash) 许可操作的权限
4)创建共享目录
[root@nfs ~]# mkdir /data
5)启动做事
#centos7启动
[root@nfs ~]# systemctl start rpcbind nfs
#centos6启动时一定要先启动rpcbind,在启动nfs
[root@nfs ~]# /etc/init.d/rpcbind start
[root@nfs ~]# /etc/init.d/nfs start
或
[root@nfs ~]# service rpcbind start
[root@nfs ~]# service nfs start
#验证启动
[root@nfs ~]# netstat -ntlp | grep rpc
tcp 0 0 0.0.0.0:36234 0.0.0.0: LISTEN 2041/rpc.statd
tcp 0 0 0.0.0.0:111 0.0.0.0: LISTEN 2030/rpcbind
tcp 0 0 0.0.0.0:20048 0.0.0.0: LISTEN 2067/rpc.mountd
tcp6 0 0 :::46149 :: LISTEN 2041/rpc.statd
tcp6 0 0 :::111 :: LISTEN 2030/rpcbind
tcp6 0 0 :::20048 :: LISTEN 2067/rpc.mountd
6)验证nfs配置
[root@nfs ~]# cat /var/lib/nfs/etab
/data172.16.1.0/24(rw,sync,wdelay,hide,nocrossmnt,secure,root_squash,all_squash,no_subtree_check,secure_locks,acl,no_pnfs,anonuid=65534,anongid=65534,sec=sys,rw,secure,root_squash,all_squash)
#涌现以上内容即为配置成功
3、客户端
1)关闭防火墙与selinux
2)安装rpcbind做事
[root@web01 ~]# yum install -y rpcbind nfs-utils
#把稳:
centos6须要安装rpcbind
centos7默认已经安装好了rpcbind,并且默认开机自启动
安装rpcbind是为了连接做事真个进程,安装nfs-utils是为了利用showmount命令
3)查看挂载点
[root@web01 ~]# showmount -e 172.16.1.31
Export list for 172.16.1.31:
/data 172.16.1.0/24
4)挂载
[root@web01 ~]# mount -t nfs
172.16.1.31:/data /backup
命令拆分:
mount #挂载命令
-t #指定挂载的文件类型
nfs #nfs文件类型
172.16.1.31: #做事真个IP地址
/data 做事端供应的可供挂载的目录
/backup 本地要挂载到做事真个目录
[root@web01 ~]# df -h #查看挂载结果
Filesystem Size Used Avail Use% Mounted on
devtmpfs 979M 0 979M 0% /dev
tmpfs 991M 0 991M 0% /dev/shm
tmpfs 991M 9.5M 981M 1% /run
tmpfs 991M 0 991M 0% /sys/fs/cgroup
/dev/mapper/centos-root 18G 3.1G 15G 17% /
/dev/sda1 1014M 163M 852M 17% /boot
tmpfs 199M 0 199M 0% /run/user/0
172.16.1.31:/data 18G 3.1G 15G 18% /backup
5.写入数据进行测试
#若没有授权,没有客户端权限写入
[root@web01 ~]# touch /backup/123.txt
touch: cannot touch ‘/backup/123.txt’: Permission denied
#做事端授权
[root@nfs ~]# chown -R nfsnobody.nfsnobody /data
#客户端再次写入即可
[root@web01 ~]# touch /backup/123.txt
#做事端查看,若文件相同同步完成
[root@nfs ~]# ls /data
123.txt
四、nfs挂载与卸载
NFS客户真个配置步骤也十分大略。先利用showmount命令,查询NFS做事器的远程共享信息,其输出格式为“共享的目录名称 许可利用客户端地址
NFS挂载:客户真个目录仅仅是做事端共享目录的一个入口,真正的数据全都是在做事真个目录,客户顿写入的数据也是在做事器存储的
把稳事变:
1.挂载目录后,原文件里的内容并没有消逝,只是被遮盖(像藏起来了)取消挂载后仍旧存在
2.取消挂载时,不要在挂载目录下操作目录本身,否则会提示操作劳碌,切换到其他目录在进行卸载即可
3.挂载时如果在挂载目录下,还是可以看到挂载前的目录下文件
,须要重新进入目录才会显示挂在后的目录内容
开机挂载(此处一定要仔细,初学者慎用)
如果希望NFS文件共享做事能一贯有效,则须要将其写入到客户端fstab文件中
#编辑fstab文件
[root@web01 ~]# vim /etc/fstab
172.16.1.31:/data /nfsdir nfs defaults 0 0
#验证fstab是否写精确
[root@web01 ~]# mount -a
1
2
3
4
5
6
卸载:
#卸载的两种办法
[root@web01 ~]# umount /backup
[root@web01 ~]# umount 172.16.1.31:/data
#逼迫卸载(一样平常不建议利用,特定场景下利用)
[root@web01 ~]# umount -lf /backup
nfs配置详解
[root@nfs ~]# cat /etc/exports
/data 172.16.1.0/24(rw,sync,all_squash)
五、NFS案例1、backup
#安装rsync[root@backup ~]# yum install rsync -y[root@backup ~]# yum install -y nfs-utils rpcbind#编写rsync做事端配置文件[root@backup ~]# vim /etc/rsyncd.confuid = rsyncgid = rsync port = 873 use chroot = nofake super = yesmax connections = 200timeout = 600ignore errors read only = falselist = false auth users = yzlsecrets file = /etc/rsync.passwdlog file = /var/log/rsyncd.log[backup]comment = welcome to oldboyedu backup!path = /backup#创建rsync做事须要利用的普通用户[root@backup ~]# useradd rsync#将用户名和密码写入rsync密码文件[root@backup ~]# echo "yzl:1" >/etc/rsync.passwd#将做事端密码文件权限设置为600[root@backup ~]# chmod 600 !$#创建模块目录,并将其属组、属主变动为普通用户[root@backup ~]# mkdir /backup#授权[root@backup ~]# chown -R rsync.rsync !$[root@backup ~]# vim /etc/exports/backup 172.16.1.0/24(rw,sync,all_squash)[root@backup ~]# systemctl start nfs#启动rsync做事端[root@backup ~]# systemctl start rsyncd
2、nfs
#创建rsync客户端密码文件为其设置600权限[root@nfs ~]# echo "1" >>/etc/rsync.passwd[root@nfs ~]# chmod 600 !$#安装nfs、rpcbind工具[root@nfs ~]# yum install -y nfs-utils rpcbind#编辑nfs配置文件[root@nfs ~]# vim /etc/exports/sersync 172.16.1.0/24(rw,sync,all_squash)#创建可供挂载的nfs目录[root@nfs ~]# mkdir /sersync[root@nfs ~]# chown -R nfsnobody.nfsnobody /sersync[root@nfs ~]# systemctl start nfs#上传sersync压缩包[root@nfs sersync]# rz -Erz waiting to receive.[root@nfs sersync]# tar -xf sersync.gz [root@nfs sersync]# mv GNU-Linux-x86/ ./[root@nfs GNU-Linux-x86]# vim confxml.xml <?xml version="1.0" encoding="ISO-8859-1"?><head version="2.5"> <host hostip="localhost" port="8008"></host> <debug start="false"/> <fileSystem xfs="false"/> <filter start="false"><exclude expression="(.)\.svn"></exclude><exclude expression="(.)\.gz"></exclude><exclude expression="^info/"></exclude><exclude expression="^static/"></exclude> </filter> <inotify><delete start="true"/><createFolder start="true"/><createFile start="true"/><closeWrite start="true"/><moveFrom start="true"/><moveTo start="true"/><attrib start="true"/><modify start="true"/> </inotify> <sersync><localpath watch="/backup"> <remote ip="172.16.1.41" name="backup"/> <!--<remote ip="192.168.8.39" name="tongbu"/>--> <!--<remote ip="192.168.8.40" name="tongbu"/>--></localpath><rsync> <commonParams params="-az"/> <auth start="true" users="yzl" passwordfile="/etc/rsync.passwd"/> <userDefinedPort start="false" port="873"/><!-- port=874 --> <timeout start="false" time="100"/><!-- timeout=100 --> <ssh start="false"/></rsync><failLog path="/tmp/rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execute once--><crontab start="false" schedule="600"><!--600mins--> <crontabfilter start="false"><exclude expression=".php"></exclude><exclude expression="info/"></exclude> </crontabfilter></crontab><plugin start="false" name="command"/> </sersync> <plugin name="command"><param prefix="/bin/sh" suffix="" ignoreError="true"/><!--prefix /opt/tongbu/mmm.sh suffix--><filter start="false"> <include expression="(.)\.php"/> <include expression="(.)\.sh"/></filter> </plugin> <plugin name="socket"><localpath watch="/opt/tongbu"> <deshost ip="192.168.138.20" port="8009"/></localpath> </plugin> <plugin name="refreshCDN"><localpath watch="/data0/htdocs/cms.xoyo.com/site/"> <cdninfo domainname="ccms.chinacache.com" port="80" username="xxxx" passwd="xxxx"/> <sendurl base="http://pic.xoyo.com/cms"/> <regexurl regex="false" match="cms.xoyo.com/site([/a-zA-Z0-9]).xoyo.com/images"/></localpath> </plugin></head>[root@nfs sersync]# ./sersync2 -dro confxml.xml
web1和web2
[root@web01 ~]# yum install -y rpcbind nfs-utils[root@web01 ~]# yum install -y httpd php[root@web01 ~]# systemctl start httpd[root@web01 ~]# cd /var/www/html[root@web01 ~]# mount -t nfs 172.16.1.31:/sersync /var/www/html[root@web01 html]# rz -Erz waiting to receive.[root@web01 html]# unzip kaoshi.zip [root@web01 html]# systemctl restart httpd网站测试并访问[root@web01 ~]# vim ping.sh#!/bin/bashping -c1 172.16.1.31 >/dev/null if [ $? -ne 0 ];then umount -lf /var/www/html && mount -t nfs 172.16.1.41:/backup /var/www/html/else df -h | grep 172.16.1.31 >/dev/null if [ $? -ne 0 ];then umount -lf /var/www/html && mount -t nfs 172.16.1.31:/sersync /var/www/html fifi[root@web01 ~]# chmod +x ping.sh [root@web01 ~]# crontab -e /var/www/html/ping.sh
NFS小结
1.NFS存储优点
1)NFS文件系统大略易用、方便支配、数据可靠、做事稳定、知足中小企业需求。
2)NFS文件系统内存放的数据都在文件系统之上,所有数据都是能看得见。
2.NFS存储局限
1)存在单点故障, 如果构建高可用掩护麻烦web -> nfs -> backup
2)NFS数据明文, 并不对数据做任何校验。
3)客户端挂载NFS做事没有密码验证, 安全性一样平常(内网利用)
3.NFS运用建议
1)生产场景应将静态数据尽可能往前端推, 减少后端存储压力
2)必须将存储里的静态资源通过CDN缓存jpg\png\mp4\avi\css\js
3)如果没有缓存或架构本身历史遗留问题太大, 在多存储也无用
摘录来源:givenchy_yzl博主