zoukankan      html  css  js  c++  java
  • NFS服务

    nfs工作模式参考

    一、NFs是什么

    NFS是Network File System的缩写,即网络文件系统。客户端通过挂载的方式将NFS服务器端共享的数据目录挂载到本地目录下。

    nfs为什么需要RPC?
    因为NFS支持的功能很多,不同功能会使用不同程序来启动,因此,NFS对应的功能所对应的端口无法固定。
    端口不固定造成客户端与服务端之间的通信障碍,所以需要RPC来从中帮忙。
    NFS启动时会随机取用若干端口,然后主动向RPC服务注册取用相关端口和功能信息,RPC使用固定端口111来监听来自NFS客户端的请求,
    并将正确的NFS服务端口信息返回给客户端,这样客户端与服务端就可以进行数据传输了。

    二、NFS的工作流程

    1
    2
    3
    4
    1、由程序在NFS客户端发起存取文件的请求,客户端本地的RPC(rpcbind)服务会通过网络向NFS服务端的RPC的111端口发出文件存取功能的请求。
    2、NFS服务端的RPC找到对应已注册的NFS端口,通知客户端RPC服务。
    3、客户端获取正确的端口,并与NFS daemon联机存取数据。
    4、存取数据成功后,返回前端访问程序,完成一次存取操作。

    所以无论客户端,服务端,需要使用NFS,必须安装RPC服务。


    三、NFS服务安装配置
    nfs-utils、rpcbind
    查看是否安装NFS

    1
    2
    > rpm -qa nfs-utils rpcbind
    > yum install nfs-utils rpcbind

     
    四、启动rpcbind服务

    启动rpc服务

    1
    > systemctl start/stop/status rpcbind.service

    查看rpc端口,111是主端口,是客户端通信的端口

    1
    2
    > lsof -i :111
    > netstat -lntup|grep rpcbind

    如果出现-bash未找到命令可执行下面命令

    1
    > yum install net-tools lsof

    查看nfs服务向rpc注册的端口信息

    1
    > rpcinfo -p localhost

    五、启动NFS服务

    1
    > systemctl start/stop/status nfs.service

    再次查看rpc注册的端口信息

    1
    > rpcinfo -p localhost


    六、NFS常见进程详解

    1
    > ps -ef|egrep "rpc|nfs"
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    rpc      101101      1  0 17:11 ?        00:00:00 /sbin/rpcbind -w
    rpcuser  101188      1  0 17:22 ?        00:00:00 /usr/sbin/rpc.statd --no-notify
    root     101190      2  0 17:22 ?        00:00:00 [rpciod]
    root     101200      1  0 17:22 ?        00:00:00 /usr/sbin/rpc.idmapd
    root     101201      1  0 17:22 ?        00:00:00 /usr/sbin/rpc.mountd
    root     101206      2  0 17:22 ?        00:00:00 [nfsd4]
    root     101207      2  0 17:22 ?        00:00:00 [nfsd4_callbacks]
    root     101213      2  0 17:22 ?        00:00:00 [nfsd]
    root     101214      2  0 17:22 ?        00:00:00 [nfsd]
    root     101215      2  0 17:22 ?        00:00:00 [nfsd]
    root     101216      2  0 17:22 ?        00:00:00 [nfsd]
    root     101217      2  0 17:22 ?        00:00:00 [nfsd]
    root     101218      2  0 17:22 ?        00:00:00 [nfsd]
    root     101219      2  0 17:22 ?        00:00:00 [nfsd]
    root     101220      2  0 17:22 ?        00:00:00 [nfsd]
    root     101243 100830  0 17:28 pts/0    00:00:00 grep -E --color=auto rpc|nfs

    nfsd(rpc.nfsd)主进程,主要是管理客户端能否登入服务端,登入者ID判别。
    mountd(rpc.mountd)管理NFS文件系统,登入者的权限管理
    rpc.lockd(非必要)用来锁定文件,用于客户端同时写入
    rpc.statd(非必要)检查文件一致性
    rpc.idmapd 名字映射后台进程

    七、配置NFS/RPCBIND开机自启动

    1
    2
    > systemctl enable rpcbind.service
    > systemctl enable nfs.service

    八、NFS服务端配置文件

    exports文件配置格式:

    1
    NFS共享的目录 NFS客户端地址1(参数1,参数2,...) 客户端地址2(参数1,参数2,...)

    说明:
    NFS共享目录:

    1
    要用绝对路径,可被nfsnobody读写。

    NFS客户端地址:

    1
    2
    3
    4
    5
    指定IP: 192.168.0.1
    指定子网所有主机: 192.168.0.0/24
    指定域名的主机: test.com
    指定域名所有主机: *.test.com
    所有主机: *

    参数:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    ro:目录只读
    rw:目录读写
    sync:将数据同步写入内存缓冲区与磁盘中,效率低,但可以保证数据的一致性
    async:将数据先保存在内存缓冲区中,必要时才写入磁盘
    all_squash:将远程访问的所有普通用户及所属组都映射为匿名用户或用户组(nfsnobody)
    no_all_squash:与all_squash取反(默认设置)
    root_squash:将root用户及所属组都映射为匿名用户或用户组(默认设置)
    no_root_squash:与rootsquash取反
    anonuid=xxx:将远程访问的所有用户都映射为匿名用户,并指定该用户为本地用户(UID=xxx)
    anongid=xxx:将远程访问的所有用户组都映射为匿名用户组账户

    例如:

    1
    /data/tmp 192.168.1.0/24(rw,sync,all_squash)

    创建需要共享的目录

    1
    2
    mkdir -p /data/tmp
    chown -R nfsnobody.nfsnobody /data/tmp

    重新加载nfs配置

    1
    > exportfs -rv/systemctl reload nfs.service

    查看nfs服务器挂载情况

    1
    > showmount -e localhost

     
    九、客户端挂载测试/本地也可以

    安装rpc

    yum -y install nfs-utils rpcbind
    systemctl start rpcbind.start

    创建目录

    1
    2
    mkdir -p /data/tmp2
    > mount -t nfs 192.168.1.233:/data/tmp /data/tmp2

    查看挂载

    1
    > df -h

    在tmp下创建文件

    1
    > touch /data/tmp/1.txt

    查看tmp2下是否有文件

    1
    > ls /data/tmp2

    卸载挂载

    1
    > umount /data/tmp2

    注意:

    我们服务器和客户端都要安装nfs-utils、rpcbind
    mount: wrong fs type, bad option, bad superblock on 125.64.41.244:/data/img, missing codepage or helper program, or other error (for several filesystems (e.g. nfs, cifs) you might need a /sbin/mount.<type> helper program) In some cases useful info is found in syslog - try dmesg | tail or so 根据错误提示,查看/sbin/mount.<type>文件,果然发现没有/sbin/mount.nfs的文件,安装nfs-utils即可
    还可以通过
    /etc/fstab自动加载,但是会存在问题,因为系统加载是先走/etc/fstab,后走网络,我们的nfs是基于网络的,方法是有的,这里不一一说了,建议将挂载写入到/etc/rc.local即可

    10. 应用实践

    服务器端

    [root@Poppy ~]# yum install rpcbind nfs-utils
    [root@joker ~]# cat /etc/exports
    # share /nfsbackup by joker for poppy at today
    /nfsbackup nfs客户端ip(rw,sync,all_squash,anonuid=1006,anongid=1006) # 开启本地账号映射,就是远程过来的用户相当于本地账号www
    [root@joker ~]# id www
    uid=1006(www) gid=1006(www) groups=1006(www)
    [root@joker ~]# ls -ld /nfsbackup/
    drwxr-xr-x 3 www www 4096 Jun 12 18:01 /nfsbackup/
    [root@joker ~]# systemctl start nfs.service 
    [root@joker ~]# showmount -e 127.0.0.1      
    Export list for 127.0.0.1:
    /nfsbackup nfs客户端ip

    客户端

    [root@Poppy ~]# yum install rpcbind nfs-utils
    [root@Poppy ~]# systemctl start rpcbind.service
    [root@Poppy ~]# mount -t nfs nfs服务器ip/nfsbackup /joker/
    /joker/的目录最好是应用的软件有写的权限
  • 相关阅读:
    飞入飞出效果
    【JSOI 2008】星球大战 Starwar
    POJ 1094 Sorting It All Out
    POJ 2728 Desert King
    【ZJOI 2008】树的统计 Count
    【SCOI 2009】生日快乐
    POJ 3580 SuperMemo
    POJ 1639 Picnic Planning
    POJ 2976 Dropping Tests
    SPOJ QTREE
  • 原文地址:https://www.cnblogs.com/jokerbj/p/9106976.html
Copyright © 2011-2022 走看看