第一章 如何查看Linux系统支持的文件系统类型
[root@CentOS7-111 ~]# ll /lib/modules/3.10.0-514.el7.x86_64/kernel/fs/
total 48
-rw-r--r--. 1 root root 21853 Nov 23 2016 binfmt_misc.ko
drwxr-xr-x. 2 root root 22 Jul 12 12:04 btrfs
drwxr-xr-x. 2 root root 27 Jul 12 12:04 cachefiles
drwxr-xr-x. 2 root root 21 Jul 12 12:04 ceph
drwxr-xr-x. 2 root root 21 Jul 12 12:04 cifs
drwxr-xr-x. 2 root root 23 Jul 12 12:04 cramfs
drwxr-xr-x. 2 root root 20 Jul 12 12:04 dlm
drwxr-xr-x. 2 root root 23 Jul 12 12:04 exofs
drwxr-xr-x. 2 root root 21 Jul 12 12:04 ext4
drwxr-xr-x. 2 root root 51 Jul 12 12:04 fat
drwxr-xr-x. 2 root root 24 Jul 12 12:04 fscache
drwxr-xr-x. 2 root root 36 Jul 12 12:04 fuse
drwxr-xr-x. 2 root root 21 Jul 12 12:04 gfs2
drwxr-xr-x. 2 root root 22 Jul 12 12:04 isofs
drwxr-xr-x. 2 root root 21 Jul 12 12:04 jbd2
drwxr-xr-x. 2 root root 22 Jul 12 12:04 lockd
-rw-r--r--. 1 root root 19597 Nov 23 2016 mbcache.ko
drwxr-xr-x. 6 root root 128 Jul 12 12:04 nfs
drwxr-xr-x. 2 root root 40 Jul 12 12:04 nfs_common
drwxr-xr-x. 2 root root 21 Jul 12 12:04 nfsd
drwxr-xr-x. 2 root root 4096 Jul 12 12:04 nls
drwxr-xr-x. 2 root root 24 Jul 12 12:04 overlayfs
drwxr-xr-x. 2 root root 24 Jul 12 12:04 pstore
drwxr-xr-x. 2 root root 25 Jul 12 12:04 squashfs
drwxr-xr-x. 2 root root 20 Jul 12 12:04 udf
drwxr-xr-x. 2 root root 20 Jul 12 12:04 xfs
第二章 NFS服务简介
1. NFS概念描述
什么是NFS?NFS是Network File Syestem的缩写,它的主要功能是通过网络让不同主机系统之间可以彼此共享文件和目录。NFS客户端(一般为应用服务器,如web)可以通过挂载(mount)的方式将NFS客户端的机器本地上看,NFS服务器的机器本地上看,NFS服务器端共享的目录就好像是客户端自己的磁盘分区或者目录一样,而实际上确实远端的服务器目录。
2. NFS在企业中的应用场景
在企业集群架构的工作中,NFS网络文件系统一般都被用来存储共享视频、图片附件等静态资源文件(一般把网站用户上传的文件都放到NFS共享里,例如BBS产品图片、附件、头像,注意网站BBS程序不要放NFS共享里),NFS是当前互联网系统架构中最常用的数据存储服务之一,特别是中小型网站公司应用频率很高。大公司或门户除了使用NFS外,还可能会使用MFS,GFS,FASTFS,TFS等分布式文件系统。
3. NFS共享文件系统作用
实现数据信息的共享,数据信息的统一管理
共享文件系统软件实现方式
共享文件系统硬件实现方式
4. NFS共享文件服务工作原理
第三章 NFS共享服务系统部署过程
1. NFS软件列表 需要安装下面软件包
nfs-utils:NFS服务的主恤,包括rpc.nfsd、rpc.mountd这两个daemon和相关文档说明,以及执行命令文件等。
rpcbind: CentOS 6.X下面的RPC主程序。NFS可以视为一个RPC程序,在启动任何一个RPC程序之前,需要做好端口和功能的对应映射工作,这个映射工作就是由rpccbind服务来完成的,因此,在提供NFS服务之前必须启动rpcbind服务才行。centos 5.x里面称为portmap
2. 检查NFSRPC软件是否安装
rpm -qa 查看文件版本信息/是否安装
[root@nfs01 ~]# rpm -qa nfs-utils rpcbind
rpm -ql查看包内内容
[root@web02 ~]# rpm -ql nfs-utils
/etc/nfsmount.conf
/etc/rc.d/init.d/nfs
/etc/rc.d/init.d/nfslock
/etc/rc.d/init.d/rpcgssd
/etc/rc.d/init.d/rpcidmapd
/etc/rc.d/init.d/rpcsvcgssd
/etc/request-key.d/id_resolver.conf
/etc/sysconfig/nfs
/sbin/mount.nfs
/sbin/mount.nfs4
/sbin/nfs_cache_getent
rpm -qf 查看文件/服务属于的打包
[root@nfs01 ~]# which rpcinfo
/usr/sbin/rpcinfo
[root@nfs01 ~]# rpm -qf /usr/sbin/rpcinfo
rpcbind-0.2.0-13.el6.x86_64
3. 启动rpcbind服务
[root@nfs01 ~]# /etc/init.d/rpcbind start
Starting rpcbind: [ OK ]
systemctl status rpcbind.service
4. rpcinfo主要服务NFS
[root@nfs01 ~]# rpcinfo -p localhost
program vers proto port service
100005 3 udp 31956 mountd
100005 3 tcp 29651 mountd
100003 2 tcp 2049 nfs
100003 3 tcp 2049 nfs
100003 4 tcp 2049 nfs
100227 2 tcp 2049 nfs_acl
100227 3 tcp 2049 nfs_acl
100003 2 udp 2049 nfs
100003 3 udp 2049 nfs
100003 4 udp 2049 nfs
100227 2 udp 2049 nfs_acl
100227 3 udp 2049 nfs_acl
100021 1 udp 13362 nlockmgr
5. 启动NFS服务
[root@nfs01 ~]# /etc/init.d/nfs start
Starting NFS services: [ OK ]
Starting NFS quotas: [ OK ]
Starting NFS mountd: [ OK ]
Starting NFS daemon: [ OK ]
Starting RPC idmapd: [ OK ]
systemctl start nfs-utils.service
6. NFS服务设置开机启动
[root@nfs01 ~]# chkconfig rpcbind on
[root@nfs01 ~]# chkconfig nfs on
[root@nfs01 ~]# chkconfig |egrep "rpcbind|nfs"
nfs 0:off 1:off 2:on 3:on 4:on 5:on 6:off
nfs-rdma 0:off 1:off 2:off 3:off 4:off 5:off 6:off
nfslock 0:off 1:off 2:off 3:on 4:on 5:on 6:off
rpcbind 0:off 1:off 2:on 3:on 4:on 5:on 6:off
[root@nfs01 ~]# head /etc/init.d/rpcbind
#! /bin/sh
#
# rpcbind Start/Stop RPCbind
#
# chkconfig: 2345 13 87
# description: The rpcbind utility is a server that converts RPC program \
# numbers into universal addresses. It must be running on the \
# host to be able to make RPC calls on a server on that machine.
#
# processname: rpcbind
[root@nfs01 ~]# head /etc/init.d/nfs
#!/bin/sh
#
# nfs This shell script takes care of starting and stopping
# the NFS services.
#
# chkconfig: - 30 60
# description: NFS is a popular protocol for file sharing across networks.
# This service provides NFS server functionality, which is \
# configured via the /etc/exports file.
# probe: true
systemctl enable rpcbind && systemctl enable nfs-utils
7. NFS软件进程详解说明
服务或进程名 | 用途说明 |
---|---|
nfsd(rpc.nfsd) | rpc.nfsd的主要功能是管理NFS客户端是否能够登入NFS服务器端主机,其中还包括登入者的ID判别等 |
mountd(rpc.mountd) | rpc.mountd的主要功能则是管理NFS文件系统,当NFS客户端顺利通过rpc.nfsd登入NFS服务器端主机时,在使用NFS服务器提供数据之前,她会去读NFS的配置文件/etc/export来比对NFS客户端的权限,通过这一关后,还要经过NFS服务器端本地文件系统使用权限(就是owner、group、other权限)等认证程序。如果都通过了,NFS客户端就可以取得使用NFS服务器端文件的权限。注意:这个/etc/export文件也是我们用来管理NFS共享目录的使用权限与安全设置的地方,特别强调,NFS本身设置的是网络共享权限,整个共享目录的权限还和目录自身的系统权限有关。 |
rpc.lockd | 用来锁定文件,用于客户端同时写入。 |
rpc.statd | 检查文件的一致性,与rpc.lockd有关,两个服务器需要客户端,服务端同时开启才可以,rpc.statd监听来自其他主机重启的通知,并且管理当本地系统重启时主机列表 |
rpc.idmapd | 名字映射后台进程 |
8. NFS配置文件信息编写
[root@nfs01 ~]# vim /etc/exports
/data 172.16.1.0/24(rw,sync)
/data 10.0.0.0/24(rw,sync)
[root@nfs01 scripts]# mkdir -p /data
reload重新读取配置文件 可以不影响正常使用 却别与restart
9. 修改共享目录权限
[root@nfs01 ~]# useradd -s /sbin/nologin -M nfsnobody
[root@nfs01 ~]# chown -R nfsnobody.nfsnobody /data
10. NFS软件服务平滑重启
[root@nfs01 ~]# /etc/init.d/nfs reload
11. NFS服务挂载列表查看
[root@nfs01 ~]# showmount -e 172.16.1.31
Export list for 172.16.1.31:
/data 10.0.0.0/24,172.16.1.0/24
12. NFS服务本地测试挂载
[root@nfs01 ~]# mount -t nfs 172.16.1.31:/data/ /tmp/
[root@nfs01 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 8.8G 1.5G 6.9G 18% /
tmpfs 931M 0 931M 0% /dev/shm
/dev/sda1 190M 38M 142M 22% /boot
172.16.1.31:/data/ 8.8G 1.5G 6.9G 18% /mnt
172.16.1.31:/data/ 8.8G 1.5G 6.9G 18% /tmp
[root@nfs01 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 8.8G 1.5G 6.9G 18% /
tmpfs 931M 0 931M 0% /dev/shm
/dev/sda1 190M 38M 142M 22% /boot
172.16.1.31:/data/ 8.8G 1.5G 6.9G 18% /mnt
13. 卸载NFS挂载目录
[root@nfs01 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 8.8G 1.5G 6.9G 18% /
tmpfs 931M 0 931M 0% /dev/shm
/dev/sda1 190M 38M 142M 22% /boot
172.16.1.31:/data/ 8.8G 1.5G 6.9G 18% /mnt
[root@nfs01 ~]# umount /mnt/
[root@nfs01 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 8.8G 1.5G 6.9G 18% /
tmpfs 931M 0 931M 0% /dev/shm
/dev/sda1 190M 38M 142M 22% /boot
14. NFS客户端进行共享目录挂载
[root@web02 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 8.8G 1.5G 6.9G 18% /
tmpfs 931M 0 931M 0% /dev/shm
/dev/sda1 190M 38M 142M 22% /boot
[root@web02 ~]# mount -t nfs 172.16.1.31:/data/ /data/
[root@web02 ~]# ll /data/
total 0
-rw-r--r-- 1 nfsnobody nfsnobody 0 May 9 11:46 1.txt
-rw-r--r-- 1 nfsnobody nfsnobody 0 May 4 21:46 a
-rw-r--r-- 1 nfsnobody nfsnobody 0 May 4 21:46 b
-rw-r--r-- 1 nfsnobody nfsnobody 0 May 9 14:57 b.txt
-rw-r--r-- 1 nfsnobody nfsnobody 0 May 4 21:46 c
-rw-r--r-- 1 nfsnobody nfsnobody 0 May 4 21:46 d
-rw-r--r-- 1 nfsnobody nfsnobody 0 May 4 21:46 e
-rw-r--r-- 1 nfsnobody nfsnobody 0 May 4 21:46 f
15. 测试读写删除等权限
[root@web02 ~]# cd /data/
[root@web02 data]# ll
total 0
-rw-r--r-- 1 nfsnobody nfsnobody 0 May 9 11:46 1.txt
-rw-r--r-- 1 nfsnobody nfsnobody 0 May 4 21:46 a
-rw-r--r-- 1 nfsnobody nfsnobody 0 May 4 21:46 b
-rw-r--r-- 1 nfsnobody nfsnobody 0 May 9 14:57 b.txt
-rw-r--r-- 1 nfsnobody nfsnobody 0 May 4 21:46 c
-rw-r--r-- 1 nfsnobody nfsnobody 0 May 4 21:46 d
-rw-r--r-- 1 nfsnobody nfsnobody 0 May 4 21:46 e
-rw-r--r-- 1 nfsnobody nfsnobody 0 May 4 21:46 f
[root@web02 data]# rm -f 1.txt a b
[root@web02 data]# ll
total 0
-rw-r--r-- 1 nfsnobody nfsnobody 0 May 9 14:57 b.txt
-rw-r--r-- 1 nfsnobody nfsnobody 0 May 4 21:46 c
-rw-r--r-- 1 nfsnobody nfsnobody 0 May 4 21:46 d
-rw-r--r-- 1 nfsnobody nfsnobody 0 May 4 21:46 e
-rw-r--r-- 1 nfsnobody nfsnobody 0 May 4 21:46 f
第四章 前端服务器开机自启动共享NFS
1. 加入到/etc/rc,local
[root@web02 data]# tail -1 /etc/rc.local
/bin/mount -t nfs 172.16.1.31:/data /mnt
2. 利用/etc/fstab 进行自动挂载,需要结合netd
客户端挂载参数信息查看:/proc/mounts
服务端挂载信息查看:/var/lib/nfs/etab
为什么在/etc/rc.local里面挂载就可以呢?
[root@web02 data]# ll /etc/rc.d/rc3.d/| egrep "rc.local"
lrwxrwxrwx. 1 root root 11 Mar 9 18:12 S99local -> ../rc.local
[root@web02 data]# head /etc/init.d/network
#! /bin/bash
#
# network Bring up/down networking
#
# chkconfig: 2345 10 90
# description: Activates/Deactivates all network interfaces configured to \
# start at boot time.
#
### BEGIN INIT INFO
# Provides: $network
NFS共享服务需要建立在网络服务之上,网络的启动顺序为10,而rc.local为99,网络已经启动,故可以使用。
[root@web02 data]# /etc/init.d/netfs start
Mounting filesystems: [ OK ]
[root@web02 data]# vim /etc/fstab
#
# /etc/fstab
# Created by anaconda on Thu Mar 9 18:10:24 2017
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=6d4bdd81-a1ce-449a-b0fb-f544eb78bd21 / ext4 defaults 1 1
UUID=af7c3c1a-71f7-4a82-ad4f-ce2e3435d0a7 /boot ext4 defaults 1 2
UUID=67843c7f-86a2-478f-a615-630c61ba208e swap swap defaults 0 0
tmpfs /dev/shm tmpfs defaults 0 0
devpts /dev/pts devpts gid=5,mode=620 0 0
sysfs /sys sysfs defaults 0 0
proc /proc proc defaults 0 0
172.16.1.31:/data /mnt nfs defaults 0 0
3. 服务器关机顺序
服务器关机顺序:
前端nfs客户端先关机、后端nfs服务器后关机
服务器开机顺序:
后端nfs服务器先开机,前端nfs客户端后开机
第五章 NFS配置文件详解
NFS常用路径 | 说明 |
---|---|
/etc/exports | NFS服务主配置文件,配置NFS具体共享服务的地点,默认内容为空。以行为单位。 |
/usr/sbin/exportfs | NFS服务的管理命令。例如可以加载NFS配置生效,还可以直接配置NFS共享目录,即无需配置/etc/exports实现共享。exportfs -rv ======/etc/init.d/nfs reload这里讲了一个服务平滑重启的概念,超市,银行到时间了如何提供服务,拒绝新来的,已有的顾客继续提供服务,网站平滑重启,提升用户体验是必须考虑的。 |
/usr/sbin/showmount | 常用来再客户端,查看NFS配置及挂载结果的命令 |
/var/lib/nfs/etab | NFS配置文件的完整参数设定的文件(有很多没有配置但是默认就有的NFS参数) |
/var/lib/nfs/xtab | 适合C5.x记录曾经挂载过的NFS客户端的信息,包括IP地址等,Centos6.4没有此文件了 |
第六章 mount挂载及fstab文件参数
第七章 showmount命令说明
短格式 | 长格式 | 用途及示例结果 |
---|---|---|
-e | –exports | 显示服务器输出的目录列表 showmount -e 172.16.1.31 |
-d | –directories | 显示NFS服务器中提供共享的目录 |
-a | –all | 显示NFS服务器的ip和可被挂载的目录[root@CentOS7-111 data]# showmount -a 172.16.1.111 All mount points on 172.16.1.111: |
第八章 NFS共享服务排错思路
1. 排查是否网络连通性有问题
PING 172.16.1.31 (172.16.1.31) 56(84) bytes of data.
64 bytes from 172.16.1.31: icmp_seq=1 ttl=64 time=0.015 ms
64 bytes from 172.16.1.31: icmp_seq=2 ttl=64 time=0.020 ms
2. 共享目录挂载很卡
mount -t nfs 172.16.1.31:/data /mnt
3. 原因分析:
NFS服务端重启之后。立刻进行挂载会出现此问题,因为NFS自身重启的时候,拥有无敌的时间,默认是90秒;
在无敌时间内,是不能对共享目录进行更改的操作;
在系统配置中/etc/sysconfig/nfs中指定了无敌时间的配置参数
[root@nfs01 ~]# vim /etc/sysconfig/nfs
48 #NFSD_V4_GRACE=90
49 #NFSD_V4_LEASE=90
50 #NLM_GRACE_PERIOD=90
第九章 企业生产场景NFS配置案例
参数名称 | 参数用途 |
---|---|
rw 重点掌握 | Read-write,表示可读写权限 |
ro | Read-only 表示只读权限 |
sync 重点掌握 (同步) | 请求或写入数据时,数据同步写入到NFS Server的硬盘后才返回。有点,数据安全不会丢。缺点,性能比不启用该参数要差 |
async 重点掌握 (异步) | 写入时数据会先写到内存缓冲区,知道硬盘有空档才会再写入磁盘,这样可以提升谢日效率!风险为若服务器宕机或不正常关机,会损失缓冲区中未写入磁盘的 数据。解决办法:服务器主板电池或加UPS,AB双路电源不间断电源.停电—-UPS(一般可撑30分钟至2小时左右)—发电机 |
no_root_squash | 保持root用户不进行映射访问NFS Server共享目录的用户是root,它对该共享目录具有root权限,这个配置原本是为无盘客户端准备的,用户应避免使用。 |
root_squash 重点掌握 | 如果访问NFS Server共享目录的用户是root,则它的权限将被压缩成匿名用户,同时,它的UID和GID通长会变为nfsnobody账号身份。所有的客户端和服务端都需要有一个相同的UID和GID的用户,即nfsnobody(UID必须相同) |
all_squash | 不管访问NFS Server共享目录的用户身份如何,它的权限都将被压缩成匿名用户,同时它的UID和GID都会变成nobody或nfsnobody账号身份。在多个NFS客户端同时读写NFS Server数据是,这个参数很有用。1配NFS生产重要技巧。2确保所有服务器对NFS共享目录具备相同的权限。3 all_squash所有用户都压缩。 |
anonuid 重点掌握 | 指定的是匿名用户的UID参数以anon*开头即指anonymous匿名用户,这个用户的UID设置值通长为nfsnobody的UID值,当然也可以自行设置这个UID值,但是UID必须存在于/etc/passwd中,,在多NFS Clients,如多台web server共享一个NFS Clients保持同样的用户权限,即为配置的匿名UID对应用户的权限,这个参数很有用,一般默认即可。 |
anongid 重点掌握 | 指定的是匿名用户组的GID同anonuid,区别就是把UID(用户id)换成gid |
提示:更多参数请查看man exports |
第十章 NFS共享系统服务的优缺点
NFS共享服务优点 | NFS共享服务缺点 |
---|---|
配置管理简单易用 | 存在单点异常故障 |
文件系统数据可见 | 大并发性能效率低 |
部署实施快速方便 | 访问认证机制简单 |
存储数据稳定可靠 | 数据传输无机密性 |
服务性能非常稳定 | 软件耦合机制过高 |