操作步骤
一、在每台机器上安装
apt-get install nfs-kernel-server nfs-common -y
#centos系统
yum install nfs-utils.x86_64
二、配置nfs配置文件
vim /etc/exports
/mnt/nfs_from_master03 10.20.2.110(rw,sync,no_root_squash)
要挂出去的路径----允许那台机器访问---(权限:rw读写,ro只读,不降级root权限,sync保持同步)
三、重启nfs-kernel-service
systemctl restart nfs-kernel-server.service
四、如果起不来看看是不是要挂出去的目录还没创建
五、在客户端创建同名目录(可选)
六、挂载
mount 192.168.0.123:/mnt/where/u/want /mnt/where/localhost
七、开机自动挂载
如果有去网上查过,基本上大多数的建议都是让我们编辑/etc/fstab文件,但我要在这里说的的是
个人强烈建议不要贸然编辑/etc/fstab文件
因为如果在编辑此文件时出现失误,你的操作系统很大可能无法正常启动
就像我们在AWS本次实践中的一样(AWS-EC2 + AWS-EFS),官方文档也强调,由于需要指定EFS网络文件系统,所以在EC2实例自动挂载时候需要加入特定参数 “ _netdev ”
否则,会出现EC2无响应并且EFS挂载失败的情况。
https://docs.aws.amazon.com/zh_cn/efs/latest/ug/mount-fs-auto-mount-onreboot.html
在实际情况下,我们采用的方法是利用rc.local文件,原理参考
https://www.cnblogs.com/defifind/p/9285456.html
关键操作步骤如下
7.1修改rc.local.service
cd /lib/systemd/system
找到 rc.local.service 文件,并打开它查看
vim rc.local.service
---
# This file is part of systemd.
#
# systemd is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
# This unit gets pulled automatically into multi-user.target by
# systemd-rc-local-generator if /etc/rc.local is executable.
[Unit]
Description=/etc/rc.local Compatibility
ConditionFileIsExecutable=/etc/rc.local
After=network.target
[Service]
Type=forking
ExecStart=/etc/rc.local start
TimeoutSec=0
RemainAfterExit=yes
#----这里开始,下面是我们要手动加入的内容---#
[Install]
WantedBy=multi-user.target
Alias=rc-local.service
7.2创建 /etc/rc.local 文件
Ubuntu18.04默认是没有这个文件的
vim /etc/rc.local
---
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
#-----把你需要执行的命令写在这里,并确保它被写在“ exit0 ”之前---#
mount -t nfs4 -o nfsvers=4.1,rsize=1048576,wsize=1048576,hard,timeo=600,retrans=2,noresvport fs-2de59714.efs.ap-southeast-2.amazonaws.com:/efs_from_amazon/ /mnt/efs_from_amazon/
echo "this just a test" > /auto_mount_test.txt
exit 0
7.3为rc.local添加执行权限
chmod +x /etc/rc.local
7.4 创建软链接
ln -s /lib/systemd/system/rc.local.service /etc/systemd/system/
完了重启
7.5注意
如果以上步骤都完成了,依然挂不上,可以先在/etc/rc.local写一些简单的任务,验证rc.local是否有效。
如果简单任务成功执行,说明是rc.local生效的时候nfs还没启动。
所以需要我们在任务前稍等一会,就像这样
sleep 8;mount -t nfs -o rw 172.31.151.191:/mnt/nfs_from_master03 /mnt/nfs_from_master03
2023-11-15更新
使用fstab的方法实现自动挂载唯一的顾虑是 担心出错 导致影响操作系统启动
那么如果有办法在不重启的情况下,验证配置文件是否正确,那么这个问题也就解决了
#在修改完fstab文件后执行如下命令,如果配置正确则会直接按照配置文件的内容完成挂载,否则说明配置有问题
mount -a