FastDFS多group配置

简介

FastDFS是一个高可用的分布式文件存储系统,功能包括:文件存储、文件同步、文件访问、高可用和负载均衡。适合大小在4KB~500MB的文件存储。

FastDFS分为Tracker Server和Storage Server。

Tracker Server主要提供调度工作和负载均衡,负责管理所有的Storage Server,每个Storage Server在启动后会连接到Tracker Server,告知自己所属的group信息,并保持周期性的心跳。

Storage Server是存储服务,提供容量和备份服务;以group为单位,每个group内可以有多台Storage Server,数据互为备份。

安装

0x01 环境

Centos7 X64,单机

0x02 依赖安装

FastDFS基于C语言开发的,需要编译安装。

yum -y install gcc gcc-c++ libevent git

0x03 下载安装包

源码地址:https://github.com/happyfish100

wget https://github.com/happyfish100/fastdfs/archive/V6.06.tar.gz
wget https://github.com/happyfish100/libfastcommon/archive/V1.0.43.tar.gz
wget https://github.com/happyfish100/fastdfs-nginx-module/archive/V1.22.tar.gz

PS:也可以git clone到本地编译安装

0x04 安装

先安装前置包libfastcommon

解压编译

tar -zxvf V1.0.43.tar.gz
cd libfastcommon-1.0.43
./make.sh
./make.sh install

libfastcommon安装好后会将库文件拷贝到/usr/lib64下,FastDFS引用/usr/lib目录下的文件,需要将库文件拷贝到/usr/lib下

cp /usr/lib64/libfastcommon.so /usr/lib

安装FastDFS

解压编译

tar -zxvf V6.06.tar.gz
cd fastdfs-6.06
./make.sh
./make.sh install

0x05 配置

安装成功后,在/etc目录下会生成一个fdfs目录,这个是存放FastDFS配置文件的目录,里面有4个.sample结尾的实例配置文件。每个文件都拷贝一个副本。

cd /etc/fdfs/
cp tracker.conf.sample tracker.conf
cp storage.conf.sample storage.conf
cp storage_ids.conf.sample storage_ids.conf
cp client.conf.sample client.conf

主要用到的配置文件有tracker.conf和storage.conf

Tracker Server配置

使用文本编辑工具打开tracker.conf,我使用的是vim

vim tracker.conf

修改以下配置

#文件存储和log日志存储路径
base_path=/data/fdfs/file
#Tracker Server端口,22122为默认端口,可以自己修改
port=22122

注意:启动FastDFS时,并不会自动创建此目录,需要先手动创建,否则,启动会报错。

mkdir -p /data/fdfs/file

Storage Server配置

打开storage.conf

vim storage.conf

修改以下配置信息

#文件存储路径
base_path=/data/fdfs/file/storage
#store_path数量,按需求配置
store_path_count=1
#store_path路径,对应store_path_count的数量,从0开始,store_path_count配置几个,store_path数量就配置多少个。目录不要和base_path重复。
store_path0=/data/fdfs/file/storage/path0
#Storage Server的端口
port=23000
#Tracker Server地址,此处是单机部署,所以只有一个地址,如果是集群部署,就配置多个tracker_server
tracker_server=191.168.1.100:22122

注意:同Tracker配置一样,此处的目录也要手动创建

mkdir -p /data/fdfs/file/storage
mkdir -p /data/fdfs/file/storage/path0

0x06 启动服务

启动Tracker Server

/usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf start

启动Storage Server

/usr/bin/fdfs_storaged /etc/fdfs/storage.conf start

查看启动情况

通过ps -aux | grep fdfs可以查看FastDFS启动情况。 也可以到配置的base_path下查看log日志。

0x07 测试

FastDFS提供了测试功能,可以通过fdfs_test测试文件的上传和下载功能。 修改配置文件

vim /etc/fdfs/client.conf

修改以下配置

#和tracker.conf的base_path保持一致
base_path=/data/fdfs/file
#Tracker Server地址
tracker_server=192.168.1.100:22122

测试命令

/usr/bin/fdfs_test /etc/fdfs/client.conf upload /data/fdfs/test/img/test.jpg

文件上传成功后,会返回文件的访问路径http://192.168.1.100/group1/M00/00/00/L1xWGl6QA-OAAhHEAAApmmokR2k575.jpg

文件的保存路径为:/data/fdfs/file/storage/path0/data/00/00/L1xWGl6QA-OAAhHEAAApmmokR2k575.jpg

0x07 整合Nginx

FastDFS文件访问,需要配合nginx。

解压

tar V1.22.tar.gz
cd fastdfs-nginx-module-1.22/src/

修改配置

打开config文件,修改以下配置

ngx_module_incs="/usr/local/include"
CORE_INCS="$CORE_INCS /usr/local/include"
修改为
ngx_module_incs="/usr/include/fastdfs /usr/include/fastcommon/"
CORE_INCS="$CORE_INCS /usr/include/fastdfs /usr/include/fastcommon/"

将fastdfs-nginx-module-1.22/src/mod_fastdfs.conf拷贝至/etc/fdfs/目录下

cp fastdfs-nginx-module-1.22/src/mod_fastdfs.conf /etc/fdfs/

修改/etc/fdfs/mod_fastdfs.conf

base_path=/data/fdfs/file
tracker_server=192.168.1.100:22122
store_path0=/data/fdfs/file/storage/path0
url_have_group_name=true

将/usr/lib64/libfdfsclient.so拷贝至/usr/lib/下

cp /usr/lib64/libfdfsclient.so /usr/lib/

安装nginx

安装nginx的过程在此省略,不做说明,不懂nginx安装的,可以去网上搜索教程。

此处需要说明的是,安装nginx时,需要将支持FastDFS的模块添加进去。

编译时添加

#路径写自己的下载的安装包路径。
./configure --add-module=/data/fastdfs/package/fastdfs-nginx-module-1.22/src/

其它需要的模块按照自己的需要添加。

注意:安装nginx成功之后,还需要将fast源码目录下的http.conf和mime.types文件拷贝到/etc/fdfs/目录下

cd /data/fastdfs/package/fastdfs-6.06/conf
cp http.conf /etc/fdfs/
cp mime.types /etc/fdfs/

在nginx中配置图片访问代理路径

#找到你的nginx.conf文件路径,不一定和我的一样
vim /data/nginx/conf/nginx.conf

添加配置如下

location ~/group1/M00 {
      ngx_fastdfs_module;
}

启动nginx,在浏览器打开之前的图片地址,即可查看图片。

多group配置,实现不同的项目文件路径隔离

重点来了,如果想实现文件按照不同的项目存储到不同的目录下,实现文件隔离,应该如何配置呢?请继续往下看

storage配置

比如我有两个服务,一个权限管理服务(auth),一个内容管理服务(cms)。我想实现权限管理服务的文件全部放到/data/fdfs/file/storage/auth/path0目录下,内容管理服务的文件全部放到/data/fdfs/file/storage/cms/path0下,可以进行如下配置。
拷贝storage.conf

cp storage.conf storage_auth.conf
cp storage.conf storage_cms.conf

修改storage_auth.conf如下配置

group_name=auth
port=23000
base_path=/data/fdfs/file/storage/auth
store_path0=/data/fdfs/file/storage/auth/path0
tracker_server=192.168.1.100:22122

修改storage_cms.conf如下配置

group_name=cms
port=23001
base_path=/data/fdfs/file/storage/cms
store_path0=/data/fdfs/file/storage/cms/path0
tracker_server=192.168.1.100:22122

创建/data/fdfs/file/storage/auth目录和/data/fdfs/file/storage/cms目录,否则启动报错

mkdir -p /data/fdfs/file/storage/auth
mkdir -p /data/fdfs/file/storage/auth/path0
mkdir -p /data/fdfs/file/storage/cms
mkdir -p /data/fdfs/file/storage/cms/path0

启动Storage Server

/usr/bin/fdfs_storaged /etc/fdfs/storage_auth.conf start
/usr/bin/fdfs_storage /etc/fdfs/storage_cms.conf start

通过ps -ef | grep fdfs可以查看启动的服务情况,可以看到一个tracker服务和两个storage服务

root     25245     1  0 3月02 ?       00:10:15 /usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf start
root     25261     1  0 3月02 ?       00:09:50 /usr/bin/fdfs_storaged /etc/fdfs/storage_auth.conf start
root     27162     1  0 5月12 ?       00:01:12 /usr/bin/fdfs_storaged /etc/fdfs/storage_cms.conf start

注意:因为此时配置了2个storage的,每个storage有单独的group,nginx访问时,需要修改/etc/fdfs/mod_fastdfs.conf配置文件

#group_count对应你要配置的group数量,注意,此处和集群配置的区别
group_count = 2

#group_count有多少,此处配置相同的group
[group1]
group_name=auth
storage_server_port=23000
store_path_count=1
store_path0=/data/fdfs/file/storage/auth/path0

[group2]
group_name=cms
storage_server_port=23001
store_path_count=1
store_path0=/data/fdfs/file/storage/cms/path0

修改nginx配置,添加auth和cms的路径代理

location ~/auth/M00 {
      ngx_fastdfs_module;
}

location ~/cms/M00 {
      ngx_fastdfs_module;
}

配置完成后,重启nginx