当前位置: 首页 > backend >正文

Linux-搭建NFS服务器

Linux-搭建NFS服务器

  • 前言
  • 一、网络配置
  • 二、在nfs服务器上安装nfs-utils软件包
  • 三、设置共享目录
  • 四、挂载NFS共享目录


前言

NFS(Network File System,网络文件系统) 是一种分布式文件系统协议,最初由 Sun Microsystems 于 1984 年开发。它允许客户端计算机通过网络访问远程服务器上的文件,就像访问本地文件一样。NFS 是 Unix 和 Linux 系统中常用的文件共享协议,支持跨平台文件共享

NFS解决了数据一致性的问题,实现了共享文件功能,所有web服务器都到NFS服务器里去获取网页文件、图片、音视频文件、css文件、js文件等,随便访问哪台web服务器,看到的内容都是一样的

一、网络配置

配置静态ip地址,防止今后服务器ip地址的变化,导致服务不能访问
vim /etc/NetworkManager/system-connections/ens33.nmconnection

禁用selinux功能
setenforce 0
vim /etc/selinux/config SELINUX=disabled

关闭firewalld服务和设置开机不启动

systemctl stop firewalld
systemctl disable firewalld


二、在nfs服务器上安装nfs-utils软件包

[root@dns-nfs ~]# yum install nfs-utils -y

建议web集群内的在web1和web2上也需要安装nfs-utils软件,因为节点服务器里需要支持nfs网络文件系统,需要挂载共享目录

在web1和web2上都安装nfs-utils软件,不需要启动nfs服务,主要是使用nfs服务器共享的文件夹,需要去挂载nfs文件系统

如果web服务器不安装nfs-utils软件,在mount挂载服务器的共享目录的时候,不能识别nfs文件系统,导致挂载不成功。

[root@web-1 ~]# yum install nfs-utils -y
[root@web-2 ~]# yum install nfs-utils -y

在nfs服务器上启动nfs服务并且设置开机启动就可以了(服务名称操作系统间可能会有不同)

  • centos中为nfs
  • rocky中为nfs-server

[root@dns-nfs ~]# service nfs-server start
Redirecting to /bin/systemctl start nfs-server.service
[root@dns-nfs named]# systemctl enable nfs-server
Created symlink /etc/systemd/system/multi-user.target.wants/nfs-server.service → /usr/lib/systemd/system/nfs-server.service.

查看nfs相关的进程

[root@dns-nfs ~]# ps aux|grep nfs
root       2399  0.0  0.0      0     0 ?        S<   10:31   0:00 [nfsd4_callbacks]
root       2405  0.0  0.0      0     0 ?        S    10:31   0:00 [nfsd]
root       2406  0.0  0.0      0     0 ?        S    10:31   0:00 [nfsd]
root       2407  0.0  0.0      0     0 ?        S    10:31   0:00 [nfsd]
root       2408  0.0  0.0      0     0 ?        S    10:31   0:00 [nfsd]
root       2409  0.0  0.0      0     0 ?        S    10:31   0:00 [nfsd]
root       2410  0.0  0.0      0     0 ?        S    10:31   0:00 [nfsd]
root       2411  0.0  0.0      0     0 ?        S    10:31   0:00 [nfsd]
root       2412  0.0  0.0      0     0 ?        S    10:31   0:00 [nfsd]
root       2443  0.0  0.0 112824   972 pts/0    S+   10:32   0:00 grep --color=auto nfs
root@dns-nfs ~]# netstat -anplut|grep nfs
[root@dns-nfs ~]#

查看是否有相关的nfs进程去监听某些端口,发现没有,为什么呢?
-> 因为nfsd进程将监听某个端口的功能外包给了rpc相关的进程


三、设置共享目录

新建提供web网页的目录/web/html文件夹

[root@dns-nfs ~]# mkdir /web/html -p

创建首页文件index.html,然后上传feng.jpg图片

[root@dns-nfs html]# ls
feng.jpg index.html

配置共享文件夹,允许其他人过来读写,/etc/exports文件是nfs共享目录的配置文件

[root@nfs-server ~]# vim /etc/exports
/web/html 192.168.168.0/24(rw,sync,all_squash)

刷新服务,输出共享目录

[root@nfs-server ~]# exportfs -rv
exporting 192.168.168.0/24:/web/html


四、挂载NFS共享目录

[root@web-1 ~]# mount  192.168.168.139:/web/html   /usr/local/nginx1/html/
# 查看linux系统里的磁盘分区使用情况
[root@web-1 ~]# df -Th
文件系统                  类型      容量  已用  可用 已用% 挂载点
devtmpfs                  devtmpfs  4.0M     0  4.0M    0% /dev
tmpfs                     tmpfs     1.8G     0  1.8G    0% /dev/shm
tmpfs                     tmpfs     725M  9.0M  716M    2% /run
/dev/mapper/rl-root       xfs        17G  2.2G   15G   13% /
/dev/sda1                 xfs       960M  242M  719M   26% /boot
tmpfs                     tmpfs     363M     0  363M    0% /run/user/0
192.168.168.139:/web/html nfs4       17G  1.5G   16G    9% /usr/local/nginx1/html# 进入查看,发现能看nfs服务器共享的文件夹里的内容
[root@web-1 ~]# cd /usr/local/nginx1/html/
[root@web-1 html]# ls
feng.jpg  index.html

在web2上挂着nfs共享目录

[root@web-2 ~]# mount 192.168.168.139:/web/html   /usr/local/nginx1/html/
[root@web-2 ~]# df -Th
文件系统                  类型      容量  已用  可用 已用% 挂载点
devtmpfs                  devtmpfs  4.0M     0  4.0M    0% /dev
tmpfs                     tmpfs     1.8G     0  1.8G    0% /dev/shm
tmpfs                     tmpfs     725M  9.0M  716M    2% /run
/dev/mapper/rl-root       xfs        17G  2.6G   15G   16% /
/dev/sda1                 xfs       960M  242M  719M   26% /boot
tmpfs                     tmpfs     363M     0  363M    0% /run/user/0
192.168.168.139:/web/html nfs4       17G  1.5G   16G    9% /usr/local/nginx8/html
[root@web-2 ~]# cd /usr/local/nginx8/html/
[root@web-2 html]# ls
feng.jpg  index.html

访问网站看是否网页内容都是一样,访问LB和web1、web2,验证内容是否一样

卸载的命令umount

[root@web-2 html]# umount /usr/local/nginx1/html/
umount.nfs4: /usr/local/nginx1/html: device is busy
[root@web-2 html]# pwd
/usr/local/nginx1/html
[root@web-2 html]# cd
[root@web-2 ~]# umount /usr/local/nginx1/html/
[root@web-2 ~]# df
文件系统                   1K-块    已用     可用 已用% 挂载点
devtmpfs                  485796       0   485796    0% /dev
tmpfs                     497852       0   497852    0% /dev/shm
tmpfs                     497852    7776   490076    2% /run
tmpfs                     497852       0   497852    0% /sys/fs/cgroup
/dev/mapper/centos-root 17811456 2259608 15551848   13% /
/dev/sda1                1038336  153860   884476   15% /boot
tmpfs                      99572       0    99572    0% /run/user/0

设置web1和web2开机自动挂着nfs共享目录

[root@web-1 html]# vim /etc/rc.local
#添加下面的配置
mount 192.168.168.139:/web/html /usr/local/nginx1/html/

http://www.xdnf.cn/news/19402.html

相关文章:

  • 【PyTorch】基于YOLO的多目标检测(一)
  • 【CNB.COOL】智能花卉分类系统 – 部署指北
  • 由题构造 嵌入汇编(汇编)
  • python调用豆包大模型给人脸生成卡通图像
  • 八大排序--快速排序
  • 福彩双色球第2025100期数据统计
  • hardhat 3 测试框架选择
  • linux系统学习(14.日志管理)
  • 华秋DFM检查PCB设计缺陷、一键导出Gerber、BOM、坐标文件
  • 第八章 光照
  • Qt QNetworkAccessManager 简述及例程
  • C++11——万能模板及完美转发
  • GMTapSDK 扩展使用文档
  • 【开题答辩全过程】以 基于springboot的垃圾分类管理系统为例,包含答辩的问题和答案
  • LSTM原理理解
  • 8.29学习总结
  • 大语言模型(LLM)简介与应用分享
  • Linux-数据库
  • 旅游景点库系统的设计与实现(代码+数据库+LW)
  • 力扣hot100:轮转数组(常规思路与三步反转讲解)(189)
  • mmaction安装的详细说明帖
  • 王立群《读史记-刘邦》读书笔记
  • 嵌入式C学习笔记之编码规范
  • 数学分析原理答案——第七章 习题12
  • AI大模型实战解析-RAG知识库+LangChain项目实战
  • Linux系统的进程管理
  • Unity3D Gizmos 调试可视化
  • Qt中UDP回显服务器和客户端
  • 第二十七天-ADC模数转换实验
  • python反转字符串