文章目录
nfs服务介绍
1.nfs(network file system):网络文件系统,不同的机器之间通过网络实现文件共享,以此来保证网页数据的一致性,给后端的服务器提供相同的网页和数据,实现数据同源。
2.nfs的优缺点:
优点:任何一台linux服务器都可以搭建,成本较低,构建容易。
缺点:使用传统的网络来传输数据,速度有限,性能一般,在传统tcp/ip网络上传输,与网络质量、磁盘IO、cpu、内存等因素有关。
3.nfs本身没有对外监听的端口号,而是外包给了rpc服务,rpc帮助nfs监听端口。
一、安装nfs服务步骤
1.安装相关软件
yum install nfs-utils -y
2.启动nfs服务
[root@localhost /]service nfs start
[root@localhost /] ps aux|grep nfs
root 3406 0.0 0.0 0 0 ? S< 10:41 0:00 [nfsd4_callbacks]
root 3412 0.0 0.0 0 0 ? S 10:41 0:00 [nfsd]
root 3413 0.0 0.0 0 0 ? S 10:41 0:00 [nfsd]
root 3414 0.0 0.0 0 0 ? S 10:41 0:00 [nfsd]
root 3415 0.0 0.0 0 0 ? S 10:41 0:00 [nfsd]
root 3416 0.0 0.0 0 0 ? S 10:41 0:00 [nfsd]
root 3417 0.0 0.0 0 0 ? S 10:41 0:00 [nfsd]
root 3418 0.0 0.0 0 0 ? S 10:41 0:00 [nfsd]
root 3419 0.0 0.0 0 0 ? S 10:41 0:00 [nfsd]
root 3429 0.0 0.0 112824 976 pts/0 S+ 10:41 0:00 grep --color=auto nfs
3.编辑共享文件的配置文件
[root@localhost web] vim /etc/exports #共享目录的配置文件
[root@localhost web] cat /etc/exports
/web 192.168.81.0/24(ro,all_squash,sync)
/web 是共享的文件夹路径,使用绝对路径,需要自己新建
192.168.81.0/24 允许过来访问的客户机的ip地址网段
(rw,all_squash,sync)表示权限的限制
rw为可读 ro为只读
all_squash:任何客户机上的用户过来访问的时候都认为是普通用户
root_squash:当NFS客户端以root管理员访问的时候。映射为NFS匿名用户
no_root_squash:当NFS客户端以root管理员访问时,映射为NFS服务器的root管理员
sync:同时将数据写入到内存与硬盘中,保证不丢失数据
async:优先将数据保存到内存中,再写入硬盘,效率更高,但是可能会丢失数据
[root@localhost /] mkdir /web #创建共享文件夹
[root@localhost /] cd web
[root@localhost web] ls
[root@localhost web] mkdir sanchuang
[root@localhost web] ls
sanchuang
[root@localhost web] vim index.html
[root@localhost web] ls
index.html sanchuang
编辑配置文件以及新建目录后,刷新服务
[root@localhost web] exportfs -rv #相当于重启nfs服务,让修改的etc/exports文件生效
exporting 192.168.81.0/24:/web
4.在其他web服务器上挂载使用共享目录(共享文件夹)
建议将共享目录直接放在根目录下,更加简洁
每台挂载nfs共享目录的客户机都需要安装nfs-utils
将nfs共享目录挂载到nginx提供网页服务的目录/usr/local/scnginx99/html
[root@localhost web] service firewalld stop #关闭防火墙,防止其他机器不能访问
Redirecting to /bin/systemctl stop firewalld.service
[root@web1 ~] mount 192.168.81.201:/web /usr/local/scnginx99/html
[root@web1 ~] df #查看挂载命令
文件系统 1K-块 已用 可用 已用% 挂载点
devtmpfs 919504 0 919504 0% /dev
tmpfs 931512 0 931512 0% /dev/shm
tmpfs 931512 9736 921776 2% /run
tmpfs 931512 0 931512 0% /sys/fs/cgroup
/dev/mapper/centos-root 17811456 2855288 14956168 17% /
/dev/sda1 1038336 153668 884668 15% /boot
tmpfs 186304 0 186304 0% /run/user/0
192.168.81.201:/web 17811456 1544320 16267136 9% /usr/local/scnginx99/html
[root@web1 ~] cd /usr/local/scnginx99/html
[root@web1 html] ls #此时nfs服务器中的页面已经对该目录中的页面进行覆盖
index.html sanchuang
二、设置开机自动挂载nfs系统文件(两种方式)
方法1:.修改/etc/rc.local
在文件中添加配置mount 192.168.81.201:/web /usr/local/scnginx99/html
保存后使用命令增加权限 chmod +x /etc/rc.d/rc.local
方法2:修改/etc/fstab
[root@web2 conf] vim /etc/fstab
[root@web2 conf] cat /etc/fstab
#
# /etc/fstab
# Created by anaconda on Sat Jan 28 23:44:15 2023
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/centos-root / xfs defaults 0 0
UUID=e2cf0ecb-e416-437e-8df7-04c913130275 /boot xfs defaults 0 0
/dev/mapper/centos-swap swap swap defaults 0 0
192.168.81.201:/web /usr/local/scnginx99/html nfs defaults 0 0 #添加配置
配置解释:
192.168.81.201:/web(挂载分区nfs文件系统) /usr/local/scnginx99/html(在本地的挂载点) nfs(文件系统类型) defaults(挂载选项使用默认) 0(是否支持dump命令进行备份) 0(是否开机的时候进行分区的文件系统检查,分区的文件系统是否有问题)