前期准备:
三台服务器(192.168.187.129,192.168.187.130,192.168.187.131),129和130部署fastdfs服务(tracker,storage,nginx),131做nginx负载均衡
基于group模式的文件存储扩容
group模式扩容是通过给group的机器添加硬盘的方式,实现某个group的扩容。fastdfs在一台服务器支持多个store_path,每个store_path指向一个存储路径。添加了硬盘后,通过把新的store_path指向新硬盘的挂载点,再修改配置文件,就能实现group的扩容。group模式扩容主要步骤如下:
- 1.停止现有的tracker服务,storage服务,操作指令如:/etc/init.d/fdfs_trackerd stop ,/etc/init.d/fdfs_storaged stop ;
- 2.修改/etc/fdfs/storage.conf配置文件,修改字段store_path_count,新增store_path1文件存储路径,新建store_path1的文件目录;
一.FastDFS搭建工具下载
wget -c https://github.com/happyfish100/fastdfs/archive/V6.06.tar.gz
wget -c https://github.com/happyfish100/libfastcommon/archive/V1.0.43.tar.gz
wget -c https://github.com/happyfish100/fastdfs-nginx-module/archive/V1.22.tar.gz
wget -c http://nginx.org/download/nginx-1.18.0.tar.gz
二.安装编译工具及库文件
yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel pcre-devel
三.解压文件
tar -zxvf V1.0.43.tar.gz
tar -zxvf V6.06.tar.gz
tar -zxvf V1.22.tar.gz
tar -zxvf nginx-1.18.0.tar.gz
四.编译安装libfastcommon
1、进入解压后的目录
cd /home/libfastcommon-1.0.43
2、编译安装
./make.sh && ./make.sh install
3.检查(出现libfastcommon.so即成功)
ls /usr/lib|grep libfastcommon
五.编译安装fastdfs
1、进入解压后的目录
cd /home/fastdfs-6.06
2.编译安装
./make.sh && ./make.sh install
3.检查
ls /usr/bin|grep fdfs
4.配置FastDFS跟踪器(Tracker)
cd /etc/fdfs
cp tracker.conf.sample tracker.conf
vim tracker.conf
编辑tracker.conf 文件
# 提供服务的端口
port=22122
# Tracker 数据和日志目录地址(根目录必须存在,子目录会自动创建)
base_path=/home/fastdfs/tracker
# 要上载文件的存储服务器的哪个路径
# 0: 轮询
# 2: 负载平衡,选择上传文件的最大可用空间路径
# group模式扩容是通过给group的服务器挂载硬盘的方式实现扩容这块选择2
store_path: 2
创建tracker基础数据目录,即base_path对应的目录
mkdir -p /home/fastdfs/tracker
启动Tracker
service fdfs_trackerd start
查看 FastDFS Tracker 是否已成功启动 ,22122端口正在被监听,则算是Tracker服务安装成功。
netstat -unltp|grep fdfs
设置Tracker开机启动
chkconfig fdfs_trackerd on
关闭Tracker命令
service fdfs_trackerd stop
5.配置 FastDFS 存储 (Storage)
cd /etc/fdfs
cp storage.conf.sample storage.conf
vim storage.conf
编辑storage.conf文件
# storage server 服务端口
port=23000
# Storage 数据和日志目录地址
base_path = /home/fastdfs/storage
# 存储路径个数对应store_path0个数 默认为1
store_path_count = 2
# storage文件存储路径 当挂载一块新磁盘作为文件存储目录的时候配置这里实现基于group的文件存储扩容
store_path0 = /home/fastdfs/file1
store_path1 = /home/fastdfs/file2
# 有多个 tracker server 时,每个 tracker server 写一行
tracker_server = 192.168.187.129:22122
tracker_server = 192.168.187.130:22122
创建Storage基础数据目录(base_path,base_path0,base_path1路径)
mkdir -p /home/fastdfs/storage
mkdir -p /home/fastdfs/file1
mkdir -p /home/fastdfs/file2
启动 Storage
service fdfs_storaged start
查看 Storage 是否成功启动,23000 端口正在被监听,就算 Storage 启动成功。
netstat -unltp|grep fdfs
查看Storage和Tracker是否在通信(ACTIVE表示正常)
/usr/bin/fdfs_monitor /etc/fdfs/storage.conf
设置 Storage 开机启动
chkconfig fdfs_storaged on
关闭Storage命令
service fdfs_storaged stop
6.配置 Tracker 服务器中的客户端
cd /etc/fdfs
cp client.conf.sample client.conf
vim client.conf
编辑client.conf文件
# Client 的数据和日志目录
base_path=/home/fastdfs/client
# Tracker端口
tracker_server=192.168.187.129:22122
tracker_server=192.168.187.130:22122
创建client文件夹
mkdir -p /home/fastdfs/client
六.安装 nginx 和 fastdfs-nginx-module
1.进入nginx解压目录
cd /home/nginx-1.18.0
(1) 隐藏并替换nginx名称
vim src/core/nginx.h
#将其中的nginx改为httpdserver
#define NGINX_VERSION "1.18.0"
#define NGINX_VER "httpdserver/" NGINX_VERSION
(2) 修改http的ResponseHeader信息
vim src/http/ngx_http_header_filter_module.c
#将其中的nginx改为httpdserver
static u_char ngx_http_server_string[] = "Server: httpdserver" CRLF;
(3) 修改错误页的底部Footer
vim src/http/ngx_http_special_response.c
#将其中的nginx改为httpdserver
static u_char ngx_http_error_tail[] =
"<hr><center>httpdserver</center>" CRLF
"</body>" CRLF
"</html>" CRLF
2.配置nginx并添加 http_stub_status_module 模块
./configure --add-module=../fastdfs-nginx-module-1.22/src
3.编译、安装
make && make install
4.复制 fastdfs-nginx-module 源码中的配置文件到/etc/fdfs 目录, 并修改
cd /home/fastdfs-nginx-module-1.22/src
cp mod_fastdfs.conf /etc/fdfs/
cd /etc/fdfs
vim mod_fastdfs.conf
编辑mod_fastdfs.conf文件
# 连接超时时间
connect_timeout=10
# Tracker Server
tracker_server=192.168.187.129:22122
tracker_server=192.168.187.130:22122
# StorageServer 默认端口
storage_server_port=23000
# 如果文件ID的uri中包含/group**,则要设置为true
url_have_group_name = true
# 必须和storage.conf中的配置一致
store_path_count=2
# 必须和storage.conf中的配置一致
store_path0=/home/fastdfs/file1
store_path1=/home/fastdfs/file2
5.复制 FastDFS 的部分配置文件到/etc/fdfs 目录
cd /home/fastdfs-6.06/conf/
cp http.conf mime.types /etc/fdfs/
6.配置nginx,修改nginx.conf
vim /usr/local/nginx/conf/nginx.conf
添加fastdfs-nginx模块
location ~/group([0-9])/M0([0-9]) {
ngx_fastdfs_module;
}
7.启动nginx
/usr/local/nginx/sbin/nginx
8.设置开机启动
vim /etc/rc.local
添加一行:
/usr/local/nginx/sbin/nginx
设置执行权限
chmod 755 /etc/rc.local
注意:listen 80 端口值是要与 /etc/fdfs/storage.conf 中的 http.server_port=80 相对应。如果改成其它端口,则需要统一
七.上传测试
在linux内部执行如下命令上传图片
/usr/bin/fdfs_upload_file /etc/fdfs/client.conf 1.png
上传成功后返回文件ID号:group1/M00/00/00/rBQKI187TWGAGzxQAACjsz5SpwM855.png
八.nginx的负载均衡配置
http {
include mime.types;
default_type application/octet-stream;
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
upstream backserver{
server 192.168.187.130:80;
server 192.168.187.129:80;
}
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
location ~/group([0-9])/M0([0-9]) {
proxy_pass http://backserver;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}