分布式存储FastDFS搭建
一、FastDFS分布式系统架构
FastDFS:是一个开源的轻量级分布式文件系统,功能包括:文件存储、文件同步、文件访问(文件上传、文件下载)等,解决了大容量存储和负载均衡的问题。特别适合中小文件(建议范围:4KB < file_size <500MB),对以文件为载体的在线服务,如相册网站、视频网站等。
FastDFS是为互联网应用量身定做的分布式文件系统,充分考虑了冗余备份、负载均衡、线性扩容等机制,并注重高可用、高性能等指标。和现有的类Google FS分布式文件系统相比,FastDFS的架构和设计理念有其独到之处,主要体现在轻量级、分组方式和对等结构三个方面。
FastDFS架构图
FastDFS服务端有两个角色:跟踪器(tracker)和存储节点(storage)。跟踪器主要做调度工作,在访问上起负载均衡的作用。
FastDFS模块介绍
(1) tracker server:跟踪服务器,用来调度来自客户端的请求,且在内存中记录所有存储组和存储服务器的信息状态。
(2) storage server:存储服务器,用来存储文件(data)和文件属性(metadata)。
(3) client:客户端,业务请求发起方,通过专用接口基于TCP协议与tracker server和storage server进行交互。
(4) group:组,也可称为卷,同组内上的文件是完全相同的。
(5) 文件标识:包括两部分,组名(group)和文件名(含路径)
(6) 文件相关属性:键值对(Key Value Pair)方式
(7) 文件名:与原文件名并不相同。由storage server根据特定信息生成,并且可逆,文件名包含:源存储服务器的IP地址、文件创建时间戳、文件大小、随机数和文件扩展名等。
二、前期准备
2.1、主机规划
IP | 描述 | 分组 |
---|---|---|
192.168.80.7 | 跟踪服务器1&分布式存储服务器1&web负载均衡服务器 | group1 |
192.168.80.17 | 跟踪服务器2&分布式存储服务器2&web负载均衡服务器 | group2 |
192.168.80.27 | 跟踪服务器3&分布式存储服务器3&web负载均衡服务器 | group3 |
2.2、关闭防火墙
# 停止
systemctl stop firewalld.service
# 禁用
systemctl disable firewalld.service
2.3、关闭SELinux
setenforce 0
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/sysconfig/selinux
2.4、设置时间同步
# 设置时区
timedatectl set-timezone Asia/Shanghai
# 同步时间
yum install -y ntpdate
ntpdate time1.aliyun.com
三、部署FastDFS
3.1、安装libfastcommon
#安装依赖包
yum install -y make cmake gcc gcc-c++
#创建软件包存放目录
mkdir -pv /usr/local/soft/package
cd /usr/local/soft/package
#下载libfastcommon
wget https://github.com/happyfish100/libfastcommon/archive/V1.0.43.tar.gz
#解压并安装
tar -xf V1.0.43.tar.gz
cd libfastcommon-1.0.43
./make.sh
./make.sh install
3.2、安装FastDFS
#下载安装包
cd /usr/local/soft/package
wget https://github.com/happyfish100/fastdfs/archive/V6.06.tar.gz
#解压并安装
tar -xf V6.06.tar.gz
cd fastdfs-6.06
./make.sh
./make.sh install
3.3、配置跟踪器Tracker
(1) 复制配置文件
cd /etc/fdfs
cp tracker.conf.sample tracker.conf
(2) 修改配置文件
vim /etc/fdfs/tracker.conf
#注意以下项目的修改
disabled=false //启用配置文件
port=22122 //tracker 的端口号,一般采用 22122 这个默认端口
base_path = /usr/local/soft/fastdfs/tracker //tracker 的数据文件和日志目录
store_lookup=2 //上传文件采取组选择方式,0.轮巡、1.指定组、2.选择最大空闲空间的组
(3) 创建数据目录
mkdir -p /usr/local/soft/fastdfs/tracker
(4) 启动tracker服务
/etc/init.d/fdfs_trackerd start
(5) 设置tracker服务开机启动
chkconfig fdfs_trackerd on
3.4、配置FastDFS存储服务
(1) 复制配置文件
cd /etc/fdfs
cp storage.conf.sample storage.conf
(2) 修改配置文件
vim /etc/fdfs/storage.conf
#注意以下项目的修改
disabled=false //启用配置文件
group_name = group1 //组名(按主机规划里的修改各台主机的配置)
base_path = /usr/local/soft/fastdfs/storage //设置storage的数据和日志目录
store_path_count = 1 //存储路径个数,需要和 store_path 个数匹配
store_path0=/usr/local/soft/fastdfs/storage //存储路径
tracker_server=192.168.80.7:22122 //tracker 服务器的 IP 地址和端口
tracker_server=192.168.80.17:22122 //多个 tracker 直接添加多条配置
tracker_server=192.168.80.27:22122 //多个 tracker 直接添加多条配置
http.server_port=8088 //设置 http 端口号
(3) 创建数据目录
mkdir -p /usr/local/soft/fastdfs/storage
(4) 启动storage服务
/etc/init.d/fdfs_storaged start
(5) 设置storage服务开机启动
chkconfig fdfs_storaged on
(6) 查看集群信息
#所有storage节点都启动之后,可以在任一storage节点上使用如下命令查看集群信息
/usr/bin/fdfs_monitor /etc/fdfs/storage.conf
3.5、配置客户端服务
(1) 复制配置文件
cd /etc/fdfs
cp client.conf.sample client.conf
(2) 修改配置文件
vim /etc/fdfs/client.conf
#注意以下项目的修改
base_path = /usr/local/soft/fastdfs/client //设置client的数据和日志目录
tracker_server=192.168.80.7:22122 //tracker服务器的IP地址和端口
tracker_server=192.168.80.17:22122 //多个 tracker 直接添加多条配置
tracker_server=192.168.80.27:22122 //多个 tracker 直接添加多条配置
(3) 创建数据目录
mkdir -p /usr/local/soft/fastdfs/client
(4) 测试上传
#新建一个文件
touch /usr/local/soft/package/a.txt
#使用FastDFS上传命令,将测试文件上传到FastDFS存储里
/usr/bin/fdfs_upload_file /etc/fdfs/client.conf /usr/local/soft/package/a.txt
##如能返回以下文件ID号,说明上传成功
group1/M00/00/00/wKhQB2B2giWABJqWAAAAAAAAAAA166.txt
#删除
/usr/bin/fdfs_delete_file /etc/fdfs/client.conf group1/M00/00/00/wKhQB2B2giWABJqWAAAAAAAAAAA166.txt
3.6、安装fastdfs-nginx-module
此部分安装在各个存储节点上,需要通过nginx安装。
(1) 安装依赖包
yum install -y gcc gcc-c++ make automake autoconf libtool pcre pcre-devel zlib zlib-devel openssl openssl-devel
(2) 安装nginx和fastdfs-nginx-module
#下载安装包
cd /usr/local/soft/package
wget https://github.com/happyfish100/fastdfs-nginx-module/archive/V1.22.tar.gz
wget http://nginx.org/download/nginx-1.18.0.tar.gz
#解压
tar -xf V1.22.tar.gz
tar -xf nginx-1.18.0.tar.gz
#新建nginx用户
useradd -r -s /sbin/nologin nginx
#将fastdfs-nginx-module拷贝至nginx目录
mkdir /usr/local/soft/package/nginx-1.18.0/add_module
cp -r fastdfs-nginx-module-1.22 nginx-1.18.0/add_module/fastdfs-nginx-module
#编译nginx
cd nginx-1.18.0
./configure \
--user=nginx \
--group=nginx \
--prefix=/usr/local/soft/nginx \
--with-http_ssl_module \
--with-http_stub_status_module \
--with-stream \
--add-module=/usr/local/soft/package/nginx-1.18.0/add_module/fastdfs-nginx-module/src/
#安装nginx
make && make install
#复制fastdfs-nginx-module源码中的配置文件到/etc/fdfs目录,并修改
cp /usr/local/soft/package/nginx-1.18.0/add_module/fastdfs-nginx-module/src/mod_fastdfs.conf /etc/fdfs
vim /etc/fdfs/mod_fastdfs.conf
##注意修改以下内容
tracker_server=192.168.80.7:22122 //tracker 服务器的 IP 地址和端口
tracker_server=192.168.80.17:22122 //多个 tracker 直接添加多条配置
tracker_server=192.168.80.27:22122 //多个 tracker 直接添加多条配置
group_name=group1 //组名(按主机规划里的修改各台主机的配置)
url_have_group_name = true
store_path0=/usr/local/soft/fastdfs/storage
group_count = 3
[group1]
group_name=group1
storage_server_port=23000
store_path_count=1
store_path0=/usr/local/soft/fastdfs/storage
[group2]
group_name=group2
storage_server_port=23000
store_path_count=1
store_path0=/usr/local/soft/fastdfs/storage
[group3]
group_name=group3
storage_server_port=23000
store_path_count=1
store_path0=/usr/local/soft/fastdfs/storage
#复制FastDFS的部分配置文件到/etc/fdfs目录
cp /usr/local/soft/package/fastdfs-6.06/conf/http.conf /etc/fdfs
cp /usr/local/soft/package/fastdfs-6.06/conf/mime.types /etc/fdfs
(3) 修改nginx配置
cd /usr/local/soft/nginx/conf
vim nginx.conf
#可参考以下配置
user nginx;
worker_processes 2;
error_log /usr/local/soft/nginx/logs/error.log;
pid /usr/local/soft/nginx/logs/nginx.pid;
worker_rlimit_nofile 65535;
events
{
use epoll;
multi_accept on;
worker_connections 65530;
}
http {
include mime.types;
default_type application/octet-stream;
log_format main
'remote_addr:"$remote_addr"|'
'remote_user:"$remote_user"|'
'time_local:[$time_local]|'
'request:[$request]|'
'status:"$status"|'
'body_bytes_sent:"$body_bytes_sent"|'
'http_referer:"$http_referer"|'
'http_user_agent:"$http_user_agent"|'
'request_time:"$request_time"|'
'request_body:"$request_body"|'
'http_host:"$http_host"|'
'request_length:"$request_length"|'
'upstream_status:"$upstream_status"|'
'upstream_addr:"$upstream_addr"|'
'upstream_response_time:"$upstream_response_time"'
;
map $time_iso8601 $logdate {
'~^(?<ymd>\d{4}-\d{2}-\d{2})' $ymd;
default 'date-not-found';
}
access_log logs/access-$logdate.log main;
charset utf-8;
server_tokens off;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 40s;
client_header_timeout 10;
client_body_timeout 10;
client_header_buffer_size 4k;
large_client_header_buffers 4 32k;
server_names_hash_bucket_size 128;
client_max_body_size 100m;
send_timeout 10;
reset_timedout_connection on;
open_file_cache max=102400 inactive=20s;
open_file_cache_min_uses 1;
open_file_cache_valid 30s;
open_log_file_cache max=10;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 128k;
fastcgi_buffers 256 128k;
fastcgi_busy_buffers_size 256k;
fastcgi_temp_file_write_size 256k;
fastcgi_intercept_errors on;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffering on;
proxy_buffer_size 128k;
proxy_buffers 256 128k;
proxy_busy_buffers_size 256k;
proxy_temp_file_write_size 256k;
gzip on;
gzip_min_length 256;
gzip_buffers 4 16k;
gzip_http_version 1.1;
gzip_vary on;
gzip_comp_level 5;
gzip_disable "MSIE [1-6]\.";
gzip_proxied any;
gzip_types
application/atom+xml
application/javascript
application/json
application/ld+json
application/manifest+json
application/rss+xml
application/vnd.geo+json
application/vnd.ms-fontobject
application/x-font-ttf
application/x-web-app-manifest+json
application/xhtml+xml
application/xml
font/opentype
image/bmp
image/svg+xml
image/x-icon
text/cache-manifest
text/css
text/plain
text/vcard
text/vnd.rim.location.xloc
text/vtt
text/x-component
text/x-cross-domain-policy;
######################################
#fastdfs
server {
listen 8088; ## 该端口为storage.conf中的http.server_port相同
server_name localhost;
location ~/group[0-9]/ {
ngx_fastdfs_module;
}
access_log /usr/local/soft/nginx/logs/storge.log;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
(4) 启动nginx
#启动
/usr/local/soft/nginx/sbin/nginx
#停止
/usr/local/soft/nginx/sbin/nginx -s stop
#重载
/usr/local/soft/nginx/sbin/nginx -s reload
(5) 测试
#上传一张照片至服务器,如a.png
#使用FastDFS上传命令,将图片上传到FastDFS存储里
/usr/bin/fdfs_upload_file /etc/fdfs/client.conf /usr/local/soft/package/a.png
##如能返回以下文件ID号,说明上传成功
group1/M00/00/00/wKhQB2B2nayAdY7zAAAUBwdbV-o889.png
#然后通过浏览器访问图片url
http://192.168.80.7:8088/group1/M00/00/00/wKhQB2B2nayAdY7zAAAUBwdbV-o889.png
3.7、安装Nginx负载均衡
此部分安装在各个跟踪节点上,即tracker节点上,主要为了提供http访问的反向代理、负载均衡以及缓存服务,如果跟踪节点与存储节点安装在一台服务器上,刚在3.6
的基础上配置nginx即可。
以下为跟踪节点单独配置时的步骤:
(1) 安装依赖包
yum install -y gcc gcc-c++ make automake autoconf libtool pcre pcre-devel zlib zlib-devel openssl openssl-devel
(2) 安装nginx
#下载安装包
cd /usr/local/soft/package
wget http://nginx.org/download/nginx-1.18.0.tar.gz
#解压
tar -xf nginx-1.18.0.tar.gz
#新建nginx用户
useradd -r -s /sbin/nologin nginx
#编译nginx
./configure \
--user=nginx \
--group=nginx \
--prefix=/usr/local/soft/nginx \
--with-http_ssl_module \
--with-http_stub_status_module \
--with-stream
#安装nginx
make && make install
(3) 配置nginx
cd /usr/local/soft/nginx/conf
vim nginx.conf
#部分配置省略,以下为负载均衡配置,配置在http{}中
upstream fdfs_group1 {
server 192.168.10.202:8888 weight=1 max_fails=2 fail_timeout=30s;
}
upstream fdfs_group2 {
server 192.168.10.203:8888 weight=1 max_fails=2 fail_timeout=30s;
}
upstream fdfs_group3 {
server 192.168.10.204:8888 weight=1 max_fails=2 fail_timeout=30s;
}
server{
listen 8000;
server_name localhost;
access_log /usr/local/soft/nginx/logs/lb_access.log main;
error_log /usr/local/soft/nginx/logs/storge.log/lb_error.log;
location /group1/M00 {
proxy_pass http://fdfs_group1;
}
location /group2/M00 {
proxy_pass http://fdfs_group2;
}
location /group3/M00 {
proxy_pass http://fdfs_group3;
}
(4) 启动nginx
#启动
/usr/local/soft/nginx/sbin/nginx
#停止
/usr/local/soft/nginx/sbin/nginx -s stop
#重载
/usr/local/soft/nginx/sbin/nginx -s reload