centos7创建nfs

NFS

准备

yum -y install nfs-utils rpcbind
# 查看是否安装成功
rpm -qa nfs-utils 

创建目录

mkdir -p /mnt/v1
mkdir -p /mnt/v2

# 对同步文件夹进行授权
chown -R nfsnobody.nfsnobody /mnt/v1
chown -R nfsnobody.nfsnobody /mnt/v2

服务机器

# 共享整个根目录:
# 只能一个目录
cat >> /etc/exports <<EOF
/mnt/v1 172.16.0.0/16(rw,sync,no_root_squash,fsid=0)
EOF
# 共享单个目录
# 支持多个目录
cat >> /etc/exports <<EOF
/mnt/v1 172.16.0.0/16(rw,sync,no_root_squash)
/mnt/v2 172.16.0.0/16(rw,sync,no_root_squash)
EOF

rw表示可读写;sync表示同步写,fsid=0表示将/data找个目录包装成根目录
no_root_squash:NFS客户端连接服务端时如果使用的是root的话,那么对服务端分享的目录来说,也拥有root权限。
# 启动
systemctl start rpcbind.service
systemctl start nfs-server.service
# 开机
systemctl enable rpcbind.service
systemctl enable nfs-server.service

#使配置生效
exportfs -rv
#可以查看到已经ok
exportfs
# 查看是否启动
ss -tnulp | grep 111

# 查看是否启动
showmount -e localhost
rpcinfo -p localhost

# 打开防火墙
firewall-cmd --add-service=nfs --permanent --zone=public
firewall-cmd --add-service=mountd --permanent --zone=public
firewall-cmd --add-service=rpc-bind --permanent --zone=public
firewall-cmd --reload

客户端

# 首先是安裝nfs,同上,然后启动rpcbind服务
yum -y install nfs-utils
# 先为rpcbind做开机启动:
systemctl enable rpcbind.service

# 然后启动rpcbind服务:
# 注意:客户端不需要启动nfs服务
systemctl start rpcbind.service

# 检查 NFS 服务器端是否有目录共享:showmount -e nfs服务器的IP
[root@VM-16-9-centos ~]# showmount -e 172.16.16.5
Export list for 172.16.16.5:
/mnt/v2 172.16.0.0/16
/mnt/v1 172.16.0.0/16
[root@VM-16-9-centos ~]#

# 挂载远程服务器NFS分区到本地挂载点
mkdir -p /mnt/{v1,v2}
mount -t nfs 172.16.16.5:/mnt/v1 /mnt/v1
mount -t nfs 172.16.16.5:/mnt/v2 /mnt/v2

# df -h 查看是否挂载成功。
[root@VM-16-9-centos mnt]# df -h
172.16.16.5:/mnt/v1   99G   21G   74G  23% /mnt/v1
[root@VM-16-9-centos mnt]#

# 取消挂载
umount 172.16.16.5:/mnt/v1
umount 172.16.16.5:/mnt/v2
# 或者
umount /mnt/v1 # 本地挂载点
umount /mnt/v2 # 本地挂载点

# 开机挂载
vi /etc/fstab

# 添加 
172.16.16.5:/mnt/v1 /mnt/v1 nfs defaults 0 0
172.16.16.5:/mnt/v2 /mnt/v2 nfs defaults 0 0

rancher使用
导入yaml创建即可

apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv1
  labels:
    name: pv1
spec:
  nfs:
    path: /mnt/v1
    server: 172.16.16.5
  accessModes: ["ReadWriteMany","ReadWriteOnce","ReadOnlyMany"]
  capacity:
    storage: 50Gi

也可以通过界面创建
通过集群全局界面进入



umount -l /mnt 强行解除挂载
选择》软件安装》安装NFS客户端。

windos 使用,建立bat文件

@rem   注释命令  相当于C语言的/*------------*/
@echo off
@rem color 0a
title  挂载172.18.5.164
pause
@echo 执行showmount命令结果如下
showmount -e 172.18.5.164
 
pause
 
@echo 对 E 盘符进行操作
choice /C 123 /M "挂载E盘符按1,解挂按2,取消操作按3"
if errorlevel 3 goto end
if errorlevel 2 goto umount
if errorlevel 1 goto mount
 
:mount
mount 172.18.5.164:/data/mnt/v1 E: 
 
@echo 如挂载成功 是否执行 net use /persistent:yes
choice  /M "执行按Y,不执行按N"
if errorlevel 2 goto no
if errorlevel 1 goto yes
:no
goto end
:yes
@echo on
net use /persistent:yes
@echo off
goto end
 
goto end
:umount
umount E:
goto end
 
:end
echo  全部OK,欢迎使用!
pause
posted @ 2021-09-29 16:03  刘文江  阅读(7)  评论(0)    收藏  举报  来源