NFS在CentOS7下的安装与部署
NFS概要
NFS的主要功能是通过网络让不同的机器系统之间可以彼此共享文件和目录
实际应用:在企业Web应用架构中,NFS网络文件系统一般用于存储共享的视频、图片、附件等静态资源,一般把网站上用户上传的文件都放在NFS共享里,然后前端所有节点访问这些静态资源时都会读取NFS上的资源
NFS安装
NFS所需要的两个包:nfs-utils和rpcbind:[root@localhost ~]# yum install -y nfs-utils rpcbind
检查是否安装成功(有显示软件名则表示安装成功,无显示表示失败):[root@localhost ~]# rpm -qa nfs-utils rpcbind
rpcbind服务
概要:
- rpcbind是一个RPC服务,用于统一管理NFS端口的服务,并且统一对外的端口是111;RPC管理服务器端的NFS端口分配,客户端要传数据时,客户端的RPC会先向服务器端的RPC要服务器的端口,要到端口之后再建立连接,然后传输数据
- NFS服务器端需要先启动RPC,再启动NFS,这样NFS才能到RPC去注册端口信息
- 如果RPC服务重新启动,原来已经注册好的NFS端口数据就会全部丢失,因此RPC服务管理的NFS程序也要重新启动以重新向RPC注册
检查服务启动状态:[root@localhost ~]# systemctl status rpcbind.service
rpcbind启动命令:[root@localhost ~]# systemctl start rpcbind.service
查看系统中启用的端口:[root@localhost ~]# netstat -lnt
检查rpc服务是否正常的显示信息:[root@localhost ~]# rpcinfo -p localhost
NFS服务
启动NFS
启动NFS:[root@localhost ~]# systemctl start nfs.service
(注:要先启动rpcbind再启动nfs)
查看服务启动的状态:[root@localhost ~]# systemctl status nfs.service
设置两个服务开机自启动:[root@localhost ~]# systemctl enable nfs.service[root@localhost ~]# systemctl enable rpcbind.service
CentOS以下版本命令:
chkconfig rpcbind on和chkconfig nfs on
查看NFS服务自启动状态:[root@localhost ~]#systemctl list-unit-files|grep enabled|grep rpcbind[root@localhost ~]#systemctl list-unit-files|grep nfs
CentOS7以下版本:
[root@localhost ~]# chkconfig --list | grep nfs
配置NFS
创建共享目录 :[root@localhost ~]# mkdir -p /data
修改权限,/data及子文件属主属组为nfsnobody:[root@localhost ~]# chown -R nfsnobody:nfsnobody /data
查看修改后的权限:[root@localhost ~]# ll -d /data
NFS服务配置文件路径为/etc/exports,默认为空,需添加:[root@localhost ~]# vi /etc/exports
添加以下内容:/data 192.168.100.0/24(rw,sync,all_squash)
重新加载以完成配置文件更新[root@localhost ~]# exportfs -r
CentOS7以下使用 :
/etc/init.d/nfs reload ===exportfs –r,s或者重新启动两个服务
查看共享目录状态:[root@localhost ~]# cat /var/lib/nfs/etab
挂载前检查本机有哪些共享的文件:[root@localhost ~]# showmount -e 192.168.100.2
创建要挂载到本机的目录:[root@localhost ~]# mkdir /mnt/nfs
将共享目录/data挂载到/mnt/目录:[root@localhost ~]# mount -t nfs 192.168.100.128:/data /mnt
使用df -h查看挂载情况:[root@localhost ~]# df -hs
检查结果,在/mnt目录下分别创建test目录及test.txt文件,如果在/data目录下能够正常访问,表明配置成功。
Windows下挂载NFS
- in+R打开运行窗口,输入
OptionalFeatures打开Windows功能窗口,找到并勾选上NFS服务,点击确定 - cmd查看远程服务器有哪些共享目录:
showmount -e 192.168.100.2 mount \\IP地址\nfs的目录 本地挂载点挂载NFS
总结
出现端口错误的情况,一般是由于NFS先于RPC启动,导致了RPC没有提供端口号给NFS;一定要记住先启动RPC再启动NFS

