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/