CentOS8 挂载网络磁盘方法(NFS)

《CentOS8 挂载网络磁盘方法(NFS)》

两台服务器,一个有主硬盘,另一个无主硬盘,通过使用共享网络硬盘的方式,使另一台服务也可以提供外部服务

服务端nfs

//安装nfs-utils, rpcbind
#yum install -y nfs-utils rpcbind

//设置rpcbind和nfs服务开机自动启动
#systemctl enable nfs-server  //服务名字不再是:nfs.service
#systemctl enable rpcbind.service

//启动rpcbind和nfs服务,顺序不要弄反,反了会出现类似program not registered的错误
#/bin/systemctl start rpcbind.service 
#/bin/systemctl start nfs-server

//创建共享目录(nfs提供的服务)
#mkdir /data/share

//编辑共享设置文件/etc/exports
#vi /etc/exports
文件后面添加如下内容:

//共享给192.168.0网段的主机
/data/share 192.168.0.0/24(ro,all_squash,no_subtree_check)

//*代表允许所有的网段访问(也可以使用具体的IP)
/data/share *(ro,all_squash,no_subtree_check)

//使用具体的IP
/data/share 192.168.0.100(ro,all_squash,no_subtree_check)

---------------------------
/storage80 192.168.0.0/24(ro,fsid=0,all_squash)
/storage80/storage 192.168.0.0/24(ro,fsid=1,all_squash)
/storage80/videos/video1 192.168.0.0/24(ro,fsid=2,all_squash)
/storage80/videos/video2 192.168.0.0/24(ro,fsid=3,all_squash)
---------------------------


------------------------------------------------------------
ro:该选项表示read only,客户端只允许读共享文件
rw:挂接此目录的客户端对该共享目录具有读写权限
sync:资料同步写入内存和硬盘
no_root_squash:root登入NFS主机,使用该共享目录时相当于该目录的拥有者,如果是root的话,那么对于这个共享的目录来说,他就具有root的权限,这个参数『极不安全』,不建议使用
all_squash:不论登入NFS的使用者身份为何,他的身份都会被压缩成为匿名使用者,通常也就是nobody(只有读权限)安全
no_subtree_check:不检查父目录的权限。
sync:将数据同步写入内存缓冲区与磁盘中,效率低,但可以保证数据的一致性;
async:将数据先保存在内存缓冲区中,必要时才写入磁盘;效率高
------------------------------------------------------------

//让配置文件生效
#exportfs -a

//查看是否共享成功
#showmount -e
----------------------------
Export list for localhost.localdomain:
/storage2 192.168.0.0/24


//防火墙放行!!!!!!!!!!!
//新增一个nfs区域
firewall-cmd --new-zone=nfs --permanent

//在nfs区域添加一个nfs,mountd,rpc-bind服务
firewall-cmd --zone=nfs --add-service=nfs --add-service=rpc-bind --add-service=mountd --permanent


//在nfs区域添加一个白名单
firewall-cmd --zone=nfs --add-source=192.168.0.100 --permanent
firewall-cmd --zone=nfs --add-source=192.168.0.110 --permanent

//重启
firewall-cmd --reload

//查看
firewall-cmd --list-all --zone=nfs
------------------------------------------------
nfs (active)
  target: default
  icmp-block-inversion: no
  interfaces: 
  sources: 192.168.0.100 192.168.0.110
  services: mountd nfs rpc-bind
  ports: 
  protocols: 
  forward: no
  masquerade: no
  forward-ports: 
  source-ports: 
  icmp-blocks: 
  rich rules: 


//重启服务器
#/bin/systemctl restart rpcbind.service 
#/bin/systemctl restart nfs-server

客户端

#yum install -y nfs-utils rpcbind
#systemctl enable nfs-server  //服务名字不再是:nfs.service
#systemctl enable rpcbind.service
#/bin/systemctl restart rpcbind.service 
#/bin/systemctl restart nfs-server

//查看是否可以探测到服务器的共享 (192.168.0.90是服务端)
#showmount -e 192.168.0.90

//挂载(独立的盘都需要挂载)
#mount -t nfs -o vers=3 -o proto=tcp -o nolock -o ro,soft,timeo=8,retry=3 192.168.0.90:/storage90 /storage90
#mount -t nfs -o vers=3 -o proto=tcp -o nolock -o ro,soft,timeo=8,retry=3 192.168.0.80:/storage80 /storage80


timeo的单位是0.1秒,配置为30就是隔3秒客户端向服务器端请求。
ro/rw:挂载文件系统为只读(ro)或读写(rw)模式。
hard/soft:指定NFS客户端与服务器端之间通信的方式,hard表示使用强制方式,遇到超时等异常情况会重试(阻塞),而soft则表示使用软方式,遇到超时等异常情况会放弃并报错。
vers:指定NFS协议版本,常用的有v3、v4等版本。
timeo:设置NFS客户端在重试请求之前等待的秒数(单位0.1s)。
rsize/wsize:设置读取和写入的数据块大小,rsize表示读取的块大小,wsize表示写入的块大小。
noac:禁用客户端的缓存,每次访问文件都会从服务器读取最新的版本。(文件不同步主要是由这个原因造成)
intr:允许中断操作,即允许用户通过Ctrl+C等方式中断NFS操作。

常见问题

//当从机挂载时出现以下报错时:
#mount -t nfs -o vers=3 -o proto=tcp -o nolock -o ro,soft,timeo=8,retry=3 192.168.0.90:/storage90 /storage90
----------------------------------------
mount.nfs: Stale file handle
这是因为(主机重启)硬盘突然与客户端断开连接,可能导致mount.nfs: Stale file handle问题
这时需要重启主机(服务端)的nfs-server , 注意不是客户端
#systemctl status nfs-server.service

nfs与sftp性能对比

一直都在纠结,nfs和sftp到底哪个性能更好,个人感觉是nfs更好,下面来做个对比测试:

//nfs测试2万次,图片是否存在 160.90297698975ms,156.73494338989ms
平均用时:167.9379940033ms
$path='6319/1/5/22_147587.jpg';
for ($i=0;$i<20000;$i++){
	if($this->getComicExists($path)){}
}

//sftp 测试2万次,图片是否存在 用时5867.2170639038ms,5154.1180610657ms
 平均用时 4148.551940918ms
$path='6319/1/5/22_147587.jpg';
for ($i=0;$i<20000;$i++){
	if($this->getComicExists($path,'comic_back')){}
}

性能差距将近24倍, nfs完胜!!
可能两都差距就在于sftp需要用户认证,而nfs不需要每次都认证


点赞

发表评论

邮箱地址不会被公开。 必填项已用*标注