天天看点

FastDFS分布式文件系统搭建与配置

作者:leenhem
FastDFS分布式文件系统搭建与配置

FastDFS分布式文件系统搭建

介绍

FastDFS是一个开源的轻量级的高性能分布式文件系统。

其主要功能包括:文件存储、文件同步和文件访问(文件上传和文件下载),解决了大容量和负载均衡问题。FastDFS应满足基于照片分享网站、视频分享网站等文件的网站的服务要求,适合存储 4KB~500MB 之间的小文件,特别适合以文件为载体的在线服务,如图片、视频、文档等等。

FastDFS服务安装环境准备

FastDFS安装所需要的组件

名称 说明
OpenResty 文件下载使用的服务
Libfatscommon FastDFS分操作的一些公共函数包
FastDFS Nginx Module FastDFS和Nginx的关联模块
FastDFS FastDFS分布式文件系统

环境准备

服务 IP地址 操作系统
Tracker Server 跟踪器 192.168.65.116 CentOS Linux release 7.3
Storage Server1 存储节点 192.168.65.74 CentOS Linux release 7.3
Storage Server1 存储节点 192.168.65.75 CentOS Linux release 7.3

服务器上安装基础环境

yum install gcc gcc-c++ make pcre-devel zlib-devel openssl-devel           

FastDFS服务安装路径规划

位置 说明
/dfs/soft 所有安装包
/dfs/tracker tracker服务器数据存储位置
/dfs/storage storage服务器数据存储位置
[root@fdfs-116 ~]# mkdir /dfs/tracker -p  #创建数据存储目录(tracker服务器)
[root@fdfs-74 ~]# mkdir /dfs/storage -p #创建数据存储目录(storage服务器)
[root@fdfs-75 ~]# mkdir /dfs/storage -p #创建数据存储目录(storage服务器)           

安装libfastcommon(tracker和storage都安装)

cd /dfs/soft #切换到安装目录准备下载安装包
git clone https://github.com/happyfish100/libfastcommon.git --depth 1
#如果服务器未联网,需要手动下载libfastcommon的安装包
#解压后进入libfastcommon目录,手动./make.sh && ./make.sh install
[root@fdfs-116 soft]# unzip libfastcommon-master.zip
[root@fdfs-116 soft]# cd libfastcommon-master
[root@fdfs-116 libfastcommon-master]# ./make.sh && ./make.sh install #编译安装           

安装FastDFS(tracker和storage都安装)

cd /dfs/soft #切换到安装目录准备下载安装包
git clone https://github.com/happyfish100/fastdfs.git --depth 1
#如果服务器未联网需要手动下载FastDfs的安装包
#解压后进入fastdfs目录中,并手动./make.sh && ./make.sh install
[root@fdfs-116 soft]# tar -zxvf FastDFS_v5.08.tar.gz 
[root@fdfs-116 soft]# cd FastDFS
[root@fdfs-116 FastDFS]# ./make.sh && ./make.sh install  #编译安装

#配置文件准备
#tracker配置文件(tracker服务器使用)
[root@fdfs-116 FastDFS]# cp conf/tracker.conf /etc/fdfs/
#storage配置文件(storage服务器使用)
[root@fdfs-74 FastDFS]# cp conf/storage.conf /etc/fdfs/
[root@fdfs-75 FastDFS]# cp conf/storage.conf /etc/fdfs/
#客户端文件,测试用
[root@fdfs-116 FastDFS]# cp conf/client.conf /etc/fdfs/
#Fastdfs为fastdfs安装路径,这里是我的安装路径,请以自己的实际安装路径为准
[root@fdfs-116 FastDFS]# cp conf/http.conf conf/mime.types /etc/fdfs/
[root@fdfs-74 FastDFS]# cp conf/http.conf conf/mime.types /etc/fdfs/
[root@fdfs-75 FastDFS]# cp conf/http.conf conf/mime.types /etc/fdfs/           

安装fastdfs-nginx-module(storage服务器安装)

cd /dfs/soft #切换到安装目录准备下载安装包
git clone https://github.com/happyfish100/fastdfs-nginx-module.git --depth 1
#如果服务器未连接互联网,需要手动下载fastdfs-nginx-module包
#此处的fastdfs-nginx-module包需要注意版本
# /dfs/soft/fastdfs-nginx-module为解压目录,这里是我的目录,实际请以自己的为准
[root@fdfs-74 soft]# unzip fastdfs-nginx-module.zip 
[root@fdfs-74 soft]# cp /dfs/soft/fastdfs-nginx-module/src/mod_fastdfs.conf /etc/fdfs/
[root@fdfs-75 soft]# unzip fastdfs-nginx-module.zip 
[root@fdfs-75 soft]# cp /dfs/soft/fastdfs-nginx-module/src/mod_fastdfs.conf /etc/fdfs/           

安装OpenResty(storage服务器安装)

cd /dfs/soft #切换到安装目录准备下载安装包
wget https://openresty.org/download/openresty-1.19.3.1.tar.gz #下载nginx压缩包
#如果服务器未连接互联网,需要手动下载nginx包,并解压
[root@fdfs-74 soft]# tar -zxf openresty-1.19.3.1.tar.gz 
[root@fdfs-74 soft]# cd openresty-1.19.3.1
#添加fastdfs-nginx-module模块
#prefix指定一个自己的nginx目录
#add-module指定对应的fastdfs-nginx-module目录
#/dfs/soft/fastdfs-nginx-module这里是我自己的目录,实际请以自己的为准
[root@fdfs-74 openresty-1.19.3.1]# ./configure --add-module=/dfs/soft/fastdfs-nginx-module/src/
[root@fdfs-74 openresty-1.19.3.1]# gmake && gmake install #编译安装
#另一台Storage一样的命令
[root@fdfs-75 openresty-1.19.3.1]# ./configure --add-module=/dfs/soft/fastdfs-nginx-module/src/
[root@fdfs-75 openresty-1.19.3.1]# gmake && gmake install #编译安装           

以上3~6步,已经把FastDFS的组件安装完成

FastDFS集群配置(Tracker配置)

tracker.conf 需要修改的内容如下

vim /etc/fdfs/tracker.conf  
port=22122  # tracker服务器端口(默认22122,一般不修改)
base_path=/dfs/tracker  # 存储tracker日志和数据的根目录
store_group=group1 #设置组名
http.server_port = 8080 #tracker HTTP服务端口           

修改防火墙,开放22122端口和8080端口(iptables或firewall,此处以firewall为例)

# 开放端口
firewall-cmd --permanent --add-port=22122/tcp --add-port=8080/tcp
#重启防火墙(修改配置后要重启防火墙)
firewall-cmd --reload
# 查询端口是否开放
firewall-cmd --query-port=22122/tcp
firewall-cmd --query-port=8080/tcp           

启动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服务           

FastDFS集群配置(Storage配置)

storage.conf 需要修改的内容如下

vim /etc/fdfs/storage.conf
port=23000  # storage服务端口(默认23000,一般不修改)
base_path=/dfs/storage  # 数据和日志文件存储根目录
store_path0=/dfs/storage  # 第一个存储目录
tracker_server=192.168.65.116:22122  # tracker服务器1
#tracker_server=xxx.xxx.xx.xxx:22122 # tracker服务器2
http.server_port=8888  # http访问文件的端口(默认8888,看情况修改,和OpenResty中保持一致)           

修改防火墙,开放23000端口和8888端口(iptables或firewall,此处以firewall为例)

# 开放端口
firewall-cmd --permanent --add-port=23000/tcp --add-port=8888/tcp
#重启防火墙(修改配置后要重启防火墙)
firewall-cmd --reload
# 查询端口是否开放
firewall-cmd --query-port=23000/tcp
firewall-cmd --query-port=8888/tcp           

启动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服务           

配置Client测试(tracker服务器)

修改client.conf配置文件

vim /etc/fdfs/client.conf
#需要修改的内容如下
base_path=/dfs/tracker #tracker服务器
tracker_server=192.168.65.116:22122  # tracker服务器1
#tracker_server=xxx.xxx.xx.xxx:22122 # tracker服务器2
http.tracker_server_port=8080 #修改端口           

测试上传文件

#保存后测试,返回ID表示成功 如:group1/M00/00/00/xx.tar.gz
[root@fdfs-116 soft]# fdfs_upload_file /etc/fdfs/client.conf ./7-1664778559.png 
group1/M00/00/00/wKhBSmQAQ5KAKKUsAADlaTAPbnU808.png           

配置OpenResty使可以通过HTTP访问(两台Storage都需要配置)

修改fastdfs-nginx-module

vim /etc/fdfs/mod_fastdfs.conf
#需要修改的内容如下
tracker_server=192.168.65.116:22122  # 服务器1
#tracker_server=xxx.xxx.xx.xx:22122 # 服务器2
url_have_group_name=true #访问路径中携带组名,如group1
store_path0=/dfs/storage #storage地址           

修改Nginx配置

#配置nginx.config
vim /usr/local/openresty/nginx/conf/nginx.conf
#添加如下配置
server {
    listen       8888;    #该端口为storage.conf中的http.server_port相同
    server_name  localhost;
    location ~/group[0-9]/ {
        ngx_fastdfs_module;
    }
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
    root   html;
    }
}           

启动Nginx服务

#测试Nginx配置
[root@fdfs-74 openresty-1.19.3.1]# /usr/local/openresty/nginx/sbin/nginx -t
ngx_http_fastdfs_set pid=32222
nginx: the configuration file /usr/local/openresty/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/openresty/nginx/conf/nginx.conf test is successful

 /usr/local/openresty/nginx/sbin/nginx #启动nginx
 /usr/local/openresty/nginx/sbin/nginx -s reload #重启nginx
 /usr/local/openresty/nginx/sbin/nginx -s stop #停止nginx           

测试一下看上传的文件能不能访问

group1/M00/00/00/wKhBSmQAQ5KAKKUsAADlaTAPbnU808.png           
FastDFS分布式文件系统搭建与配置

FastDFS

继续阅读