前言
作者也是刚刚接触这个FastDFS文件系统,为什么我们需要一个分布式的文件系统,随着我们的网站资源的不断扩大,用户、后台上传的图片、视频、文件往往是一台服务器储存不了的,就算能储存,也会产生IO瓶颈,给用户的体验带来不便,也不利于后期的业务扩展。所以我们就必须使用分布式的文件构架。这样我们的业务不断扩大 我们只需要不断的去添加储存服务器。我们也不用担心某台服务器宕机,因为FastDFS 同一分组自带备份。
术语
FastDFS两个主要的角色:Tracker Server 和 Storage Server
Tracker Server:跟踪服务器,主要负责调度storage节点与client通信,在访问上起负载均衡的作用,和记录storage节点的运行状态,是连接client和storage节点的枢纽。
Storage Server:存储服务器,保存文件和文件的meta data(元数据)
Group:文件组,也可以称为卷。同组内服务器上的文件是完全相同的,做集群时往往一个组会有多台服务器,上传一个文件到同组内的一台机器上后,FastDFS会将该文件即时同步到同组内的其它所有机器上,起到备份的作用。
meta data:文件相关属性,键值对(Key Value Pair)方式。
正式安装准备工作
1.准备两台以上的Centos6.x服务器,作者这里是使用的虚拟机环境。
跟踪服务器(Tracker Server):192.168.181.131
存储服务器(Storage Server):192.168.181.132
FastDFS安装文件:https://github.com/happyfish100
Nginx:从这里获取Nginx及教程
在所有的服务器下安装以下依赖
yum install make cmake gcc gcc-c++
安装libfatscommon
shell> cd /usr/local/src
shell> unzip libfastcommon-master.zip
shell> cd libfastcommon-master
shell> ll
-rw-r--r--. 1 root root 6670 10月 8 09:39 HISTORY
-rw-r--r--. 1 root root 566 10月 8 09:39 INSTALL
-rw-r--r--. 1 root root 1438 10月 8 09:39 libfastcommon.spec
-rwxr-xr-x. 1 root root 3099 10月 8 09:39 make.sh
drwxr-xr-x. 2 root root 4096 10月 8 09:39 php-fastcommon
-rw-r--r--. 1 root root 812 10月 8 09:39 README
drwxr-xr-x. 3 root root 4096 10月 8 09:39 src
## 编译、安装
shell> ./make.sh
shell> ./make.sh install
安装FastDFS
shell> cd /usr/local/src
shell> unzip fastdfs-master-V5.05.zip
shell> cd fastdfs-master
shell> ll
[root@localhost fastdfs-master]# ll
drwxr-xr-x. 3 root root 4096 8月 8 15:17 client
drwxr-xr-x. 2 root root 4096 8月 8 15:17 common
drwxr-xr-x. 2 root root 4096 8月 8 15:17 conf
-rw-r--r--. 1 root root 35067 8月 8 15:17 COPYING-3_0.txt
-rw-r--r--. 1 root root 2881 8月 8 15:17 fastdfs.spec
-rw-r--r--. 1 root root 32259 8月 8 15:17 HISTORY
drwxr-xr-x. 2 root root 46 8月 8 15:17 init.d
-rw-r--r--. 1 root root 7755 8月 8 15:17 INSTALL
-rwxr-xr-x. 1 root root 5548 8月 8 15:17 make.sh
drwxr-xr-x. 2 root root 4096 8月 8 15:17 php_client
-rw-r--r--. 1 root root 2380 8月 8 15:17 README.md
-rwxr-xr-x. 1 root root 1768 8月 8 15:17 restart.sh
-rwxr-xr-x. 1 root root 1680 8月 8 15:17 stop.sh
drwxr-xr-x. 4 root root 4096 8月 8 15:17 storage
drwxr-xr-x. 2 root root 4096 8月 8 15:17 test
drwxr-xr-x. 2 root root 4096 8月 8 15:17 tracker
## 编译、安装
shell> ./make.sh
shell> ./make.sh install
安装完毕查看服务脚本
/etc/init.d/fdfs_storaged
/etc/init.d/fdfs_trackerd
配置文件
/etc/fdfs
shell> ll /etc/fdfs/
总用量 20
-rw-r–r--. 1 root root 1461 10月 17 15:15 client.conf.sample
-rw-r–r--. 1 root root 7927 10月 17 15:15 storage.conf.sample
-rw-r–r--. 1 root root 7200 10月 17 15:15 tracker.conf.sample
命令行
shell> ll /usr/bin/ | grep fdfs
-rwxr-xr-x. 1 root root 315391 10月 17 15:15 fdfs_appender_test
-rwxr-xr-x. 1 root root 315168 10月 17 15:15 fdfs_appender_test1
-rwxr-xr-x. 1 root root 302016 10月 17 15:15 fdfs_append_file
-rwxr-xr-x. 1 root root 301692 10月 17 15:15 fdfs_crc32
-rwxr-xr-x. 1 root root 302075 10月 17 15:15 fdfs_delete_file
-rwxr-xr-x. 1 root root 302810 10月 17 15:15 fdfs_download_file
-rwxr-xr-x. 1 root root 302400 10月 17 15:15 fdfs_file_info
-rwxr-xr-x. 1 root root 316229 10月 17 15:15 fdfs_monitor
-rwxr-xr-x. 1 root root 1101974 10月 17 15:15 fdfs_storaged
-rwxr-xr-x. 1 root root 325327 10月 17 15:15 fdfs_test
-rwxr-xr-x. 1 root root 320448 10月 17 15:15 fdfs_test1
-rwxr-xr-x. 1 root root 447878 10月 17 15:15 fdfs_trackerd
-rwxr-xr-x. 1 root root 303002 10月 17 15:15 fdfs_upload_appender
-rwxr-xr-x. 1 root root 304022 10月 17 15:15 fdfs_upload_file
配置tracker(跟踪)服务器
复制配置文件
shell> cp /etc/fdfs/tracker.conf.sample /etc/fdfs/tracker.conf
修改配置文件
shell> vim /etc/fdfs/tracker.conf
# 修改的内容如下:
disabled=false # 启用配置文件
port=22122 # tracker服务器端口(默认22122)
base_path=/fastdfs/tracker # 存储日志和数据的根目录
创建工作目录
shell> mkdir -p /fastdfs/tracker
配置防火墙
shell> vi /etc/sysconfig/iptables
-A INPUT -m state --state NEW -m tcp -p tcp --dport 22122 -j ACCEPT
shell> service iptables restart
各种命令
1.启动 shell> /etc/init.d/fdfs_trackerd start
2.检查 ps -ef | grep fdfs_trackerd
3.停止 shell> /etc/init.d/fdfs_trackerd stop
4.自启 shell> chkconfig fdfs_trakcerd on
配置storage(储存)服务器
###复制配置文件
cp /etc/fdfs/storage.conf.sample /etc/fdfs/storage.conf
编辑配置文件
shell> vi /etc/fdfs/storage.conf
# 修改的内容如下:
disabled=false # 启用配置文件
port=23000 # storage服务端口
base_path=/fastdfs/storage # 数据和日志文件存储根目录
store_path0=/fastdfs/storage # 第一个存储目录
tracker_server=192.168.181.131:22122 # tracker服务器IP和端口
http.server_port=8888 # http访问文件的端口
创建工作目录
shell> mkdir -p /fastdfs/storage
开放防火墙
这里我就不多说了。开放端口为23000
各种命令
1.启动 shell> /etc/init.d/fdfs_storaged start
2.检查 ps -ef | grep fdfs_storaged
3停止 shell> /etc/init.d/fdfs_storaged stop
4.自启 shell> chkconfig fdfs_storaged on
上传测试
修改配置文件
shell> cp /etc/fdfs/client.conf.sample /etc/fdfs/client.conf
shell> vim /etc/fdfs/client.conf
# 修改以下配置,其它保持默认
base_path=/fastdfs/tracker
tracker_server=192.168.181.131:22122
上传一张图片
wget https://www.baidu.com/img/bd_logo1.png
shell> /usr/bin/fdfs_upload_file /etc/fdfs/client.conf /usr/local/bd_logo1.png
返回文件ID号:group1/M00/00/00/wKgAyVgEjSGANYtkAAB7mZt7HdQ08538.png
安装fastdfs-nginx-module
为什么要安装
FastDFS 通过 Tracker 服务器,将文件放在 Storage 服务器存储,但是同组存储服务器之间需要进入文件复制,有同步延迟的问题。假Tracker 服务器将文件上传到了 192.168.181.131,上传成功后文件ID已经返回给客户端。此时 FastDFS 存储集群机制会将这个文件同步到同组存储 192.168.181.132,在文件还没有复制完成的情况下,客户端如果用这个文件 ID 在 192.168.181.132 上取文件,就会出现文件无法访问的错误。而 fastdfs-nginx-module 可以重定向文件连接到源服务器取文件,避免客户端由于复制延迟导致的文件无法访问错误
解压
shell> cd /usr/local/src
shell> unzip fastdfs-nginx-module-master.zip
安装nginx
安装及下载在文章准备工作里我已经写明了安装方法了。这里我就多说一句。加上add-module (将fastdfs-nginx-module-master录制加载后面).路径以你的实际路径为准。
./configure --prefix=/opt/nginx --sbin-path=/usr/bin/nginx --add-module=/usr/local/src/fastdfs-nginx-module/src
复制mod_fastdfs.conf
shell> cp /usr/local/src/fastdfs-nginx-module/src/mod_fastdfs.conf /etc/fdfs/
shell> vi /etc/fdfs/mod_fastdfs.conf
修改mod_nginx.conf
connect_timeout=10 # 客户端访问文件连接超时时长(单位:秒)
base_path=/tmp # 临时目录
tracker_server=192.168.181.131:22122 # tracker服务IP和端口
storage_server_port=23000 # storage服务端口
group_name=group1 # 组名
url_have_group_name=true # 访问链接前缀加上组名
store_path0=/fastdfs/storage # 文件存储路径
复制 FastDFS 的部分配置文件到/etc/fdfs 目录
shell> cd /usr/local/src/fastdfs-master/conf
shell> cp http.conf mime.types /etc/fdfs/
在/fastdfs/storage文件存储目录下创建软连接,将其链接到实际存放数据的目录
shell> ln -s /fastdfs/storage/data/ /fastdfs/storage/data/M00
配置nginx
user nobody;
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 8888;
server_name localhost;
location ~/group([0-9])/M00 {
ngx_fastdfs_module;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
访问文件
http://192.168.181.132:8888/group1/M00/00/00/wKgAyVgEjSGANYtkAAB7mZt7HdQ08538.png
问题补充
在使用过程中发现。我们在启动 重启 关闭的时候没有加载配置文件。
启动FastDFS
tracker: /usr/bin/fdfs_trackered /etc/fdfs/tracker.conf
storage: /usr/bin/fdfs_storaged /etc/fdfs/storage.conf
关闭FastDFS
tracker:/usr/bin/stop.sh /usr/bin/fdfs_tracker
storage:/usr/bin/stop.sh /usr/bin/fdfs_storage
或者 killall fdfs_trackered(storaged) -------注意,千万不要使用-9强行杀死进程。
重启FastDFS
tracker:/usr/bin/restart.sh /usr/bin/fdfs_trackered
storage:/usr/bin/restart.sh /usr/bin/fdfs_storaged
查看集群情况
在任意一台storage(tracker也可以) /usr/bin/fdfs_monitor /etc/fdfs/storage.conf
操作集群
储存服务器:
/etc/init.d/fdfs_storaged start
/etc/init.d/fdfs_storaged stop
跟踪服务器:
/etc/init.d/fdfs_trackerd start
/etc/init.d/fdfs_trackerd stop
监控:(任意一台服务器)
/usr/bin/fdfs_monitor /etc/fdfs/storage.conf