FastDFS安装详解

25次阅读

共计 5243 个字符,预计需要花费 14 分钟才能阅读完成。

FastDFS 是一款开源的分布式文件系统 (Distributed File System), 能够提供小型文件的储存、备份和下载功能。以下是它的安装方式。
基于 6 台 CentOS 内外网连通的主机,CentOS 安装过程省略
主要 参考官方 Github Repo 的 Wiki 页面 和 FastDFS 分布式文件系统(系列文章)

用 root 安装
编译环境
yum install git gcc gcc-c++ make automake autoconf libtool pcre pcre-devel zlib zlib-devel openssl-devel wget vim -y
磁盘目录

说明
位置

所有安装包
/usr/local/src

数据存储位置
/home/dfs/

这里我为了方便把日志什么的都放到了 dfs
mkdir /home/dfs #创建数据存储目录
cd /usr/local/src #切换到安装目录准备下载安装包
安装 libfatscommon
libfatscommon 是 fastdfs 常用函数封装的库
git clone https://github.com/happyfish100/libfastcommon.git –depth 1
cd libfastcommon/
./make.sh && ./make.sh install #编译安装
安装 FastDFS
fastdfs 包含 tracker, storage 和 client 这三个角色,通过后期的 config 配置(storage 还需要安装 fastdfs-nginx-module)和运行,本主机就能成为 “tracker/storage/client”
cd /usr/local/src

git clone https://github.com/happyfish100/fastdfs.git –depth 1
cd fastdfs/
./make.sh && ./make.sh install #编译安装
#暂时直接复制到目标目录,没有其他修改,相关配置可以在跑通后详细阅读和修改
cp /usr/local/src/fastdfs/conf/http.conf /etc/fdfs/ #供 nginx 访问使用
cp /usr/local/src/fastdfs/conf/mime.types /etc/fdfs/ #供 nginx 访问使用
#配置文件准备 (Notice: 根据本机的角色,按需复制粘贴配置文件!)
cp /etc/fdfs/tracker.conf.sample /etc/fdfs/tracker.conf
cp /etc/fdfs/storage.conf.sample /etc/fdfs/storage.conf
cp /etc/fdfs/client.conf.sample /etc/fdfs/client.conf #客户端文件,测试用
获取 fastdfs-nginx-module
此 module 用于避免上传完成后,且 group storage 同步文件完成前,客户端从正在同步的主机处下载文件导致的出错
cd /usr/local/src

git clone https://github.com/happyfish100/fastdfs-nginx-module.git –depth 1

cp /usr/local/src/fastdfs-nginx-module/src/mod_fastdfs.conf /etc/fdfs
安装 nginx

tracker 和 storage 均需要安装 nginx
对于 tracker, nginx 可以均衡负载到不同的 storage
对于 storage, nginx 可以提供反向代理,从而访问数据存储位置的文件
由于需要使用启用非默认 module , 故需要通过源码编译安装,不能使用 yum . 使用源码安装 nginx 的注意事项见《nginx – make install》

wget http://nginx.org/download/nginx-1.15.7.tar.gz #下载 nginx mainline 版本的压缩包
tar -zxvf nginx-1.15.4.tar.gz #解压
cd nginx-1.15.7/
#记得添加 fastdfs-nginx-module 模块
# nginx configure arguments 内容丰富、配置自由,详见《nginx – make install》
./configure \
–prefix=/usr/local/nginx \
–user=www-data \
–group=www-data \
–conf-path=/etc/nginx/nginx.conf \
–add-module=/usr/local/src/fastdfs-nginx-module/src/
make && make install #编译安装
安装完成后,新增 www-data 用户组 和 www-data 用户
/usr/sbin/groupadd -f www-data
/usr/sbin/useradd -g www-data www-data
配置

需要配置 nginx, tracker, storage 和 client
tracker 和 storage 需要搭配 nginx 使用,故在本教程中,nginx 的配置都是关于 tracker 和 storage 的,与 client 无关。
tracker, storage 和 client 都有自身的配置项,需要单独配置。

tracker
vim /etc/fdfs/tracker.conf
base_path=/home/yuqing/FastDFS
改为:
base_path=/home/dfs

# 将 tracker 和 storage 的 http.server_port 统一改为 80 可以正常访问,但是否最优方案,还需要观察
http.server_port=8080
改为:
http.server_port=80
storage
vim /etc/fdfs/storage.conf
#当前主机是什么组,就写什么组
group_name=group1

base_path=/home/yuqing/FastDFS
改为:
base_path=/home/dfs

store_path0=/home/yuqing/FastDFS
改为:
store_path0=/home/dfs/fdfs_storage

# 如果有多个挂载磁盘则定义多个 store_path,如下
#store_path_count=..
以及:
#store_path1=…..
#store_path2=……

# 配置 tracker 服务器:IP
tracker_server=192.168.56.11:22122
#如果有多个则配置多个 tracker
tracker_server=192.168.56.12:22122

# 配置 http 端口
http.server_port=80
client
vim /etc/fdfs/client.conf
base_path=/home/yuqing/fastdfs
改为:
base_path=/home/dfs

tracker_server=192.168.56.11:22122 # 服务器 1
tracker_server=192.168.56.12:22122 # 服务器 2
… #服务器 n
mod_FastDFS
vim /etc/fdfs/mod_fastdfs.conf
base_path=/home/yuqing/fastdfs
改为:
base_path=/home/dfs

tracker_server=192.168.56.11:22122
tracker_server=192.168.56.12:22122
url_have_group_name=true #url 中包含 group 名称
store_path0=/home/dfs/fdfs_storage #指定文件存储路径
nginx
vim /etc/nginx/nginx.conf
#storage 群 group1 组
upstream storage_server_group1 {
server 192.168.56.11:80 weight=10;
server 192.168.56.21:80 weight=10;
server 192.168.56.22:80 weight=10;
}
#storage 群 group2 组
upstream storage_server_group2 {
server 192.168.56.12:80 weight=10;
server 192.168.56.31:80 weight=10;
server 192.168.56.32:80 weight=10;
}

server {
# 监听 80 端口
listen 80;
server_name 本机 IP;

#此处 /group1 和 /group2 都粘贴,用于 proxy_pass
location /group1{
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://storage_server_group1;
}

location /group2{
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://storage_server_group2;
}

# 留意当前主机作为 storage 是 group1 还是 group2,然后修改 location /…/…
# M00 是 FastDFS 自动生成编号,对应 store_path0=/home/dfs/fdfs_storage,如果 FastDFS 定义 store_path1,这里就是 M01
location /group1/M00/{
root /home/dfs/fdfs_storage/data;
ngx_fastdfs_module;
}
}
检查 /usr/lib 有无 libfdfsclient.so
ll /usr/lib/libfdfsclient.so

如果没有就

cp /usr/lib64/libfdfsclient.so /usr/lib/
启动
关闭防火墙
生产环境中,我不建议关闭防火墙。只需要开放相关端口就可以正常运行。
systemctl stop firewalld.service #关闭
systemctl restart firewalld.service #重启
tracker
/etc/init.d/fdfs_trackerd start #启动 tracker 服务
/etc/init.d/fdfs_trackerd restart #重启动 tracker 服务
/etc/init.d/fdfs_trackerd stop #停止 tracker 服务
chkconfig fdfs_trackerd on #自启动 tracker 服务
storage
/etc/init.d/fdfs_storaged start #启动 storage 服务
/etc/init.d/fdfs_storaged restart #重动 storage 服务
/etc/init.d/fdfs_storaged stop #停止动 storage 服务
chkconfig fdfs_storaged on #自启动 storage 服务
检测集群
/usr/bin/fdfs_monitor /etc/fdfs/storage.conf
# 会显示有几台 tracker, 几个 group, 几个 storage 等详细信息
nginx
/usr/local/nginx/sbin/nginx #启动 nginx
/usr/local/nginx/sbin/nginx -s reload #重启 nginx
/usr/local/nginx/sbin/nginx -s stop #停止 nginx
测试
client 使用 fdfs 命令上传
fdfs_upload_file /etc/fdfs/client.conf /path/to/a_file
上传完成后,获取 如 /group1/M00/00/00/xxxxxxxxxxxxx 的路径,说明上传成功
client 使用 fdfs 命令下载
fdfs_download_file /etc/fdfs/client.conf /group1/M00/00/00/xxxxxxxxxxxxx
如果下载成功会有相应提示
client 使用 wget 下载
wget http://ip//group1/M00/00/00/xxxxxxxxxxxxx
如果配置完全正确,那么使用集群中任意主机的 IP 都能够下载文件。如果下载成功,会有相应提示。
Done!

正文完
 0