[toc]前端
什么是文件系统,NFS文件系统又是什么? 简单的说,文件系统就是经过软件对磁盘上的数据进行组织和管理的一种机制,对其的一种封装或透视。linux
NFS,Network File System。顾名思义,网络文件系统,即经过网络,对在不一样主机上的文件进行共享。web
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上的一致
网络
为何须要使用NFS呢? 在生产环境,咱们不多使用单机部署应用,由于单机存在单点故障,一旦宕机,将没法为前端业务提供服务,这是客户没法容忍的。如今提倡的高可用及7*24服务,是咱们追求的目标。 对于一些小的应用,双机成为了主流。咱们来看一种简单集群场景, 应用分别部署在A及B上, 前端经过F5或着web服务器访问应用。以下图:架构
那么当客户端发出一个请求时,F5根据必定的机制进行转发,有可能有A服务器进行响应,也有可能 由B服务器进行响应。 而在web应用中上传一些静态文件是很常见的一种功能,如图片。假设用户在某一时间,上传了一张照片到A服务器上,那么下次访问时,被F5路由到了B服务器上,因为B服务器上并无存储上传的照片,因此将形成用户没法看到本身上传的照片。并发
那么很容易想到,是否是能够把文件上传到一个公用的服务器上呢? 这样无论访问的是A仍是B,读、取文件都只存在一份。答案是确定的,这个公用的服务器咱们也称之为文件服务器。上面的架构就演变成了下面的架构:
app
注:此处固然也能够经过sftp或ftp进行文件上传和下载。dom
首先:NFS包括两部分,服务端及客户端
因为NFS服务功能不少,会有不少端口,这些端口还有可能不固定,那么客户端就没法与服务器进行通讯,由于程序间通讯必须经过端口(tcp/udp都是端到端通讯),那么就须要一个中间的桥接机制,RPC进程即充当这样一个角色,RPC的端口是必定的(111),当NFS启动时,会向RPC进行注册, 那么客户端PRC就能与服务器RPC进行通讯, 从而进行文件的传输。
当客户端用户打开一个文件或目录时,内核会判断,该文件是本地文件仍是远程共享目录文件,若是是远程文件则经过RPC进程访问远程NFS服务端的共享目录,若是是本地文件,则直接打开。 为了更好的并发,RPC进程及NFS进程都有多个。
# 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
[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
[root@xavi ~]# chmod 777 /home/nfstestdir/
[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
[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
[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.
上节内容中咱们已经安装好了nfs-utils包
使用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网段
[root@xavi ~]# setenforce 0 setenforce: SELinux is disabled [root@xavi-002 ~]# getenforce Disabled
[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
[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)
[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.
假如A的NFS服务停掉了,可是B和C还在挂载着A分享的分区,这就没法正常工做了,因为B和C上面的进程是d状态(不可中断进程,在ps一节中学习到的),一旦A中止了,B和C必定会出问题,没法服务,既不能够重启,也不能够关闭,就会把系统整挂掉。因此NFS必定不能够随便重启! 若是必须重启怎么办?先把B和C上面的目录卸载下来!
[root@xavi ~]# umount /mnt
如上这种方法只适合挂载了少许的机器,若是挂载的机器不少怎么办呢?
经常使用选项:
在服务器端操做:
# exportfs -avr exporting 192.168.72.0/24:/home/nfssharedir
[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)
[root@xavi ~]# exportfs -avr exporting 192.168.72.133:/tmp exporting 192.168.72.0/24:/home/nfstestdir
[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
[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
[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
NFS 4版本会有该问题
咱们在分享一个目录且权限设置为no_root_squash,客户端看到的文件属主、组不是root,倒是nobody!这个不是设置错误,是软件自己自带的BUG。
[root@xavi-002 ~]# mount -t nfs -o,nfsvers=3 192.168.72.130:/tmp /tmp/xavi/
[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