NFS服务配置

[toc]前端

NFS服务配置

1、NFS介绍

1.1 NFS文件系统

什么是文件系统,NFS文件系统又是什么? 简单的说,文件系统就是经过软件对磁盘上的数据进行组织和管理的一种机制,对其的一种封装或透视。linux

NFS,Network File System。顾名思义,网络文件系统,即经过网络,对在不一样主机上的文件进行共享。web

1.2 NFS起源与应用场景

NFS最先由Sun公司开发,分2,3,4三个版本,2和3由Sun起草开发,4.0开始Netapp公司参与并主导开发,最新为4.1版本,更新比较慢其实4.1是从2010年就开始使用了。vim

NFS数据传输基于RPC协议,RPC为Remote Procedure Call的简写。服务器

NFS应用场景是:A,B,C三台机器上须要保证被访问到的文件是同样的,A共享数据出来,B和C分别去挂载A共享的数据目录,从而B和C访问到的数据和A上的一致 mark网络

1.3 NFS架构

为何须要使用NFS呢? 在生产环境,咱们不多使用单机部署应用,由于单机存在单点故障,一旦宕机,将没法为前端业务提供服务,这是客户没法容忍的。如今提倡的高可用及7*24服务,是咱们追求的目标。 对于一些小的应用,双机成为了主流。咱们来看一种简单集群场景, 应用分别部署在A及B上, 前端经过F5或着web服务器访问应用。以下图:架构

mark

那么当客户端发出一个请求时,F5根据必定的机制进行转发,有可能有A服务器进行响应,也有可能 由B服务器进行响应。 而在web应用中上传一些静态文件是很常见的一种功能,如图片。假设用户在某一时间,上传了一张照片到A服务器上,那么下次访问时,被F5路由到了B服务器上,因为B服务器上并无存储上传的照片,因此将形成用户没法看到本身上传的照片。并发

那么很容易想到,是否是能够把文件上传到一个公用的服务器上呢? 这样无论访问的是A仍是B,读、取文件都只存在一份。答案是确定的,这个公用的服务器咱们也称之为文件服务器。上面的架构就演变成了下面的架构: markapp

NFS提供了一种机制,可让A、B访问C中的一个共享目录,就像是操做本地文件同样。

注:此处固然也能够经过sftp或ftp进行文件上传和下载。dom

1.4 NFS的原理:

mark

首先:NFS包括两部分,服务端及客户端

因为NFS服务功能不少,会有不少端口,这些端口还有可能不固定,那么客户端就没法与服务器进行通讯,由于程序间通讯必须经过端口(tcp/udp都是端到端通讯),那么就须要一个中间的桥接机制,RPC进程即充当这样一个角色,RPC的端口是必定的(111),当NFS启动时,会向RPC进行注册, 那么客户端PRC就能与服务器RPC进行通讯, 从而进行文件的传输。

当客户端用户打开一个文件或目录时,内核会判断,该文件是本地文件仍是远程共享目录文件,若是是远程文件则经过RPC进程访问远程NFS服务端的共享目录,若是是本地文件,则直接打开。 为了更好的并发,RPC进程及NFS进程都有多个。

2、服务端配置NFS

2.1 需求:两台机器。xavi001为服务器(192.168.72.130),xavi002为客户端进行测试(192.168.72.133):

2.2 在服务端和测试段都安装上rpcbind

# yum install -y nfs-utils      //yum工具安装nfs-utils时会一并安装recbind

小技巧关于安装包印象总体安装进度时,能够把相关安装源禁掉

如下以epel为例

[root@xavi ~]# cd /etc/yum.repos.d/
[root@xavi yum.repos.d]# ls
CentOS7-Base-163.repo  CentOS-fasttrack.repo  CentOS-Vault.repo
CentOS-CR.repo         CentOS-Media.repo      epel.repo
CentOS-Debuginfo.repo  CentOS-Sources.repo    epel-testing.repo
[root@xavi yum.repos.d]# mv epel.repo epel.repo.1
  • 将安装的epel文件move到另外地方,这样禁止好,能够加快安装进程。

2.3 编辑配置文件

[root@xavi ~]# vim /etc/exports 

/home/nfstestdir 192.168.72.0/24(rw,sync,all_squash,anonuid=1000,anongid=1000)

小括号内的权限选项含义:

rw: 读写

ro: 只读

sync: 同步模式,内存数据实时写入磁盘

async :非同步模式

no_root_squash: 客户端挂载NFS共享目录后,root用户不受约束,权限很大

root_squash: 与上面选项相对,客户端上的root用户收到约束,被限定成某个普通用户

all_squash: 客户端上全部用户在使用NFS共享目录时都被限定为一个普通用户

anonuid/anongid: 和上面几个选项搭配使用,定义被限定用户的uid和gid

2.4 建立目录并修改权限,方便后面测试

[root@xavi ~]# chmod 777 /home/nfstestdir/

2.5 查看rpc服务是否启动

[root@xavi ~]# netstat -lntp  //111端口说明rpcbind已启动
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      1/systemd           
tcp        0      0 192.168.122.1:53        0.0.0.0:*               LISTEN      1866/dnsmasq        
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1050/sshd           
tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN      1048/cupsd          
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1693/master         
tcp6       0      0 :::111                  :::*                    LISTEN      1/systemd           
tcp6       0      0 :::22                   :::*                    LISTEN      1050/sshd           
tcp6       0      0 ::1:631                 :::*                    LISTEN      1048/cupsd          
tcp6       0      0 ::1:25                  :::*                    LISTEN      1693/master   
[root@xavi ~]# ps aux |grep rpc  //rpcbind服务已启动
root        385  0.0  0.0      0     0 ?        S<   10:00   0:00 [rpciod]
rpcuser    2657  0.0  0.0  44464  1824 ?        Ss   10:11   0:00 /usr/sbin/rpc.statd
rpc        2659  0.0  0.0  64964  1348 ?        Ss   10:11   0:00 /sbin/rpcbind -w
root       2660  0.0  0.0  42564   940 ?        Ss   10:11   0:00 /usr/sbin/rpc.mountd
root       2661  0.0  0.0  43812   536 ?        Ss   10:11   0:00 /usr/sbin/rpc.idmapd
root       2700  0.0  0.0 112684   976 pts/0    S+   10:12   0:00 grep --color=auto rpc

2.6 启动NFS服务,这是rpc的相关联的服务也会被启动

[root@xavi ~]# systemctl start nfs
[root@xavi ~]# ps aux |grep nfs
root       2672  0.0  0.0      0     0 ?        S<   10:11   0:00 [nfsd4_callbacks]
root       2678  0.0  0.0      0     0 ?        S    10:11   0:00 [nfsd]
root       2679  0.0  0.0      0     0 ?        S    10:11   0:00 [nfsd]
root       2680  0.0  0.0      0     0 ?        S    10:11   0:00 [nfsd]
root       2681  0.0  0.0      0     0 ?        S    10:11   0:00 [nfsd]
root       2682  0.0  0.0      0     0 ?        S    10:11   0:00 [nfsd]
root       2683  0.0  0.0      0     0 ?        S    10:11   0:00 [nfsd]
root       2684  0.0  0.0      0     0 ?        S    10:11   0:00 [nfsd]
root       2685  0.0  0.0      0     0 ?        S    10:11   0:00 [nfsd]
root       2690  0.0  0.0 112680   976 pts/0    S+   10:11   0:00 grep --color=auto nfs
[root@xavi ~]# ps aux |grep rpc
root        385  0.0  0.0      0     0 ?        S<   10:00   0:00 [rpciod]
rpcuser    2657  0.0  0.0  44464  1824 ?        Ss   10:11   0:00 /usr/sbin/rpc.statd
rpc        2659  0.0  0.0  64964  1348 ?        Ss   10:11   0:00 /sbin/rpcbind -w
root       2660  0.0  0.0  42564   940 ?        Ss   10:11   0:00 /usr/sbin/rpc.mountd
root       2661  0.0  0.0  43812   536 ?        Ss   10:11   0:00 /usr/sbin/rpc.idmapd
root       2700  0.0  0.0 112684   976 pts/0    S+   10:12   0:00 grep --color=auto rpc

2.7 设置NFS开机启动项

[root@xavi ~]# systemctl enable nfs      
Created symlink from /etc/systemd/system/multi-user.target.wants/nfs-server.service to /usr/lib/systemd/system/nfs-server.service.

3、NFS客户端设置

上节内容中咱们已经安装好了nfs-utils包

3.1 shwomount查看服务端共享目录

使用showmount -e IP能够查看NFS的共享状况

[root@xavi-002 ~]# showmount -e 192.168.72.130
Export list for 192.168.72.130:
/home/nfstestdir 192.168.241.0/24 //共享目录为/home/nfstestdir,信任主机为192.168.241.0/24网段

共享目录为/home/nfstestdir,信任主机为192.168.241.0/24网段

若是没有出现共享目录,有多是服务端没有开启rpc服务,或者如今客户端的防火墙没有关闭,注意NFS是特殊的服务,必须关闭防火墙

[root@xavi ~]# setenforce 0
setenforce: SELinux is disabled
[root@xavi-002 ~]# getenforce
Disabled

3.2 在客户端挂载NFS

[root@xavi-002 ~]# mount -t nfs 192.168.72.130:/home/nfstestdir /mnt/
[root@xavi-002 ~]# df -h
文件系统                         容量  已用  可用 已用% 挂载点
/dev/sda3                         17G  3.6G   13G   23% /
devtmpfs                         479M     0  479M    0% /dev
tmpfs                            489M     0  489M    0% /dev/shm
tmpfs                            489M  7.1M  482M    2% /run
tmpfs                            489M     0  489M    0% /sys/fs/cgroup
/dev/sda1                        197M  109M   88M   56% /boot
tmpfs                             98M     0   98M    0% /run/user/0
192.168.72.130:/home/nfstestdir   17G   10G  6.2G   63% /mnt

3.3 在增长的/mnt分区,就是NFS共享的目录,在该目录下建立测试文件:

[root@xavi-002 ~]# cd /mnt/
[root@xavi-002 mnt]# ls
[root@xavi-002 mnt]# touch xavilinux.123
[root@xavi-002 mnt]# ls -l
总用量 0
-rw-r--r-- 1 xavi xavi 0 3月  27 10:55 xavilinux.123
[root@xavi-002 mnt]# id xavi
uid=1000(xavi) gid=1000(xavi) 组=1000(xavi),10(wheel)

3.4 在服务端查看

[root@xavi ~]# ls -l /home/nfstestdir/
总用量 0
-rw-r--r-- 1 xavi xavi 0 3月  27 10:55 xavilinux.123
[root@xavi ~]# id xavi
uid=1000(xavi) gid=1000(xavi) 组=1000(xavi),1004(grptest1)

能够看到建立的新文件xavilinux.txt全部者和所属组为xavi,其uid和gid都为1000.

4、exportfs命令

4.1 原理

mark

假如A的NFS服务停掉了,可是B和C还在挂载着A分享的分区,这就没法正常工做了,因为B和C上面的进程是d状态(不可中断进程,在ps一节中学习到的),一旦A中止了,B和C必定会出问题,没法服务,既不能够重启,也不能够关闭,就会把系统整挂掉。因此NFS必定不能够随便重启! 若是必须重启怎么办?先把B和C上面的目录卸载下来!

注意: 在重启nfs服务以前须要先将全部挂载点卸载,不然将发生程序错误,严重者会拖垮系统。

4.2 卸载已经挂在的目录:umount /mnt

[root@xavi ~]# umount /mnt

如上这种方法只适合挂载了少许的机器,若是挂载的机器不少怎么办呢?

4.3 exportfs:

经常使用选项:

  • [ ] -a 所有挂载或者所有卸载
  • [ ] -r 从新挂载
  • [ ] -u 卸载某一个目录
  • [ ] -v 显示共享目录

在服务器端操做:

# exportfs -avr
exporting 192.168.72.0/24:/home/nfssharedir
  • 这样就可让服务器直接生效,不须要再次重启nfs服务,也不会对客户端形成以前所说的错误!

4.4 测试(再次增长分享目录):

[root@xavi ~]# vi /etc/exports

/home/nfstestdir 192.168.72.0/24(rw,sync,all_squash,anonuid=1000,anongid=1000)
/tmp 192.168.72.133(rw,sync,no_root_squash)
  • 针对192.168.72.133客户端分享一个tmp目录
[root@xavi ~]# exportfs -avr
exporting 192.168.72.133:/tmp
exporting 192.168.72.0/24:/home/nfstestdir

4.5 使用客户端测试:

[root@xavi-002 ~]# showmount -e 192.168.72.130
Export list for 192.168.72.130:
/home/nfstestdir 192.168.72.0/24
/tmp             192.168.72.133
  • 在没有重启nfs服务的状况,正常的看到了挂载的目录。

4.6 再次挂载:

[root@xavi-002 ~]# mount -t nfs 192.168.72.130:/tmp /tmp/xavi/
[root@xavi-002 ~]# df -h
文件系统             容量  已用  可用 已用% 挂载点
/dev/sda3             17G  3.6G   13G   23% /
devtmpfs             479M     0  479M    0% /dev
tmpfs                489M     0  489M    0% /dev/shm
tmpfs                489M  7.1M  482M    2% /run
tmpfs                489M     0  489M    0% /sys/fs/cgroup
/dev/sda1            197M  109M   88M   56% /boot
tmpfs                 98M     0   98M    0% /run/user/0
192.168.72.130:/tmp   17G   10G  6.2G   63% /tmp/xavi

4.7 建立一个测试文本

[root@xavi-002 ~]# vim /tmp/xavi/1212.txt
[root@xavi-002 ~]# ls -l /tmp/xavi/1212.txt
-rw-r--r-- 1 root root 22 3月  29 00:05 /tmp/xavi/1212.txt
  • 咱们发现是root权限,这个是由于我们刚刚使用的no_root-squash

5、NFS客户端问题

5.1 客户端文件属主属组nobody

NFS 4版本会有该问题

咱们在分享一个目录且权限设置为no_root_squash,客户端看到的文件属主、组不是root,倒是nobody!这个不是设置错误,是软件自己自带的BUG。

5.2 解决方案:

方案1: 在客户端挂载的时候加上 -o nfsvers=3,其目的是制定nfs使用3版本。

[root@xavi-002 ~]# mount -t nfs -o,nfsvers=3 192.168.72.130:/tmp /tmp/xavi/

方案2:在服务端和客户端配置:vim /etc/idmapd.conf

[root@xavi-002 ~]# vim /etc/idmapd.conf

[General]
#Verbosity = 0
# The following should be set to the local NFSv4 domain name
# The default is the host's DNS domain name.
#Domain = local.domain.edu

# The following is a comma-separated list of Kerberos realm
# names that should be considered to be equivalent to the
"/etc/idmapd.conf" 114L, 3580C
  • 把“#Domain = local.domain.edu” 改成 “Domain = xxx.com” (这里的xxx.com,随意定义),而后再重启rpcidmapd服务
相关文章
相关标签/搜索