天天看点

关于 Linux中NFS的一些笔记NFS

写在前面

  • 学习K8s的

    Volumes

    相关,遇到

    NFS挂载

    ,所以总结一下
  • 这里主要是实战,理论很少。
  • 为了方便,部分地方使用了

    ansible

    ,只用了

    shell

    模块,不影响阅读

亲近的人是不应该分开太久的。没见面的时候朝思暮想,可一旦见到,是否双方都会无可奈何地感觉到这条鸿沟呢?虽然可怕,但这也许更接近事实。——中岛敦《山月记》

*

NFS

NFS(Network File System, 网络文件系统),用来为客户机提供共享使用的文件夹;

将NFS服务器分享的目录,挂载到本地机器当中,本地NFS的客户端应用可以读写位于远端NFS服务器上的文件,在客户端端看起来,就像访问本地文件一样。

NFS本身的服务并没有提供数据传递的协议,而是通过使用 RPC(远程过程调用 Remote Procedure Call)来实现。当NFS启动后,会随机的使用一些端口,NFS就会向RPC去注册这些端口。RPC就会记录下这些端口,RPC会开启111端口。通过client端和sever端端口的连接来进行数据的传输。在启动nfs之前,首先要确保rpc服务启动。但是本质上还使用的TCP协议

使用NFS网络文件系统提供的共享目录存储数据时,我们需要在系统中部署一个NFSServer

服务端

下载服务需要的包,设置开机自启

┌──[[email protected]]-[~] #软件包:nfs-utils
└─$yum -y install nfs-utils.x86_64
┌──[[email protected]]-[~] #系统服务:nfs-server
└─$systemctl enable nfs-server.service  --now           

创建网络共享文件夹,写入测试信息

┌──[[email protected]]-[~]
└─$mkdir -p /liruilong
┌──[[email protected]]-[/liruilong]
└─$cd /liruilong/;echo `date` > liruilong.txt
┌──[[email protected]]-[/liruilong]
└─$cd /liruilong/;cat  liruilong.txt
2021年 11月 27日 星期六 21:57:10 CST           

exports

配置文件解析 (服务端)

语法: 文件夹路径 客户机地址(权限) 客户机地址(权限)....

PS1: /public 192.168.4.0/24

┌──[[email protected]]-[/liruilong]
└─$cat /etc/exports
┌──[[email protected]]-[/liruilong]  #任意主机可以访问 (ro)只读的方式
└─$echo "/liruilong *(rw,sync,no_root_squash)" > /etc/exports           

刷新配置exportfs -arv

┌──[[email protected]]-[/liruilong]
└─$exportfs -arv
exporting *:/liruilong
┌──[[email protected]]-[/liruilong]
└─$showmount -e  #查看当前机器服务列表
Export list for vms81.liruilongs.github.io:
/liruilong *
┌──[[email protected]]-[/liruilong]
└─$
           

客户端

这里为了方便,使用了

ansible

然后我们需要在所有的使用节点安装nfs-utils,然后挂载

┌──[[email protected]]-[~/ansible]
└─$ansible node -m shell -a "yum -y install nfs-utils"
┌──[[email protected]]-[~/ansible]
└─$ansible node -m shell -a "systemctl enable nfs-server.service  --now"           

nfs共享文件测试:查看指定机器的共享文件列表:showmount -e vms81.liruilongs.github.io

┌──[[email protected]]-[~/ansible] #查看指定机器的共享文件列表
└─$ansible node -m shell -a "showmount -e vms81.liruilongs.github.io"
192.168.26.83 | CHANGED | rc=0 >>
Export list for vms81.liruilongs.github.io:
/liruilong *
192.168.26.82 | CHANGED | rc=0 >>
Export list for vms81.liruilongs.github.io:
/liruilong *
┌──[[email protected]]-[~/ansible]
└─$           

挂载方式

手动挂载

挂载测试,这里我们通过手动的方式挂载

┌──[[email protected]]-[~/ansible]
└─$ansible node  -m shell -a "mount  vms81.liruilongs.github.io:/liruilong /mnt"

192.168.26.82 | CHANGED | rc=0 >>

192.168.26.83 | CHANGED | rc=0 >>

┌──[[email protected]]-[~/ansible]
└─$ansible node -m shell -a "cd /mnt/;ls"
192.168.26.83 | CHANGED | rc=0 >>
liruilong.txt
192.168.26.82 | CHANGED | rc=0 >>
liruilong.txt           

查看挂载信息

┌──[[email protected]]-[~/ansible]
└─$ansible node -m shell -a "df -h | grep  liruilong"
192.168.26.82 | CHANGED | rc=0 >>
vms81.liruilongs.github.io:/liruilong  150G  8.3G  142G    6% /mnt
192.168.26.83 | CHANGED | rc=0 >>
vms81.liruilongs.github.io:/liruilong  150G  8.3G  142G    6% /mnt           

取消挂载

┌──[[email protected]]-[~/ansible]
└─$ansible node  -m shell -a "umount /mnt"           

当然,挂载方式还可以使用开机自动挂载,触发挂载的方式

开机自动挂载

挂载机器环境准备

┌──[[email protected]]-[~/ansible]
└─$ansible 192.168.26.100 -m shell  -a "yum -y install nfs-utils"
┌──[[email protected]]-[~/ansible]
└─$ansible 192.168.26.100 -m shell  -a "systemctl enable nfs-utils --now"
┌──[[email protected]]-[~/ansible]
└─$ansible 192.168.26.100 -m shell  -a "showmount -e 192.168.26.81"
192.168.26.100 | CHANGED | rc=0 >>
Export list for 192.168.26.81:
/vdisk     *
/tmp       *
/liruilong *           

配置 etc/fstab

┌──[[email protected]]-[~/ansible]
└─$ansible 192.168.26.100 -m shell  -a "echo '192.168.26.81:/liruilong   /mnt/nfsmount   nfs defaults,_
netdev 0 0' >> /etc/fstab"
192.168.26.100 | CHANGED | rc=0 >>

┌──[[email protected]]-[~/ansible]
└─$ansible 192.168.26.100 -m shell  -a "cat  /etc/fstab | grep liruilong"
192.168.26.100 | CHANGED | rc=0 >>
192.168.26.81:/liruilong   /mnt/nfsmount   nfs defaults,_netdev 0 0           

刷新配置:测试

┌──[[email protected]]-[~/ansible]
└─$ansible 192.168.26.100 -m shell  -a " mkdir /mnt/nfsmount"
┌──[[email protected]]-[~/ansible]
└─$ansible 192.168.26.100 -m shell  -a " mount -a"
┌──[[email protected]]-[~/ansible]
└─$ansible 192.168.26.100 -m shell  -a " ls -l /mnt/nfsmount"
192.168.26.100 | CHANGED | rc=0 >>
总用量 4
-rw-r--r-- 1 root root 43 11月 27 21:57 liruilong.txt
┌──[[email protected]]-[~/ansible]
└─$ll /liruilong/
总用量 4
-rw-r--r-- 1 root root 43 11月 27 21:57 liruilong.txt           

触发挂载

由 autofs 服务提供的 “按需访问” 机制,只要访问挂载点,就会触发响应,自动挂载指定设备;闲置超过时限(默认5分钟)后,会自动卸载

安装需要的软件包

┌──[[email protected]]-[~/ansible]
└─$ansible 192.168.26.100 -m shell -a "yum -y install autofs"
┌──[[email protected]]-[~/ansible]
└─$ansible 192.168.26.100 -m shell -a "systemctl enable  autofs --now"
192.168.26.100 | CHANGED | rc=0 >>
Created symlink from /etc/systemd/system/multi-user.target.wants/autofs.service to /usr/lib/systemd/system/autofs.service.           

查看配置文件位置

┌──[[email protected]]-[~/ansible]
└─$ansible 192.168.26.100 -m shell -a "rpm -qc autofs"
[WARNING]: Consider using the yum, dnf or zypper module rather than running 'rpm'.  If you need to use
command because yum, dnf or zypper is insufficient you can add 'warn: false' to this command task or
set 'command_warnings=False' in ansible.cfg to get rid of this message.
192.168.26.100 | CHANGED | rc=0 >>
/etc/auto.master
/etc/auto.misc
/etc/auto.net
/etc/auto.smb
/etc/autofs.conf
/etc/autofs_ldap_auth.conf
/etc/sysconfig/autofs
/usr/lib/systemd/system/autofs.service
┌──[[email protected]]-[~/ansible]
└─$           

配置文件编辑

配置自动挂载需要修改两个配置文件,一个

/etc/auto.master

/etc/auto.master

这个配置文件为主配置文件,配置当前机器挂载的目录的配置文件

┌──[[email protected]]-[~/ansible]
└─$ansible 192.168.26.100 -m shell -a "echo '/liruilong  /etc/auto.misc' >> /etc/auto.master"
192.168.26.100 | CHANGED | rc=0 >>
           

这个配置文件为挂载目录挂载的远程目录配置文件的挂载

┌──[[email protected]<font color=camel></font>longs.github.io]-[~/ansible]
└─$ansible 192.168.26.100 -m shell -a "echo 'sy    -fstype=nfs   192.168.26.81:/liruilong '  >>/etc/auto.misc"
192.168.26.100 | CHANGED | rc=0 >>           

重启服务

┌──[[email protected]]-[~/ansible]
└─$ansible 192.168.26.100 -m shell -a "systemctl restart autofs"
192.168.26.100 | CHANGED | rc=0 >>
           

触发挂载,查看

┌──[[email protected]]-[~/ansible]
└─$ls /liruilong/
liruilong.txt  mGAX.23  work
┌──[[email protected]]-[~/ansible]
└─$ansible 192.168.26.100 -m shell -a "ls /liruilong/sy"
192.168.26.100 | CHANGED | rc=0 >>
liruilong.txt
mGAX.23
work
┌──[[email protected]]-[~/ansible]
└─$ansible 192.168.26.100 -m shell -a "df -ah | grep liruilong"
192.168.26.100 | CHANGED | rc=0 >>
192.168.26.81:/liruilong  150G  8.5G  142G    6% /mnt/nfsmount
/etc/auto.misc               0     0     0     - /liruilong
192.168.26.81:/liruilong  150G  8.5G  142G    6% /liruilong/sy
┌──[[email protected]]-[~/ansible]
└─$           

继续阅读