FastDFS_v5.05+nginx+cache集群安装配置手册

转载请出自出处:http://www.cnblogs.com/hd3013779515/

1.FastDFS简单介绍

FastDFS是由淘宝的余庆先生所开发,是一个轻量级、高性能的开源分布式文件系统,用纯C语言开发,包括文件存储、文件同步、文件访问(上传、下载)、存取负载均衡、在线扩容、相同内容只存储一份等功能,适合有大容量存储需求的应用或系统。做分布式系统开发时,其中要解决的一个问题就是图片、音视频、文件共享的问题,分布式文件系统正好可以解决这个需求。同类的分布式文件系统有谷歌的GFS、HDFS(Hadoop)、TFS(淘宝)等。

源码开放下载地址:https://github.com/happyfish100
早期源码开放下载地址:https://sourceforge.net/projects/fastdfs/files/
官网论坛:http://bbs.chinaunix.net/forum-240-1.html

FastDFS系统架构

001

FastDFS文件上传流程:

002

1、client询问tracker上传到的storage,不需要附加参数;
2、tracker返回一台可用的storage;
3、client直接和storage通讯完成文件上传。

FastDFS文件下载流程:

003

1、client询问tracker下载文件的storage,参数为文件标识(组名和文件名);
2、tracker返回一台可用的storage;
3、client直接和storage通讯完成文件下载。

术语

FastDFS两个主要的角色:Tracker Server 和 Storage Server
Tracker Server跟踪服务器,主要负责调度storage节点与client通信,在访问上起负载均衡的作用,和记录storage节点的运行状态,是连接client和storage节点的枢纽。
Storage Server存储服务器,保存文件和文件的meta data(元数据)
Group文件组,也可以称为卷。同组内服务器上的文件是完全相同的,做集群时往往一个组会有多台服务器,上传一个文件到同组内的一台机器上后,FastDFS会将该文件即时同步到同组内的其它所有机器上,起到备份的作用。
meta data文件相关属性,键值对(Key Value Pair)方式,如:width=1024, height=768。和阿里云OSS的meta data相似。

2.FastDFS集群环境安装

2.1集群规划

跟踪服务器负载均衡节点1:192.168.137.160 dfs-nginx-proxy-1
跟踪服务器负载均衡节点2:192.168.137.161 dfs-nginx-proxy-2
跟踪服务器1:192.168.137.162 dfs-tracker-1
跟踪服务器2:192.168.137.163 dfs-tracker-2
存储服务器1:192.168.137.164 dfs-storage-group1-1
存储服务器2:192.168.137.165 dfs-storage-group1-2
存储服务器3:192.168.137.166 dfs-storage-group2-1
存储服务器4:192.168.137.167 dfs-storage-group2-2
HA虚拟IP:192.168.137.170
HA软件:Keepalived
操作系统:CentOS 6.8
用户:root
数据目录:/fastdfs

2.2安装包

FastDFS_v5.05.tar.gz:FastDFS源码
libfastcommon-master.zip:(从 FastDFS 和 FastDHT 中提取出来的公共 C 函数库)
fastdfs-nginx-module_v1.16.tar.gz:storage节点http服务nginx模块
nginx-1.6.2.tar.gz:Nginx安装包
ngx_cache_purge-2.3.tar.gz:Nginx图片缓存清除模块
keepalived-1.2.18.tar.gz:高可用
 
可以从百度网盘下载。
链接:http://pan.baidu.com/s/1jHIwtsq 密码:n757

2.3 FastDFS集群架构图

004

外部统一访问192.168.137.170这个虚拟IP,来访问集群中各节点的文件。

2.4安装集群节点

所有跟踪服务器和存储服务器节点上执行以下操作,即:162 ~ 167节点

1> 安装所需的依赖包
shell> yum install make cmake gcc gcc-c++
2> 安装libfatscommon
shell> cd /home/test
shell> unzip libfastcommon-master.zip -d /usr/local/fast/
shell> cd /usr/local/fast/libfastcommon-master/
## 编译、安装
shell> ./make.sh
shell> ./make.sh install
 
FastDFS主程序设置的目录为/usr/local/lib/,所以需要创建/usr/lib64下的一些核心执行程序的软连接文件。
shell> mkdir /usr/local/lib/
shell> ln -s /usr/lib64/libfastcommon.so /usr/local/lib/libfastcommon.so
shell> ln -s /usr/lib64/libfastcommon.so /usr/lib/libfastcommon.so
shell> ln -s /usr/lib64/libfdfsclient.so /usr/local/lib/libfdfsclient.so
shell> ln -s /usr/lib64/libfdfsclient.so /usr/lib/libfdfsclient.so
 
3> 安装FastDFS
shell> cd /home/test
shell> tar -zxvf FastDFS_v5.05.tar.gz -C /usr/local/fast
shell> cd /usr/local/fast/FastDFS/
## 编译、安装
shell> ./make.sh
shell> ./make.sh install
 
因为FastDFS服务脚本设置的bin目录为/usr/local/bin/下,但是实际安装在了/usr/bin/下面。所以需要修改配置文件的路径,也就是修改下面两个文件。
 
shell> vim /etc/init.d/fdfs_storaged
进行全局替换命令:%s+/usr/local/bin+/usr/bin
shell> vim /etc/init.d/fdfs_trackerd
进行全局替换命令:%s+/usr/local/bin+/usr/bin
 

 

2.5配置跟踪节点(192.168.137.162,192.168.137.163)

说明:每个节点执行相同的操作

1> 复制tracker样例配置文件,并重命名
shell> cp /etc/fdfs/tracker.conf.sample /etc/fdfs/tracker.conf
2> 修改tracker.conf配置文件
shell> vim /etc/fdfs/tracker.conf
# 修改的内容如下:
disabled=false              # 启用配置文件
port=22122                  # tracker服务器端口(默认22122)
base_path=/fastdfs/tracker  # 存储日志和数据的根目录

其它参数保留默认配置, 具体配置解释可参考官方文档说明:http://bbs.chinaunix.net/thread-1941456-1-1.html

3> 创建base_path指定的目录
shell> mkdir -p /fastdfs/tracker
4> 防火墙中打开tracker服务器端口( 默认为 22122)

shell> vi /etc/sysconfig/iptables
添加如下端口行:
-A INPUT -m state --state NEW -m tcp -p tcp --dport 22122 -j ACCEPT
重启防火墙:
shell> service iptables restart

5> 启动tracker服务器
shell> /etc/init.d/fdfs_trackerd start

初次启动,会在/fastdfs/tracker目录下生成logs、data两个目录:
clip_image010

检查FastDFS Tracker Server是否启动成功:ps -ef | grep fdfs_trackerd
clip_image012

6> 停止tracker服务器
shell> /etc/init.d/fdfs_trackerd stop
7> 设置tracker服务开机启动
shell> chkconfig fdfs_trackerd on

2.6配置存储节点

group1 192.168.137.164,192.168.137.165
group2 192.168.137.166,192.168.137.167

说明:每个节点执行相同的操作

1> 复制storage样例配置文件,并重命名
shell> cp /etc/fdfs/storage.conf.sample /etc/fdfs/storage.conf
2> 编辑配置文件
shell> vi /etc/fdfs/storage.conf
 
# 修改的内容如下:
disabled=false                      # 启用配置文件
port=23000                          # storage服务端口
group_name=group1                   # 组名(第一组为group1,第二组为group2,依次类推...)
base_path=/fastdfs/storage          # 数据和日志文件存储根目录
store_path0=/fastdfs/storage        # 第一个存储目录,第二个存储目录起名为:store_path1=xxx,其它存储目录名依次类推...
store_path_count=1                  # 存储路径个数,需要和store_path个数匹配
tracker_server=192.168.137.162:22122  # tracker服务器IP和端口
tracker_server=192.168.137.163:22122  # tracker服务器IP和端口
http.server_port=8888               # http访问文件的端口

其它参数保留默认配置, 具体配置解释可参考官方文档说明:http://bbs.chinaunix.net/thread-1941456-1-1.html

3> 创建基础数据目录
shell> mkdir -p /fastdfs/storage
4> 防火墙中打开storage服务器端口( 默认为 23000)

shell> vi /etc/sysconfig/iptables
添加如下端口行:
-A INPUT -m state --state NEW -m tcp -p tcp --dport 23000 -j ACCEPT
重启防火墙:
shell> service iptables restart

5> 启动storage服务器
shell> /etc/init.d/fdfs_storaged start

初次启动,会在/fastdfs/storage目录下生成logs、data两个目录。
005

检查FastDFS Tracker Server是否启动成功:
ps -ef | grep fdfs_storaged
clip_image016
各节点启动后,使用tail -f /fastdfs/storage/logs/storaged.log命令监听存储节点的日志,可以看到存储节点链接到跟踪服务器,并提示哪一个为leader跟踪服务器,同时也能看到同一组中其它节点加入进来的日志信息。

所有存储节点都启动之后,可以在任一存储节点上使用如下命令查看集群的状态信息:

shell> /usr/bin/fdfs_monitor /etc/fdfs/storage.conf
6> 停止storage服务器
shell> /etc/init.d/fdfs_storaged stop
7> 设置storage服务开机启动
shell> chkconfig fdfs_storaged on

2.7文件上传测试

1> 修改tracker服务器client.conf配置文件
shell> cp /etc/fdfs/client.conf.sample /etc/fdfs/client.conf
shell> vi /etc/fdfs/client.conf
base_path=/fastdfs/tracker
tracker_server=192.168.137.162:22122
tracker_server=192.168.137.163:22122
2> 执行文件上传命令
shell> /usr/bin/fdfs_upload_file /etc/fdfs/client.conf /home/test/FastDFS_v5.05.tar.gz

返回以下ID号,说明文件上传成功:
group1/M00/00/00/wKiJpFkS0ByATie5AAVFOL7FJU4.tar.gz
group2/M00/00/00/wKiJplkS0CyAJyzTAAVFOL7FJU4.tar.gz
(从返回的ID号中也可以看出,同一个文件分别存储在两个组内group1和group2,但也有可能在同一组中,具体策略是由FastDFS根据服务器的存储情况来分配的)

2.8存储节点安装Nginx和fastdfs-nginx-module模块

说明:每个节点执行相同的操作

1> fastdfs-nginx-module作用说明

FastDFS 通过 Tracker 服务器,将文件放在 Storage 服务器存储,但是同组存储服务器之间需要进入文件复制流程,有同步延迟的问题。假设 Tracker 服务器将文件上传到了 192.168.137.164,上传成功后文件 ID已经返回给客户端。此时 FastDFS 存储集群机制会将这个文件同步到同组存储 192.168.137.165,在文件还没有复制完成的情况下,客户端如果用这个文件 ID 在 192.168.137.165上取文件,就会出现文件无法访问的错误。而 fastdfs-nginx-module 可以重定向文件连接到源服务器(192.168.137.164)上取文件,避免客户端由于复制延迟导致的文件无法访问错误。

2> 安装nginx和fastdfs-nginx-module模块
安装fastdfs-nginx-module_v1.16.tar.gz
 
shell> cd /home/test
shell> tar -zxvf fastdfs-nginx-module_v1.16.tar.gz -C /usr/local/fast/
shell> cd /usr/local/fast/fastdfs-nginx-module/src/
shell> vim /usr/local/fast/fastdfs-nginx-module/src/config
 
CORE_INCS="$CORE_INCS /usr/local/include/fastdfs /usr/local/include/fastcommon/"
 
改为
 
CORE_INCS="$CORE_INCS /usr/include/fastdfs /usr/include/fastcommon/"
 
 
## 安装nginx所需的依赖包
shell> yum install gcc gcc-c++ make automake autoconf libtool pcre pcre-devel zlib zlib-devel openssl openssl-devel
## 编译安装nginx(添加fastdfs-nginx-module模块)
shell> cd /home/test
shell> tar -zxvf nginx-1.6.2.tar.gz -C /usr/local/
shell> cd /usr/local/nginx-1.6.2/
shell> ./configure --add-module=/usr/local/fast/fastdfs-nginx-module/src/
shell> make && make install
3> 复制 fastdfs-nginx-module 源码中的配置文件到/etc/fdfs 目录,并修改
shell> cp /usr/local/fast/fastdfs-nginx-module/src/mod_fastdfs.conf /etc/fdfs/
shell> vi /etc/fdfs/mod_fastdfs.conf
第一组存储服务器的mod_fastdfs.conf配置
connect_timeout=10
base_path=/tmp
tracker_server=192.168.137.162:22122
tracker_server=192.168.137.163:22122
storage_server_port=23000
group_name=group1                       # 第一组storage的组名
url_have_group_name=true
store_path0=/fastdfs/storage
group_count=2
[group1]
group_name=group1
storage_server_port=23000
store_path_count=1
store_path0=/fastdfs/storage
[group2]
group_name=group2
storage_server_port=23000
store_path_count=1
store_path0=/fastdfs/storage
第二组存储服务器的mod_fastdfs.conf配置

第二组的mod_fastdfs.confg配置与第一组的配置只有group_name不同:
group_name=group2

4> 复制FastDFS源文件目录中HTTP相关的配置文件到/etc/fdfs目录
shell> cd /usr/local/fast/FastDFS/conf
shell> cp http.conf mime.types /etc/fdfs/
5> 创建数据存放目录的软链接
shell> ln -s /fastdfs/storage/data/ /fastdfs/storage/data/M00
6> 配置fastdfs-nginx-module(Nginx简洁版样例)
shell> vi /usr/local/nginx/conf/nginx.conf
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;

        # FastDFS 文件访问配置(fastdfs-nginx-module模块)
        location ~/group([0-9])/M00 {
            ngx_fastdfs_module;
        }

        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
            root   html;
        }   
    }
}

注意:
A、8888 端口值要与/etc/fdfs/storage.conf 中的 http.server_port=8888 相对应,因为 http.server_port 默认为 8888,如果想改成 80,则要对应修改过来。
B、Storage 对应有多个 group 的情况下,访问路径带 group 名,如:http://xxxx/group1/M00/00/00/xxx, 对应的 Nginx 配置为:

location ~/group([0-9])/M00 {
    ngx_fastdfs_module;
}

C、如下载时如发现老报 404,将nginx.conf第一行user nobody;修改为user root;后重新启动。

7> 防火墙中打开Nginx的 8888 端口
shell> vi /etc/sysconfig/iptables
## 添加
-A INPUT -m state --state NEW -m tcp -p tcp --dport 8888 -j ACCEPT
## 重启防火墙
shell> service iptables restart
8> 启动Nginx
shell> /usr/local/nginx/sbin/nginx
ngx_http_fastdfs_set pid=xxx        # fastdfs-nginx-module进程ID

重启 Nginx 的命令为:/usr/local/nginx/sbin/nginx -s reload

设置Nginx开机启动:

shell> vi /etc/rc.local
# 加入
/usr/local/nginx/sbin/nginx
shell> chmod +x /etc/rc.local  
9> 通过浏览器访问测试时上传的文件

http://192.168.137.164:8888/group1/M00/00/00/wKiJpFkS0ByATie5AAVFOL7FJU4.tar.gz

http://192.168.137.166:8888/group2/M00/00/00/wKiJplkS0CyAJyzTAAVFOL7FJU4.tar.gz

2.9跟踪节点安装Nginx和ngx_cache_purge模块

说明:每个节点执行相同的操作

tracker节点:192.168.137.162,192.168.137.163

在 tracker 上安装的 nginx 主要为了提供 http 访问的反向代理、负载均衡以及缓存服务。

1> 安装Nginx所需的依赖包
shell> yum install gcc gcc-c++ make automake autoconf libtool pcre pcre-devel zlib zlib-devel openssl openssl-devel
2> 安装nginx和ngx_cache_purge模块
shell> cd /home/test
shell> tar -zxvf nginx-1.6.2.tar.gz -C /usr/local/
shell> tar -zxvf ngx_cache_purge-2.3.tar.gz -C /usr/local/fast/
shell> cd /usr/local/nginx-1.6.2
shell> ./configure --add-module=/usr/local/fast/ngx_cache_purge-2.3
shell> make && make install
3> 配置Nginx,设置tracker负载均衡以及缓存
shell> vi /usr/local/nginx/conf/nginx.conf
user  nobody;
worker_processes  1;
events {
    worker_connections  1024;
    use epoll;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';
    #access_log  logs/access.log  main;
    sendfile       on;
    tcp_nopush     on;
    keepalive_timeout  65;
    #gzip on;

    #设置缓存
    server_names_hash_bucket_size 128;
    client_header_buffer_size 32k;
    large_client_header_buffers 4 32k;
    client_max_body_size 300m;
    proxy_redirect off;
    proxy_set_header Host $http_host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_connect_timeout 90;
    proxy_send_timeout 90;
    proxy_read_timeout 90;
    proxy_buffer_size 16k;
    proxy_buffers 4 64k;
    proxy_busy_buffers_size 128k;
proxy_temp_file_write_size 128k; 
#设置缓存存储路径、存储方式、分配内存大小、磁盘最大空间、缓存期限 
proxy_cache_path /fastdfs/cache/nginx/proxy_cache levels=1:2 keys_zone=http-cache:500m max_size=10g inactive=30d;
    proxy_temp_path /fastdfs/cache/nginx/proxy_cache/tmp;

    #设置 group1 的服务器
    upstream fdfs_group1 {
         server 192.168.137.164:8888 weight=1 max_fails=2 fail_timeout=30s;
         server 192.168.137.165:8888 weight=1 max_fails=2 fail_timeout=30s;
    }

    #设置 group2 的服务器
    upstream fdfs_group2 {
         server 192.168.137.166:8888 weight=1 max_fails=2 fail_timeout=30s;
         server 192.168.137.167:8888 weight=1 max_fails=2 fail_timeout=30s;
    }

    server {
        listen       8000;
        server_name  localhost;
        #charset koi8-r;
        #access_log  logs/host.access.log  main;

        #设置 group 的负载均衡参数
        location /group1/M00 {
            proxy_next_upstream http_502 http_504 error timeout invalid_header;
            proxy_cache http-cache;
            proxy_cache_valid  200 304 12h;
            proxy_cache_key $uri$is_args$args;
            proxy_pass http://fdfs_group1;
            expires 30d;
        }

        location /group2/M00 {
            proxy_next_upstream http_502 http_504 error timeout invalid_header; proxy_cache http-cache;
            proxy_cache_valid 200 304 12h;
            proxy_cache_key $uri$is_args$args;
            proxy_pass http://fdfs_group2;
            expires 30d;
        }

        #设置清除缓存的访问权限
        location ~/purge(/.*) {
            allow 127.0.0.1;
            allow 192.168.1.0/24;
            deny all;
            proxy_cache_purge http-cache $1$is_args$args;
        }
        #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; 
        }
    } 
}

按以上 nginx 配置文件的要求,创建对应的缓存目录:

shell> mkdir -p /fastdfs/cache/nginx/proxy_cache
shell> mkdir -p /fastdfs/cache/nginx/proxy_cache/tmp
4> 防火墙打开Nginx 8000 端口
shell> vi /etc/sysconfig/iptables
## 添加如下配置
-A INPUT -m state --state NEW -m tcp -p tcp --dport 8000 -j ACCEPT 
shell> service iptables restart # 重新启动防火墙
5> 启动Nginx
shell> /usr/local/nginx/sbin/nginx

设置开机启动:

shell> vi /etc/rc.local
## 加入以下配置
/usr/local/nginx/sbin/nginx
shell> chmod +x /etc/rc.local  
6> 文件访问测试

前面直接通过访问Storage节点中的Nginx访问文件:
http://192.168.137.164:8888/group1/M00/00/00/wKiJpFkS0ByATie5AAVFOL7FJU4.tar.gz

http://192.168.137.166:8888/group2/M00/00/00/wKiJplkS0CyAJyzTAAVFOL7FJU4.tar.gz

现在可以通过Tracker中的Nginx来进行访问:
(1)、通过 Tracker1 中的 Nginx 来访问
http://192.168.137.162:8000/group1/M00/00/00/wKiJpFkS0ByATie5AAVFOL7FJU4.tar.gz

http://192.168.137.162:8000/group2/M00/00/00/wKiJplkS0CyAJyzTAAVFOL7FJU4.tar.gz

(2)、通过 Tracker2 中的 Nginx 来访问
http://192.168.137.163:8000/group1/M00/00/00/wKiJpFkS0ByATie5AAVFOL7FJU4.tar.gz

http://192.168.137.163:8000/group2/M00/00/00/wKiJplkS0CyAJyzTAAVFOL7FJU4.tar.gz

由上面的文件访问效果可以看到,每一个Tracker中的Nginx都单独对后端的Storage组做了负载均衡,但整套FastDFS集群,如果想对外提供统一的文件访问地址,还需要对两个Tracker中的Nginx进行HA集群

 

2.10配置Tracker服务器高可用、反向代理与负载均衡

使用Keepalived + Nginx组成的高可用负载均衡集群,做两个Tracker节点中Nginx的负载均衡。

1> 安装keepalived与Nginx

分别在192.168.137.160和192.168.137.161两个节点安装Keepalived与Nginx。
keepalived安装与配置:请参考本人Nginx相关blog
Nginx的安装与配置:请参考本人Nginx相关blog

2> 配置Keeyalived + Nginx高可用

请参考本人Nginx相关blog

注意:将VIPIP地址修改为192.168.137.170

3> 配置nginx对tracker节点的负载均衡

2个节点的Nginx配置相同,如下所示:

shell> vi /usr/local/nginx/conf/nginx.conf
user  root;
worker_processes  1;
events {
    worker_connections  1024;
    use epoll;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;

    ## FastDFS Tracker Proxy
    upstream fastdfs_tracker {
         server 192.168.137.162:8000 weight=1 max_fails=2 fail_timeout=30s;
         server 192.168.137.163:8000 weight=1 max_fails=2 fail_timeout=30s;
    }

    server {
        listen       80;
        server_name  localhost;
        location / {
            root html;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root html;
        }

        ## FastDFS Proxy
        location /dfs {
            root   html;
            index  index.html index.htm;
            proxy_pass  http://fastdfs_tracker/;
            proxy_set_header Host  $http_host;
            proxy_set_header Cookie $http_cookie;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;
            client_max_body_size  300m;
        }
    } 
}
4> 重启192.168.137.160 和 192.168.137.161 中的Nginx
shell> /usr/local/nginx/sbin/nginx -s reload
5> 通过虚拟IP访问文件测试

现在可以通过 Keepalived+Nginx 组成的高可用负载集群的 VIP(192.168.137.170)来访问 FastDFS 集群中的文件了:
http://192.168.137.170/dfs/group1/M00/00/00/wKiJpFkS0ByATie5AAVFOL7FJU4.tar.gz

http://192.168.137.170/dfs/group2/M00/00/00/wKiJplkS0CyAJyzTAAVFOL7FJU4.tar.gz

注意:千万不要使用 kill -9 命令强杀 FastDFS 进程,否则可能会导致 binlog 数据丢失。

参考:

http://blog.csdn.net/xyang81/article/details/52928230

http://blog.chinaunix.net/uid-20196318-id-4058561.html

http://bbs.chinaunix.net/forum-240-1.html

原文地址:https://www.cnblogs.com/hd3013779515/p/6884202.html