nfs

作者: 1220 | 来源:发表于2019-04-17 14:53 被阅读0次

1、什么是NFS?

全称 network file system 网络文件系统
通过网络存储和组织文件的一种方法或机制。
什么是文件系统?
文件系统是操作系统用于明确存储设备(常见的是磁盘,也有基于NAND Flash的固态硬盘)或分区上的文件的方法和数据结构;即在存储设备上组织文件的方法
之前学过的文件系统有ext

2、为什么要用共享存储?

前端所有的应用服务器接收到用户上传的图片、文件、视频,都会统一
的放到后端的存储上。
为什么要共享?所有节点服务器都需要将内容存到存储上,取的统一来取。

3、共享存储的种类

单点存储系统就是NFS,中小型企业,阿里云服务的NAS服务,OSS对象存储。
NFS性能不太高。
大型企业会用分布式存储FastDFS、Ceph、GlsterFS、Mfs

大型存储厂商:EMC、Netapp 几十万。
艺龙旅行网 存储用的EMC 传统企业。

硬件存储:传统企业:稳定、2台 双主机头 几十块硬盘 RAID10。
BAT 曾经用硬件,超贵、

阿里云 去IOE Ibm Orace EMC

4、NFS工作原理

NFS网络文件系统

启动NFS服务,而且还有启动很多端口。
NFS功能,需要很多服务。每个服务都有端口,而且经常变化。
如何让客户端找到这些端口呢?就需要一个经纪人(rpc服务)

NFS服务:

1、NFS服务(有很多进程和端口),把自己的端口告诉RPC。
2、RPC服务(对外固定端口111)

客户端请求NFS服务,先找RPC 111,查到NFS的端口,发给客户。
原理见图:


image.png
image.png

5、安装

服务器端和客户端都要安装:

yum install nfs-utils rpcbind -y
rpm -qa nfs-utils rpcbind 

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

启动RPC

[root@nfs01 ~]# systemctl start rpcbind.service 
[root@nfs01 ~]# systemctl enable rpcbind.service 

看看有没有注册的端口

[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

启动NFS

[root@nfs01 ~]# systemctl start nfs
[root@nfs01 ~]# systemctl enable nfs
[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  44521  status
    100024    1   tcp  59204  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  55758  nlockmgr
    100021    3   udp  55758  nlockmgr
    100021    4   udp  55758  nlockmgr
    100021    1   tcp  30472  nlockmgr
    100021    3   tcp  30472  nlockmgr
    100021    4   tcp  30472  nlockmgr

[root@nfs01 ~]# netstat -lntup|egrep "rpc|nfs"
tcp        0      0 0.0.0.0:20048           0.0.0.0:*      LISTEN      9516/rpc.mountd     
tcp        0      0 0.0.0.0:59204           0.0.0.0:*      LISTEN      9463/rpc.statd      
tcp6       0      0 :::20048                :::*           LISTEN      9516/rpc.mountd     
tcp6       0      0 :::14450                :::*           LISTEN      9463/rpc.statd      
udp        0      0 0.0.0.0:44521           0.0.0.0:*                  9463/rpc.statd      
udp        0      0 0.0.0.0:20048           0.0.0.0:*                  9516/rpc.mountd     
udp        0      0 0.0.0.0:695             0.0.0.0:*                  9424/rpcbind        
udp        0      0 127.0.0.1:735           0.0.0.0:*                  9463/rpc.statd      
udp6       0      0 :::20048                :::*                       9516/rpc.mountd     
udp6       0      0 :::695                  :::*                       9424/rpcbind        
udp6       0      0 :::4835                 :::*                       9463/rpc.statd 

6、配置nfs

NFS配置文件 /etc/exports

[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)待共享的目录 存东西的目录 取东西的目录例如:/data
2)访问的主机,
172.16.1.7(web01) 单个主机
172.16.1.0/24 网段
172.16.1.* 网段
master 主机名
3)()权限
rw 可读写 read write
ro 只读 read only
sync 写到磁盘才算完成,安全 慢
async 异步写到远程缓冲区,快 不安全

实践:

[root@nfs01 ~]# vim /etc/exports
/data 172.16.1.0/24(rw,sync)           《======进入编辑

[root@nfs01 ~]# tail -1 /etc/exports  《=====查看
/data 172.16.1.0/24(rw,sync)

[root@nfs01 ~]# mkdir -p /data         《=======创建目录
[root@nfs01 ~]# ls -ld /data
drwxr-xr-x 2 root root 51 4月  16 10:24 /data

NFS默认用户nfsnobody

[root@nfs01 ~]# grep nfsno /etc/passwd
nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin

[root@nfs01 ~]# chown -R nfsnobody.nfsnobody /data   《=======设置用户,用户组
[root@nfs01 ~]# ls -ld /data                                          
drwxr-xr-x 2 nfsnobody nfsnobody 51 4月  16 10:24 /data 《======查看

重启NFS

[root@nfs01 ~]# systemctl reload nfs   《==生产场景必须要实现平滑重启。
[root@nfs01 ~]# exportfs -r              
上述二者等价,选一个即可。

[root@nfs01 ~]# showmount -e 172.16.1.31
Export list for 172.16.1.31:
/data 172.16.1.0/24                              #《==看到共享的目录了

服务单本地挂载:

[root@nfs01 ~]# mount -t nfs 172.16.1.31:/data /mnt
[root@nfs01 ~]# df -h
文件系统           容量  已用  可用 已用% 挂载点
/dev/sda3           19G  1.8G   18G   10% /
devtmpfs           476M     0  476M    0% /dev
tmpfs              487M     0  487M    0% /dev/shm
tmpfs              487M  7.6M  479M    2% /run
tmpfs              487M     0  487M    0% /sys/fs/cgroup
/dev/sda1          253M  136M  118M   54% /boot
tmpfs               98M     0   98M    0% /run/user/0
172.16.1.31:/data   19G  1.8G   18G   10% /mnt

[root@nfs01 ~]# touch /mnt/oldboy.txt
[root@nfs01 ~]# ls /mnt/
oldboy.txt

换到web01挂载

1)安装

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

2)启动

[root@web01 ~]# systemctl start rpcbind      《==========开启RPC服务
[root@web01 ~]# systemctl enable rpcbind    《=========激活RPC开机自启动服务

[root@web01 ~]# netstat -lntup|grep rpc         
udp        0      0 0.0.0.0:775             0.0.0.0:*                           11624/rpcbind       
udp6       0      0 :::775                  :::*                                11624/rpcbind       

[root@web01 ~]# netstat -lntup|grep rpcbind
udp        0      0 0.0.0.0:775   0.0.0.0:*     11624/rpcbind       
udp6       0      0 :::775        :::*          11624/rpcbind       

[root@web01 ~]# ps -ef|grep rpcbind
rpc       11624      1  0 12:17 ?        00:00:00 /sbin/rpcbind -w

3)查看NFS服务器提供的共享目录

[root@web01 ~]# showmount -e 172.16.1.31
Export list for 172.16.1.31:
/data 172.16.1.0/24         《=============到这就已经成功了

nt_create: RPC: Program not registered
《=======如果出现这种情况就是开启服务顺序错误,
服务端需要以此关闭nfs和rpcbind,然后先开启rpcbind ,再开启nfs
意思是:程序 没有 注册
RPC服务开启了,但是NFS没有告诉RPC服务端口。

##4)挂载测试

[root@web01 ~]# mount -t nfs 172.16.1.31:/data /mnt
[root@web01 ~]# df -h
文件系统           容量  已用  可用 已用% 挂载点
/dev/sda3           19G  1.8G   18G   10% /
devtmpfs           476M     0  476M    0% /dev
tmpfs              487M     0  487M    0% /dev/shm
tmpfs              487M  7.6M  479M    2% /run
tmpfs              487M     0  487M    0% /sys/fs/cgroup
/dev/sda1          253M  136M  118M   54% /boot
tmpfs               98M     0   98M    0% /run/user/0
172.16.1.31:/data   19G  1.8G   18G   10% /mnt
[root@web01 ~]# touch /mnt/oldgirl.txt
[root@web01 ~]# ls /mnt/
oldboy.txt  oldgirl.txt

相关文章

网友评论

      本文标题:nfs

      本文链接:https://www.haomeiwen.com/subject/pwxnwqtx.html