访问网络附加存储:nfs

文章目录

访问网络附加存储

一、网络附加存储

1.1、存储类型

  • DAS:Direct Attached Storage,直连附加存储,将存储设备通过总线(SCSI、PCI、

    IDE等)接口直接连接到一台服务器上使用。

  • NAS:Network Attached Storage,网络附加存储,将设备通过TCP/IP网络共享,

    提供文件存储

  • SAN:Storage Area Network,存储区域网络,将设备通过光纤通道或者TCP/IP网

    络共享出去,提供块设备。SAN存储可以进一步划分为FC-SAN和IP-SAN。


存储类型 ### 1.2、RPC通信介绍

NFS 支持的功能相当的多,而不同的功能都会使用不同的程序来启动,每启动一个功能就会启用一些端口来传输数据,因此,NFS的功能所对应的端口没有固定住,而是随机取用一些未被使用的小于1024的端口提供服务。但如此一来客户端如何获得服务器端的相关端口呢?

此时我们就得需要远程过程调用(RPC)的服务!RPC最主要的功能是注册每个NFS功能所对应的端口,并且汇报给客户端,让客户端可以连结到正确的端口。


RPC通信原理

1.3、通过NFS挂载NAS

NFS是Network File System的缩写,即网络文件系统。最早由Sun公司开发,用来在UNIX&Linux系统间实现磁盘文件共享的一种方法。它的主要功能是通过网络让不同的主机系统之间可以共享文件或目录。NFS客户端(一般为应用服务器,例如web)可以通过挂载(mount)的方式将NFS服务端共享的数据目录挂载到NFS客户端本地系统中(就是某一个挂载点下)。从NFS客户端的机器本地看,NFS服务端共享的目录就好像是客户自己的磁盘分区或者目录一样,而实际上确是远端的NFS服务端的目录。

在企业集群架构的工作场景中,NFS网络文件系统一般被用来存储共享视频、图片、附件等静态资源文件。一般是把网站用户上传的文件都放在NFS共享里,例如,BBS产品的图片、附件、头像,特别是中小网站公司应用频率更高。

NFS服务器导出共享目录,NFS客户端将导出的共享挂载到本地。

挂载NFS共享方式:

  • 使用mount命令手动挂载。

  • 使用/etc/fstab条目在启动时自动挂载。

  • 按需挂载:使用autofs服务或systemd. automount功能。

1.4、NFS挂载过程

服务端
  1. 安装环境依赖包

    bash 复制代码
    rpm -q rpcbind nfs-utils 
    
    or
    
    yum -y install nfs-utils rpcbind
  2. 设置共享目录

    bash 复制代码
    mkdir -p /opt/nfs
    chmod 777 /opt/nfs
    
    vim /etc/exports
     /opt/nfs *.*.*.*/*(rw,sync,no_root_squash)

    客户机地址可以是主机名、IP 地址、网段地址,允许使用"*"、"?"通配符。

    "rw" 表示允许读写,"ro" 表示为只读。

    sync :表示同步写入到内存与硬盘中。

    no_root_squash : 表示当客户机以root身份访问时赋予本地root权限(默认是root_squash)。

    root_squash :表示客户机用root用户访问该共享目录时,将root用户映射成匿名用户。

    bash 复制代码
    all_squash :所有访问用户都映射为匿名用户或用户组。
    async :将数据先保存在内存缓冲区中,必要时才写入磁盘。
    subtree_check(默认):若输出目录是一个子目录,则nfs服务器将检查其父目录的权限。
    no_subtree_check :即使输出目录是一个子目录,nfs服务器也不检查其父目录的权限,这样可以提高效率。
    anonuid=xxx :指定NFS服务器/etc/passwd文件中匿名用户的UID 
    anongid=xxx :指定NFS服务器/etc/group文件中匿名用户的GID 
  3. 关闭防火墙,启动 NFS 服务程序

手动加载NFS共享服务时,应该先启动rpcbind,再启动nfs

bash 复制代码
systemctl stop firewalld
systemctl start rpcbind
systemctl start nfs
systemctl enable rpcbind
systemctl enable nfs

netstat -anptu | grep 111            ##rpcbind默认使用端口111
netstat -anptu | grep 2049             ##nfs默认使用端口2049
  1. 查看本机发布的 NFS 共享目录
bash 复制代码
exportfs -rv                            #发布共享
showmount -e
客户端
  1. 安装环境依赖包

    bash 复制代码
    rpm -q rpcbind nfs-utils 
    
    or
    
    yum -y install nfs-utils rpcbind
  2. 启动 NFS 服务程序

手动加载NFS共享服务时,应该先启动rpcbind,再启动nfs

复制代码
```bash
systemctl stop firewalld
systemctl start rpcbind
systemctl start nfs
systemctl enable rpcbind
systemctl enable nfs

netstat -anptu | grep 111            ##rpcbind默认使用端口111
netstat -anptu | grep 2049             ##nfs默认使用端口2049
```
  1. 挂载点:使用 mkdir 在合适的位置创建挂载点。

    bash 复制代码
    mkdir -p /opt/test
  2. 挂载:与分区上的文件系统一样,NFS共享必须先进行挂载才可用。

无论在哪种情况下,您都必须作为root用户登录,或使用sudo 命令,以超级用户身份运行这些命令。

  • 临时挂载

    bash 复制代码
    mount -t nfs -o rw,sync "nfs-serverIP":/opt/nfs /opt/test
       -t nfs选项是NFS共享的文件系统类型(默认值)。
       -o sync选项使mount立即与NFS服务器同步写操作(默认值为async)。
       下一次系统启动时,此NFS共享将不可用 ,可用于在持久提供共享之前对挂载NFS共享进行测试。
  • 永久挂载

    bash 复制代码
      vim /etc/fstab
        ...
      "nfs-serverIP":/opt/nfs /opt/test nfs rw,sync 0 0
  1. 卸载

    bash 复制代码
    umount "nfs-serverIP":/opt/nfs
    
    or
    
    umount mountpoint

二、实验:搭建NFS服务端及挂载到nfs客户端

服务端

  1. 安装环境依赖包

  2. 设置共享目录

  3. 关闭防火墙,启动 NFS 服务程序

  4. 查看本机发布的 NFS 共享目录

客户端

  1. 安装环境依赖包
  1. 启动 NFS 服务程序

  2. 创建挂载点

  3. 挂载

测试

在nfs服务端/opt/nfs下创建"wl.txt"文件,内容为"wl love to bath point 88 technician";在nfs客户端查看是否有"wl.txt"文件,并检查文件内容是否与服务端一致。

命令合集

服务端
bash 复制代码
[root@nfs-server ~]#yum -y install nfs-utils rpcbind
已加载插件:fastestmirror, langpacks
Determining fastest mirrors
local                                                                                                              | 3.6 kB  00:00:00     
软件包 1:nfs-utils-1.3.0-0.68.el7.2.x86_64 已安装并且是最新版本
软件包 rpcbind-0.2.0-49.el7.x86_64 已安装并且是最新版本
无须任何处理

[root@nfs-server ~]#mkdir -p /opt/nfs

[root@nfs-server ~]#chmod 777 /opt/nfs

[root@nfs-server ~]#vim /etc/exports

        /opt/nfs 10.190.33.0/24(rw,sync,no_root_squash)

[root@nfs-server ~]#systemctl stop firewalld

[root@nfs-server ~]#systemctl start rpcbind

[root@nfs-server ~]#systemctl start nfs

[root@nfs-server ~]#systemctl enable rpcbind

[root@nfs-server ~]#systemctl enable nfs
Created symlink from /etc/systemd/system/multi-user.target.wants/nfs-server.service to /usr/lib/systemd/system/nfs-server.service.

[root@nfs-server ~]#netstat -anptu | grep 111
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      713/rpcbind         
tcp6       0      0 :::111                  :::*                    LISTEN      713/rpcbind         
udp        0      0 0.0.0.0:111             0.0.0.0:*                           713/rpcbind         
udp6       0      0 :::111                  :::*                                713/rpcbind         

[root@nfs-server ~]#netstat -anptu | grep 2049
tcp        0      0 0.0.0.0:2049            0.0.0.0:*               LISTEN      -                   
tcp6       0      0 :::2049                 :::*                    LISTEN      -                   
udp        0      0 0.0.0.0:2049            0.0.0.0:*                           -                   
udp6       0      0 :::2049                 :::*                                -                   

[root@nfs-server ~]#exportfs -rv 
exporting 10.190.33.0/24:/opt/nfs

[root@nfs-server ~]#showmount -e
Export list for nfs-server:
/opt/nfs 10.190.33.0/24

[root@nfs-server ~]#echo wl love to bath point 88 technician > /opt/nfs/wl.txt

[root@nfs-server ~]#cat /opt/nfs/wl.txt 
wl love to bath point 88 technician
客户端
bash 复制代码
[root@nfs-client- ~]#yum -y install nfs-utils rpcbind
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
local                                                                                                              | 3.6 kB  00:00:00     
软件包 1:nfs-utils-1.3.0-0.68.el7.2.x86_64 已安装并且是最新版本
软件包 rpcbind-0.2.0-49.el7.x86_64 已安装并且是最新版本
无须任何处理

[root@nfs-client- ~]# systemctl start rpcbind

[root@nfs-client- ~]# systemctl stop firewalld

[root@nfs-client- ~]# systemctl start rpcbind

[root@nfs-client- ~]# systemctl start nfs

[root@nfs-client- ~]# systemctl enable rpcbind

[root@nfs-client- ~]# systemctl enable nfs
Created symlink from /etc/systemd/system/multi-user.target.wants/nfs-server.service to /usr/lib/systemd/system/nfs-server.service.

[root@nfs-client- ~]# mkdir -p /opt/test

[root@nfs-client- ~]# mount -t nfs -o rw,sync 10.190.33.10:/opt/nfs /opt/test

[root@nfs-client- ~]# mount -a

[root@nfs-client- ~]# df
文件系统                   1K-块    已用     可用 已用% 挂载点
devtmpfs                  480736       0   480736    0% /dev
tmpfs                     497816       0   497816    0% /dev/shm
tmpfs                     497816    8508   489308    2% /run
tmpfs                     497816       0   497816    0% /sys/fs/cgroup
/dev/mapper/centos-root 38770180 4585544 34184636   12% /
/dev/sda1                1038336  177468   860868   18% /boot
/dev/sr0                 4635056 4635056        0  100% /mnt/sr
tmpfs                      99564      12    99552    1% /run/user/42
tmpfs                      99564       0    99564    0% /run/user/0
10.190.33.10:/opt/nfs   38770304 4584960 34185344   12% /opt/test

[root@nfs-client- ~]# vim /etc/fstab
    ...
        10.190.33.10:/opt/nfs /opt/test nfs rw,sync 0 0

[root@nfs-client- ~]# mount -a

[root@nfs-client- ~]# df -h
文件系统                 容量  已用  可用 已用% 挂载点
devtmpfs                 470M     0  470M    0% /dev
tmpfs                    487M     0  487M    0% /dev/shm
tmpfs                    487M  8.4M  478M    2% /run
tmpfs                    487M     0  487M    0% /sys/fs/cgroup
/dev/mapper/centos-root   37G  4.4G   33G   12% /
/dev/sda1               1014M  174M  841M   18% /boot
/dev/sr0                 4.5G  4.5G     0  100% /mnt/sr
tmpfs                     98M   12K   98M    1% /run/user/42
tmpfs                     98M     0   98M    0% /run/user/0
10.190.33.10:/opt/nfs     37G  4.4G   33G   12% /opt/test

[root@nfs-client- ~]# ll /opt/test/
总用量 4
-rw-r--r--. 1 root root 36 5月   6 01:44 wl.txt

[root@nfs-client- ~]# cat /opt/test/wl.txt 
wl love to bath point 88 technician
相关推荐
盘古开天16662 小时前
如何用废弃电脑变成服务器搭建web网站(公网访问零成本)
服务器·电脑·免费公网ip
xuanzdhc4 小时前
Linux 基础IO
linux·运维·服务器
愚润求学4 小时前
【Linux】网络基础
linux·运维·网络
bantinghy5 小时前
Linux进程单例模式运行
linux·服务器·单例模式
小和尚同志6 小时前
29.4k!使用 1Panel 来管理你的服务器吧
linux·运维
帽儿山的枪手6 小时前
为什么Linux需要3种NAT地址转换?一探究竟
linux·网络协议·安全
shadon1789 天前
回答 如何通过inode client的SSLVPN登录之后,访问需要通过域名才能打开的服务
linux
AWS官方合作商9 天前
AWS ACM 重磅上线:公有 SSL/TLS 证书现可导出,突破 AWS 边界! (突出新功能的重要性和突破性)
服务器·https·ssl·aws
小米里的大麦9 天前
014 Linux 2.6内核进程调度队列(了解)
linux·运维·驱动开发
算法练习生9 天前
Linux文件元信息完全指南:权限、链接与时间属性
linux·运维·服务器