作者:lizhonglin
Centos7安装NFS服务器和客户端,并实现自动挂载共享目录.现在有3台服务器 s1(主),s2(从), s3(从)需要实现文件实时同步,我们可以安装Nfs服务端和客户端来实现!
Installed:
nfs-utils.x86_64 1:1.3.0-0.65.el7
Dependency Installed:
gssproxy.x86_64 0:0.7.0-26.el7 keyutils.x86_64 0:1.5.8-3.el7 libbasicobjects.x86_64 0:0.1.1-32.el7 libcollection.x86_64 0:0.7.0-32.el7 libevent.x86_64 0:2.0.21-4.el7 libini_config.x86_64 0:1.3.1-32.el7
libnfsidmap.x86_64 0:0.25-19.el7 libpath_utils.x86_64 0:0.2.1-32.el7 libref_array.x86_64 0:0.1.5-32.el7 libverto-libevent.x86_64 0:0.2.5-4.el7
一、安装 NFS 服务器所需的软件包:
yum install -y nfs-utils
二、编辑exports文件,添加从机
vim /etc/exports
/home/nfs/ 192.169.0.0/24(rw,sync,fsid=0)
/home/nfs/ 192.169.0.0/24(insecure,rw,sync,no_root_squash,fsid=0)
no_root_squash 权限问题
同192.168.248.0/24一个网络号的主机可以挂载NFS服务器上的/home/nfs/
目录到自己的文件系统中
rw
表示可读写
sync
表示同步写
fsid=0
表示将/data
找个目录包装成根目录*
三、启动nfs
服务
先为rpcbind
和nfs
做开机启动:(必须先启动rpcbind
服务)
systemctl enable rpcbind.service
systemctl enable nfs-server.service
然后分别启动rpcbind
和nfs
服务:
systemctl start rpcbind.service
systemctl start nfs-server.service
确认NFS服务器启动成功:
rpcinfo -p
检查 NFS 服务器是否挂载我们想共享的目录 /home/nfs/
:
exportfs -r
使配置生效
exportfs
可以查看到已经好了.
/home/nfs 192.168.152.0/24
配置自动挂载
安装服务
yum install autofs -y
Installed:
autofs.x86_64 1:5.0.7-106.el7
Dependency Installed:
hesiod.x86_64 0:3.2.1-3.el7
启动
systemctl start autofs
安装后自动生成 /net
目录,目录下有可访问的文件系统
[root@k8s-master-1 home]# cd /net
[root@k8s-master-1 net]# cd 192.169.0.152
[root@k8s-master-1 192.169.0.152]# ls
home
[root@k8s-master-1 192.169.0.152]#
查看路径
[root@k8s-master-1 192.169.0.152]# pwd
/net/192.169.0.152
[root@k8s-master-1 192.169.0.152]#
开始实现autofs
自动挂载:
1.编辑主配置文件
vim /etc/auto.master
修改成如下
/misc /etc/auto.misc
/home/nfs /etc/auto.mnt # 关联挂载目录与子配置文件
2.配置子配置文件
子配置文件是自己在/etc
下面新建的,1).vim auto.mnt
*192.169.0.152:home/nfs/&
或者
echo "*192.169.0.152:home/nfs/&" > /etc/auto.mnt
&代表任意可指定目录挂载
3.修改默认的挂载时间
要修改默认的挂载时间是编辑/etc/sysconfig/autofs
,默认更新挂载的时间为300秒,这里更改为6秒。
TIMEOUT=6
四、在从机上安装NFS 客户端
首先是安裝 nfs
,同上,然后启动rpcbind
服务
先为rpcbind
做开机启动:
systemctl enable rpcbind.service
然后启动rpcbind
服务:
systemctl start rpcbind.service
注意:客户端不需要启动nfs
服务
检查 NFS 服务器端是否有目录共享:showmount -e nfs服务器的IP
[root@k8s-node-1 home]# showmount -e 192.169.0.152
Export list for 192.169.0.152:
/home/nfs 192.169.0.0/24
添加开机自动挂载
在/etc/fstab
里面添加
192.169.0.152:/home/nfs /home/nfs nfs defaults 0 0
或者
echo "192.169.0.152:/home/nfs /home/nfs nfs defaults 0 0" >> /etc/fstab
远程登录
[root@k8s-node-1 home]# ssh root@192.169.0.152
root@192.169.0.152's password:
Last login: Thu Apr 2 13:41:42 2020 from 192.169.0.66
[root@k8s-master-1 ~]# ls
anaconda-ks.cfg
[root@k8s-master-1 ~]# exit
登出
Connection to 192.169.0.152 closed.
[root@k8s-node-1 home]#
mount
是用来挂载文件系统的,可以在系统启动时挂载(/etc/fstab
),也可以在系统启动后挂载(使用mount
命令)。而光盘、软盘、NFS
、SMB
等文件系统具有动态性,即需要的时候才有必要挂载。光驱和软盘一般知道什么时候需要挂载,但是NFS
和SMB
就不一定能确定挂载时间。而且NFS
、SMB
是基于网络的,不管使用或者不使用,都会造成资源浪费(多少情况下,不用到这些的时候,这些挂载其实是不需要一直存在的)。
而autofs
恰好提供了一种功能:如果长时间不用一个文件系统,autofs
会把这个文件系统卸载;当你需要用到的时候,只需要cd到挂载点或者ls下挂载点,autofs
自动挂接。
临时挂载目录
在从机上使用 mount 挂载服务器端的目录/home/nfs
到客户端某个目录下:
cd /home && mkdir /nfs
mount -t nfs 192.169.0.152:/home/nfs /home/nfs
如果挂载的目录没有需要先创建在挂载.
查看是否挂载成功
df -h
这样每次重启动了系统也可以完成共享目录自动挂载.