项目实践:为集群中的应用服务器配置后端NFS服务作为NAS存储系统

  • 项目实践:为集群中的应用服务器配置后端NFS服务作为NAS存储系统已关闭评论
  • A+

一.简介

1.NFS网络文件系统

NFS(Network File System)即网络文件系统。

它是Unix系统之间共享文件的一种协议。

它支持多个节点同时挂载以及并发写入。

它允许网络中的计算机之间通过网络共享资源。

将NFS主机分享的目录,挂载到本地客户端当中,本地NFS的客户端应用可以透明地读写位于远端NFS服务器上的文件,在客户端端看起来,就像访问本地文件一样。

2.RPC远程过程调用

RPC,基于C/S模型。

程序可以使用这个协议请求网络中另一台计算机上某程序的服务而不需知道网络细节,甚至可以请求对方的系统调用。

对于Linux而言,文件系统是在内核空间实现的,即文件系统比如ext3、ext4等是在Kernel启动时,以内核模块的身份加载运行的。

二.原理

NFS本身的服务并没有提供数据传递的协议,而是通过使用RPC(远程过程调用 Remote Procedure Call)来实现。当NFS启动后,会随机的使用一些端口,NFS就会向RPC去注册这些端口。RPC就会记录下这些端口,RPC会开启111端口。通过client端和sever端端口的连接来进行数据的传输。在启动nfs之前,首先要确保rpc服务启动。

三.应用场景及规划信息

三台WEB服务器组成一个应用集群,后端通过一台NFS服务器作为NAS客户端,为前端应用服务器提供图片,视频存储。

基础规划信息:

  1. 操作系统:  
  2. CentOS Linux release 7.8.2003 (Core)  
  3. IP地址规划:  
  4. NFS服务器(NAS服务器):192.168.250.193  
  5. WEB服务器1(NFS客户端):192.168.250.194  
  6. WEB服务器2(NFS客户端):192.168.250.195  
  7. WEB服务器3(NFS客户端):192.168.250.196  

四.NFS服务端安装与配置

1.安装NFS服务

  1. [root@mynfs01 ~]# yum install -y nfs-utils  
  2. 已加载插件:fastestmirror, langpacks  
  3. Loading mirror speeds from cached hostfile  
  4.  * base: mirrors.aliyun.com  
  5.  * extras: mirrors.aliyun.com  
  6.  * updates: mirrors.aliyun.com  
  7. base                                                                                                                                                                                      | 3.6 kB  00:00:00       
  8. extras                                                                                                                                                                                    | 2.9 kB  00:00:00       
  9. updates                                                                                                                                                                                   | 2.9 kB  00:00:00       
  10. (1/2): extras/7/x86_64/primary_db                                                                                                                                                         | 246 kB  00:00:00       
  11. (2/2): updates/7/x86_64/primary_db                                                                                                                                                        |  14 MB  00:00:06       
  12. 正在解决依赖关系  
  13. --> 正在检查事务  
  14. ---> 软件包 nfs-utils.x86_64.1.1.3.0-0.66.el7 将被 升级  
  15. ---> 软件包 nfs-utils.x86_64.1.1.3.0-0.68.el7.2 将被 更新  
  16. --> 解决依赖关系完成  
  17.   
  18. 依赖关系解决  
  19.   
  20. =====================================================================================================  
  21.  Package                                          架构                                          版本                                                        源                                              大小  
  22. =====================================================================================================  
  23. 正在更新:  
  24.  nfs-utils                                        x86_64                                        1:1.3.0-0.68.el7.2                                          updates                                        413 k  
  25.   
  26. 事务概要  
  27. =====================================================================================================  
  28. 升级  1 软件包  
  29.   
  30. 总下载量:413 k  
  31. Downloading packages:  
  32. No Presto metadata available for updates  
  33. nfs-utils-1.3.0-0.68.el7.2.x86_64.rpm                                                                                                                                                     | 413 kB  00:00:05       
  34. Running transaction check  
  35. Running transaction test  
  36. Transaction test succeeded  
  37. Running transaction  
  38.   正在更新    : 1:nfs-utils-1.3.0-0.68.el7.2.x86_64                                                                                                                                                          1/2   
  39.   清理        : 1:nfs-utils-1.3.0-0.66.el7.x86_64                                                                                                                                                            2/2   
  40.   验证中      : 1:nfs-utils-1.3.0-0.68.el7.2.x86_64                                                                                                                                                          1/2   
  41.   验证中      : 1:nfs-utils-1.3.0-0.66.el7.x86_64                                                                                                                                                            2/2   
  42.   
  43. 更新完毕:  
  44.   nfs-utils.x86_64 1:1.3.0-0.68.el7.2                                                                                                                                                                              
  45.   
  46. 完毕!  
  47. [root@mynfs01 ~]  

启动后查询状态

  1. [root@mynfs01 /]# systemctl status nfs-server  
  2. ● nfs-server.service - NFS server and services  
  3.    Loaded: loaded (/usr/lib/systemd/system/nfs-server.service; disabled; vendor preset: disabled)  
  4.    Active: active (exited) since 六 2022-03-05 07:35:11 CST; 10min ago  
  5.   Process: 17212 ExecStartPost=/bin/sh -c if systemctl -q is-active gssproxy; then systemctl reload gssproxy ; fi (code=exited, status=0/SUCCESS)  
  6.   Process: 17196 ExecStart=/usr/sbin/rpc.nfsd $RPCNFSDARGS (code=exited, status=0/SUCCESS)  
  7.   Process: 17192 ExecStartPre=/usr/sbin/exportfs -r (code=exited, status=0/SUCCESS)  
  8.  Main PID: 17196 (code=exited, status=0/SUCCESS)  
  9.     Tasks: 0  
  10.    CGroup: /system.slice/nfs-server.service  
  11.   
  12. 3月 05 07:35:11 mynfs01 systemd[1]: Starting NFS server and services...  
  13. 3月 05 07:35:11 mynfs01 systemd[1]: Started NFS server and services.  
  14. [root@mynfs01 /]  

2.创建共享目录

  1. [root@mynfs01 /]# mkdir -p /mydata  
  2. [root@mynfs01 /]# mkdir -p /mylogs  
  3. [root@mynfs01 /]# mkdir -p /mytmp  
  4. [root@mynfs01 /]  

3.编写配置文件

  1. [root@mynfs01 /]# vim /etc/exports  
  2. [root@mynfs01 /]  
  3.   
  4.   
  5. /mytmp *(rw,sync,no_subtree_check,no_root_squash)  
  6. /mydata *(rw,sync,no_subtree_check,no_root_squash)  
  7. /mylogs *(rw,sync,no_subtree_check,no_root_squash)  
  8.   
  9. 注:上面*也可以指定具体的IP地址或子网  

上面参数的作用:

  1. Rw:该主机对该共享目录有读写权限  
  2. Sync:资料同步写入到内存与硬盘中  
  3. no_subtree_check:和上面相对,不检查父目录权限  
  4. No_root_squash:客户机用root访问该共享文件夹时,不映射root用户(不压制root,当client端使用root挂载时,也有root权限)  

4.重启服务

  1. [root@mynfs01 /]# systemctl restart nfs-server  
  2. [root@mynfs01 /]# systemctl enable nfs-server  
  3. Created symlink from /etc/systemd/system/multi-user.target.wants/nfs-server.service to /usr/lib/systemd/system/nfs-server.service.  
  4. [root@mynfs01 /]  

5.常用的查询命令工具

  • 1)显示已经mount到本机nfs目录的客户端机器
  1. [root@mynfs01 /]# showmount -e localhost  
  2. Export list for localhost:  
  3. /mylogs *  
  4. /mydata *  
  5. /mytmp  *  
  6. [root@mynfs01 /]  
  • 2)显示NFS文件系统信息
  1. [root@mynfs01 /]# exportfs -v  
  2. /mytmp          <world>(sync,wdelay,hide,no_subtree_check,sec=sys,rw,secure,no_root_squash,no_all_squash)  
  3. /mydata         <world>(sync,wdelay,hide,no_subtree_check,sec=sys,rw,secure,no_root_squash,no_all_squash)  
  4. /mylogs         <world>(sync,wdelay,hide,no_subtree_check,sec=sys,rw,secure,no_root_squash,no_all_squash)  
  5. [root@mynfs01 /]  
  • 3)查看NFS系统运行状态
  1. [root@mynfs01 mydata]# nfsstat  
  2. Server rpc stats:  
  3. calls      badcalls   badclnt    badauth    xdrcall  
  4. 482        0          0          0          0         
  5.   
  6. Server nfs v4:  
  7. null         compound       
  8. 1         0% 481      99%   
  9.   
  10. Server nfs v4 operations:  
  11. op0-unused   op1-unused   op2-future   access       close        commit         
  12. 0         0% 0         0% 0         0% 6         0% 0         0% 0         0%   
  13. create       delegpurge   delegreturn  getattr      getfh        link           
  14. 0         0% 0         0% 0         0% 70       10% 9         1% 0         0%   
  15. lock         lockt        locku        lookup       lookup_root  nverify        
  16. 0         0% 0         0% 0         0% 6         0% 0         0% 0         0%   
  17. open         openattr     open_conf    open_dgrd    putfh        putpubfh       
  18. 0         0% 0         0% 0         0% 0         0% 69       10% 0         0%   
  19. putrootfh    read         readdir      readlink     remove       rename         
  20. 6         0% 0         0% 2         0% 0         0% 0         0% 0         0%   
  21. renew        restorefh    savefh       secinfo      setattr      setcltid       
  22. 0         0% 0         0% 0         0% 0         0% 0         0% 0         0%   
  23. setcltidconf verify       write        rellockowner bc_ctl       bind_conn      
  24. 0         0% 0         0% 0         0% 0         0% 0         0% 0         0%   
  25. exchange_id  create_ses   destroy_ses  free_stateid getdirdeleg  getdevinfo     
  26. 1         0% 1         0% 0         0% 0         0% 0         0% 0         0%   
  27. getdevlist   layoutcommit layoutget    layoutreturn secinfononam sequence       
  28. 0         0% 0         0% 0         0% 0         0% 3         0% 479      73%   
  29. set_ssv      test_stateid want_deleg   destroy_clid reclaim_comp   
  30. 0         0% 0         0% 0         0% 0         0% 1         0%   
  31.   
  32. [root@mynfs01 mydata]  
  • 4)查看rpc执行信息,可以用于检测rpc运行情况
  1. [root@mynfs01 /]# rpcinfo | grep tcp  
  2.     100000    4    tcp6      ::.0.111               portmapper superuser  
  3.     100000    3    tcp6      ::.0.111               portmapper superuser  
  4.     100000    4    tcp       0.0.0.0.0.111          portmapper superuser  
  5.     100000    3    tcp       0.0.0.0.0.111          portmapper superuser  
  6.     100000    2    tcp       0.0.0.0.0.111          portmapper superuser  
  7.     100024    1    tcp       0.0.0.0.142.173        status     29  
  8.     100024    1    tcp6      ::.208.214             status     29  
  9.     100005    1    tcp       0.0.0.0.78.80          mountd     superuser  
  10.     100005    1    tcp6      ::.78.80               mountd     superuser  
  11.     100005    2    tcp       0.0.0.0.78.80          mountd     superuser  
  12.     100005    2    tcp6      ::.78.80               mountd     superuser  
  13.     100005    3    tcp       0.0.0.0.78.80          mountd     superuser  
  14.     100005    3    tcp6      ::.78.80               mountd     superuser  
  15.     100003    3    tcp       0.0.0.0.8.1            nfs        superuser  
  16.     100003    4    tcp       0.0.0.0.8.1            nfs        superuser  
  17.     100227    3    tcp       0.0.0.0.8.1            nfs_acl    superuser  
  18.     100003    3    tcp6      ::.8.1                 nfs        superuser  
  19.     100003    4    tcp6      ::.8.1                 nfs        superuser  
  20.     100227    3    tcp6      ::.8.1                 nfs_acl    superuser  
  21.     100021    1    tcp       0.0.0.0.138.78         nlockmgr   superuser  
  22.     100021    3    tcp       0.0.0.0.138.78         nlockmgr   superuser  
  23.     100021    4    tcp       0.0.0.0.138.78         nlockmgr   superuser  
  24.     100021    1    tcp6      ::.132.54              nlockmgr   superuser  
  25.     100021    3    tcp6      ::.132.54              nlockmgr   superuser  
  26.     100021    4    tcp6      ::.132.54              nlockmgr   superuser  
  27. [root@mynfs01 /]  
  • 5)查看网络端口

NFS默认是使用111端口

  1. [root@mynfs01 /]# netstat -anpl | grep 111  
  2. tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      738/rpcbind           
  3. tcp6       0      0 :::111                  :::*                    LISTEN      738/rpcbind           
  4. udp        0      0 0.0.0.0:111             0.0.0.0:*                           738/rpcbind           
  5. udp6       0      0 :::111                  :::*                                738/rpcbind           
  6. unix  2      [ ]         DGRAM                    101110   1/systemd              
  7. [root@mynfs01 /]  

五.客户端安装

1.安装客户端工具

  1. [root@mynfs02 ~]# yum -y install nfs-utils  
  2. 已加载插件:fastestmirror, langpacks  
  3. Loading mirror speeds from cached hostfile  
  4.  * base: mirrors.aliyun.com  
  5.  * extras: mirrors.aliyun.com  
  6.  * updates: mirrors.aliyun.com  
  7. base                                                                                      | 3.6 kB  00:00:00       
  8. extras                                                                                    | 2.9 kB  00:00:00       
  9. updates                                                                                   | 2.9 kB  00:00:00       
  10. (1/2): extras/7/x86_64/primary_db                                                         | 246 kB  00:00:06       
  11. (2/2): updates/7/x86_64/primary_db                                                        |  14 MB  00:00:13       
  12. 正在解决依赖关系  
  13. --> 正在检查事务  
  14. ---> 软件包 nfs-utils.x86_64.1.1.3.0-0.66.el7 将被 升级  
  15. ---> 软件包 nfs-utils.x86_64.1.1.3.0-0.68.el7.2 将被 更新  
  16. --> 解决依赖关系完成  

2.查看NFS服务器上的共享目录

这里需要关闭NFS服务端的防火墙,或者开放rpc端口

  1. [root@mynfs02 ~]# showmount -e 192.168.250.193  
  2. Export list for 192.168.250.193:  
  3. /mylogs *  
  4. /mydata *  
  5. /mytmp  *  
  6. [root@mynfs02 ~]  

3.建立本地挂载目录

  1. [root@mynfs02 /]# mkdir -p {/mnt/mydata,/mnt/mylogs,/mnt/mytmp}  
  2. [root@mynfs02 /]  
  3.   
  4. [root@mynfs02 mnt]# ll  
  5. 总用量 0  
  6. drwxr-xr-x. 2 root root 6 3月   5 08:48 mydata  
  7. drwxr-xr-x. 2 root root 6 3月   5 08:48 mylogs  
  8. drwxr-xr-x. 2 root root 6 3月   5 08:49 mytmp  
  9. [root@mynfs02 mnt]  

4.手动挂载共享目录

  1. [root@mynfs02 mnt]# mount -t nfs 192.168.250.193:/mydata /mnt/mydata  
  2. [root@mynfs02 mnt]  
  3. [root@mynfs02 mnt]# mount -t nfs 192.168.250.193:/mylogs /mnt/mylogs  
  4. [root@mynfs02 mnt]  
  5. [root@mynfs02 mnt]# mount -t nfs 192.168.250.193:/mytmp /mnt/mytmp  
  6. [root@mynfs02 mnt]  

5.查看挂载

  1. [root@mynfs02 /]# df -Th  
  2. 文件系统                类型      容量  已用  可用 已用% 挂载点  
  3. devtmpfs                devtmpfs  1.9G     0  1.9G    0% /dev  
  4. tmpfs                   tmpfs     1.9G     0  1.9G    0% /dev/shm  
  5. tmpfs                   tmpfs     1.9G  9.3M  1.9G    1% /run  
  6. tmpfs                   tmpfs     1.9G     0  1.9G    0% /sys/fs/cgroup  
  7. /dev/mapper/centos-root xfs        50G  4.1G   46G    9% /  
  8. /dev/vda1               xfs      1014M  185M  830M   19% /boot  
  9. /dev/mapper/centos-home xfs        42G   37M   42G    1% /home  
  10. tmpfs                   tmpfs     379M   12K  379M    1% /run/user/42  
  11. tmpfs                   tmpfs     379M     0  379M    0% /run/user/0  
  12. 192.168.250.193:/mydata nfs4       50G  4.1G   46G    9% /mnt/mydata  
  13. 192.168.250.193:/mylogs nfs4       50G  4.1G   46G    9% /mnt/mylogs  
  14. 192.168.250.193:/mytmp  nfs4       50G  4.1G   46G    9% /mnt/mytmp  
  15. [root@mynfs02 /]  

六.测试

在浏览器上访问WEB站点进行测试,过程略。

moonrong
  • 版权声明:本站原创文章,于2022年3月5日15:30:45,由 发表,共 13614 字。
  • 版权声明: 本文由于2022年3月5日15:30:45 发表在 好派笔记,共 13614 字。