zoukankan      html  css  js  c++  java
  • linux_NFS

    NFS是什么?

           网络文件系统,又叫共享存储,通过网络连接让不同主机之间实现共享存储。

           应用于存放图片、附件、视频等用户上传文件

           相关同类应用:大型网站nfs有压力,使用moosefs(mfs),GhusterFS、FastDFS

    NFS挂载和普通分区挂载有什么区别?

           df -h 或 cat /proc/mounts  查看挂载信息

           设备名是 ip地址加NFS目录,挂载点也是空目录

           NFS需要rpc来进行管理,rpc相当于中介,NFS房子供应商

           普通挂载是写入文件系统的本地磁盘,而NFS是挂载远端服务目录

           普通挂载数据存放在本地磁盘,NFS数据存放在NFS服务器磁盘中

    NFS哪些优缺点?

           优点:

        1. 数据可见

        2. 简单,可控,可靠

        3. 非常稳定

      缺点:

        1. 存在单点故障,一旦nfs挂了会影响web服务器

        2. nfs在特大并发,其效率和性能有限(2千万/日pv NFS还是能支持),需要联合CDN使用

        3. 认证基于ip和主机名,不对数据完整性验证

        4. nfs和程序耦合度高

    NFS环境搭建:

      1. 服务端和客户端都需要安装相关依赖软件依赖

    yum install -y nfs-utils rpcbind
    
    rpm -aq | egrep "nfs|rpc"           # 检查

      2. 服务端

        其配置文件: /etc/exports

        1. 创建nfs挂载目录,假如挂载blog上传文件

    mkdir /data/nfs-blog -p                # 创建挂载目录

        2. 添加nfs管理用户

    useradd nfs-master -u 888 -s /sbin/nologin -M
    
    id nfs-master         # 检查

        3. 授权目录

    chown -R nfs-master. nfs-master /data
    
    ll -d /data                # 检查

        4. 编辑配置文件

    vim /etc/exports                          # 写入一下数据
    
    /data 172.16.1.0/24(rw,sync,all_squash,anonuid=888,anongid=888)
    
    # /data nfs服务器开放远端挂载点
    
    # 172.16.1.0/24 可以挂着nfs 的网段
    
    # rw 读写权限
    
    # sync 数据会同步写入磁盘中,async数据会先缓存,然后再写入磁盘
    
    # all_squash压缩身份为 anonuid 和 anongid 指定的主和组,其实是nfs-master
    
    # cat /var/lib/nfs/etab 查看挂载参数设置详情

        5. 启动服务

    /etc/init.d/rpcbind start                # 启动rpc
    
    /etc/init.d/nfs start                       # 启动nfs
    
    ps -ef | egrep "rpc|nfs"                # 检查
    
    # 这两个启动,有个专业细节,必须rpc先启动,然后再启动nfs

        6. 检查是否可以挂载

    showmount -e 172.16.1.5           # 检查是否可以挂载
    
    # 如果出现以下数据,表示nfs文件服务配置成功
    
    # Export list for 172.16.1.5:
    
    # /data 172.16.1.0/24

        7. 把这两个服务加入开机自启动

    chkconfig rpcbind on
    
    chkconfig nfs on
    
    chkconfig --list | egrep "rpcbind|nfs"   # 检查

      3. 客户端

    # 查看nfs服务器是否有挂载点
    showmount -e 172.16.1.5

        1. 启动rpc服务并把其加入开机自启

    /etc/init.d/rpcbind start                # 启动rpc
    
    ps -ef | egrep "rpcbind"               # 检查
    
    chkconfig rpcbind on                   # 让其开机自启
    
    chkconfig --list | grep rpcbind      # 检查

        2. 挂载

    mount -t nfs -o bg,hard,intr,nosuid,noexec,noatime 172.16.1.5:/data/nfs-blog /application/nginx/html/blog/wp-content/uploads
    
    # -t 制定文件系统
    
    # -o 声明后面指定参数
    
    # bg 后台持续挂载,不影响其他程序, fg前端持续挂载,直至超时
    
    # hard 会持续一直尝试挂载,直至挂载成功,保证服务的可靠性
    
    # intr 会阻止 hard 的死缠难打,防止hard死锁
    
    # nosuid nfs文件中数据suid权限失效
    
    # noexec nfs文件中数据不能执行
    
    # noatime 不更新存取时间
    
    # 挂载点格式为: nfs服务器ip:nfs开放挂载点,可以是其挂载点下子目录
    
    # 挂载目录是本地空目录
    

        3. 挂载命令添加到开机自启动

    echo 'mount -t nfs -o bg,hard,intr,nosuid,noexec,noatime 172.16.1.5:/data/nfs-blog /application/nginx/html/blog/wp-content/uploads' >> /etc/rc.local
    
    tail -1  /etc/rc.local # 检查 

    NFS相关优化

      1. 服务端(内核参数):

    echo -n "net.core.wmem_default = 8388608
    net.core.rmem_default = 8388608
    net.core.rmem_max = 16777216
    net.core.wmem_max = 16777216" >> /etc/sysctl.conf
    
    tail -4 /etc/sysctl.conf    # 检查
    
    sysctl -p                 # 告诉内核这个参数已经更改

      2. 存储优化

        硬件: ssd/sas 磁盘,硬件RAID最好做RAID10,至少RAID5,网卡至少1千兆

        统一服务端和客户端nfs文件身份,确保数据的一致性,exports参数选择sync确保数据安全

        特大型网站nfs有压力,可选择 moosefs(mfs),GhusterFS、FastDFS

    相关其他知识点:

           强制卸载挂载点: mount -lf /mnt

           也可以通过 /etc/fstab文件进行挂载,但这会带来一些问题,比较复杂也很危险,建议不用

  • 相关阅读:
    leetcode 131. Palindrome Partitioning
    leetcode 526. Beautiful Arrangement
    poj 1852 Ants
    leetcode 1219. Path with Maximum Gold
    leetcode 66. Plus One
    leetcode 43. Multiply Strings
    pytorch中torch.narrow()函数
    pytorch中的torch.repeat()函数与numpy.tile()
    leetcode 1051. Height Checker
    leetcode 561. Array Partition I
  • 原文地址:https://www.cnblogs.com/2bjiujiu/p/8097866.html
Copyright © 2011-2022 走看看