FastDFS集群搭建与使用(CentOS 7)

部署

环境准备

集群架构

服务器IP服务(端口)
服务器1192.168.2.130tracker0(22122)
服务器2192.168.2.150tracker1(22122)
服务器3192.168.2.124storage0(23000)、nginx(8888)
服务器4192.168.2.119storage1(23000)、nginx(8888)
对外开放的服务器公网IP/域名nginx(80/443)

所需文件

文件描述下载
libfastcommon_V1.0.43.tar.gzFastDFS分离出的一些公用函数包下载
fastdfs_V6.06.tar.gzFastDFS本体下载
fastdfs-nginx-module_V1.22.tar.gzFastDFS和nginx的关联模块,解决组内同步延迟问题下载
nginx-1.16.1.tar.gz下载

官方文档

服务器1:Tracker0

1. 安装编译环境

yum install git gcc gcc-c++ make automake autoconf libtool pcre pcre-devel zlib zlib-devel openssl-devel wget vim -y

2. 安装libfastcommon

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

3. 安装FastDFS

安装
tar -zxvf fastdfs_V6.06.tar.gz
cd fastdfs-6.06/
./make.sh
./make.sh install
cd ..
配置
tracker.conf
cp /etc/fdfs/tracker.conf.sample /etc/fdfs/tracker.conf
vim /etc/fdfs/tracker.conf
#需要修改的内容如下
port=22122 # tracker服务器端口(默认22122,一般不修改)
base_path=/data/fastdfs # 存储日志和数据的根目录
use_storage_id = true # 使用id来区分storage,方便迁移
storage_ids_filename = storage_ids.conf
id_type_in_filename = id

确保配置文件的目录都存在

mkdir -p /data/fastdfs
storage_ids.conf
cp /etc/fdfs/storage_ids.conf.sample /etc/fdfs/storage_ids.conf
vim /etc/fdfs/storage_ids.conf
# 将storage的ip添加进去
100001   group1  172.18.0.2
100002   group1  172.18.0.3
启动
# 启动
fdfs_trackerd /etc/fdfs/tracker.conf start
# 停止
fdfs_trackerd /etc/fdfs/tracker.conf stop
# 重启
fdfs_trackerd /etc/fdfs/tracker.conf restart

服务器2:Tracker1

部署方式同服务器1

服务器3:Storage0 + Nginx

1. 安装编译环境

yum install git gcc gcc-c++ make automake autoconf libtool pcre pcre-devel zlib zlib-devel openssl-devel wget vim -y

2. 安装libfastcommon

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

3. 安装FastDFS

安装
tar -zxvf fastdfs_V6.06.tar.gz
cd fastdfs-6.06/
./make.sh
./make.sh install
cd ..
配置
storage.conf
cp /etc/fdfs/storage.conf.sample /etc/fdfs/storage.conf
vim /etc/fdfs/storage.conf
#需要修改的内容如下
port=9000 # storage服务端口(默认23000,一般不修改)
base_path=/data/fastdfs # 数据和日志文件存储根目录
store_path0=/data/fastdfs/store_path0 # 文件存储目录,可根据磁盘设置
tracker_server = 192.168.2.130:22122 # tracker服务器IP和端口
tracker_server = 192.168.2.150:22122 # tracker服务器IP和端口
http.server_port=8888 # http访问文件的端口(默认8888,看情况修改,和nginx中保持一致)

确保配置文件中设置的目录都存在

mkdir -p /data/fastdfs
mkdir -p /data/fastdfs/store_path0
启动
# 启动
fdfs_storaged /etc/fdfs/storage.conf start
# 停止
fdfs_storaged /etc/fdfs/storage.conf stop
# 重启
fdfs_storaged /etc/fdfs/storage.conf restart
#查看集群状态
fdfs_monitor /etc/fdfs/storage.conf list

4. 安装Nginx

安装
tar -zxvf fastdfs-nginx-module_V1.22.tar.gz
tar -zxvf nginx-1.16.1.tar.gz
cd nginx-1.16.1
./configure --add-module=../fastdfs-nginx-module-1.22/src/
make
make install
cd ..
配置
mod_fastdfs.conf
cp fastdfs-6.06/conf/http.conf /etc/fdfs/
cp fastdfs-6.06/conf/mime.types /etc/fdfs/
cp fastdfs-nginx-module-1.22/src/mod_fastdfs.conf /etc/fdfs/
vim /etc/fdfs/mod_fastdfs.conf
#需要修改的内容如下
tracker_server = 192.168.2.130:22122 # tracker服务器IP和端口
tracker_server = 192.168.2.150:22122 # tracker服务器IP和端口
storage_server_port=23000
url_have_group_name=true
store_path0=/data/fastdfs/store_path0
nginx.config
vim /usr/local/nginx/conf/nginx.conf
#添加如下配置
server {
    listen       8888;    ## 该端口为storage.conf中的http.server_port相同
    server_name  localhost;
    location ~/group1/ {
        ngx_fastdfs_module;
    }
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
    root   html;
    }
}
启动
# 启动
/usr/local/nginx/sbin/nginx
# 停止
/usr/local/nginx/sbin/nginx -s stop
# 重启
/usr/local/nginx/sbin/nginx -s reload

服务器4:Storage1+ Nginx

部署方式同服务器3

服务器1-4:Client(仅供测试)

可在任意安装了FastDFS上的服务器上运行测试

配置

cp /etc/fdfs/client.conf.sample /etc/fdfs/client.conf
vim /etc/fdfs/client.conf
#需要修改的内容如下
base_path=/data/fastdfs_client # 数据和日志文件存储根目录
tracker_server = 192.168.2.130:22122 # tracker服务器IP和端口
tracker_server = 192.168.2.150:22122 # tracker服务器IP和端口
use_storage_id = true # 使用id来区分storage,方便迁移

确保配置文件中的目录都存在

mkdir -p /data/fastdfs_client

启动

# 测试上传,上传成功后返回文件路径:group1/M00/00/00/rBIAAV6ZN62AKxKOAA_ZI6CZBeI.tar.gz
fdfs_upload_file /etc/fdfs/client.conf /root/fastdfs/nginx-1.17.8.tar.gz

配置外部访问的Nginx

参考配置

upstream fdfs_group01 {
        server 192.168.2.124:8888 weight=1 max_fails=2 fail_timeout=30s;
        server 192.168.2.119:8888 weight=1 max_fails=2 fail_timeout=30s;
    }

server {
   listen       80;
   server_name  localhost;
   
   location / {
       root   html;
       index  index.html index.htm;
   }

   location /files {
       proxy_next_upstream http_502 http_504 error timeout invalid_header;
       proxy_pass http://fdfs_group01/;
       expires 30d;
   }
}

尝试在浏览器中访问

http://127.0.0.1/files/group1/M00/00/00/rBIAAV6ZN62AKxKOAA_ZI6CZBeI.tar.gz

这里127.0.0.1应换成外网可访问的nginx的地址

一切正常时,会弹出下载框

开启token防盗链

开启后访问文件需要一个定期失效的token,以防止链接被滥用。

配置

vim /etc/fdfs/http.conf
# 需要配置的内容如下
# if use token to anti-steal
# default value is false (0)   
# 是否做token检查,缺省值为false。
http.anti_steal.check_token=true
 
# token TTL (time to live), seconds
# default value is 600
# TTL,即生成token的有效时长(秒)
http.anti_steal.token_ttl=3600
 
# secret key to generate anti-steal token
# this parameter must be set when http.anti_steal.check_token set to true
# the length of the secret key should not exceed 128 bytes
# 生成token的密钥,尽量设置得长一些,千万不要泄露出去
http.anti_steal.secret_key=14789632
 
# return the content of the file when check token fail
# default value is empty (no file sepecified)
# 检查失败,返回的文件内容,需指定本地文件名
http.anti_steal.token_check_fail=

启动

# 重启tracker
fdfs_trackerd /etc/fdfs/tracker.conf restart
# 重启storage
fdfs_storaged /etc/fdfs/storage.conf restart
# 重启nginx
./nginx -s reload
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值