文章目录
一、存储分类
- 文件级存储----->FTP、NFS、SAMBA
- 需要文件系统来操作磁盘
- 块级存储----->iSCSI
- 厂商会虚拟出来一个文件系统帮助直接操作相应的磁盘块
- 对象存储----->cepth、swift
- 对象有自己的属性,传输的时候会先检查是否已经存在过,所以就会出现有的文件传输很快,有的文件传输很慢
二、NFS网络文件系统
主要用于linux系统之间共享文件系统的一种协议,通过网络让不同的主机之间共享文件或目录
- NFS最早是希望可以把一台电脑的硬盘挂载到另一台电脑给另一台电脑用
- 如果想使用ftp资源的话,必须要下载到本地之后才可以打开,所以ftp的资源是一个纯网络资源,nfs就可以直接打开共享的文件
- NFS:Network File System网络文件系统,基于内核的文件系统。sun公司开发,通过使用NFS,用户和程序可以像访问本地文件一样访问远端系统上的文件,基于RPC(Remote Procedure Call Protocol远程过程调用)实现RPC采用C/S模式,客户机请求程序调用进程发送一个有进程参数的调用信息到服务进程,然后等待应答信息。在服务器端,进程保持睡眠状态直到调用信息为止。当一个调用信息到达,服务器获得进程参数,计算结果,发送答复信息,然后等待下一个调用信息,最后,客户端调用进程接收答复信息,获得进程结果,然后调用执行继续进行
三、RPC远程过程调度
- NFS本身没有网络传输功能,而是基于远程过程调用协议实现的,
- RPC提供一个面向过程的远程服务的接口
- 可以通过网络从远程主机程序上请求服务,而不需要了解底层网络技术的协议
- 工作在OSI模型的会话层,它可以为遵从RPC协议应用层协议提供端口注册功能
- 事实上,有很多服务(NFS和NIS等)都可以向RPC注册端口
- RPC使用网络端口111来监听客户端的请求
四、RPC的协议模型
1、基于rpc的服务(此处指的是nfs服务,在别处有可能是代表其他服务)在启动时向portmapper注册端口
2、基于rpc的客户端联系服务端portmapper询问服务的端口号
3、portmapper告知客户端基于rpc服务使用的端口号
4、基于rpc的客户端访问被告知基于rpc服务的端口
5、基于rpc的服务响应客户端的请求
五、NFS部署
- 安装rpcbind
yum install rpcbind -y
- 安装nfs服务
yum install nfs-utils -y
# 其实直接装nfs-utils也会以依赖的方式自动装上rpcbind
systemctl status rpcbind.socket
systemctl start nfs-server.service
systemctl enable nfs-server.service
- 比较nfs启动前后的rpcbind的状态
systemctl status rpcbind.socket
# nfs启动前,只是监听111端口,但是并没有启动对应的服务
systemctl status rpcbind
# nfs启动后,可以看到rpcbind自动启动了,也说明了nfs对rpc的依赖
- 查看已经监听的端口,在重启nfs之后,端口号会随机变化,所以需要rpc做注册
ss -ntulp
systemctl restart nfs
ss -ntulp
rpcbind -p
# 可以查看到注册的端口号
六、NFS 软件介绍
- 软件包
- nfs-utils
- 相关软件包
- rpcbind(必须)、tcp_wrappers
- kernel 支持
- nfs.ko
- 端口
- 2049(nfsd),其他端口由portmap(111)分配
- 说明:Centos6开始的portmap进程由rpcbind代替
- NFS服务主要进程
- rpc.nfsd最主要的NFS进程,管理客户端是否可登录
- rpc.mounted挂载和卸载NFS文件系统,包括权限管理
- rpc.lockd非必要,管理文件锁,避免同时写出错
- rpc.statd非必要,检查文件一致性,可修复文件
- 日志:/var/lib/nfs
- NFS配置文件
- /etc/exports
- /etc/exports.d/*.exports
七、NFS 共享配置文件格式
/dir 主机 1(opt1,opt2) 主机 2(opt1,opt2)
-
主机格式
- 单个主机:ipv4,ipv6,FQDN
- IP networks:两种掩码格式均支持
- 172.18.0.0/255.255.0.0
- 172.18.0.0/16
- wildcards:主机名通配,例如*.iproute.cn,IP不可以
- netgroups:NIS域的主机组,@group_name
- anonymous:表示使用*通配所有客户端
-
选项格式
- 默认选项
- (ro,sync,root_squash,no_all_squash)
- ro, rw
- 只能读和读写
- async
- 异步,数据变化后不立即写磁盘,等磁盘空闲时再写入,性能高
- sync
- 同步(1.0.0后为默认),数据在请求时立即写入共享存储磁盘
- root_squash
- 远程root映射为nfsnobody(默认),UID为65534,centos8为nobody,早期版本是4294967294(nfsnobody)
- no_root_squash
- 远程root映射成root用户
- all_squash
- 所有远程用户(包括root)都变成nfsnobody,centos8为nobody
- no_all_squash
- 保留共享文件的UID和GID(默认)
- anonuid和anongid
- 指名匿名用户映射为特定用户UID和组GID,而非nfsnobody,可配合all_squash使用
- 默认选项
-
配置案例
vim /etc/exports
/myshare 192.169.64.130
/myshare 192.168.64.0/24
/mysahre server.example.com
/myshare *.example.com
/myshare server?.example.com
/myshare server[0-20].example.com
/myshare 2000:472:18:b51:c32:a21
/mysahre 2000:472:18:b51::/64
/myshare *.example.com 172.25.0.0/16
/myshare desktop.example.com(ro)
/myshare desktop.example.com(ro) server[0-20].example.com(rw)
/myshare diskless.example.com(rw,no_root_squash)
# no_root_squash 登入NFS主机使用分享目录的使用者,如果是root的话,那么对于这个分享的目录来说,他就具有root权限,单词squash是压缩压扁的意思
八、NFS工具
rpcinfo
rpcinfo工具可以查看RPC相关信息
查看注册在指定主机的RPC程序
rpcinfo -p hostname
查看rpc注册程序
rpcinfo -s hostname
exports
可以用于管理NFS导出的文件系统
常见选项:
- -v:查看本机所有NFS共享
- -r:重读配置文件,并共享目录
- -a:输出本机所有共享
- -au:停止本机所有共享
showmount
常见用法:
showmount -e hostname
配置案例:
服务端
[root@localhost ~]# vim /etc/exports
/myshare 192.168.64.0/24
[root@localhost ~]# mkdir /myshare
[root@localhost ~]# cd /myshare
[root@localhost myshare]# echo "hello" > file
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]#setenforce 0
客户端
[root@localhost ~]# yum install nfs-utils -y
[root@localhost ~]# showmount -e 192.168.64.129
# 虽然我们自己配置了共享,但是没有重读配置文件,所以读不到
服务端:
[root@localhost ~]# exportfs -r
[root@localhost ~]# exportfs -v
客户端
[root@localhost ~]# showmount -e 192.168.64.129
[root@localhost ~]# mkdir /mnt/nfs
[root@localhost ~]# mount -t nfs 192.168.64.129:/myshare /mnt/nfs
[root@localhost ~]# df -h
[root@localhost ~]# umount /mnt/nfs
# 现在是只读模式,想要修改模式要去改配置文件
服务端:
[root@localhost ~]# vim /etc/exports
/myshare 192.168.64.0/24(rw,sync,root_squash,no_all_squash)
[root@localhost ~]# exportfs -v
[root@localhost ~]# exportfs -r
[root@localhost ~]# exportfs -v
客户端:
[root@localhost ~]# showmount -e 192.168.64.129
[root@localhost ~]# mount -t nfs 192.168.64.129/myshare /mnt/nfs
[root@localhost ~]# df -h
[root@localhost nfs]# cd /mnt/nfs
[root@localhost nfs]# touch myfile
# 权限不够,虽然给了rw权限,但是目录权限被linux控制
服务端:
[root@localhost ~]# ll -d /myshare
[root@localhost ~]# chmod a+w /myshare
客户端
[root@localhost nfs]# touch myfile
# 当前创建的文件属主和属组都是nfsnobody,如果使用普通用户登录,进入挂载的目录下创建文件,属主和属组是当前登录的普通用户,而在服务端查看当前用户创建的文件显示的是uid和gid,如果在服务端上再创建一个用户,他的uid和客户端的uid相同,则在服务端的看到的文件属组和属主会被服务端的用户代替,需要修改/etc/exports中的权限,加上no_root_squash,all_squash
mount.nfs
NFS 相关的挂载选项:man 5 nfs
-
fg:(默认)前台挂载
-
bg:后台挂载
-
hard:(默认)非持续请求
-
soft:非持续请求
-
intr和hard配合:请求可中断
-
rsize和wsize:一次读和写数据最大字节数,rsize=32768
-
_netdev:无网络不可挂载
提示:基于安全考虑,建议使用nosuid,_netdev,noexec
挂载选项
范例:临时挂载NFS共享
mount -o rw,nosuid,fg,hard,intr 192.168.64.129:/test/dir /mnt/nfs
范例:开机挂载
vim /etc/fstab
192.168.64.129:/mysahre /mnt/nfs nfs defaults,_netdev 0 0
九、自动挂载
可使用autofs服务按需要挂载外围设备,NFS共享等,并在空虚5分钟后自动卸载,设备一直挂载可能比较耗费资源
相关包和文件
- 软件包:autofs
- 服务文件:/usr/lib/systemd/system/autofs.service
- 配置文件:/etc/auto.master
配置文件格式
参考帮助:man 5 autofs
- 自动挂载资源有两种格式
- 相对路径法:将mount point路径分成dirname和basename分别配置,可能会影响现有的目录结构
- 绝对路径法:直接匹配全部绝对路径名称,不会影响本地目录结构
相对路径法
/etc/auto.master
格式
挂载点的dirname 指定目录的配置文件路径
- 指定目录的配置文件格式
挂载点的basename 挂载选项 选项设备
案例
- 相对路径法为支持通配符
客户端:
[root@localhost ~]# yum install autofs -y
[root@localhost ~]# vim /etc/auto.master
/misc /etc/auto.misc
[root@localhost ~]# mkdir /misc
[root@localhost ~]# vim /etc/auto.misc
# 表示/misc下面的子目录和nfs共享/export子目录同名
* -fstype=nfs,vers=3 192.168.64.129:/myshare/&
[root@localhost ~]# systemctl restart autofs
服务端:
[root@localhost myshare]# mkdir -p dir/dir1
客户端
[root@localhost ~]# df -h
[root@localhost ~]# ll /misc
总用量为0
[root@localhost ~]# cd /misc/dir
[root@localhost dir]# ll
绝对路径法
- /etc/auto.master格式
/- 指定配置文件路径
- 指定配置文件格式
绝对路径 挂载选项 选项设备
案例
- 绝对路径法
客户端
[root@localhost dir]# vim /etc/auto.misc
# 表示/misc下面的子目录和nfs共享/export子目录同名
#* -fstype=nfs,vers=3 192.168.64.129:/myshare/&
# 把之前挂载的注释掉,防止重复挂载出问题
[root@localhost dir]# vim /etc/auto.master
/- /etc/auto.direct
#添加一行,/-表示直接挂载在根目录下
[root@localhost dir]# vim /etc/auto.direct
/nfsdir -fstype=nfs 192.168.64.129:/myshare
[root@localhost dir]# mkdir /nfsdir
[root@localhost dir]# systemctl restart autofs
[root@localhost dir]# df -h
[root@localhost ~]# cd /nfsdir/
[root@localhost ~]# df -h
案例,基于NFS搭建web后端NAS存储
- 关闭防火墙和selinux
- 在两台机器上准备web
yum install httpd -y
systemctl start httpd
#可以通过浏览器访问ip地址,看到默认界面,可在/var/www/html/index.html修改界面
- 准备nas端
yum install nfs-utils -y
mkdir /webdata
vim /webdata/index.html #准备共享文件
vim /etc/exports
/webdata 192.168.239.0/24(rw)
systemctl start nfs-server.service
systemctl enable nfs-server.service
- 查看共享
[root@server1 ~]# yum install -y nfs-utils
[root@server1 ~]# showmount -e 192.168.239.131
[root@server2 ~]# yum install -y nfs-utils
[root@server2 ~]# showmount -e 192.168.239.131
- 挂载后端nas存储,分别在两台机器上执行
mount -t nfs 192.168.239.131:/webdata /var/www/html
df -h
自动挂载+实践案例
将server端的用户家目录共享出来,客户端在登录这个用户的时候,看到家目录下的文件是一致的
- 服务端
[root@NFSserver ~]# mkdir /data
[root@NFSserver ~]# useradd -d /data/user user
[root@NFSserver ~]# id user
uid=1000(user) gid=1000(user) 组=1000(user)
[root@NFSserver ~]# vim /etc/exports.d/test.exports
/data/user *(rw,anonuid=1000,anongid=1000,all_squash)
[root@NFSserver ~]# exports -r
[root@NFSserver ~]# exports -v
[root@NFSserver ~]# cd /data/user
[root@NFSserver user]# touch file.txt
[root@NFSserver user]# echo "hello" > file.txt
- 在客户端上实现autofs
[root@NFSclient ~]# vim /etc/auto.master
/data /etc/auto.home
[root@NFSclient ~]# vim /etc/auto.home
* -fstype=nfs,vers=3 192.168.64.129:/data/user&
[root@NFSclient ~]# systemctl restart autofs
[root@NFSclient ~]# mkdir /data
[root@NFSclient ~]# useradd -d /data/user -u 1000 user
测试
[root@server1 data]# mount -t nfs 192.168.64.129:/data/user /data
[root@NFSclient /]# su - user
[user@NFSclient ~]$ ll
总用量 0 -rw-rw-r--. 1 user user 0 4月 30 10:13 file