NFS共享存储服务介绍及搭建方法

NFS共享存储服务介绍及搭建方法

一、NFS共享服务介绍

  • NFS(Network File System)网络文件系统

    1、依赖于RPC(远程过程调用)

    2、需安装nfs-utils、rpcbind软件包

    3、系统服务:nfs、rpcbind

    4、配置文件路径:/etc/exports

网络文件系统,英文Network File System(NFS),是由SUN公司研制的UNIX协议(presentation layer protocol),能使使用者访问网络上别处的文件就像在使用自己的计算机一样

二、搭建NFS共享服务

实验环境:VMware Workstation 15.5、X Shell 6、Centos 7.6

实验步骤:

1、为了方便识别,分别将两台虚拟机主机名设置为server与client

[root@localhost ~]# hostname server
[root@localhost ~]# su
[root@server ~]# 
[root@localhost ~]# hostname client
[root@localhost ~]# su
[root@client ~]# 

2、在服务端安装nfs-utils与rpcbind,nfs-utils两个软件

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

3、在根目录下建立一个用于共享的目录

[root@server ~]# mkdir /share

4、为服务端添加一块用于共享的磁盘并为其创建分区并格式化

[root@localhost ~]# fdisk /dev/sdb   ## 对新创建的磁盘进行分区
欢迎使用 fdisk (util-linux 2.23.2)。

更改将停留在内存中,直到您决定将更改写入磁盘。
使用写入命令前请三思。

Device does not contain a recognized partition table
使用磁盘标识符 0x2afa2b90 创建新的 DOS 磁盘标签。

命令(输入 m 获取帮助):n        ## 创建新分区
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p):           ## 默认P 主分区
Using default response p
分区号 (1-4,默认 1):     ## 默认即可
起始 扇区 (2048-41943039,默认为 2048):    ## 默认即可
将使用默认值 2048
Last 扇区, +扇区 or +size{K,M,G} (2048-41943039,默认为 41943039):    ## 默认即可
将使用默认值 41943039
分区 1 已设置为 Linux 类型,大小设为 20 GiB

命令(输入 m 获取帮助):w    ## 保存并退出
The partition table has been altered!

Calling ioctl() to re-read partition table.
正在同步磁盘。

5、编辑nfs配置文件添加需要进行远程共享的目录

[root@localhost ~]# vi /etc/exports   ## 编辑配置文件

## 写入如下内容:
/share  192.168.50.0/24(rw,sync,no_root_squash)

PS:/share:需要共享的文件目录
    192.168.50.0/24:允许访问的网段
    rw,sync,no_root_squash:rw可读写,sync同步,no_root_squash:root远程用户使用时不降权

6、开启rpcbind与nfs服务并设置为开机自启动

[root@localhost ~]# systemctl start rpcbind
[root@localhost ~]# systemctl start nfs
[root@localhost ~]# systemctl enable rpcbind
[root@localhost ~]# systemctl enable nfs

7、查看nfs服务是否启动

[root@localhost ~]# netstat -natp | grep rpc*

tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      8514/rpcbind        
tcp        0      0 0.0.0.0:20048           0.0.0.0:*               LISTEN      18347/rpc.mountd    
tcp        0      0 0.0.0.0:37237           0.0.0.0:*               LISTEN      18327/rpc.statd     
tcp6       0      0 :::111                  :::*                    LISTEN      8514/rpcbind        
tcp6       0      0 :::20048                :::*                    LISTEN      18347/rpc.mountd    
tcp6       0      0 :::43906                :::*                    LISTEN      18327/rpc.statd     

8、查看nfs服务器相关信息

[root@localhost ~]# showmount -e   ## -e或--exports 显示NFS服务器的输出清单

Export list for localhost.localdomain:
/share 192.168.50.0/24

到此,NFS的服务端就配置完成了,现在来配置客户端

9、给客户端安装一个httpd服务

[root@client ~]# yum -y install httpd

10、使用mount命令将远程NFS服务器挂载到本地

## 注意:在挂载之前要关闭服务端的系统核心防护并清空防火墙规则!
[root@localhost ~]# iptables -F
[root@localhost ~]# setenforce 0

[root@client ~]# mount 192.168.50.131:/share /var/www/html/

## 使用df命令查看挂载情况

[root@client ~]# df -Th
文件系统              类型      容量  已用  可用 已用% 挂载点
/dev/sda3             xfs       295G  4.9G  291G    2% /
devtmpfs              devtmpfs  1.9G     0  1.9G    0% /dev
tmpfs                 tmpfs     1.9G     0  1.9G    0% /dev/shm
tmpfs                 tmpfs     1.9G   14M  1.9G    1% /run
tmpfs                 tmpfs     1.9G     0  1.9G    0% /sys/fs/cgroup
/dev/sda1             xfs      1014M  174M  841M   18% /boot
tmpfs                 tmpfs     378M   44K  378M    1% /run/user/1000
tmpfs                 tmpfs     378M     0  378M    0% /run/user/0
192.168.50.131:/share nfs4      20G  1.1G  18.9G    1% /var/www/html   ## 挂载成功!

如果想永久挂载,则可以进行如下设置:

[root@client ~]# vi /etc/fstab 

## 添加如下内容:

192.168.50.131:/share   /var/www/html   nfs     defaults,_netdev   0 0

含义:需要挂载的设备+挂载点(挂载到哪个地方)+文件系统+权限+0 0

U3JOg0.md.png

11、创建一个网站测试首页

[root@client ~]# vi /var/www/html/index.html

## 添加一个测试页面代码

<h1>this is a test website!</h1>

12、这个时候还不能访问,还没有开启httpd服务

[root@client ~]# service httpd start   ## 开启httpd服务

## 开启服务后还需要关闭客户机的系统核心防护并清空防火墙规则

[root@client ~]# iptables -F
[root@client ~]# setenforce 0

13、输入IP打开网站首页查看一下
U3JXvV.png

搭建成功!

注意:一定要记住,虽然网站是搭建在客户机之中,但服务端网页是写在服务端之中的!

验证一下:

[root@localhost share]# cat /share/index.html  ## 查看服务端内index.html文件内容
<h1>this is a test website!</h1>   ## 可以看到这就是之前在客户端上创建的web页面

三、常见故障处理

如果这个时候突然NFS服务端宕机或者因为某些原因无法访问会发生什么呢?

将服务端的nfs服务停止来人为模拟服务器业务故障

[root@localhost ~]# systemctl stop nfs

这时候再用df命令查看一下
U3JL3q.png

这时候会发现无论如何使用df命令都没有反应

所以这个时候可以用强制卸载来解决

[root@client ~]# umount -lf /var/www/html
[root@client ~]# df -Th
文件系统       类型      容量  已用  可用 已用% 挂载点
/dev/sda3      xfs       295G  4.9G  291G    2% /
devtmpfs       devtmpfs  1.9G     0  1.9G    0% /dev
tmpfs          tmpfs     1.9G     0  1.9G    0% /dev/shm
tmpfs          tmpfs     1.9G   14M  1.9G    1% /run
tmpfs          tmpfs     1.9G     0  1.9G    0% /sys/fs/cgroup
/dev/sda1      xfs      1014M  174M  841M   18% /boot
tmpfs          tmpfs     378M   44K  378M    1% /run/user/1000
tmpfs          tmpfs     378M     0  378M    0% /run/user/0

PS: -l, --lazy   立即断开文件系统,所有清理以后执行
     -f, --force  强制卸载(遇到不响应的NFS系统时)

猜你喜欢

转载自blog.csdn.net/u014042047/article/details/107298535