NFS存储服务

存储服务存在价值:
1.可以实现数据统一共享存储
2.节省架构服务运营成本

如何实现数据存储:
NFS(network file system --- 网络共享文件系统  不支持windows)
FTP(文件共享存储        --- windows比较方便简单 linux如何部署FTP --- 数据权限设置)
samba (linux部署服务端  可以让 windows 与 linux同时可以访问)

分布式存储:
Moosefs(mfs)、GlusterFS、FastDFS(推荐)


存储服务部署过程
RPC 远程过程调用程序---中介服务
服务端部署


wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo

1.nfs服务安装好之后,会自动创建nfsnobody虚拟用户
2.需要确认4个和用户相关的文件不能上锁
[root@nfs01 ~]# yum install nfs-utils rpcbind -y

[root@nfs01 ~]# rpm -qa nfs-utils
nfs-utils-1.3.0-0.61.el7.x86_64
[root@nfs01 ~]# rpm -qa rpcbind
rpcbind-0.2.0-47.el7.x86_64

[root@nfs01 ~]# vim /etc/exports
/data 172.16.1.0/24(rw,sync)

sync : 同步方式存储数据
同步方式存储数据:用户有数据存储  ----  存储服务器(磁盘中)           存储安全性高
异步方式存储数据:用户有数据存储  ----  内存 --- 存储服务器(磁盘中)  存储效率高

[root@nfs01 ~]# mkdir -p /data
[root@nfs01 ~]# id nfsnobody
uid=65534(nfsnobody) gid=65534(nfsnobody) groups=65534(nfsnobody)
[root@nfs01 ~]# chown nfsnobody:nfsnobody /data

[root@nfs01 ~]# ls -ld /data
drwxr-xr-x 2 nfsnobody nfsnobody 6 Jul 12 03:43 /data

[root@nfs01 ~]# systemctl enable rpcbind
[root@nfs01 ~]# systemctl start rpcbind 
[root@nfs01 ~]# systemctl status rpcbind

[root@nfs01 ~]# systemctl enable nfs
[root@nfs01 ~]# systemctl start nfs
[root@nfs01 ~]# systemctl status nfs

[root@nfs01 ~]# ps -ef | egrep "rpc|nfs" 
rpc       7283     1  0 Jul18 ?        00:00:00 /sbin/rpcbind -w
rpcuser   7398     1  0 00:01 ?        00:00:00 /usr/sbin/rpc.statd
root      7404     2  0 00:01 ?        00:00:00 [rpciod]
root      7409     1  0 00:01 ?        00:00:00 /usr/sbin/rpc.idmapd
root      7412     1  0 00:01 ?        00:00:00 /usr/sbin/rpc.mountd
root      7417     2  0 00:01 ?        00:00:00 [nfsd4_callbacks]
root      7465     2  0 00:01 ?        00:00:00 [nfsd]
root      7466     2  0 00:01 ?        00:00:00 [nfsd]
root      7467     2  0 00:01 ?        00:00:00 [nfsd]
root      7468     2  0 00:01 ?        00:00:00 [nfsd]
root      7469     2  0 00:01 ?        00:00:00 [nfsd]
root      7470     2  0 00:01 ?        00:00:00 [nfsd]
root      7471     2  0 00:01 ?        00:00:00 [nfsd]
root      7472     2  0 00:01 ?        00:00:00 [nfsd]
root      7548  6778  0 00:17 pts/0    00:00:00 grep -E --color=auto rpc|nfs


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

检查nfs服务是否存在可以共享目录
[root@web01 ~]# showmount -e 10.0.0.31
Export list for 10.0.0.31:
/data 172.16.1.0/24
[root@web01 ~]# showmount -e 172.16.1.31
Export list for 172.16.1.31:
/data 172.16.1.0/24


[root@web01 ~]# mount -t nfs 172.16.1.31:/data /mnt

[root@web01 ~]# df -Th
Filesystem        Type      Size  Used Avail Use% Mounted on
/dev/sda2         xfs        50G  1.9G   48G   4% /
devtmpfs          devtmpfs  229M     0  229M   0% /dev
tmpfs             tmpfs     240M     0  240M   0% /dev/shm
tmpfs             tmpfs     240M  5.3M  234M   3% /run
tmpfs             tmpfs     240M     0  240M   0% /sys/fs/cgroup
/dev/sdc1         xfs       200G   33M  200G   1% /backup
/dev/sdb1         xfs       500G   33M  500G   1% /data
/dev/sda1         xfs       197M  105M   93M  54% /boot
tmpfs             tmpfs      48M     0   48M   0% /run/user/0
172.16.1.31:/data nfs4      500G   33M  500G   1% /mnt


测试nfs存储
[root@web01 ~]# cd /mnt/
[root@web01 /mnt]# touch 1.txt

[root@web01 /mnt]# ls -l
total 0
-rw-r--r-- 1 nfsnobody nfsnobody 0 Jul 19 00:20 1.txt

服务端检查
[root@nfs01 ~]# ls -l /data/
total 0
-rw-r--r-- 1 nfsnobody nfsnobody 0 Jul 19 00:20 1.txt


[root@nfs01 ~]# cat /etc/yum.conf
[main]
cachedir=/var/cache/yum/$basearch/$releasever --- 下载软件保存路径
keepcache=0 --- 下载软件保留下来,不要被删除



[root@nfs01 ~]# ls -l /etc/exports
-rw-r--r-- 1 root root 30 Jul 18 23:49 /etc/exports
存储服务配置文件编写格式
ro  只读
rw  读写
sync  同步存储数据
async 异步存储数据
all_squash     将所有普通用户映射为指定nfsnobody用户
no_all_squash  将所有普通用户都不做映射
root_squash    将root用户映射为指定nfsnobody用户
no_root_squash 将root用户不做映射


all_squash映射实践:
[root@nfs01 ~]# vim /etc/exports
/data 172.16.1.0/24(rw,sync,all_squash)

[root@nfs01 ~]# systemctl reload nfs
restart 将所有连接会话都会直接断开
reload  只会将没有数据传输链接断开, 重新建立连接   让用户访问感受更好


[root@web01 /mnt]# useradd natasha
[root@web01 /mnt]# id natasha
uid=1000(natasha) gid=1000(natasha) groups=1000(natasha)

[root@web01 /mnt]# su - natasha
[natasha@web01 ~]$ cd /mnt/
[natasha@web01 mnt]$ touch 2.txt
[natasha@web01 mnt]$ ls -l
total 0
-rw-r--r-- 1 nfsnobody nfsnobody 0 Jul 19 00:20 1.txt
-rw-rw-r-- 1 nfsnobody nfsnobody 0 Jul 19 00:30 2.txt --- 所有普通用户都会做映射



no_all_squash映射实践:
[root@nfs01 ~]# vim /etc/exports
/data 172.16.1.0/24(rw,sync,no_all_squash)

[root@nfs01 ~]# chmod 777 /data
[root@nfs01 ~]# ls -ld /data   
drwxrwxrwx 2 nfsnobody nfsnobody 32 Jul 19 00:42 /data

[root@nfs01 ~]# systemctl reload nfs


[natasha@web01 mnt]$ touch 3.txt
[natasha@web01 mnt]$ ls -l
total 0
-rw-r--r-- 1 nfsnobody nfsnobody 0 Jul 19 00:20 1.txt
-rw-rw-r-- 1 nfsnobody nfsnobody 0 Jul 19 00:30 2.txt
-rw-rw-r-- 1 natasha   natasha   0 Jul 19 00:43 3.txt

[root@nfs01 ~]# ls -l /data/
total 0
-rw-r--r-- 1 nfsnobody nfsnobody 0 Jul 19 00:20 1.txt
-rw-rw-r-- 1 nfsnobody nfsnobody 0 Jul 19 00:30 2.txt
-rw-rw-r-- 1      1000      1000 0 Jul 19 00:43 3.txt --- 用户传输数据身份没有变化,看用户uid信息



root_squash映射实践:
[root@nfs01 ~]# vim /etc/exports
/data 172.16.1.0/24(rw,sync,root_squash)

[root@nfs01 ~]# systemctl reload nfs

[root@web01 /mnt]# touch 4.txt
[root@web01 /mnt]# ls -l
total 0
-rw-r--r-- 1 nfsnobody nfsnobody 0 Jul 19 00:20 1.txt
-rw-rw-r-- 1 nfsnobody nfsnobody 0 Jul 19 00:30 2.txt
-rw-rw-r-- 1 natasha   natasha   0 Jul 19 00:43 3.txt
-rw-r--r-- 1 nfsnobody nfsnobody 0 Jul 19 00:47 4.txt

[root@nfs01 /data]# ll
total 0
-rw-r--r-- 1 nfsnobody nfsnobody 0 Jul 19 00:20 1.txt
-rw-rw-r-- 1 nfsnobody nfsnobody 0 Jul 19 00:30 2.txt
-rw-rw-r-- 1      1000      1000 0 Jul 19 00:43 3.txt
-rw-r--r-- 1 nfsnobody nfsnobody 0 Jul 19 00:47 4.txt --- root用户会映射为指定的nfsnobody用户



no_root_squash映射实践:
[root@nfs01 ~]# vim /etc/exports
/data 172.16.1.0/24(rw,sync,no_root_squash)

[root@nfs01 ~]# systemctl reload nfs

[root@web01 /mnt]# touch 5.txt
[root@web01 /mnt]# ls -l
total 0
-rw-r--r-- 1 nfsnobody nfsnobody 0 Jul 19 00:20 1.txt
-rw-rw-r-- 1 nfsnobody nfsnobody 0 Jul 19 00:30 2.txt
-rw-rw-r-- 1 natasha   natasha   0 Jul 19 00:43 3.txt
-rw-r--r-- 1 nfsnobody nfsnobody 0 Jul 19 00:47 4.txt
-rw-r--r-- 1 root      root      0 Jul 19 00:49 5.txt

[root@nfs01 ~]# ls -l /data/
total 0
-rw-r--r-- 1 nfsnobody nfsnobody 0 Jul 19 00:20 1.txt
-rw-rw-r-- 1 nfsnobody nfsnobody 0 Jul 19 00:30 2.txt
-rw-rw-r-- 1      1000      1000 0 Jul 19 00:43 3.txt
-rw-r--r-- 1 nfsnobody nfsnobody 0 Jul 19 00:47 4.txt
-rw-r--r-- 1 root      root      0 Jul 19 00:49 5.txt



练习:
服务端:
/data/w   可读可写权限,所有用户都做映射,  采用同步传输数据
/data/r   只能读取数据,只有root用户组映射,采用异步传输数据

客户端:
backup   /data/w  --挂载点 /data/w   可以存储数据
web01    /data/r  --挂载点 /data/r   不能存储数据

服务端共享目录权限存在继承关系
建议设置共享目录时,不要存在父级与子级关系

服务端
[root@nfs01 ~]# vim /etc/exports
/data/w 172.16.1.0/24(rw,sync,all_squash)
/data/r 172.16.1.0/24(ro,async,root_squash)

[root@nfs01 ~]# mkdir -p /data/{r,w}
[root@nfs01 ~]# chown nfsnobody:nfsnobody /data/{r,w}
[root@nfs01 ~]# ls -ld /data/w
drwxr-xr-x 2 nfsnobody nfsnobody 6 Jul 19 00:55 /data/w
[root@nfs01 ~]# ls -ld /data/r
drwxr-xr-x 2 nfsnobody nfsnobody 6 Jul 19 00:55 /data/r

[root@nfs01 ~]# systemctl reload nfs


客户端
[root@web01 ~]# mkdir /data/r -p
[root@web01 ~]# mount -t nfs 172.16.1.31:/data/r /data/r
[root@web01 ~]# df -Th /data/r
Filesystem          Type  Size  Used Avail Use% Mounted on
172.16.1.31:/data/r nfs4  500G   33M  500G   1% /data/r


[root@backup ~]# yum -y install nfs-utils
[root@backup ~]# mkdir /data/w -p
[root@backup ~]# mount -t nfs 172.16.1.31:/data/w /data/w
[root@backup ~]# df -Th /data/w
Filesystem          Type  Size  Used Avail Use% Mounted on
172.16.1.31:/data/w nfs4  500G   33M  500G   1% /data/w


进行卸载:umount -lf


nfs挂载常见问题
异常问题一:
ls: cannot open directory .: Stale file handle  (文件句柄错误)
出现原因:当父级和子级目录同时进行挂载时,一旦父级目录取消共享,但是客户端还是处于挂载状态
问题解决:将和父级目录有关的所有挂载点全部卸载,重新挂载

异常问题二:
Cannot register service: RPC: Unable to receive;
出现原因:服务启动顺序不正确
问题解决:关闭所有服务,按顺序进行启动

异常问题三:
出现挂载卡死情况
clnt_create: RPC: Port mapper failure - Unable to receive: errno 113 (No route to host)
出现原因:服务端开启防火墙,阻止客户端访问
问题解决:关闭防火墙

异常问题四:
服务端采用restart情况,会有一个90s延迟;造成客户端挂载好共享目录,90s内无法存储数据
出现原因: 
[root@nfs01 ~]# cat /etc/sysconfig/nfs|grep 90
#NFSD_V4_GRACE=90
#NFSD_V4_LEASE=90

nfs存储排错原理:
01.检查服务端服务是否启动
02.检查nfs服务是否向rpc服务注册
[root@nfs01 ~]# rpcinfo -p 127.0.0.1
[root@nfs01 ~]# rpcinfo -p localhost
   program vers proto   port  service
    100000    4   tcp    111  portmapper
    100000    3   tcp    111  portmapper
    100000    2   tcp    111  portmapper
    100000    4   udp    111  portmapper
    100000    3   udp    111  portmapper
    100000    2   udp    111  portmapper
    100024    1   udp  54921  status
    100024    1   tcp  45810  status
    100005    1   udp  20048  mountd
    100005    1   tcp  20048  mountd
    100005    2   udp  20048  mountd
    100005    2   tcp  20048  mountd
    100005    3   udp  20048  mountd
    100005    3   tcp  20048  mountd
    100003    3   tcp   2049  nfs
    100003    4   tcp   2049  nfs
    100227    3   tcp   2049  nfs_acl
    100003    3   udp   2049  nfs
    100003    4   udp   2049  nfs
    100227    3   udp   2049  nfs_acl
    100021    1   udp  38851  nlockmgr
    100021    3   udp  38851  nlockmgr
    100021    4   udp  38851  nlockmgr
    100021    1   tcp  35456  nlockmgr
    100021    3   tcp  35456  nlockmgr
    100021    4   tcp  35456  nlockmgr


[root@web01 ~]# rpcinfo -p 172.16.1.31
   program vers proto   port  service
    100000    4   tcp    111  portmapper
    100000    3   tcp    111  portmapper
    100000    2   tcp    111  portmapper
    100000    4   udp    111  portmapper
    100000    3   udp    111  portmapper
    100000    2   udp    111  portmapper
    100024    1   udp  54921  status
    100024    1   tcp  45810  status
    100005    1   udp  20048  mountd
    100005    1   tcp  20048  mountd
    100005    2   udp  20048  mountd
    100005    2   tcp  20048  mountd
    100005    3   udp  20048  mountd
    100005    3   tcp  20048  mountd
    100003    3   tcp   2049  nfs
    100003    4   tcp   2049  nfs
    100227    3   tcp   2049  nfs_acl
    100003    3   udp   2049  nfs
    100003    4   udp   2049  nfs
    100227    3   udp   2049  nfs_acl
    100021    1   udp  38851  nlockmgr
    100021    3   udp  38851  nlockmgr
    100021    4   udp  38851  nlockmgr
    100021    1   tcp  35456  nlockmgr
    100021    3   tcp  35456  nlockmgr
    100021    4   tcp  35456  nlockmgr


[root@web01 ~]# showmount -e 172.16.1.31
Export list for 172.16.1.31:
/data/r 172.16.1.0/24
/data/w 172.16.1.0/24
/data   172.16.1.0/24


记录nfs服务默认配置参数信息
[root@nfs01 ~]# cat /var/lib/nfs/etab
/data/r 172.16.1.0/24(ro,async,wdelay,hide,nocrossmnt,secure,root_squash,no_all_squash,no_subtree_check,secure_locks,acl,no_pnfs,anonuid=65534,anongid=65534,sec=sys,ro,secure,root_squash,no_all_squash)
/data/w 172.16.1.0/24(rw,sync,wdelay,hide,nocrossmnt,secure,root_squash,all_squash,no_subtree_check,secure_locks,acl,no_pnfs,anonuid=65534,anongid=65534,sec=sys,rw,secure,root_squash,all_squash)
/data   172.16.1.0/24(rw,sync,wdelay,hide,nocrossmnt,secure,no_root_squash,no_all_squash,no_subtree_check,secure_locks,acl,no_pnfs,anonuid=65534,anongid=65534,sec=sys,rw,secure,no_root_squash,no_all_squash)
[root@nfs01 ~]#


可以平滑重启nfs服务,可以临时设置共享存储目录
[root@nfs01 ~]# exportfs -rv
exporting 172.16.1.0/24:/data/r
exporting 172.16.1.0/24:/data/w
exporting 172.16.1.0/24:/data


临时创建目录:exportfs -o rw,sync  192.168.10.0/24:/data01


实现开机自动挂载:
方法一:编写/etc/rc.local
mount -t nfs 172.16.1.31:/data  /mnt

方法二:编写/etc/fstab文件
172.16.1.31:/data   /mnt    nfs  defaults   0 0


centos6
启动系统 -- 加载fstab -- 启动network网络服务 -- netfs           (在系统启动完毕之后,再次加载fstab)
centos7
启动系统 -- 加载fstab -- 启动network网络服务 -- remote-fs.target(在系统启动完毕之后,再次加载fstab)


启动nfs服务客户端很慢:
出现原因:在客户端上配置自动nfs服务挂载 --- 耦合度太高
解决问题:取消自动挂载
启动顺序 先开启后端服务:nfs mysql backup 缓存服务,再开启前端服务:web服务 负载均衡服务


挂载参数说明:
defaults: 
rw, suid(setuid), dev, exec, auto(mount -a), nouser, and async(异步存储)

noatime
访问文件时不更新文件的inode时间戳,高并发环境下,推荐显示应用该选项,可以提高系统I/O性能。性能优化

nodiratime
不更新文件系统上的directory inode时间戳,高并发环境,推荐显式应用该选项,可以提高系统I/O性能。性能优化

remount
在不进行卸载挂载点时,直接重新挂载修改挂载参数
文件系统只读,mount -o remount,rw /
rsize=262144    用户 (读取) --- web01 /data  2M --- nfs /data 10M 压力大
                          设置一个缓存区 262144字节  设置大小和内存有关
wsize=262144    用户(存储)  --- web01 /data 10M --- nfs /data 10M 压力大
                          设置一个缓冲区 262144字节
hard            --- 当服务端处于关闭状态,客户端会处于一直挂载
soft            --- 当服务端处于关闭状态,不会一直挂载
proto=tcp       --- 挂载协议
客户端重要文件:/proc/mounts  --- 查看到mount挂载命令默认参数信息
原文地址:https://www.cnblogs.com/zhouwanchun/p/11208767.html