小阿轩yx-FastDFS分布式存储
FastDFS简介
- 是一个开源的轻量级分布式文件系统
- 它对文件进行管理
- 解决了大容量存储和负载均衡的问题
- 特别适合以文件为载体的在线服务
- 充分考虑了冗余备份、负载均衡、线性扩容机制
- 注重高可用、高性能指标
- 很容易搭建一套高性能的文件服务器集群提供文件上传、下载等服务
功能包括
- 文件存储
- 文件同步
- 文件访问
- 文件上传
- 文件下载
FastDFS 原理
- 纯 c 实现
- 特别适合以中小文件(建议范围:4KB < file_size < 500MB)为载体的在线服务
目前提供
- C
- Java
- PHP
- API
三个角色
- 跟踪服务器(Tracker Server)
- 存储服务器(Storage Server)
- 客户端(Client)
client
- 请求 Tracker Server 进行文件上传、下载
- 通过 Tracker server 调度最终由 Storage server 完成文件上传和下载
- 在底层存储通过逻辑分组概念,使得通过在同组内配置多个 Storage。实现软 RAID10
FastDFS 架构
Tracker server
- 跟踪服务器
- 主要做调度工作
- 起到均衡的作用
Storage server
- 存储服务器
- 主要提供容量和备份服务
Client
- 客户端
- 上传下载数据的服务器
- 也就是我们自己的项目所部署在的服务器
FastDFS 向使用者提供基本文件访问接口,比如 upload、download、append、delete
- 跟踪服务器和存储节点都可以由一台或多台服务器构成
- 跟踪服务器和存储节点可以随时增加或者下线不会影响线上服务
文件的上传
上传的内部机制
- 选择 tracker server
- 选择 storage server
- 选择 storage path
- 生成 Fileid
- 生成文件名
选择 tracker server
支持选择 group 的规则
- Round robin:所有的 group 间轮询
- Specified group:指定某一个确定的 group
- Load balance:剩余存储空间多 group 优先
选择 storage server
支持选择 storage 的规则
- Round robin,在group内的所有storage间轮询
- First server ordered by ip,按ip排序
- First server ordered by priority,按优先级排序(优先级在storage上配置)
选择 storage path
支持规则
- Round robin,多个存储目录间轮询
- 剩余存储空间最多的优先
生成 Fileid
生成文件名
文件的下载
定位文件
组名
-
文件上传后所在的 storage 组名称,在文件上传成功后有 storage 服务器返回,需要客户端自行保存
虚拟磁盘路径
- stroage 配置的虚拟路径,与磁盘选项 store_path* 对应。
- 如果配置了 store_path0 则是 M00
- 如果配置了 store_path1 则是 M01,以此类推。
数据两级目录
- storge 服务器在每个虚拟磁盘路径下创建的两级目录,用于存储数据文件。
文件名
- 与文件上传时不同
- 由存储服务器根据特定信息生成,文件名包含:源存储服务器 IP 地址、文件创建时间戳、文件大小、随机数和文件拓展名等信息
FastDFS 通过 FID 定位到需要访问的文件过程
- 通过组名tracker能够很快的定位到客户端需要访问的存储服务器组,并将选择合适的存储服务器提供客户端访问
- 存储服务器根据“文件存储虚拟磁盘路径”和“数据文件两级目录”可以很快定位到文件所在目录,并根据文件名找到客户端需要访问的文件
同步时间管理
- 每个存储服务器都需要定时将自身的信息上报给 tracker ,信息就包括了本地同步时间(即,同步到最新文件的时间戳)。
同步信息图
集成 nginx
- FastDFS 通过 Tracker 服务器,将文件放在 Storage 服务器存储,但是同组存储服务器之间需要进入文件复制,有同步延迟问题
- fastdfs-nginx-module可以重定向文件连接到文件上传时的源服务器取文件,避免客户端由于复制延迟导致的文件无法访问错误
- 使用nginx反向代理后,后端可以以HTTP请求的方式来访问文件资源。访问nginx反向代理+上传文件时的ID
https://www.cnblogs.com/withLevi/p/15835469.html
FastDFS 部署
环境要求
服务器
- CentOS LInux release 7.9.2009(Core)
FastDFS 下载地址
https://github.com/happyfish100
- fastdfs
- libfastcommon
- fastdfs-nginx-module
- nginx-1.18.0
- 跟踪服务器1【主机】(Tracker1):192.168.10.101
- 跟踪服务器2【备机】(Tracker2):192.168.10.102
- 存储服务器1(Storage1):192.168.10.103
- 存储服务器2(Storage2):192.168.10.104
- 代理服务器(nginx)
上传 fastdfs、fastdfs-nginx-module、libfastcommon 包至 Linux服务器
- 192.168.10.101
- 192.168.10.102
- 192.168.10.103
- 192.168.10.104
上传 nginx 包至 Linux 服务器
- 192.168.10.103
- 192.168.10.104
安装前的准备
检查 Linux 上是否安装了
- gcc*
- libevent
- libevent-devel
yum list installed | grep gcc
yum list installed | grep libevent
yum list installed | grep libevent-devel
如果没有安装,则需进行安装
[root@localhost ~]# yum -y install gcc* zlib-devel libtool pcre-devel libevent
- 安装 libfastcommon 库
- libfastcommon 库是 FastDFS 文件系统运行需要的公共 C 语言函数库
关闭防火墙、内核机制
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# setenforce 0
解压
[root@localhost ~]# tar zxvf libfastcommon-1.0.36.tar.gz
进入
[root@localhost ~]# cd libfastcommon-1.0.36
查看总用量
[root@localhost libfastcommon-1.0.36]# 11
总用量 32
drwxrwxr-x. 2 root root 117 4月 5 2017 doc
-rw-rw-r--. 1 root root 8005 4月 5 2017 HISTORY
-rw-rw-r--. 1 root root 566 4月 5 2017 INSTALL
-rw-rw-r--. 1 root root 1606 4月 5 2017 libfastcommon.spec
-rwxrwxr-x. 1 root root 3099 4月 5 2017 make.sh
drwxrwxr-x. 2 root root 191 4月 5 2017 php-fastcommon
-rw-rw-r--. 1 root root 2763 4月 5 2017 README
drwxrwxr-x. 3 root root 4096 6月 28 21:00 src
执行 make 脚本进行编译
[root@localhost libfastcommon-1.0.36]# ./make.sh
执行 make install 进行安装
[root@localhost libfastcommon-1.0.36]# ./make.sh install
进入
[root@localhost libfastcommon-1.0.36]# cd /usr/lib64
查看所有以 libfast 开头的内容
[root@localhost lib64]# ll libfast**
拷贝
[root@localhost lib64]# cp libfastcommon.so /usr/lib
解压
[root@localhost ~]# tar zxvf fastdfs-5.11.tar.gz
进入
[root@localhost ~]# cd fastdfs-5.11
执行 make 脚本进行编译
[root@localhost fastdfs-5.11]# ./make.sh
执行 make install 进行安装
[root@localhost fastdfs-5.11]# ./make.sh install
查看安装后的效果
// 查看 FastDFS 相关的可执行程序
[root@localhost ~]# ll /usr/bin/fdfs*
命令工具检查
[root@localhost ~]# ll /usr/bin/fdfs*
-rwxr-xr-x 1 root root 455560 Dec 30 18:11 /usr/bin/fdfs_append_file
-rwxr-xr-x 1 root root 468952 Dec 30 18:11 /usr/bin/fdfs_appender_test
-rwxr-xr-x 1 root root 468784 Dec 30 18:11 /usr/bin/fdfs_appender_test1
-rwxr-xr-x 1 root root 452336 Dec 30 18:11 /usr/bin/fdfs_crc32
-rwxr-xr-x 1 root root 455584 Dec 30 18:11 /usr/bin/fdfs_delete_file
-rwxr-xr-x 1 root root 456568 Dec 30 18:11 /usr/bin/fdfs_download_file
-rwxr-xr-x 1 root root 456168 Dec 30 18:11 /usr/bin/fdfs_file_info
-rwxr-xr-x 1 root root 476728 Dec 30 18:11 /usr/bin/fdfs_monitor
-rwxr-xr-x 1 root root 455744 Dec 30 18:11 /usr/bin/fdfs_regenerate_filename
-rwxr-xr-x 1 root root 1629192 Dec 30 18:11 /usr/bin/fdfs_storaged
-rwxr-xr-x 1 root root 478496 Dec 30 18:11 /usr/bin/fdfs_test
-rwxr-xr-x 1 root root 477816 Dec 30 18:11 /usr/bin/fdfs_test1
-rwxr-xr-x 1 root root 662632 Dec 30 18:11 /usr/bin/fdfs_trackerd
-rwxr-xr-x 1 root root 456312 Dec 30 18:11 /usr/bin/fdfs_upload_appender
-rwxr-xr-x 1 root root 457688 Dec 30 18:11 /usr/bin/fdfs_upload_file
- /usr/bin 是 Linux 的环境变量,可通过 echo $PATH 查看
查看配置文件
[root@localhost ~]# ll /etc/fdfs/
total 72
-rw-r--r-- 1 root root 1909 Dec 30 20:29 client.conf
-rw-r--r-- 1 root root 965 Dec 30 18:14 http.conf
-rw-r--r-- 1 root root 31172 Dec 30 18:14 mime.types
-rw-r--r-- 1 root root 3756 Dec 31 09:31 mod_fastdfs.conf
-rw-r--r-- 1 root root 10250 Dec 30 21:23 storage.conf
-rw-r--r-- 1 root root 620 Dec 30 18:09 storage_ids.conf
-rw-r--r-- 1 root root 9138 Dec 30 18:23 tracker.conf
-
注意需要把解压后的 fastdfs-5.11/conf 目录下的两个文件拷贝到/etc/fdfs/,否则后续会有很多奇怪问题不好解决
检查文件与目录
//服务脚本检查
[root@localhost ~]# 1l /etc/init.d/
总用量 48
-rwxr-xr-x. 1 root root 961 7月21 21:05 fdfs_storaged
-rwxr-xr-x. 1 root root 963 7月21 21:05 fdfs trackerd
-rw-r--r--. 1 root root 18281 5月22 2020 functions
-rwxr-xr-x. 1 root root 4569 5月22 2020 netconsole
-rwxr-xr-x. 1 root root 7928 5月22 2020 network
-rw-r--r--. 1 root root 1160 10月2 2020 README
配置 tracker 服务器(在101和102两个主机上执行)
FastDFS 配置文件详解
- client.conf.sample:客户端的配置文件,测试用
- stroage.conf.sample:存储器的配置文件
- tracker.conf.sample:跟踪器的配置文件
复制 tracker 样例配置文件,并重命名
[root@localhost ~]# cp /etc/fdfs/tracker.conf.sample /etc/fdfs/tracker.conf
修改 tracker 配置文件
[root@localhost ~l# vim /etc/fdfs/tracker.conf
//启用配置文件
disabled=false
//tracker 服务器端口(默认 22122)
port=22122
//存储日志和数据的根目录
base path=/fastdfs/tracker
//设置存储组名称
store group=group1
创建 base_path 指定的目录
[root@localhost ~]# mkdir -p /fastdfs/tracker
启动 tracker 服务器
[root@localhost ~]# /etc/init.d/fdfs trackerd start
//初次启动,会在/fastdfs/tracker 目录下生成 logs、data 两个目录
drwxr-xr-x2 root root 4096 1月4 15:00 data
drwxr-xr-x2 root root 4096 1月4 14:38 10gS
检查 FastDFS Tracker Server 是否启动成功
[root@localhost ~]# ps -efgrep fdfs_trackerd
重启服务
fdfs trackerd /etc/fdfs/tracker.conf restart
配置 STORAGE 服务(在103和104主机上设置)
复制 storage 样例配置文件、并重命名
[root@localhost ~]#cp /etc/fdfs/storage.conf.sample /etc/fdfs/storage.conf
编辑配置文件
[root@localhost ~]# vi /etc/fdfs/storage.conf
#修改的内容如下:#启用配置文件
disabled=false#storage 服务端囗
port=23000#数据和日志文件存储根目录
base path=/fastdfs/storage#第一个存储目录
store path0=/fastdfs/storage#tracker 服务器 IP 和端囗
tracker server=192.168.10.101:22122# tracker 服务器 IP2 和端囗
tracker server=192.168.10.102:22122#和 tracker 中的组名保持一致
group name=group1# http 访问文件的端口
http.server port=8888
创建基础数据目录
[root@localhost ~]# mkdir -p /fastdfs/storage
启动 storage 服务器,并检查进程
[root@localhost ~]# /etc/init.d/fdfs_storaged start
[root@localhost ~]# ps -efgrep fdfs_storaged
重启用的命令
fdfs storaged /etc/fdfs/storage.conf restart
配置 client(在任意一个 Fastdfs 节点上配置)
[root@localhost ~]# cp /etc/fdfs/client.conf.sample /etc/fdfs/client.conf
[root@localhost ~]# mkdir -p /fastdfs/tracker
[root@localhost ~l# vim /etc/fdfs/client.conf
#修改以下配置,其它保持默认
base path=/fastdfs/tracker
# tracker 服务器 IP 和端囗
tracker server=192.168.10.103:22122
# tracker 服务器 IP2 和端口
tracker server=192.168.10.104:22122
上传 logo.jpg 图片至服务器
测试上传文件
[root@localhost ~]# fdfs upload file /etc/fdfs/client.conf:logo.jpg
group1/M00/00/00/wKgKaGadD5aAZBiJAAFMnKMUrMI833.jpg
或
[root@localhost ~]# fdfs test /etc/fdfs/client.conf upload logo.jpg
This is FastDFs client test program v5.11
Copyright(c)2008,Happy Fish /YuQing
FastDFS may be copied only under the terms of the GNu GeneralPublic License V3, which may be found in the FastDFs source kit.Please visit the FastDFS Home Page http://www.csource.org/for more detail.
[2024-07-21 21:38:14]base path=/fasfdfs/tracker,DEBUGconnect timeout=30,network timeout=60,tracker server count=2,anti steal token=0, anti steal secret keyg connection pool max idle time=3600s.length=0,use connection pool=0.use storage id=0,storage server id count:0
tracker query storage store list without group :server 1.group name=,ip addr=192.168.10.103,port=23000server 2.group name=,ip addr=192.168.10.104,port=23000server 3.group_name=,ip addr=192.168.10.105,port=23000group_name=group1,ip_addr=192.168.10.103,port=23000
storage_upload_by_filename
group name=group1,remote filename=M00/00/00/wKgKZ2adDaAY5f4AAFMnKMUrMI096.jPg
source ip address: 192.168.10.103
file timestamp=2024-07-21 21:38:14
file size=85148
file crc32=2736041154
example file
url:http://192.168.10.103/group1/M00/00/00/wKgKZ2adDBaAY5f4AAFMnKMUrMI096.jPgstorage upload slave by filename
group_name=group1,remote_filename=M00/00/00/wKgKZ2adDBaAY5f4AAFMnKMUrMI096_big.jpg
source ip address: 192.168.10.103
file timestamp=2024-07-21 21:38:14
file size=85148
file crc32=2736041154
example file
url:http://192.168.10.103/group1/M00/00/00/wKgKZ2adD0aAY5f4AAFMnKMUrMI096_big.jpg
group1/M00:此目录是生成的虚拟目录
文件存储的目录为:/fastdfs/storage/data/00/00
[root@localhost ~]# ll /fastdfs/storage/data/00/00
总用量 260
-rw-r--r--. 1 root root 85148 7月 21 21:39 wKgKaGadD5aAZBiJAAFMnKMUrMI833.jPg
-rw-r--r--. 1 root root 85148 7月 21 21:38 WKgKZ2adDBaAY5f4AAFMnKMUrMI096 big.jpg
-rw-r--r--. 1 root root 49 7月 21 21:38 wKgKZ2adD0aAY5f4AAFMnKMUrMI096 big.jpg-m
-rw-r--r--. 1 root root 85148 7月 21 21:38 WKgKZ2adDaAY5f4AAFMnKMUrMI096.jPg
-rw-r--r--. 1 root root 49 7月 21 21:38 wKgKZ2adDOaAY5f4AAFMnKMUrMI096.jpg-m
在所有 storage 节点安装 fastdfs-nginx-module 和nginx
- storage 上的 nginx 是用于访问具体的文件,并且需要使用 fastdfs 模块。
解压井修改fastdfs-nginx-module 的配置文件
[root@localhost ~]# tar zxvf fastdfs-nginx-module_v1.16.tar.gz
[root@localhost ~]# cd fastdfs-nginx-module/src
进入解压完的目录,到 src 日录,修改 config 文件,将文件路径中存在的 local 删掉
[root@localhost src]# vim config
解压 nginx
[root@localhost ~]# tar zxvf nginx-1.19.5.tar.gz
添加用户
[root@localhost ~]# useradd nginX
进入 nginx 目录
[root@localhost ~]# cd nginx-1.19.5
拷贝文件
[root@localhost nginx-1.19.5]# cp /usr/include/fastcommon/* /usr/include/fastdfs/
配置文件路径
[root@localhost nginx-1.19.5]# ./configure-prefix=/usr/local/nginx --add-module=/root/fastdfs-nginx-module/src/
- 注意:注意:这里的-add-module后面的值就是刚刚拷贝的 fastdfs-nginx-module 的src 目录
执行 make 编译
[root@localhost nginx-1.19.5]# make
执行编译安装
[root@localhost nginx-1.19.5]# make install
配置 FASTDFS
将 fastdfs-nginx-module 模块下的 mod fastdfs.conf 文件拷贝到 /etc/fdfs/目录
[root@localhost ~]# cp /root/fastdfs-nginx-module/src/mod_fastdfs.conf /etc/fdfs/
[root@localhost ~]# vim /etc/fdfs/mod_fastdfs.conf//找到下面的配置进行修改:
1、base_path=/tmp 用户存储日志的,将其改为/fastdfs/temp
2、tracker_server=tracker:22122 修改为当前服务器的 ip 地址
3、group_name=groupname1 修改为之前 storage 中同样的名字
4、url_have_group_name = false 这个表示是否需要增加一个上述自定义名字的需要,改为 true修改参数如下:
connect_timeout=10
base_path=/tmp
# tracker 服务器 IP 和端口tracker
tracker_server=192.168.10.101:22122
# tracker 服务器 IP2 和端囗
server=192.168.10.102:22122
# url 中包含 group 名称
url_have_group_name=truegroup_name=group1
storage_server_port=23000
store_path_count=1
store_path0=/fastdfs/storage
复制 FastDFs 的部分配置文件到/etc/fdfs 目录
[root@localhost ~]# cp /root/fastdfs-5.11/conf/http.conf /etc/fdfs
[root@localhost ~]# cp /root/fastdfs-5.11/conf/mime.types /etc/fdfs
在/fastdfs/storage 文件存储目录下创建软连接,将其链接到实际存放数据的目录
[root@localhost ~]# ln -s /fastdfs/storage/data/ /fastdfs/storage/data/M00
配置 nginx
进入 nginx 目录
[root@localhost ~]# cd /usr/local/nginx/
修改 nginx.conf 文件,在 http 指令块中新增如下配置
[root@localhost nginx]# vim conf/nginx.conf
http {includemime.types;default typeapplication/octet-stream;sendfile on:keepalive timeout 65;server {listen80;server_name localhost;location / {root html;index index.html index.htm;location /group1/M00 {ngx_fastdfs_module;}}
}
创建一个客户端文件
[root@localhost nginx]# mkdir -p /var/temp/nginx/client
重启 storage
[root@localhost nginx]# fdfs storaged /etc/fdfs/storage.conf restart
重启 tracker
[root@localhost nginx]# fdfs trackerd /etc/fdfs/tracker.conf restart
浏览器访问图片
- http://192.168.10.103/group1/M00/00/00/wKgKZ2adDBaAY5f4AAFMKMUrMI096 big.jpg
或
- http://192.168.10.104/group1/M00/00/00/wKgKZ2adD0aAY5f4AAFMnKMUrMI096 big.jpg
部署 nginx 代理(在105主机上配置)
下载所需的依赖
[root@localhost ~]# yum -y install pcre-devel zlib-devel gcc*
解压
[root@localhost ~]# tar zxvf nginx-1.19.5.tar.gz
添加用户
[root@localhost nginx-1.19.5]# useradd nginx
配置路径
root@localhost nginx-1.19,5]#./configure --prefix=/usr/local/nginx --user=nginx
--group=nginx
执行 make install 编译安装
[root@localhost nginx-1.19.5]# make && make install
链接文件路径
[root@localhost ~]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
修改配置文件
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
http {include mime.types;default_type: application/octet-stream;sendfile on;keepalive_timeout 65;upstream storage_server_group1 {server 192.168.10.103:80 weight=10;192.168.10.104:80 weight=10;
}server {listen 80;server_name localhost;location /{proxy_pass http://storage_server_group1;}}
}
打开浏览器测试访问
- http://192.168.10.103/group1/M00/00/00/wKgKZ2adDBaAY5f4AAFMnKMUrMI096 big.jpg
小阿轩yx-FastDFS分布式存储