美文网首页
自动化安装NFS+autofs服务器

自动化安装NFS+autofs服务器

作者: ghbsunny | 来源:发表于2017-08-19 14:14 被阅读0次

    1 概述

    通过NFS搭建共享盘,方便共享资料

    本次使用,服务器ip的地址是192.168.32.11。需要共享的目录是/sharedisk

    2 服务器端

    2.1 安装软件

    服务器端安装软件包,有两个包

    yum -y install nfs-utils rpcbind

    2.2. 开启服务

    service nfs start

    service rpcbind start

    注意: 后续要重启服务的话,重启顺序建议是

    service rpcbind restart

    service nfs restart

    如果 [root@localhost /]#showmount -e 192.168.32.11

    出现报错  clnt_create: RPC: Program not registered    这个报错是由于重启顺序导致的

    按照以下顺序重启

    /etc/init.d/rpcbind stop

    /etc/init.d/nfs stop

    /etc/init.d/rpcbind start

    /etc/init.d/nfs start

    测试  showmount -e 192.168.32.113

    配置vim /etc/exports,服务器端的配置这个文件默认是空文件,写入以下的语句

    /sharedisk 192.168.32.0/24(rw,no_root_squash,insecure,sync)

    注意这里的192.168.32.0/24指定是允许哪些机器来挂载,这里如果没有指定对应的ip,客户端挂载的时候,会出现服务器端拒绝的报错

    2.3  配置生效

    exportfs -r

    2.4 检查

    输入命令

    showmount  -e

    结果如下,看到允许挂载的机器列表

    Export list for localhost.localdomain:

    /sharedisk 172.16.0.0/16,192.168.32.0/24

    2.5 共享文件夹的权限设置

    建议操作,把共享盘的权限设置为777.让其他机器可以自由访问该盘。当然,这个要根据实际情况分配权限

    chmod 777 /sharedisk

    2.6 查看所用到的端口

    rpcinfo -p

    添加允许端口到iptables或关闭iptables

    如果这台服务器不暴露在公网时或只能通过内网IP访问时这样做,否则不建议这样做。根据实际情况而定

    iptables -A INPUT -p tcp --dport 111 -j ACCEPT

    iptables -A INPUT -p udp --dport 111 -j ACCEPT

    2.7 服务器端设置开机启动nfs盘

    echo " /dev/sdb1/            /sharedisk              ext4      defaults      0 0">>/etc/fstab

    3 客户端

    3.1 安装软件

    客户端要安装nfs-utils

    yum install nfs-utils

    如果没有安装,挂载的时候会出现如下的报错

    [root@localhost sharedisk]# mount -amount: wrong fs type, bad option, bad superblock on 192.168.32.11:/sharedisk,      missing codepage or helper program, or other error      (for several filesystems (e.g. nfs, cifs) you might      need a /sbin/mount.helper program)

    In some cases useful info is found in syslog - try dmesg | tail or so.

    3.2 检查服务端共享情况

    showmount -e 192.168.32.11

    如果这里没有出现共享盘,要根据报错进行排查

    3.3 挂载共享盘

    3.3.1 挂载测试

    客户端临时挂载的语句如下

    mount -t nfs 192.168.32.11:/sharedisk/sharepoint/ /sharedisk/sharepoint

    注意,这里如果挂载不上,出现服务器端拒绝的报错,可能是/etc/exports这个文件没有把要挂载的机器写入

    这里并不建议用长期挂载的形式,因为nfs服务长期挂载,当服务器关机或者停止nfs进程的时候,会导致客户端没有正常卸载,这样一来,客户端关机关不了,只能通过断电才能关机。

    一下是写入/etc/fstab文件的语句,开机挂载,但是建议不要这么做,了解一下就好。

    echo " 192.168.32.11:/sharedisk /sharedisk            nfs        defaults  0 0">>/etc/fstab

    3.3.2 autofs挂载

    这里建议的解决办法是通过autofs进行挂载。

    客户端安装软件autofs

    yum install autofs

    配置autofs

    vim /etc/auto.master

    /nfsfile /etc/auto.nfs

    找一个目录,指定给autofs用

    vim  /etc/auto.nfs

    share  -rw,bg,soft,rsize=32768,wsize=32768 192.168.32.61:/sharedisk

    注意,auto.nfs这个文件写入的share这个是要挂载的目录,不能存在,完整目录是/nfsfile/share,进入 该目录/nfsfile就会自动生成,不要自己创建。后面是指定挂载的属性,最后是指定挂载的服务的盘。

    autofs会自动检测挂载点是否有使用,间隔5分钟没有使用,就会自动卸载/nfsfile/share这个盘。

    这里需要特别注意,如果autofs还没卸载,而且客户端正常访问/nfsfile/share这个挂载点,此时,服务器端停止nfs服务,或者服务器断网,或者是服务器挂机,其实/nfsfile/share相当于是一个不存在的目录,如果在/nfsfile下访问share,或者直接在share下访问,是有问题的,终端将会被卡在,解决办法是换个终端,等了20分钟后,该终端有可以使用,但是不建议这个时候在挂载点操作,最好是先进入到其他目录下工作,nfs服务器恢复正常后,再重新进入/nfsfile/share访问资源。

    但是如果nfs服务器还没恢复正常,还有终端在访问/nfsfile/share,即被卡在了/nfsfile/share路径下,此时如果关机,centOS7的nfs将不能正常关机,需要断电才能关机。

    启动服务

    启动服务后,cd /nfsfile/share就可以访问到nfs共享盘的资源了

    service autofs restart

    4 自动化脚本

    4.1 NFS 脚本

    #!/bin/bash

    #

    #******************************************************************************

    #Author:               Sunny

    #Date:                 2017-09-07

    #FileName:             install_nfs.sh

    #version:              1.0

    #Your change info:

    #Description:          For auto install nfs

    #Copyright(C):         2017  All rihts reserved

    #*****************************************************************************

    read -p  "Please input your choice,server or client: " choice

    read -p "if it is server,input full path dir as serverfile (eg:/sharedisk2): " serverfile

    read -p "if it is server,input path you want to  mount  serverfile(eg:/dev/sdb1: ): " mpoint

    case $choice in

    server)

    [ -e $serverfile ] && echo $serverfile is exist || mkdir $serverfile

    rpm -q nfs-utils &>/dev/null || yum -y install nfs-utils &>/dev/null;

    rpm -q rpcbind &>/dev/null || yum -y install rpcbind &>/dev/null;

    { service rpcbind restart;rpcinfo -p |grep portmapper; }&>/dev/null || echo rpcbind does not start

    { service nfs restart;rpcinfo -p |grep nfs; }&>/dev/null || echo nfs does not start

    echo "$serverfile 192.168.32.0/24(rw,no_root_squash,sync)">>/etc/exports

    echo "$serverfile 172.18.0.0/16(rw,no_root_squash,sync)">>/etc/exports

    exportfs -r

    if  $(grep "$serverfile"  /etc/fstab&>/dev/null) ;then

    echo "$serverfile already in /etc/fstab,please if you really want to mount $serverfile on $mpoint"

    elif  $(grep "$mpoint"  /etc/fstab&>/dev/null) ;then

    echo "$mpoint already in /etc/fstab,please if you really want to mount $serverfile on $mpoint"

    else

    echo " $mpoint            $serverfile              ext4      defaults      0 0">>/etc/fstab

    mount -a

    fi

    showmount  -e &>/dev/null && echo "nfs server start ok,you can check in client"|| echo "nfs start fail,please check"

    ;;

    client)

    rpm -q nfs-utils &>/dev/null || yum -y install nfs-utils &>/dev/null;

    showmount -e 192.168.32.61

    ;;

    esac

    unset choice

    unset serverfile

    unset mpoint

    exit

    4.2 autofs脚本

    #!/bin/bash

    #

    #***********************************************************************

    #Author:              Sunny

    #Date:                2017-09-08

    #FileName:            install_autofs.sh

    #version:              1.0

    #Your change info:

    #Description:          For install autofs by auto

    #Copyright(C):        2017  All rihts reserved

    #***********************************************************************

    read -p "Please input a full path  dir you want to auto mount(default:/nfsfile): " autodir

    read -p "Please creat a file in /etc you want to assign for autofs(default:auto.nfs): " autofile

    read -p "Please input remote source file ip and file (default:192.168.32.61:/sharedisk): " remotesrc

    if [ -z ${autodir:-} ];then

    autodir=/nfsfile

    fi

    if [ -z ${autofile:-} ];then

    autofile=auto.nfs

    fi

    if [ -z ${remotesrc:-} ];then

    remotesrc=192.168.32.61:/sharedisk

    fi

    [ -e "$autodir" ]&>/dev/null && echo "$autodir already exist"|| mkdir "$autodir"

    rpm -q autofs &>/dev/null || yum -y install autofs&>/dev/null;

    #to assign a dir(in the eg is share ) which you want to access to the source dir

    #attention,share is a dir,but is should not exist,the dir will exist when you access to it

    if [ -e /etc/$autofile ];then

    echo "$autofile is exist,please check"

    exit  6;

    else

    cat >>/etc/$autofile<

    share  -rw,bg,soft,rsize=32768,wsize=32768 $remotesrc

    eof

    sed -r -i.bak "/^\/misc/ a $autodir  \/etc\/$autofile" /etc/auto.master

    fi

    service autofs restart &>/dev/null || echo "something unexpect happened,please check autofs service"

    [ -e /root/bakfile ] || mkdir /root/bakfile

    mv /etc/*.bak /root/bakfile

    unset autodir

    unset autofile

    unset remotesrc

    exit

    5 注意事项

    1 需要注意的是,如果通过写入配置文件的方式挂载,由于 NFS 使用的这个 RPC 服务,当客户端连上服务器时,那么你的服务器想要关机, 那可就会成为『不可能的任务』!如果你的服务器上面还有客户端在联机,那么你要关机, 可能得要等到数个钟头才能够正常的关机成功!

    建议你的 NFS Server 想要关机之前,能先『关掉 rpcbind 与 nfs 』这两个东西!如果无法正确的将这两个 daemons 关掉,那么先以 netstat -utlp 找出 PID ,然后以 kill将它关掉先!这样才有办法正常的关机成功。

    也可以利用 showmount -a localhost 来查出来那个客户端还在联机,或者是查阅 /var/lib/nfs/rmtab 或 xtab 等档案来检查亦可。找到这些客户端后,先关掉这些客户端的连接,即停掉客户端的nfs服务,确认没问题后在依次关机

    这个注意事项是摘自鸟哥私房菜的13章 nfs的介绍,但是这里并没有测试到和介绍一样的情况,以上都查询不到信息。

    2  如果不是通过autofs挂载,而是直接写入配置文件/etc/fstab,当客户端没有全部卸载nfs盘,centos7客户端的机器上就不能正常关机,关机会卡在

    3 如果服务器网络断了,那么挂载nfs的客户端使用就会有问题,如果客户端所在的目录有挂载盘,或者是对本地挂载盘做了链接,那么通过ls 或者cd到对应的挂载盘,终端就会卡死,需要重新开一个终端,建议用autofs进行挂载

    相关文章

      网友评论

          本文标题:自动化安装NFS+autofs服务器

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