18.集群架构之NFS网络文件系统

1.NFS简介

NFS是Network File System的缩写及网络文件系统。主要功能是通过局域网络让不同的主机系统之间可以共享文件或目录。

NFS系统和Windows网络共享、网络驱动器类似, 只不过windows用于局域网, NFS用于企业集群架构中.

大型网站, 会用到更复杂的分布式文件系统FastDFS,glusterfs,HDFS,ceph

2.应用

1.用户访问NFS客户端,将请求转化为函数
2.NFS通过TCP/IP连接服务端
3.NFS服务端接收请求,会先调用portmap进程进行端口映射
4.Rpc.nfsd进程用于判断NFS客户端能否连接服务端;
5.Rpc.mount进程用于判断客户端对服务端的操作权限
6.如果通过权限验证,可以对服务端进行操作,修改或读取

image-20220104163506575

3.DFS搭建

开启m01, nfs, web01, web02, web03, 
五台主机
一定要启动 v*p*n 不然 xshell 无法使用 172.16.1.xxx的局域网

image-20220104174014504

3.1服务端
# 1.安装NFS和rpcbind
[root@nfs ~]#  yum install nfs-utils rpcbind -y
# 2.创建挂载点  
[root@nfs ~]# mkdir -p /web/nfs{1..9}
# 3.配置挂载点
[root@nfs ~]# vim /etc/exports
/web/nfs1  172.16.1.0/20(rw,sync,all_squash)
:wq
格式:
[挂载点] [可以访问的IP]([权限])
# 4.关闭selinux和防火墙
[root@nfs ~]# setenforce 0
[root@nfs ~]# systemctl disable --now firewalld
# 5.启动Nfs和rpcbind服务
[root@nfs ~]# systemctl start nfs-server 
[root@nfs ~]# systemctl start rpcbind
# 6.检查服务端是否正常 showmount -e [服务端的地址,默认是本机地址]
[root@nfs ~]# showmount -e 172.16.1.31
Export list for 172.16.1.31:
/web/nfs1 172.16.1.0/20
[root@nfs ~]#  showmount -e
Export list for nfs:
/web/nfs1 172.16.1.0/20
# 7.给挂载点授权
[root@nfs ~]# chown -R nfsnobody.nfsnobody /web
# 8. 查看权限信息
[root@nfs ~]# cat /var/lib/nfs/etab
/web/upload	172.16.1.0/20(rw,sync,wdelay,hide,nocrossmnt,secure,root_squash,all_squash,no_subtree_check,secure_locks,acl,no_pnfs,anonuid=666,anongid=666,sec=sys,rw,secure,root_squash,all_squash)
# 9.开机自启动NFS和rpcbind服务
[root@nfs ~]# vim /etc/rc.local
/usr/bin/systemctl start nfs-server 
/usr/bin/systemctl start rpcbind
[root@nfs ~]# chmod +x /etc/rc.d/rc.local 
3.2客户端
# 1.安装NFS
[root@web01 ~]# yum install -y nfs-utils
# 2.创建目录
[root@web01 opt]# cd /opt
[root@web01 opt]# mkdir nfs
# 3.挂载NFS
[root@web01 opt]# mount -t nfs 172.16.1.31:/web/nfs1  /opt/nfs/
# -t 指定类型 nfs类型 挂载点为172.16.1.31:/web/nfs1  挂载到/opt/nfs/下
# 4.测试NFS文件同步功能
[root@web01 opt]# cd nfs
[root@web01 nfs]# touch 1.txt
# 我挂载你电脑的硬盘 我创建了文件 ,你电脑也能看见...

image-20220104190235651

# 重复上面的步奏,挂载到另外两台计算机去

image-20220104194042730

4.NFS配置详解

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的用户GID,必须存在系统 (常用)
配置文件
/etc/exports

设置完后都重新挂载一次不会创建文件操作会卡
4.1读写
控制读写
rw ro
# 1.修改 只读模式
[root@nfs nfs1]# vi /etc/exports
/web/nfs1  172.16.1.0/20(ro,sync,all_squash)
:wq
[root@nfs nfs1]# systemctl restart nfs-server rpcbind
# 卸载 挂载点再重新挂载
[root@web01 opt]# umount /opt/nfs/
[root@web01 opt]# mount -t nfs 172.16.1.31:/web/nfs1  /opt/nfs/

[root@web01 opt]# cd nfs/
[root@web01 nfs]# ll
total 0
-rw-r--r-- 1 nfsnobody nfsnobody 0 Jan  4 19:00 1.txt
-rw-r--r-- 1 nfsnobody nfsnobody 0 Jan  4 19:11 2.txt
# 创建文件
[root@web01 nfs]# touch 3.txt
touch: cannot touch ‘3.txt’: Read-only file system
# 只读文件系统
# 2.修改 读写模式
[root@nfs nfs1]# vi /etc/exports
/web/nfs1  172.16.1.0/20(rw,sync,all_squash)
:wq
[root@nfs nfs1]# systemctl restart nfs-server rpcbind
[root@web01 nfs]# cd ..  # 退出挂载的目录才能卸载它
[root@web01 opt]# umount /opt/nfs/
[root@web01 opt]# mount -t nfs 172.16.1.31:/web/nfs1  /opt/nfs/
[root@web01 opt]# cd nfs/
[root@web01 nfs]# touch 3.txt
[root@web01 nfs]# ll
total 0
-rw-r--r-- 1 nfsnobody nfsnobody 0 Jan  4 19:00 1.txt
-rw-r--r-- 1 nfsnobody nfsnobody 0 Jan  4 19:11 2.txt
-rw-r--r-- 1 nfsnobody nfsnobody 0 Jan  4  2022 3.txt
4.2制文件权限
root_squash
no_root_squash
all_squash    常用
no_all_squash 不常用,不好测试
# 1.匿名用户
[root@nfs nfs1]# vi /etc/exports
/web/nfs1  172.16.1.0/20(rw,sync,root_squash)
:wq
[root@nfs nfs1]# systemctl restart nfs-server rpcbind
[root@web01 opt]# cd nfs/
[root@web01 nfs]# touch 4.txt
...

image-20220104200720453

# 2.root管理员 
[root@nfs nfs1]# vi /etc/exports
/web/nfs1  172.16.1.0/20(rw,sync,no_root_squash)
:wq
[root@nfs nfs1]# systemctl restart nfs-server rpcbind
[root@web01 nfs]# touch 5.txt

image-20220104203058526

4.3控制写模式
sync   常用
async  不常用
4.4控制用户
anonuid
anongid
# 为服务器创建统一用户 nfs web01 web02 web03
groupadd www -g 666
useradd www -u 666 -g 666 -M -r -s /sbin/nologin 
# 修改挂载点权限
[root@nfs nfs1]# chown -R www.www /web/
[root@nfs nfs1]# ll
total 0
-rw-r--r-- 1 www www 0 Jan  4 19:00 1.txt
-rw-r--r-- 1 www www 0 Jan  4 19:11 2.txt
-rw-r--r-- 1 www www 0 Jan  4 19:31 3.txt
-rw-r--r-- 1 www www 0 Jan  4 19:42 4.txt
-rw-r--r-- 1 www www 0 Jan  4 20:30 5.txt
# 修改穿件文件默认用户
[root@nfs nfs1]# vi /etc/exports
/web/nfs1  172.16.1.0/20(rw,sync,all_squash,anonuid=666, anongid=666)
:wq
[root@nfs nfs1]# systemctl restart nfs-server rpcbind
[root@web01 opt]# umount /opt/nfs/
[root@web01 opt]# mount -t nfs 172.16.1.31:/web/nfs1  /opt/nfs/

[root@web01 opt]# cd nfs/
[root@web01 nfs]# touch 6.txt
[root@web01 nfs]# ll
...
-rw-r--r-- 1 www www 0 Jan  4 20:49 6.txt

5.搭建考试系统

5.1搭建WEB服务
# 1.安装web软件
[root@web01 opt]# yum install httpd php php-devel -y
# 2.将代码放置于网站的根目录
[root@web01 opt]# cd /var/www/html/

1

image-20220104205635559

* 需要借助 lrzsz 工具才能拖动
# 3.unzip 解压
[root@web01 html]# unzip kaoshi.zip
Archive:  kaoshi.zip
  inflating: info.php                
  inflating: bg.jpg                  
  inflating: index.html              
  inflating: upload_file.php   
# 4. 这个考试系统的文件默认上传到upload文件中需要手动创建
root@web01 html]# mkdir upload
# 5.授权 现在文件的属主属组都是root 设置为 www
[root@web01 html]# chown -R www.www /var/www/html
[root@web01 html]# ll
total 80
-rw-r--r-- 1 www www 38772 Apr 27  2018 bg.jpg
-rw-r--r-- 1 www www  2633 May  4  2018 index.html
-rw-r--r-- 1 www www    52 May 10  2018 info.php
-rw-r--r-- 1 www www 26995 Dec 30 11:31 kaoshi.zip
drwxr-xr-x 2 www www     6 Jan  4 21:24 upload  # 属主属组不是www 无法上传
-rw-r--r-- 1 www www  1192 Jan 10  2020 upload_file.php
# 6.关闭selinux和防火墙
[root@nfs ~]# setenforce 0
setenforce: SELinux is disabled  # 提示setenforce:SELinux已禁用
[root@nfs ~]# systemctl disable --now firewalld
# 7.修改web软件的用户
[root@web01 html]# vim /etc/httpd/conf/httpd.conf
User www
Group www

image-20220104211407011

# 8.启动
[root@web01 html]# systemctl start httpd
# 9.浏览器访问
172.16.1.7
(禁用浏览器缓存不然,流量器会优先从缓存中取已经存在的信息,就不去访问服务器.)
# 10.通过开机自启动脚本挂载

[root@web01 ~]# vim /etc/rc.local
/usr/bin/setenforce 0
/usr/bin/systemctl disable --now firewalld
/usr/bin/systemctl start httpd
[root@web01 ~]# chmod +x /etc/rc.d/rc.local 

image-20220104210538808

image-20220105095202018

1

# 10.测试 找几张图片上传下
#(文件不要大于400kb,我预估的不知考试系统代码谁写的我拿2.3M的传不上去)
1.上传 文件格式 xx_xx.xxx  最少有两段,中间下划线分开的格式
# 查看上传的文件
[root@web01 html]# cd upload
[root@web01 upload]# ll
total 160
-rw-r--r-- 1 www www 163333 Jan  4 21:57 1_web01.jpg  # 刚才上传的图片

2.浏览器访问
http://172.16.1.7/upload/1_web01.jpg

# 可以访问就成功了
# 错误的日志文件, 前端显示上传成功,服务没有文件,查看日志文件处理
[root@web01 upload]# tail -f /var/log/httpd/error_log 

1

image-20220104220101383

# web02  web03 重复上面的步骤每各自上传一张图片
172.16.1.8 web02 上传 2_web02.jpg
172.16.1.9 web03 上传 3_web02.jpg
检测
http://172.16.1.8/upload/2_web02.jpg 
http://172.16.1.9/upload/3_web03.jpg 

image-20220104221235097

image-20220104221601405

目前数据还无法互通
http://172.16.1.7/upload/3_web03.jpg 
http://172.16.1.8/upload/3_web03.jpg 

image-20220104221820658

5.2配合NFS实现文件共享
# 1.修改NFS配置文件
[root@nfs nfs1]# vim /etc/exports
/web/upload  172.16.1.0/20(rw,sync,all_squash,anonuid=666,anongid=666)
:wq
# 2.创建挂载点
[root@nfs nfs1]# mkdir /web/upload
[root@nfs nfs1]# chown www.www /web/upload
# 3.重启NFS
[root@nfs nfs1]# systemctl restart nfs-server rpcbind
# 4.客户端安装NFS软件
[root@web01 html]# yum install nfs-utils -y
[root@web02 html]# yum install nfs-utils -y
[root@web03 html]# yum install nfs-utils -y
# 5.挂载
[root@web01 html]# mount -t nfs 172.16.1.31:/web/upload /var/www/html/upload
[root@web02 html]# mount -t nfs 172.16.1.31:/web/upload /var/www/html/upload
[root@web03 html]# mount -t nfs 172.16.1.31:/web/upload /var/www/html/upload
# 6.测试需要上传新的文件,之前的没有同步 
(文件不要大于400kb,我预估的不知考试系统代码谁写的我拿2.3M的传不上去)
172.16.1.9 上传文件 7/8访问

http://172.16.1.7/upload/0_web.jpg
http://172.16.1.8/upload/0_web.jpg

2

3

5.3自动挂载
# 通过开机自启动脚本挂载, 关闭selinux和防火墙
[root@web01 ~]# vim /etc/rc.local
/usr/bin/setenforce 0
/usr/bin/systemctl disable --now firewalld
/usr/bin/mount -t nfs 172.16.1.31:/web/upload /var/www/html/upload
[root@web01 ~]# chmod +x /etc/rc.d/rc.local 
# 软链接/etc/rc.local --> /etc/rc.d/rc.local  
# 2.通过/etc/fstab配置文件,先设置一个快照,fstab配置错了linux无法启动
[root@web02 ~]# vim /etc/fstab
# 挂载点                  挂载的目录            类型   设置默认权限    0 不备份 1 备份     0 不检查 1 检查
172.16.1.31:/web/upload  /var/www/html/upload   nfs       defaults          0                0
# 如果没有报错就说明陪着没有问题
[root@web02 ~]# mount -a  # 会有卡,报错等文件就是配置问题
[root@web02 ~]# reboot

在这里插入图片描述

* nfs 一定要先开启,不然加载不到
在启动是会做加载的动作,有一个时间限制,在这个时间内没有启动成功就跳过.

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值