零 修订记录
序号 | 修订内容 | 修订时间 |
---|---|---|
1 | 新增 | 20220225 |
一 摘要
本文主要介绍在linux 系统上部署共享存储,本次使用nfs 实现。
二 环境信息
2.1 操作系统
[root@localhost ~]# cat /etc/centos-release
CentOS Linux release 7.6.1810 (Core)
[root@localhost ~]#
2.2 nfs 版本
三 实施方案
3.1 部署视图
IP | 功能 | 安装服务 | 备注 |
---|---|---|---|
10.3.147.32 | nfs-server | 无 | 无 |
10.3.147.33 | nfs-client | 无 | 无 |
3.2 部署方案
可以用rpm 安装,也可以用docker 安装。
3.2.1 rpm 方式部署
3.2.1.1 关闭selinux
3.2.1.2 关闭防火墙(或者放行相关端口)所有机器执行
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# systemctl disable firewalld
[root@localhost ~]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
Active: inactive (dead)
Docs: man:firewalld(1)
[root@localhost ~]#
3.2.1.3 安装nfs及rpc服务 (所有机器执行)
检查是否已安装nfs,若已经安装就不需要安装了。
[root@localhost ~]# rpm -qa nfs-utils rpcbind
[root@localhost ~]#
安装nfs
[root@localhost ~]# yum install nfs-utils rpcbind
已加载插件:fastestmirror
Determining fastest mirrors
验证
[root@localhost ~]# rpm -qa nfs-utils rpcbind
rpcbind-0.2.0-47.el7.x86_64
nfs-utils-1.3.0-0.61.el7.x86_64
[root@localhost ~]#
3.2.1.4 启动服务并配置开机启动(所有机器执行)
配置开机启动
[root@localhost etc]# systemctl enable nfs.service
[root@localhost etc]# systemctl enable rpcbind.service
检查
[root@localhost etc]# systemctl list-unit-files|grep enabled|grep nfs
nfs-server.service enabled
nfs.service enabled
nfs-client.target enabled
[root@localhost etc]#
[root@localhost etc]# systemctl list-unit-files|grep enabled|grep rpcbind
rpcbind.service enabled
rpcbind.socket enabled
[root@localhost etc]#
启动
[root@localhost etc]# systemctl restart rpcbind
[root@localhost etc]# systemctl status rpcbind
● rpcbind.service - RPC bind service
Loaded: loaded (/usr/lib/systemd/system/rpcbind.service; enabled; vendor preset: enabled)
Active: active (running) since 五 2022-02-25 11:35:32 CST; 7s ago
Process: 13881 ExecStart=/sbin/rpcbind -w $RPCBIND_ARGS (code=exited, status=0/SUCCESS)
Main PID: 13882 (rpcbind)
CGroup: /system.slice/rpcbind.service
└─13882 /sbin/rpcbind -w
2月 25 11:35:32 localhost.localdomain systemd[1]: Starting RPC bind service...
2月 25 11:35:32 localhost.localdomain systemd[1]: Started RPC bind service.
[root@localhost etc]# systemctl restart nfs
[root@localhost etc]# systemctl status nfs
● nfs-server.service - NFS server and services
Loaded: loaded (/usr/lib/systemd/system/nfs-server.service; enabled; vendor preset: disabled)
Active: active (exited) since 五 2022-02-25 11:35:50 CST; 5s ago
Process: 13953 ExecStartPost=/bin/sh -c if systemctl -q is-active gssproxy; then systemctl restart gssproxy ; fi (code=exited, status=0/SUCCESS)
Process: 13935 ExecStart=/usr/sbin/rpc.nfsd $RPCNFSDARGS (code=exited, status=0/SUCCESS)
Process: 13933 ExecStartPre=/usr/sbin/exportfs -r (code=exited, status=0/SUCCESS)
Main PID: 13935 (code=exited, status=0/SUCCESS)
CGroup: /system.slice/nfs-server.service
2月 25 11:35:50 localhost.localdomain systemd[1]: Starting NFS server and services...
2月 25 11:35:50 localhost.localdomain systemd[1]: Started NFS server and services.
[root@localhost etc]#
3.2.1.5 nfs 服务端配置
此处仅配置服务端 10.3.147.32,此处共享目录是/iflytek/test,为了安全起见 将该目录权限控制为nfsnobody,该用户nfs 安装时会默认建立。
3.2.1.5.1 修改共享目录权限(服务端执行)
[root@localhost test]# chown -R nfsnobody:nfsnobody /iflytek/test
3.2.1.5.2 修改/etc/exports
修改任何配置文件 先备份
[root@localhost test]# cp /etc/exports /etc/exports.bak.orig
[root@localhost test]# cp /etc/exports{,.bak`date +"%Y%m%d%H%m%S"`}
[root@localhost test]# ll /etc/ex*
-rw-r--r--. 1 root root 0 6月 7 2013 /etc/exports
-rw-r--r-- 1 root root 0 2月 25 14:23 /etc/exports.bak20220225140253
-rw-r--r-- 1 root root 0 2月 25 14:22 /etc/exports.bak.orig
/etc/exports.d:
总用量 0
[root@localhost test]#
[root@localhost test]# echo "/iflytek/test/ 10.3.147.0/24(rw,sync,root_squash)">>/etc/exports
参数说明:
rw # 客户端对共享的目录可读写
ro # 客户端对共享的目录只读不可写
sync # 同步模式,也就是把内存的数据实时写入硬盘,但这样会降低磁盘效率
async # 非同步模式,也就是每隔一段时间才会把内存的数据写入硬盘,能保证磁盘效率,但当异常宕机/断电时,会丢失内存里的数据
no_root_squash # 客户端挂载NFS共享目录后,客户端上的root用户不受这些挂载选项的限制,权限很大
root_squash # 跟no_root_squash相反,客户端上的root用户受到这些挂载选项的限制,被当成普通用户
all_squash # 客户端上的所有用户在使用NFS共享目录时都被限定为一个普通用户
anonuid # 上面的几个squash用于把客户端的用户限定为普通用户,而anouid用于限定这个普通用户的uid,这个uid与服务端的/etc/passwd文件相对应,如:anouid=1000
# 比如我客户端用xiaoming这个用户去创建文件,那么服务端同步这个文件的时候,文件的属主会变成服务端的uid(1000)所对应的用户
anongid # 同上,用于限定这个普通用户的gid
加载配置文件
[root@localhost test]# exportfs -rv
exporting 10.3.147.0/24:/iflytek/test
[root@localhost test]#
exportfs 命令参数说明
-a :全部挂载或者卸载;
-r :重新挂载;
-u :卸载某一个目录;
-v :显示共享的目录;
3.2.1.6 nfs 客户端配置
现在客户机建立挂载目录,然后把服务端目录挂上去。
3.2.1.6.1 建立挂载目录
[root@localhost ~]# mkdir -p /iflytek/test
[root@localhost ~]# cd /iflytek/test/
[root@localhost test]# pwd
/iflytek/test
[root@localhost test]#
3.2.1.6.2 挂载
[root@localhost test]# mount -t nfs -o noexec,nosuid,nodev,rw,rsize=65536,wsize=65536 10.3.147.32:/iflytek/test /iflytek/test
参数说明:
wsize和rsize写和读缓存
async 数据不同步写到磁盘,提高性参,但降低数据安全,不推荐使用
noatime和nodiratime 这两个选是说在读写磁盘时,不更新文件和目录的时间戳,而更新文件时间戳对于工作数据必要性不大,增加了磁盘IO的次数,拖慢系统性参,
defaults 这个缺省值包括rw,suid,dev,exec,auto,nouser,and async cat /etc/fstab的结果默认大部人都是缺省值
noauto 不会自动挂载文件系统
noexec 不允许安装的直接执行任何二进制文件
ro 挂载一个只读文件系统
rw 挂载一个可写的文件系统
sync 把数据同步写入硬盘
nosuid 不允许设置用户标识或设置组标识符位
nodev 不解释字符或文件块特殊设备
intr 表示可以中断
3.2.2 验证
在客户端用root 用户建文件,会显示为nfsnobody,服务器端root 用户建文件则不会。
[root@localhost test]# ls -al
总用量 16
drwxr-xr-x 2 nfsnobody nfsnobody 81 2月 25 14:42 .
drwxr-xr-x 3 root root 18 2月 25 14:36 ..
-rw-r--r-- 1 root root 13 2月 25 14:41 1111from32.txt
-rw-r--r-- 1 nfsnobody nfsnobody 5 2月 25 14:39 222.txt
-rw-r--r-- 1 nfsnobody nfsnobody 14 2月 25 14:42 3333from33.txt
-rw-r--r-- 1 nfsnobody nfsnobody 7 2月 25 10:55 test.txt
[root@localhost test]#
3.2.3 客户端取消挂载
umount /iflytek/test/
有时需要加强版
[root@localhost /]# fuser -m -v -i -k /iflytek/test/
-bash: fuser: 未找到命令
[root@localhost /]# yum install psmisc
[root@localhost /]# fuser -m -v -i -k /iflytek/test/
用户 进程号 权限 命令
/iflytek/test: root kernel mount /iflytek/test
[root@localhost /]# df -h
文件系统 容量 已用 可用 已用% 挂载点
/dev/mapper/centos-root 76G 1.2G 74G 2% /
devtmpfs 1.9G 0 1.9G 0% /dev
tmpfs 1.9G 0 1.9G 0% /dev/shm
tmpfs 1.9G 8.6M 1.9G 1% /run
tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup
/dev/sda1 1014M 145M 870M 15% /boot
tmpfs 379M 0 379M 0% /run/user/0
10.3.147.32:/iflytek/test 100G 33M 100G 1% /iflytek/test
[root@localhost /]# umount /iflytek/test/
[root@localhost /]#
3.2.4 客户端开机自动挂载 (暂未验证成功)
客户端开机自动挂载方案很多,比如修改/etc/fstab 或mount 命令写到local 里,或者用autofs 服务
此处使用autofs方案
3.2.4.1 安装autofs 并配置为开机启动
[root@localhost test]# yum install autofs
[root@localhost test]# systemctl enable autofs
[root@localhost test]# systemctl restart autofs
[root@localhost test]# systemctl status autofs
● autofs.service - Automounts filesystems on demand
Loaded: loaded (/usr/lib/systemd/system/autofs.service; enabled; vendor preset: disabled)
Active: active (running) since 五 2022-02-25 14:50:24 CST; 5s ago
Main PID: 7563 (automount)
CGroup: /system.slice/autofs.service
└─7563 /usr/sbin/automount --foreground --dont-check-daemon
3.2.4.2 配置autofs
备份原配置文件
[root@localhost etc]# cp /etc/auto.master{,.bak`date +"%Y%m%d%H%m%S"`}
配置文件最后一行添加该行
/iflytek /etc/auto.iflytek
说明 /iflytek 是挂载的上级目录
具体挂载目录及配置看/etc/auto.iflytek
这个文件我们新建