nfs 笔记
第一章 nfs 介绍
第1章 存储与nfs存储概述
1.为什么用共享存储(数据安全 数据一致 ) 2.存储有哪些工具 (硬盘 内存) 3.共享存储应用场景有哪些 4.部署nfs共享存储 5.客户端尝试连接共享存储
什么是NFS?
NFS 是 Network File System 的缩写及网络文件系统。 NFS 主要功能是通过局域网络让不同的主机系统之间可以共享文件或目录。 NFS 系统和 Windows 网络共享、网络驱动器类似, 只不过 windows 用于局域网, NFS 用于企业集群架构中, 如果是大型网站, 会用到更复杂的分布式文件系统 FastDFS,glusterfs,HDFS 那么我们为什么要使用数据存储共享服务?
1.实现多台服务器之间数据共享
2.实现多台服务器之间数据一致
1.1 NFS应用场景
下面我将通过图解给大家展示集群需要共享存储服务的理由。 1.A 用户上传图片经过负载均衡,负载均衡将上传请求调度至 WEB1 服务器上。 2.B 用户访问 A 用户上传的图片,此时 B 用户被负载均衡调度至 WEB2 上,因为 WEB2 上没有这张图片,所以 B用户无法看到 A 用户传的图片
[图片上传失败...(image-518921-1584944684770)]
如果有共享存储的情况 1.A 用户上传图片无论被负载均衡调度至 WEB1 还是 WEB2, 最终数据都被写入至共享存储 2.B 用户访问 A 用户上传图片时,无论调度至 WEB1 还是 WEB2,最终都会上共享存储访问对应的文件,这样就可以访问到资源了
[图片上传失败...(image-84cdbe-1584944684770)]
1.2 nfs存储工作原理实现
[图片上传失败...(image-30656a-1584944684769)]
NFS工作原理 1.用户进程访问 NFS 客户端,使用不同的函数对数据进行处理 2.NFS 客户端通过 TCP/IP 的方式传递给 NFS 服务端 3.NFS 服务端接收到请求后,会先调用 portmap 进程进行端口映射。 4.nfsd 进程用于判断 NFS 客户端是否拥有权限连接 NFS 服务端。 5.Rpc.mount 进程判断客户端是否有对应的权限进行验证。 6.idmap 进程实现用户映射和压缩 7.最后 NFS 服务端会将对应请求的函数转换为本地能识别的命令,传递至内核,由内核驱动硬件。 注意: rpc 是一个远程过程调用,那么使用 nfs 必须有 rpc 服务
1.3 前提条件
1.nfs依赖于RPC服务来传递消息 2.NFS服务启动的端口号是随机的,启动之后会向本地的RCP注册 3.先启动RPC服务,再启动NFS服务 4.NFS和RPC之间的通讯是他们自己内部完成的,对于用户来说无感知 5.NFS客户端和服务端不会直接沟通,必须通过RPC服务传递消息 6.防火墙要开放RPC服务的端口
第2章 nfs服务端部署
2.1 安装服务
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" contenteditable="true" cid="n21" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Monaco, Consolas, "Andale Mono", "DejaVu Sans Mono", monospace; margin-top: 0px; margin-bottom: 20px; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(54, 59, 64); font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: normal; position: relative !important; padding: 10px 30px; border: 1px solid; width: inherit; color: rgb(184, 191, 198); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">yum install nfs-utils -y</pre>
2.2 修改配置文件
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" contenteditable="true" cid="n23" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Monaco, Consolas, "Andale Mono", "DejaVu Sans Mono", monospace; margin-top: 0px; margin-bottom: 20px; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(54, 59, 64); font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: normal; position: relative !important; padding: 10px 30px; border: 1px solid; width: inherit; color: rgb(184, 191, 198); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">[root@m71-nfs ~]# cat > /etc/exports <<EOF
/data 172.16.1.0/24(rw,sync,all_squash)
EOF
</pre>
配置文件参数解释: 执行 man exports 命令,然后切换到文件结尾,可以快速查看如下样例格式: nfs共享参数 参数作用 rw 读写权限 ro 只读权限 root_squash 当 NFS 客户端以 root 管理员访问时,映射为 NFS 服务器的匿名用户(不常用) no_root_squash 当 NFS 客户端以 root 管理员访问时,映射为 NFS 服务器的 root 管理员(不常用) all_squash 无论 NFS 客户端使用什么账户访问,均映射为 NFS 服务器的匿名用户(常用) no_all_squash 无论 NFS 客户端使用什么账户访问,都不进行压缩 sync 同时将数据写入到内存与硬盘中,保证不丢失数据 async 优先将数据保存到内存,然后再写入硬盘;这样效率更高,但可能会丢失数据 anonuid 配置 all_squash 使用,指定 NFS 的用户 UID,必须存在系统
anongid 配置 all_squash 使用,指定 NFS 的用户 UID,必须存在系统
2.3 根据配置文件的内容修改权限
nfs默认nfsnobody用户执行 所以更改配置文件内目录为该用户权限
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" contenteditable="true" cid="n28" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Monaco, Consolas, "Andale Mono", "DejaVu Sans Mono", monospace; margin-top: 0px; margin-bottom: 20px; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(54, 59, 64); font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: normal; position: relative !important; padding: 10px 30px; border: 1px solid; width: inherit; color: rgb(184, 191, 198); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">mkdir -p /data/
chown -R nfsnobody:nfsnobody /data</pre>
2.4 启动服务
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" contenteditable="true" cid="n30" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Monaco, Consolas, "Andale Mono", "DejaVu Sans Mono", monospace; margin-top: 0px; margin-bottom: 20px; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(54, 59, 64); font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: normal; position: relative !important; padding: 10px 30px; border: 1px solid; width: inherit; color: rgb(184, 191, 198); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">systemctl start nfs-server.service
</pre>
2.5 检查命令
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" contenteditable="true" cid="n32" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Monaco, Consolas, "Andale Mono", "DejaVu Sans Mono", monospace; margin-top: 0px; margin-bottom: 20px; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(54, 59, 64); font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: normal; position: relative !important; padding: 10px 30px; border: 1px solid; width: inherit; color: rgb(184, 191, 198); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">showmuout -e 172.16.1.71</pre>
2.6 加入开机自启动
在使用 NFS 服务进行文件共享之前,需要使用 RPC(Remote Procedure Call 远程过程调用服务将 NFS 服务器的IP 地址和端口号信息发送给客户端。因此,在启动 NFS 服务之前,需要先重启并启用 rpcbind 服务程序,同时都加入开机自启动
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" contenteditable="true" cid="n35" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Monaco, Consolas, "Andale Mono", "DejaVu Sans Mono", monospace; margin-top: 0px; margin-bottom: 20px; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(54, 59, 64); font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: normal; position: relative !important; padding: 10px 30px; border: 1px solid; width: inherit; color: rgb(184, 191, 198); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">systemctl enable rpcbind nfs-server
systemctl restart rpcbind nfs-server</pre>
2.7 一键安装脚本
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" contenteditable="true" cid="n37" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Monaco, Consolas, "Andale Mono", "DejaVu Sans Mono", monospace; margin-top: 0px; margin-bottom: 20px; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(54, 59, 64); font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: normal; position: relative !important; padding: 10px 30px; border: 1px solid; width: inherit; color: rgb(184, 191, 198); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">yum install nfs-utils -y
cat > /etc/exports <<EOF
/data 172.16.1.0/24(rw,sync,all_squash,anonuid=666,anongid=666)
EOF
groupadd -g 666 www
useradd -u 666 -g 666 -M -S /sbin/nologin www
mkdir -p /data/
chown -R www:www /data
systemctl start nfs-server.service
systemctl start npcbind
showmount -e
systemctl enable rpcbind nfs-server</pre>
第三章 客户端部署
3.1 安装服务
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" contenteditable="true" cid="n41" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Monaco, Consolas, "Andale Mono", "DejaVu Sans Mono", monospace; margin-top: 0px; margin-bottom: 20px; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(54, 59, 64); font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: normal; position: relative !important; padding: 10px 30px; border: 1px solid; width: inherit; color: rgb(184, 191, 198); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">yum install nfs-utils -y</pre>
<pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="" contenteditable="true" cid="n42" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Monaco, Consolas, "Andale Mono", "DejaVu Sans Mono", monospace; margin-top: 0px; margin-bottom: 20px; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(54, 59, 64); font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; position: relative !important; padding: 10px 30px; border: 1px solid; width: inherit; color: rgb(184, 191, 198); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">systemctl restart rpcbind #只需要重启rpc就可以
</pre>
3.2 挂载
<pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="" contenteditable="true" cid="n44" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Monaco, Consolas, "Andale Mono", "DejaVu Sans Mono", monospace; margin-top: 0px; margin-bottom: 20px; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(54, 59, 64); font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; position: relative !important; padding: 10px 30px; border: 1px solid; width: inherit; color: rgb(184, 191, 198); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">showmount -e 172.16.1.71
</pre>
挂载命令: 创建挂载目录 在 NFS 客户端创建一个挂载目录, 使用 mount 命令并结合-t 参数, 指定要挂载的文件系统的类型, 并在命令后面写上服务器的 IP 地址, 以及服务器上的共享目录, 最后需要写上要挂载到本地系统(客户端)的目录
<pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="" contenteditable="true" cid="n46" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Monaco, Consolas, "Andale Mono", "DejaVu Sans Mono", monospace; margin-top: 0px; margin-bottom: 20px; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(54, 59, 64); font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; position: relative !important; padding: 10px 30px; border: 1px solid; width: inherit; color: rgb(184, 191, 198); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">mount -t nfs 172.16.1.71:/data /data
df -h
检测是否可以写入
[root@backup ~]# echo "oldzhang" > /data/oldzhang.txt
[root@backup ~]# cat /data/oldzhang.txt
</pre>
3.2写入开机自动挂载
<pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="" contenteditable="true" cid="n48" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Monaco, Consolas, "Andale Mono", "DejaVu Sans Mono", monospace; margin-top: 0px; margin-bottom: 20px; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(54, 59, 64); font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; position: relative !important; padding: 10px 30px; border: 1px solid; width: inherit; color: rgb(184, 191, 198); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">vim /etc/fstab
172.16.1.71:/data /data nfs defaults 0 0
</pre>
3.4一键安装脚本
<pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="" contenteditable="true" cid="n50" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Monaco, Consolas, "Andale Mono", "DejaVu Sans Mono", monospace; margin-top: 0px; margin-bottom: 20px; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(54, 59, 64); font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; position: relative !important; padding: 10px 30px; border: 1px solid; width: inherit; color: rgb(184, 191, 198); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">yum -y install nfs-utils
mkdir -p /data/
systemctl start rpcbind
mount -t nfs 172.16.1.71:/data/ /data/
df -h|tail -1
cat > /etc/fstab << EOF
172.16.1.71:/data/ /data/ nfs defaults 0 0
EOF
</pre>