linux NFS共享存储

linux NFS共享存储

一、NFS的简介

1.1.1 什么NFS

NFS 是(Network Flie System)网络文件系统的缩写,通过网络存储和组织文件的一种方法或机制。

1.1.2 企业为什么用NFS共享存储

在企业中前端所有的应用服务器接收到用户上传的图片、文件、视频,都会统一存放到后端的存储服务器上,方便前端应用服务器的统一存取。

1.1.3 共享存储的种类

  1. 单点存储:NFS (中小型企业使用),阿里云服务的NAS服务,OSS对象服务。
  2. 分布式存储:FastDFS、Ceph、GlusterFS、Mfs(大型企业会用)。
  3. NFS的工作原理
    在c7系统中rpcbind和NFS是并行启动的所以和c6不同,c6要想启动rpcbind然后在启动NFS。我用的是c7,所以NFS工作的原理就,rpcbind和NFS同时启动之后NFS会将自己的所有服务端口注册到rpcbind上,如果客户端访问NFS会先请求rpcbind,rpcbind会把NFS的端口返回给客户端,然后客户端就会连接NFS的端口,NFS验证端口正确之后就和连接客户端。

二、NFS共享存储环境搭建

2.1.1 NFS服务端安装

  1. 安装服务端
[root@nfs01 ~]# yum install nfs-utils rpcbind -y 
[root@nfs01 ~]# rpm -qa  nfs-utils rpcbind 
rpcbind-0.2.0-49.el7.x86_64
nfs-utils-1.3.0-0.66.el7.x86_64
  1. 启动rpcbind和NFS
[root@nfs01 ~]# systemctl enable rpcbind.service 
[root@nfs01 ~]# systemctl enable nfs.service
Created symlink from /etc/systemd/system/multi-user.target.wants/nfs-server.service to /usr/lib/systemd/system/nfs-server.service.
[root@nfs01 ~]# systemctl start nfs.service
  1. 查看NFS在rpcbind上注册的端口服务
[root@nfs01 ~]# rpcinfo -p 127.0.0.1
   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   7194  status
    100024    1   tcp  20528  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  56231  nlockmgr
    100021    3   udp  56231  nlockmgr
    100021    4   udp  56231  nlockmgr
    100021    1   tcp  14003  nlockmgr
    100021    3   tcp  14003  nlockmgr
    100021    4   tcp  14003  nlockmgr
  1. 查看NFS和rpcbind的端口
[root@nfs01 ~]# netstat -luntp |grep -E "rpc|nfs"
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      1772/rpcbind        
tcp        0      0 0.0.0.0:20048           0.0.0.0:*               LISTEN      1870/rpc.mountd     
tcp        0      0 0.0.0.0:20528           0.0.0.0:*               LISTEN      1848/rpc.statd      
tcp6       0      0 :::39368                :::*                    LISTEN      1848/rpc.statd      
tcp6       0      0 :::111                  :::*                    LISTEN      1772/rpcbind        
tcp6       0      0 :::20048                :::*                    LISTEN      1870/rpc.mountd     
udp        0      0 0.0.0.0:7194            0.0.0.0:*                           1848/rpc.statd      
udp        0      0 0.0.0.0:111             0.0.0.0:*                           1772/rpcbind        
udp        0      0 0.0.0.0:20048           0.0.0.0:*                           1870/rpc.mountd     
udp        0      0 0.0.0.0:675             0.0.0.0:*                           1772/rpcbind        
udp        0      0 127.0.0.1:755           0.0.0.0:*                           1848/rpc.statd      
udp6       0      0 :::111                  :::*                                1772/rpcbind        
udp6       0      0 :::48616                :::*                                1848/rpc.statd      
udp6       0      0 :::20048                :::*                                1870/rpc.mountd     
udp6       0      0 :::675                  :::*                                1772/rpcbind      
  1. 如何配置NFS共享存储
[root@nfs01 ~]# ll /etc/exports
-rw-r--r--. 1 root root 0 Jun  7  2013 /etc/exports
查看如何配置NFS
[root@nfs01 ~]# man exports
EXAMPLE
       # sample /etc/exports file
       /               master(rw) trusty(rw,no_root_squash)
       /projects       proj*.local.domain(rw)
       /usr            *.local.domain(ro) @trusted(rw)
       /home/joe       pc001(rw,all_squash,anonuid=150,anongid=100)
       /pub            *(ro,insecure,all_squash)
       /srv/www        -sync,rw server @trusted @external(ro)
       /foo            2001:db8:9:e54::/64(rw) 192.0.2.0/24(rw)
       /build          buildhost[0-9].local.domain(rw)

  1. NFS共享存储的配置方法
    第一列:是共享的目录
    第二例:允许访问的主机(IP 域名 主机名)
    第三列:权限 (rw读写 ro只读)可选参数

  2. 配置NFS共享存储

[root@nfs01 ~]# cat /etc/exports
#this public file 
#/data  172.16.1.0/24(rw,sync) 10.0.0.0/24(ro,sync)
/data  172.16.1.0/24(rw,sync)

8.创建共享目录并授权

[root@nfs01 ~]# mkdir /data
[root@nfs01 ~]# id nfsnobody 
uid=65534(nfsnobody) gid=65534(nfsnobody) groups=65534(nfsnobody)
[root@nfs01 ~]# chown -R nfsnobody:nfsnobody /data
[root@nfs01 ~]# ll -d /data
drwxr-xr-x 2 nfsnobody nfsnobody 6 Apr 29 04:23 /data
  1. 重启NFS服务
[root@nfs01 ~]# systemctl restart nfs.service 
[root@nfs01 ~]# systemctl reload nfs 
[root@nfs01 ~]# exportfs -r
#上述三个命令是等价的,用其中一个即可。
  1. 检查是否成功
[root@nfs01 ~]# showmount -e
Export list for nfs01:
/data 172.16.1.0/24

  1. 本地挂载测试
[root@nfs01 ~]# mount -t nfs 172.16.1.31:/data /mnt
mount.nfs: access denied by server while mounting 172.16.1.31:/data
#出现权限被拒绝的报错:是配置文件里ip没有加掩码,加上掩码即可解决
[root@nfs01 ~]# mount -t nfs 172.16.1.31:/data /mnt
[root@nfs01 ~]# df -h 
Filesystem                    Size  Used Avail Use% Mounted on
devtmpfs                      979M     0  979M   0% /dev
tmpfs                         991M     0  991M   0% /dev/shm
tmpfs                         991M  9.5M  981M   1% /run
tmpfs                         991M     0  991M   0% /sys/fs/cgroup
/dev/mapper/centos_node-root   19G  2.1G   17G  12% /
/dev/sda1                     497M  156M  342M  32% /boot
tmpfs                         199M     0  199M   0% /run/user/0
172.16.1.31:/data              19G  2.1G   17G  12% /mnt
  1. 创建文件测试
[root@nfs01 ~]# cd /mnt/
[root@nfs01 /mnt]# ls -l
total 0
[root@nfs01 /mnt]# touch cs.txt
[root@nfs01 /mnt]# ls -l
total 0
-rw-r--r-- 1 nfsnobody nfsnobody 0 Apr 30 22:49 cs.txt
[root@nfs01 /mnt]# ll /data/
total 0
-rw-r--r-- 1 nfsnobody nfsnobody 0 Apr 30 22:49 cs.txt

2.1.2 客户端配置

  1. 客户端安装
[root@web01 ~]# yum install nfs-utils rpcbind -y 
#原则上客户端只安装rpcbind就可以的,但是nfs-utils安装包里有个showmount命令我们要使用所以安装上但是不要启动就可以了。
  1. 启动rpcbind
[root@web01 ~]# systemctl start rpcbind
[root@web01 ~]# systemctl enable rpcbind
[root@web01 ~]# systemctl status rpcbind
● rpcbind.service - RPC bind service
   Loaded: loaded (/usr/lib/systemd/system/rpcbind.service; enabled; vendor preset: enabled)
   Active: active (running) since Thu 2020-04-30 22:57:31 CST; 24s ago
 Main PID: 1883 (rpcbind)
   CGroup: /system.slice/rpcbind.service
           └─1883 /sbin/rpcbind -w

Apr 30 22:57:31 web01 systemd[1]: Starting RPC bind service...
Apr 30 22:57:31 web01 systemd[1]: Started RPC bind service.
  1. 端口检查
[root@web01 ~]# ss -luntp|grep rpc
udp    UNCONN     0      0         *:111                   *:*                   users:(("rpcbind",pid=1883,fd=6))
udp    UNCONN     0      0         *:786                   *:*                   users:(("rpcbind",pid=1883,fd=7))
udp    UNCONN     0      0      [::]:111                [::]:*                   users:(("rpcbind",pid=1883,fd=9))
udp    UNCONN     0      0      [::]:786                [::]:*                   users:(("rpcbind",pid=1883,fd=10))
tcp    LISTEN     0      128       *:111                   *:*                   users:(("rpcbind",pid=1883,fd=8))
tcp    LISTEN     0      128    [::]:111                [::]:*                   users:(("rpcbind",pid=1883,fd=11))
  1. 查看并挂载NFS共享存储
[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 -h
Filesystem                    Size  Used Avail Use% Mounted on
devtmpfs                      979M     0  979M   0% /dev
tmpfs                         991M     0  991M   0% /dev/shm
tmpfs                         991M  9.5M  981M   1% /run
tmpfs                         991M     0  991M   0% /sys/fs/cgroup
/dev/mapper/centos_node-root   19G  2.1G   17G  11% /
/dev/sda1                     497M  156M  342M  32% /boot
tmpfs                         199M     0  199M   0% /run/user/0
172.16.1.31:/data              19G  2.1G   17G  12% /mnt
  1. 创建文件进行测试
[root@web01 ~]# touch /mnt/web01.txt
[root@web01 ~]# ll  /mnt/
total 0
-rw-r--r-- 1 nfsnobody nfsnobody 0 Apr 30 22:49 cs.txt
-rw-r--r-- 1 nfsnobody nfsnobody 0 Apr 30 23:20 web01.txt
  1. 设置开机自启动
[root@web01 ~]# echo '172.16.1.31:/data         /mnt         nfs                 defaults   0     0' >>/etc/fstab 
[root@web01 ~]# tail -1 /etc/fstab
172.16.1.31:/data         /mnt         nfs                 defaults   0     0

7.因为在前面优化的时候把开机自动挂载的服务关了,所以没有挂载上,

[root@web01 ~]# systemctl start remote-fs.target 
[root@web01 ~]# systemctl enable remote-fs.target 
Created symlink from /etc/systemd/system/multi-user.target.wants/remote-fs.target to /usr/lib/systemd/system/remote-fs.target.
[root@web01 ~]# df -h
Filesystem                    Size  Used Avail Use% Mounted on
devtmpfs                      979M     0  979M   0% /dev
tmpfs                         991M     0  991M   0% /dev/shm
tmpfs                         991M  9.5M  981M   1% /run
tmpfs                         991M     0  991M   0% /sys/fs/cgroup
/dev/mapper/centos_node-root   19G  2.1G   17G  11% /
/dev/sda1                     497M  156M  342M  32% /boot
172.16.1.31:/data              19G  2.1G   17G  11% /data
tmpfs                         199M     0  199M   0% /run/user/0

挂载扩展

172.16.1.31:/data            /data                   nfs     defaults,_netdev          0 0

#这里的_netdev是防止nfs服务器挂掉,客户端不能启动的参数。
  1. autofs 服务
    这也是一个NFS的自动挂载工具,但是这个工具有很大的缺点,就是我往NFS共享存储里面放东西的时候,它才自动挂载NFS,平时都是卸载状态,如果并发很大的话,这样做就会降低效率。https://blog.csdn.net/weixin_42179528/article/details/90379307?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-1&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-1

2.1.3 NFS exports 重要配置参数说明

  1. rw允许在这个NFS卷上读取和写入请求。
  2. async 异步写到远程缓冲区参数
  3. sync 数据写到磁盘
  4. root_squash 如果访问NFS server共享目录的root用户,则它的权限将被压缩成匿名用户,同时它的UID和GID通常会变成nfsnobody的身份。
  5. no_root_squash 访问NFSserver共享目录的用户如果是root,它对该共享目录具有root权限
  6. all_squash 不过什么用户访问NFS server 共享目录的用户是什么身份,则它的权限将被压缩成匿名用户同时它的UID和GID通常会变成nfsnobody的身。
  7. anonuid 指定匿名用户的UID
  8. anongid 指定匿名用户的GID
  9. 这个文件中有NFS服务端的挂载参数

2.1.4 NFS服务端参数实战

服务端参数查看

[root@nfs01 ~]# cat /var/lib/nfs/etab 
/data	172.16.1.0/24(rw,sync,wdelay,hide,nocrossmnt,secure,root_squash,no_all_squash,no_subtree_check,secure_locks,acl,no_pnfs,anonuid=65534,anongid=65534,sec=sys,rw,secure,root_squash,no_all_squash)
  1. NFS默认用户改变
    创建WWW用户并指定UID和GID(在客户端也要创建相同的用户UID和GID一样)
[root@nfs01 ~]# useradd -u 888  -M  -s /sbin/nologin www
[root@nfs01 ~]# groupmod -g 888 www
[root@nfs01 ~]# id www
uid=888(www) gid=888(www) groups=888(www)

  1. 添加参数
[root@nfs01 ~]# tail -1 /etc/exports
/data 172.16.1.0/24(rw,sync,all_squash,anonuid=888,anougid=888)
  1. 把data共享目录的用户换成www
[root@nfs01 ~]# chown -R www.www /data
  1. 重启NFS服务
[root@nfs01 ~]# systemctl reload nfs
  1. 本地挂载成功
[root@nfs01 ~]# df -h
Filesystem                    Size  Used Avail Use% Mounted on
devtmpfs                      979M     0  979M   0% /dev
tmpfs                         991M     0  991M   0% /dev/shm
tmpfs                         991M  9.5M  981M   1% /run
tmpfs                         991M     0  991M   0% /sys/fs/cgroup
/dev/mapper/centos_node-root   19G  2.1G   17G  11% /
/dev/sda1                     497M  156M  342M  32% /boot
tmpfs                         199M     0  199M   0% /run/user/0
172.16.1.31:/data              19G  2.1G   17G  11% /mnt
  1. 客户顿挂载 web01
[root@web01 ~]# df -h
Filesystem                    Size  Used Avail Use% Mounted on
devtmpfs                      979M     0  979M   0% /dev
tmpfs                         991M     0  991M   0% /dev/shm
tmpfs                         991M  9.5M  981M   1% /run
tmpfs                         991M     0  991M   0% /sys/fs/cgroup
/dev/mapper/centos_node-root   19G  2.1G   17G  11% /
/dev/sda1                     497M  156M  342M  32% /boot
172.16.1.31:/data              19G  2.1G   17G  11% /data
tmpfs                         199M     0  199M   0% /run/user/0
  1. 客户端挂载 web02
[root@web02 ~]# df -h
Filesystem                    Size  Used Avail Use% Mounted on
devtmpfs                      979M     0  979M   0% /dev
tmpfs                         991M     0  991M   0% /dev/shm
tmpfs                         991M  9.5M  981M   1% /run
tmpfs                         991M     0  991M   0% /sys/fs/cgroup
/dev/mapper/centos_node-root   19G  2.1G   17G  11% /
/dev/sda1                     497M  156M  342M  32% /boot
172.16.1.31:/data              19G  2.1G   17G  11% /data
tmpfs                         199M     0  199M   0% /run/user/0
  1. 创建文件测试
[root@web02 ~]# touch /data/web02.txt
[root@web01 ~]# touch /data/web01.txt
[root@web01 ~]# ll /data/
total 0
-rw-r--r-- 1 www www 0 May  1 12:21 web01.txt
-rw-r--r-- 1 www www 0 May  1 12:20 web02.txt

2.1.5 NFS客户端参数说明

  1. /proc/mounts 查看挂载参数
[root@web01 ~]# cat /proc/mounts
172.16.1.31:/data /data nfs4 rw,relatime,vers=4.1,rsize=262144,wsize=262144,namlen=255,hard,proto=tcp,timeo=600,retrans=2,sec=sys,clientaddr=172.16.1.7,local_lock=none,addr=172.16.1.31 0 0
  1. 三种方法解决NFS服务端宕机,客户端无法启动的原因。
[root@web01 ~]# tail -3 /etc/fstab
#172.16.1.31:/data            /data                   nfs     defaults,_netdev          0 0
#172.16.1.31:/data            /data                   nfs     defaults,soft             0 0
172.16.1.31:/data            /data                   nfs     defaults,hard,intr          0 0
  1. 指定inode和block的大小
[root@web01 ~]# mount -t nfs -o hard,intr,rsize=131072,wsize=131072 172.16.1.31:/data/ /mnt

mount -0 本地参数优化
4. noexec 不允许在挂载文件中执行二进制命令,这样做可以提高安全
5. noatime 访问文件时不更新时间戳,高并发的情况下可以提高I/O性能。
6. nodiratime 访问目录时不更新时间戳,高并发的情况下可以提高I/O性能。
7. nosuid 不允许别人在这个分区上使用suid
8. 文件系统只读故障

https://blog.csdn.net/daydayup_gzm/article/details/52744540
  1. 生产中安全优化挂载(可选)
[root@web02 ~]# mount -t nfs -o nosuid,noexec,nodev,noatime,nodiratime,hard,intr,rsize=131072,wsize=131072 172.16.1.31:/data /mnt
#测试发现默认挂载就好
  1. nfs 内核优化
[root@nfs01 ~]# cat >>/etc/sysctl.conf<<EOF
> net.core.wmem_default = 8388608
> net.core.rmem_default = 8388608
> net.core.wmem_max = 16777216
> net.core.rmem_max = 16777216
> EOF
[root@nfs01 ~]# sysctl -p

net.core.wmem_default = 8388608

指定发送套接字缓冲区大小的默认值

net.core.rmem_default = 8388608

指定接收套接字缓冲区大小的默认值

net.core.wmem_max = 16777216

指定发送套接字缓冲区大小的最大值

net.core.rmem_max = 16777216

指定接收套接字缓冲区大小的最大值

三、NFS生产优化小结

3.1.1 磁盘优化

  1. 多块磁盘做raid5或raid10(最佳)。
  2. 网卡吞吐量要大,至少千兆(多块网卡可以做bond)https://www.jianshu.com/p/e31b2b5b2f22

3.1.2 服务端和客户端优化

1,请看上述笔记

3.1.3 内核优化

  1. 内核优化
[root@web02 ~]# cat >>/etc/sysctl.conf<<EOF
> net.core.wmem_default = 8388608
> net.core.rmem_default = 8388608
> net.core.wmem_max = 16777216
> net.core.rmem_max = 16777216
> EOF
[root@web02 ~]# sysctl -p

3.1.4 切割NFS

  1. 把多个目录分配到不同的NFS服务上

3.1.5 优化思想

  1. 能在前端访问解决的问题,就不要留到后端解决。

猜你喜欢

转载自www.cnblogs.com/woaiyunwei/p/12892749.html