FastDFS是一个国产开源的轻量级分布式文件系统,它对文件进行管理,功能包括:文件存储、文件同步、文件访问(文件上传、文件下载)等,解决了大容量存储和负载均衡的问题。特别适合以文件为载体的在线服务,如相册网站、视频网站等等。FastDFS服务端有两个角色:跟踪器(tracker)和存储节点(storage)。跟踪器主要做调度工作,在访问上起负载均衡的作用。
github软件仓库
- libfastcommon — FastDFS和FastDHT的通用函数库,地址:https://github.com/happyfish100/libfastcommon.git
- FastDFS — FastDFS主代码,地址: https://github.com/happyfish100/fastdfs.git
- fastdfs-nginx-module — FastDFS的nginx模块,地址:https://github.com/happyfish100/fastdfs-nginx-module.git
系统约定
操作系统:CentOS 6.x
PHP客户端1:10.99.0.8
PHP客户端2:10.99.0.9
Tracker1:10.99.0.10
Tracker2:10.99.0.11
Group1-storage1:10.99.0.12
Group1-storage2:10.99.0.13
Group2-storage1:10.99.0.14
Group2-storage2:10.99.0.15
配置文件目录: /etc/fdfs/
数据和日志目录:/data/fastdfs
安装FastDFS
下载源码
# cd /usr/local/src # git clone https://github.com/happyfish100/libfastcommon.git # git clone https://github.com/happyfish100/fastdfs.git # git clone https://github.com/happyfish100/fastdfs-nginx-module.git
安装gcc、make
# yum -y install gcc gcc-c++ make
安装FastDFS
在每一台php客户端、tracker和storage服务器上执行
# cd libfastcommon # ./make.sh # ./make.sh install # cd ../fastdfs # ./make.sh # ./make.sh install
FastDFS配置
配置Tracker两个跟踪节点
防火墙打开22122端口
# mkdir -p /data/fastdfs # cd /etc/fdfs # cp tracker.conf.sample tracker.conf # cp /usr/local/src/fastdfs/conf/http.conf ./ # cp /usr/local/src/fastdfs/conf/mime.types ./ # sed -i 's:base_path=.*:base_path=/data/fastdfs:g' tracker.conf # sed -i 's:http.server_port=.*:http.server_port=80:g' tracker.conf
启动tracker节点
# service fdfs_trackerd start
开机启动
# chkconfig fdfs_trackerd on
确认tracker是否启动成功
# cat /data/fastdfs/logs/trackerd.log
配置Storage 1两个存储节点
防火墙打开23000端口
# mkdir -p /data/fastdfs # cd /etc/fsfs # cp storage.conf.sample storage.conf # cp /usr/local/src/fastdfs/conf/http.conf ./ # cp /usr/local/src/fastdfs/conf/mime.types ./ # sed -i 's:base_path=.*:base_path=/data/fastdfs:g' storage.conf # sed -i 's:store_path0=.*:store_path0=/data/fastdfs:g' storage.conf # sed -i 's/tracker_server=.*/tracker_server=10.99.0.10:22122/g' storage.conf # sed -i '/tracker_server=.*/a\tracker_server=10.99.0.11:22122' storage.conf # sed -i 's:http.server_port=.*:http.server_port=80:g' storage.conf
启动storage节点
# service fdfs_storaged start
开机启动
# chkconfig fdfs_storaged on
查看服务器状态
# fdfs_monitor /etc/fdfs/storage.conf
确认storage是否启动成功
# cat /data/fastdfs/logs/storaged.log
配置Storage 2两个存储节点
防火墙打开23000端口
# mkdir -p /data/fastdfs # cd /etc/fsfs # cp storage.conf.sample storage.conf # cp /usr/local/src/fastdfs/conf/http.conf ./ # cp /usr/local/src/fastdfs/conf/mime.types ./ # sed -i 's:group_name=.*:group_name=group2:g' storage.conf # sed -i 's:base_path=.*:base_path=/data/fastdfs:g' storage.conf # sed -i 's:base_path=.*:base_path=/data/fastdfs:g' storage.conf # sed -i 's:store_path0=.*:store_path0=/data/fastdfs:g' storage.conf # sed -i 's/tracker_server=.*/tracker_server=10.99.0.10:22122/g' storage.conf # sed -i '/tracker_server=.*/a\tracker_server=10.99.0.11:22122' storage.conf # sed -i 's:http.server_port=.*:http.server_port=80:g' storage.conf
启动storage节点
# service fdfs_storaged start
开机启动
# chkconfig fdfs_storaged on
查看服务器状态
# fdfs_monitor /etc/fdfs/storage.conf
确认storage是否启动成功
# cat /data/fastdfs/logs/storaged.log
client客户端配置
在两个tracker节点配置
# cd /etc/fdfs # cp client.conf.sample client.conf # sed -i 's:base_path=.*:base_path=/data/fastdfs:g' client.conf # sed -i 's/tracker_server=.*/tracker_server=10.99.0.10:22122/g' client.conf # sed -i '/tracker_server=.*/a\tracker_server=10.0.99.0.11:22122' client.conf
查看服务器状态
# fdfs_monitor /etc/fdfs/client.conf
FastDFS测试
上传测试:
# fdfs_upload_file /etc/fdfs/client.conf favicon.ico
返回
group1/M00/00/00/CmMADFXZx3WAYv_iAAALLjT76zs144.ico
查看文件信息:
# fdfs_file_info /etc/fdfs/client.conf group1/M00/00/00/CmMADFXZx3WAYv_iAAALLjT76zs144.ico source storage id: 0 source ip address: 10.99.0.12 file create timestamp: 2015-08-23 13:15:33 file size: 2862 file crc32: 888924987 (0x34FBEB3B)
下载测试:
# fdfs_download_file /etc/fdfs/client.conf group1/M00/00/00/CmMADFXZx3WAYv_iAAALLjT76zs144.ico favicon.ico
删除测试:
# fdfs_delete_file /etc/fdfs/client.conf group1/M00/00/00/CmMADFXZx3WAYv_iAAALLjT76zs144.ico
FastDFS客户端PHP扩展安装
在两台php客户端服务器先编译安装好php 5.4.43
安装fastdfs php扩展
# cd /usr/local/src/fastdfs/php_client # /usr/local/php/bin/phpize # ./configure --with-php-config=/usr/local/php/bin/php-config # make # make install # cp /etc/fdfs/client.conf.sample /etc/fdfs/client.conf # cat fastdfs_client.ini >> /usr/local/php/etc/php.ini # sed -i 's:base_path=.*:base_path=/data/fastdfs:g' client.conf # sed -i 's/tracker_server=.*/tracker_server=10.99.0.10:22122/g' client.conf # sed -i '/tracker_server=.*/a\tracker_server=10.0.99.0.11:22122' client.conf
测试php客户端
新建test.php文件,内容保存如下
# php test.php bool(true) array(2) { ["group_name"]=> string(6) "group1" ["filename"]=> string(44) "M00/00/00/CmMADVXf-V6AK2RSAAALLjT76zs889.ico" }
fastdfs-nginx-module安装配置
fastdfs-nginx-module用于解决同步延迟问题:
同组之间的服务器复制文件存在延迟问题。假设Tracker服务器将文件上传到了10.99.0.13,文件ID已经返回客户端,这时后台会将这个文件复制到10.99.0.14,如果复制没有完成,客户端就用这个ID在10.99.0.14取文件,肯定会出现404错误,这个nginx扩展模块fastdfs-nginx-module可以重定向连接到源服务器取文件,避免客户端由于复制延迟的问题出现404错误。fastdfs-nginx-module模块只需要安装到storage上。
在四个存储节点安装
编译安装nginx请参考https://www.preple.com/how-to-setup-and-configure-nginx-on-centos/
编译参数增加fastdfs-nginx-module,如下是我的编译参数
# ./configure --prefix=/usr/local/nginx \ --user=nginx --group=nginx \ --with-http_ssl_module \ --with-http_spdy_module \ --with-http_realip_module \ --with-http_addition_module \ --with-http_gzip_static_module \ --with-http_stub_status_module \ --with-ipv6 \ --with-pcre \ --add-module=/usr/local/src/fastdfs-nginx-module/src # make # make install
fastdfs模块配置
# cp /usr/local/src/fastdfs-nginx-module/src/mod_fastdfs.conf /etc/fdfs/ # touch /data/fastdfs/logs/mod_fastdfs.log # chown nginx:nginx /data/fastdfs/logs/mod_fastdfs.log # vim /etc/fdfs/mod_fastdfs.conf
group1组两个storage节点修改如下:
base_path=/data/fastdfs tracker_server=10.99.0.11:22122 tracker_server=10.99.0.12:22122 url_have_group_name = true store_path0=/data/fastdfs log_filename=/data/fastdfs/logs/mod_fastdfs.log
group2组两个storage节点修改如下:
base_path=/data/fastdfs tracker_server=10.99.0.11:22122 tracker_server=10.99.0.12:22122 group_name=group2 url_have_group_name = true store_path0=/data/fastdfs log_filename=/data/fastdfs/logs/mod_fastdfs.log
Storage服务器nginx配置
增加group规则
location ~ /group[1-2]/M00 { alias /data/fastdfs/data ngx_fastdfs_module; }
启动nginx
# service nginx start