存储服务

一、概述

存储:用于存放用户上传的内容(数据),一般应用在网站集群中

为什么要存储?

  1. 如果不使用存储,用户上传的数据就直接存放在某一台网站服务器上了,用户下次访问就可能找不到
  2. 如果使用存储,用户上传的内容存放在存储上面,用户就会访问存储

二、存储分类

分类 说明
硬件存储 硬件存储,硬盘多,使用的时候挂载即可
开源软件 普通存储NFS(linux),Samba(windows)
分布式存储:GlusterFS,Ceph,FastDFS,MinIO
云产品 阿里云OSS(对象存储),七牛云存储,腾讯云COS,华为云OBS

三、NFS原理

NFS (Network File System):网络文件系统

2个服务组成:

  1. NFS服务
  2. rpcbind(portmap)服务

原理图:

关于RPC的说明:

RPC远程过程调用,本质起到调度作用

RPC服务从Centos 6开始叫rpcbind,之前叫portmapper

四、配置NFS

1. 环境说明

环境 主机
nfs服务端 nfs 10.0.0.31/172.16.1.31
nfs客户端 web01 10.0.0.7 /172.16.1.7

2. 服务端部署

#安装软件包
[root@nfs ~]# yum -y install rpcbind nfs-utils
#启动rpcbind
[root@nfs ~]# systemctl start rpcbind
[root@nfs ~]# systemctl enable rpcbind [root@nfs ~]# rpcinfo -p
program vers proto port service
100000 4 tcp 111 portmapper
100000 3 tcp 111 portmapper
100000 2 tcp 111 portmapper
100000 4 udp 111 portmapper
100000 3 udp 111 portmapper
100000 2 udp 111 portmapper #配置nfs
[root@nfs ~]# cat /etc/exports
/data/ 172.16.1.0/24(rw)
#配置:172.16.1.0/24网段对NFS服务端的/data/目录,拥有rw读写权限 #创建共享目录并且修改属主属组
[root@nfs ~]# mkdir -p /data/
[root@nfs ~]# chown nfsnobody.nfsnobody /data/
[root@nfs ~]# ll -d /data/
drwxr-xr-x 2 nfsnobody nfsnobody 6 Jan 12 17:29 /data/
#启动nfs
[root@nfs ~]# systemctl start nfs
[root@nfs ~]# systemctl enable nfs
[root@nfs ~]# rpcinfo -p
program vers proto port service
100000 4 tcp 111 portmapper
100000 3 tcp 111 portmapper
100000 2 tcp 111 portmapper
100000 4 udp 111 portmapper
100000 3 udp 111 portmapper
100000 2 udp 111 portmapper
100024 1 udp 57203 status
100024 1 tcp 34556 status
100005 1 udp 20048 mountd
100005 1 tcp 20048 mountd
100005 2 udp 20048 mountd
100005 2 tcp 20048 mountd
100005 3 udp 20048 mountd
100005 3 tcp 20048 mountd
100003 3 tcp 2049 nfs
100003 4 tcp 2049 nfs
100227 3 tcp 2049 nfs_acl
100003 3 udp 2049 nfs
100003 4 udp 2049 nfs
100227 3 udp 2049 nfs_acl
100021 1 udp 42844 nlockmgr
100021 3 udp 42844 nlockmgr
100021 4 udp 42844 nlockmgr
100021 1 tcp 35169 nlockmgr
100021 3 tcp 35169 nlockmgr
100021 4 tcp 35169 nlockmgr

注意事项:

修改/etc/exports之后,最好使用reload重载系统,restart会导致,现有连接断开,且大概1分钟才能重新连接

[root@nfs ~]# systemctl reload nfs

reload与restart区别:

reload:表示优雅重启,不会断开现有连接

restart:在nfs中,会导致客户端一段时间夯住

排错指令:

rpcinfo -p          #检查nfs服务端的rpc信息,主要检查是否有NFS信息
showmount -e ip #检查nfs服务端共享了那些信息

3.客户端挂载

#安装nfs服务,客户端只需安装,不用启动
[root@web01 ~]# yum -y install nfs-utils
#查看服务端的共享目录
[root@web01 ~]# showmount -e 172.16.1.31
Export list for 172.16.1.31:
/data 172.16.1.0/24
#挂载目录
[root@web01 ~]# mount -t nfs 172.16.1.31:/data/ /mnt/
#查看是否挂载成功
[root@web01 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 979M 0 979M 0% /dev
tmpfs 991M 0 991M 0% /dev/shm
tmpfs 991M 9.6M 981M 1% /run
tmpfs 991M 0 991M 0% /sys/fs/cgroup
/dev/mapper/centos-root 17G 2.0G 16G 12% /
/dev/sda1 1014M 138M 877M 14% /boot
tmpfs 199M 0 199M 0% /run/user/0
172.16.1.31:/data 17G 2.0G 16G 12% /mnt #挂载成功
#创建文件测试
[root@web01 ~]# touch /mnt/{01..05}.txt #服务端查看共享目录
[root@nfs ~]# ll /data/
total 0
-rw-r--r-- 1 nfsnobody nfsnobody 0 Jan 12 17:42 01.txt
-rw-r--r-- 1 nfsnobody nfsnobody 0 Jan 12 17:42 02.txt
-rw-r--r-- 1 nfsnobody nfsnobody 0 Jan 12 17:42 03.txt
-rw-r--r-- 1 nfsnobody nfsnobody 0 Jan 12 17:42 04.txt
-rw-r--r-- 1 nfsnobody nfsnobody 0 Jan 12 17:42 05.txt

五、NFS相关文件

nfs客户端/服务端 说明
NFS服务端 /etc/exports(配置文件里面的)--->/var/lib/nfs/etab(当前使用的nfs服务端配置)
NFS客户端 客户端:永久挂载 /etc/rc.local 或 /etc/fstab --->/proc/mounts(当前系统的挂载情况)

1.nfs客户端永久挂载

#方法1:挂载命令写入到/etc/rc.local 写入的时候,记得给/etc/rc.d/rc.local执行权限
[root@web01 ~]# chmod +x /etc/rc.d/rc.local #方法2:写入/etc/fstab
[root@web01 ~]# cat /etc/fstab
172.16.1.31:/data/ /data/ nfs defaults 0 0

温馨提示:

如果配置了nfs客户端永久挂载,未来要优先启动nfs服务端,否则客户端会出现开机启动慢的问题

六、NFS服务端的配置文件

/etc/exports 由nfs来管理,systemctl命令的背后是在调用exportfs命令

1.配置文件格式

/etc/exports
/data/ 172.16.1.0/24(rw)
第1部分 第2部分
共享目录 网段(选项)

2.NFS配置文件中的网络配置

网段
172.16.1.0/24 常用
172.16.1.7 指定ip
baidu.com 指定域名

3.服务端核心配置

选项 说明
rw 可以读写共享目录
ro 只读 read only
sync 同步,只要用户上传,就把数据写到磁盘上
async 异步,用户上传的数据,nfs先临时存放到内存中,过一段时间写入到磁盘,并发高,但是数据可能丢失

补充:

同步与异步:

同步:在网站架构中指的是直接访问对应的资源

异步:在网站架构中利用各种缓存达到用户优先访问缓存,缓存没有在访问对应服务

4.服务端用户压缩

NFS客户挂载NFS服务端后,创建的文件默认属于nfsnobody,这种操作叫用户压缩(映射)

用户压缩是通过NFS服务端的配置实现

选项 说明
root_squash 如果客户端是root用户访问,则到了nfs服务端会被压缩(默认的)
no_all_squash 如果客户端不是root用户访问,则不进行压缩(保护原始用户)(默认的)
all_squash 所有用户都进行压缩(不安全)
anonuid和anongid 用于指定压缩的匿名用户(默认是nfsnobody)anonuid=65534,anongid=65534

用户压缩/用户映射:NFS客户端访问NFS共享目录的时候变成了什么用户

nfs客户端用户 ----------------------> nfs服务端用户

root ----------------------> nfsnobody

5.用户压缩实例

用户压缩案例: 设置/nfsdata共享目录,匿名用户为www. 客户端挂载到/upload-video/

​ www用户的uid,gid:1999 (服务端,客户端)

#服务端
#部署nfs服务
#添加用户
[root@nfs ~]# groupadd -g 1999 www
[root@nfs ~]# useradd -u 1999 -g www -s /sbin/nologin -M www
[root@nfs ~]# id www
uid=1999(www) gid=1999(www) groups=1999(www)
#创建共享目录与修改属主属组
[root@nfs ~]# mkdir -p /nfsdata
[root@nfs ~]# chown -R www.www /nfsdata/
[root@nfs ~]# ll -d /nfsdata/
drwxr-xr-x 2 www www 6 Jan 12 19:49 /nfsdata/
#修改配置文件
[root@nfs ~]# vim /etc/exports
/data/ 172.16.1.0/24(rw)
/nfsdata/ 172.16.1.0/24(rw,all_squash,anonuid=1999,anongid=1999)
#重载服务
[root@nfs ~]# systemctl reload nfs
#查看共享目录
[root@nfs ~]# showmount -e
Export list for nfs:
/nfsdata 172.16.1.0/24
/data 172.16.1.0/24 #服务端
#新建用户
[root@web01 ~]# groupadd -g 1999 www
[root@web01 ~]# useradd -u 1999 -g www -s /sbin/nologin -M www
[root@web01 ~]# id www
uid=1999(www) gid=1999(www) groups=1999(www)
#创建目录
[root@web01 ~]# mkdir -p /upload-video
#挂载目录
[root@web01 ~]# mount -t nfs 172.16.1.31:/nfsdata/ /upload-video/
[root@web01 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 979M 0 979M 0% /dev
tmpfs 991M 0 991M 0% /dev/shm
tmpfs 991M 9.6M 981M 1% /run
tmpfs 991M 0 991M 0% /sys/fs/cgroup
/dev/mapper/centos-root 17G 2.0G 16G 12% /
/dev/sda1 1014M 138M 877M 14% /boot
tmpfs 199M 0 199M 0% /run/user/0
172.16.1.31:/nfsdata 17G 2.0G 16G 12% /upload-video
#测试
[root@web01 ~]# touch /upload-video/video.txt #查看服务端共享目录
[root@nfs ~]# ll /nfsdata
total 0
-rw-r--r-- 1 www www 0 Jan 12 19:54 video.txt

七、NFS优化

终极优化目标:

​ 尽可能让用户的请求在访问网站架构之前解决掉(尽可能把用户的请求往前推)

NFS优化:硬件(物理服务器+nfs服务)

NFS安全优化:客户挂载 只能上传,不能执行

NFS有单点故障,选择其他存储:公有云OSS(阿里云)(对象存储在代码里面调用)

[root@web01 ~]# mount -o noexec,nosuid,nodev -t nfs 172.16.1.31:/nfsdata /upload-video/
[root@web01 ~]# df -h /upload-video/
Filesystem Size Used Avail Use% Mounted on
172.16.1.31:/nfsdata 17G 2.0G 16G 12% /upload-video noexec 挂载的nfs目录中如果有命令,无法执行
nosuid 带有suid的命令,无法执行
nodev 带有特殊属性的文件

八、故障

1.nfs提示"mount: wrong fs type, bad option"

[root@web01 ~]# mount -t nfs 172.16.1.31:/data/ /mnt/
mount: wrong fs type, bad option, bad superblock on 172.16.1.31:/data/,
missing codepage or helper program, or other error
(for several filesystems (e.g. nfs, cifs) you might
need a /sbin/mount.<type> helper program) In some cases useful info is found in syslog - try
dmesg | tail or so.
#原因: 错误的文件系统类型,客户端无法识别nfs.
#解决: 客户端安装nfs服务,不用启动

2.nfs提示"umount.nfs4: /mnt: device is busy"

[root@nfs /mnt]# umount /mnt
umount.nfs4: /mnt: device is busy
#原因:在当前目录进行卸载当前目录
#解决方法:
#1.
在其他目录进行卸载
#2.直接强制卸载
[root@nfs /mnt]# umount -lf /mnt #强制卸载

3.nfs提示"touch: cannot touch ‘1.txt’: Read-only file system"

#客户端在挂载目录创建文件提示
[root@web01 /mnt]# touch 1.txt
touch: cannot touch ‘1.txt’: Read-only file system #原因:服务端的配置文件给的权限为ro
[root@nfs /mnt]# vim /etc/exports
/data/ 172.16.1.0/24(ro)
[root@nfs /mnt]# systemctl reload nfs
#解决方法:
[root@nfs /mnt]# vim /etc/exports
/data/ 172.16.1.0/24(rw) #ro改为rw

4.客户端挂载与使用失败,df -h夯住

#原因:
挂载出现问题
#解决方法:
/proc/mounts查找是否有失败挂载

最新文章

  1. sql server项目死活启动不了的问题
  2. 陨石坑之webapi使用filter
  3. Atitit.架构设计趋势 设计模式 ---微服务架构&#160;&#160;soa
  4. 自定义控件--CircleImageView(类似于QQ、微信圆形头像自定义控件)
  5. 调试单片机内部扩展RAM
  6. C++的输入和输出
  7. 地磁应用中的低功耗无线数传模块xbee PRO S2C
  8. logstash收集syslog日志
  9. SWift中 &#39;?&#39; must be followed by a call, member lookup, or subscript 错误解决方案
  10. 【转】安全加密(三):RFID标签防伪为生活开启安全模式
  11. 企业类Web原型制作分享-Kraftwerk
  12. 腾讯云服务器 - 安装redis3.2.9以及集群
  13. 关于iBatis配置xml文件时出现中文注释出错的一个问题(很坑爹.)
  14. 标 题: 有什么办法快速把pc上的网址发送到手机上
  15. Windows10电脑安装macOS Mojave系统的方法(最新版系统,含超详细步骤截图)
  16. Python构建web应用(进阶版)-&gt;对网页HTML优化逻辑显示
  17. 各种RF的比较
  18. WPF中DependencyObject与DependencyProperty的源代码简单剖析
  19. python-thread多线程
  20. 基于INTEL FPGA硬浮点DSP实现卷积运算

热门文章

  1. python基础类型,字符串
  2. CSS布局秘籍(1)-任督二脉BFC/IFC
  3. Pytest学习
  4. Linux学习环境搭建流程
  5. 一个超经典 WinForm 卡死问题的再反思
  6. JetBrains新产品Aqua——自动化测试开发工具(抢鲜体验)
  7. 顺序表代码总结——SqList
  8. Shell脚本--信号发送与捕捉
  9. 说一下 ArrayDeque 和 LinkedList 的区别?
  10. 在C#中使用Halcon开发视觉检测程序