分享

FastDFS分布式文件系统集群安装与配置

 WindySky 2017-09-15

原文链接:http://blog.csdn.net/xyang81/article/details/52928230

FastDFS集群规划

跟踪服务器负载均衡节点1:192.168.1.206 dfs-nginx-proxy-1
跟踪服务器负载均衡节点2:192.168.1.207 dfs-nginx-proxy-2
跟踪服务器1:192.168.1.200 dfs-tracker-1
跟踪服务器2:192.168.1.201 dfs-tracker-2
存储服务器1:192.168.1.202 dfs-storage-group1-1
存储服务器2:192.168.1.203 dfs-storage-group1-2
存储服务器3:192.168.1.204 dfs-storage-group2-1
存储服务器3:192.168.1.205 dfs-storage-group2-2
HA虚拟IP:192.168.1.208
HA软件:Keepalived
操作系统:CentOS 7
用户:root
数据目录:/fastdfs

安装包:
fastdfs-master-V5.05.zip:FastDFS源码
libfastcommon-master.zip:(从 FastDFS 和 FastDHT 中提取出来的公共 C 函数库)
fastdfs-nginx-module-master.zip:storage节点http服务nginx模块
nginx-1.10.0.tar.gz:Nginx安装包
ngx_cache_purge-2.3.tar.gz:Nginx图片缓存清除模块
获取安装包的方式:
1> 从这里下载打包好的所有安装包:http://download.csdn.net/detail/xyang81/9667493
2> 从作者github官网挨个下载fastdfs源码及其依赖库:https://github.com/happyfish100 和 Nginx缓存清除模块:https://github.com/FRiCKLE/ngx_cache_purge

开始前,先将所有安装包下载到各个节点的/usr/local/src目录中。

1> 本文称节点IP最后一段就代表某个节点,如:192.168.1.206,文中提到206节点,就代表192.168.1.206。
2> 本文称tracker或跟踪服务器是同一个意思
3> 本文称storage或存储服务器是同一个意思

FastDFS集群架构图

FastDFS集群架构图

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

一、安装集群节点

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

1> 安装所需的依赖包

shell> yum install make cmake gcc gcc-c++
  • 1

2> 安装libfatscommon

shell> cd /usr/local/src
shell> unzip libfastcommon-master.zip
shell> cd libfastcommon-master
## 编译、安装
shell> ./make.sh
shell> ./make.sh install
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

3> 安装FastDFS

shell> cd /usr/local/src
shell> unzip fastdfs-master-V5.05.zip
shell> cd fastdfs-master
## 编译、安装
shell> ./make.sh
shell> ./make.sh install
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

安装细节可参考上一篇文章《FastDFS分布式文件系统安装与使用(单节点)》 中的第一节。

二、配置跟踪节点(192.168.1.200,192.168.1.201)

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

1> 复制tracker样例配置文件,并重命名

shell> cp /etc/fdfs/tracker.conf.sample /etc/fdfs/tracker.conf
  • 1

2> 修改tracker.conf配置文件

shell> vim /etc/fdfs/tracker.conf
# 修改的内容如下:
disabled=false              # 启用配置文件
port=22122                  # tracker服务器端口(默认22122)
base_path=/fastdfs/tracker  # 存储日志和数据的根目录
  • 1
  • 2
  • 3
  • 4
  • 5

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

3> 创建base_path指定的目录

shell> mkdir -p /fastdfs/tracker
  • 1

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
  • 1

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

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

6> 停止tracker服务器

shell> /etc/init.d/fdfs_trackerd stop
  • 1

7> 设置tracker服务开机启动

shell> chkconfig fdfs_trakcerd on
  • 1

三、配置存储节点

group1: 192.168.1.202,192.168.1.203
group2: 192.168.1.204,192.168.1.205

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

1> 复制storage样例配置文件,并重命名

shell> cp /etc/fdfs/storage.conf.sample /etc/fdfs/storage.conf
  • 1

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.0.200:22122  # tracker服务器IP和端口
tracker_server=192.168.0.201:22122  # tracker服务器IP和端口
http.server_port=8888               # http访问文件的端口
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

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

3> 创建基础数据目录

shell> mkdir -p /fastdfs/storage
  • 1

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
  • 1

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

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

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

shell> /usr/bin/fdfs_monitor /etc/fdfs/storage.conf
  • 1

6> 停止storage服务器

shell> /etc/init.d/fdfs_storaged stop
  • 1

7> 设置storage服务开机启动

shell> chkconfig fdfs_storaged on
  • 1

四、文件上传测试

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.1.200:22122
tracker_server=192.168.1.201:22122
  • 1
  • 2
  • 3
  • 4
  • 5

2> 执行文件上传命令

shell> /usr/bin/fdfs_upload_file /etc/fdfs/client.conf /usr/local/src/FastDFS_v5.05.tar.gz
  • 1

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

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

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

1> fastdfs-nginx-module作用说明

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

2> 安装nginx和fastdfs-nginx-module模块

## 安装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 /usr/local/src
shell> tar -zxvf nginx-1.10.0.tar.gz
shell> unzip fastdfs-nginx-module-master.zip
shell> cd nginx-1.10.0
shell> ./configure --prefix=/opt/nginx --sbin-path=/usr/bin/nginx --add-module=/usr/local/src/fastdfs-nginx-module/src
shell> make && make install
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

3> 复制 fastdfs-nginx-module 源码中的配置文件到/etc/fdfs 目录,并修改

shell> cp /usr/local/src/fastdfs-nginx-module/src/mod_fastdfs.conf /etc/fdfs/
shell> vi /etc/fdfs/mod_fastdfs.conf
  • 1
  • 2

第一组存储服务器的mod_fastdfs.conf配置

connect_timeout=10
base_path=/tmp
tracker_server=192.168.1.200:22122
tracker_server=192.168.1.201: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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

第二组存储服务器的mod_fastdfs.conf配置

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

4> 复制FastDFS源文件目录中HTTP相关的配置文件到/etc/fdfs目录

shell> cd /usr/local/src/FastDFS/conf
shell> cp http.conf mime.types /etc/fdfs/
  • 1
  • 2

5> 创建数据存放目录的软链接

shell> ln -s /fastdfs/storage/data/ /fastdfs/storage/data/M00
  • 1

6> 配置fastdfs-nginx-module(Nginx简洁版样例)

shell> vi /opt/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;
        }   
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

注意:
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;
}
  • 1
  • 2
  • 3

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
  • 1
  • 2
  • 3
  • 4
  • 5

8> 启动Nginx

shell> /opt/nginx/sbin/nginx
ngx_http_fastdfs_set pid=xxx        # fastdfs-nginx-module进程ID
  • 1
  • 2

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

设置Nginx开机启动:

shell> vi /etc/rc.local
# 加入
/opt/nginx/sbin/nginx
shell> chmod +x /etc/rc.local  # centos7
  • 1
  • 2
  • 3
  • 4

9> 通过浏览器访问测试时上传的文件

http://192.168.1.202:8888/group1/M00/00/00/wKgBh1Xtr9-AeTfWAAVFOL7FJU4.tar.gz
http://192.168.1.204:8888/group2/M00/00/00/wKgBiVXtsDmAe3kjAAVFOL7FJU4.tar.gz

六、 跟踪节点安装Nginx和ngx_cache_purge模块

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

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

1> 安装Nginx所需的依赖包

shell> yum install gcc gcc-c++ make automake autoconf libtool pcre pcre-devel zlib zlib-devel openssl openssl-devel
  • 1

2> 安装nginx和ngx_cache_purge模块

shell> cd /usr/local/src
shell> tar -zxvf nginx-1.10.0.tar.gz
shell> tar -zxvf ngx_cache_purge-2.3.tar.gz
shell> cd nginx-1.10.0
shell> ./configure --prefix=/opt/nginx --sbin-path=/usr/bin/nginx --add-module=/usr/local/src/ngx_cache_purge-2.3
shell> make && make install
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

3> 配置Nginx,设置tracker负载均衡以及缓存

shell> vi /opt/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_temp_path /fastdfs/cache/nginx/proxy_cache/tmp;

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

    #设置 group2 的服务器
    upstream fdfs_group2 {
         server 192.168.1.204:8888 weight=1 max_fails=2 fail_timeout=30s;
         server 192.168.1.205: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; 
        }
    } 
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88

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

shell> mkdir -p /fastdfs/cache/nginx/proxy_cache
shell> mkdir -p /fastdfs/cache/nginx/proxy_cache/tmp
  • 1
  • 2

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 # 重新启动防火墙
  • 1
  • 2
  • 3
  • 4

5> 启动Nginx

shell> /opt/nginx/sbin/nginx
  • 1

设置开机启动:

shell> vi /etc/rc.local
## 加入以下配置
/opt/nginx/sbin/nginx
shell> chmod +x /etc/rc.local  #centos7
  • 1
  • 2
  • 3
  • 4

6> 文件访问测试

前面直接通过访问Storage节点中的Nginx访问文件:
http://192.168.1.202:8888/group1/M00/00/00/wKgBh1Xtr9-AeTfWAAVFOL7FJU4.tar.gz
http://192.168.1.204:8888/group2/M00/00/00/wKgBiVXtsDmAe3kjAAVFOL7FJU4.tar.gz

现在可以通过Tracker中的Nginx来进行访问:
(1)、通过 Tracker1 中的 Nginx 来访问
http://192.168.1.200:8000/group1/M00/00/00/wKgBh1Xtr9-AeTfWAAVFOL7FJU4.tar.gz
http://192.168.1.200:8000/group2/M00/00/00/wKgBiVXtsDmAe3kjAAVFOL7FJU4.tar.gz
(2)、通过 Tracker2 中的 Nginx 来访问
http://192.168.1.201:8000/group1/M00/00/00/wKgBh1Xtr9-AeTfWAAVFOL7FJU4.tar.gz
http://192.168.1.201:8000/group2/M00/00/00/wKgBiVXtsDmAe3kjAAVFOL7FJU4.tar.gz
由上面的文件访问效果可以看到,每一个Tracker中的Nginx都单独对后端的Storage组做了负载均衡,但整套FastDFS集群,如果想对外提供统一的文件访问地址,还需要对两个Tracker中的Nginx进行HA集群

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

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

1> 安装keepalived与Nginx

分别在192.168.1.206和192.168.1.207两个节点安装Keepalived与Nginx。
keepalived安装与配置:http://blog.csdn.net/xyang81/article/details/52554398
Nginx的安装与配置:http://blog.csdn.net/xyang81/article/details/51476293

2> 配置Keeyalived + Nginx高可用

请参考《Keepalived+Nginx实现高可用(HA)》

注意:将VIP的IP地址修改为192.168.1.208

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

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

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

    ## FastDFS Tracker Proxy
    upstream fastdfs_tracker {
         server 192.168.1.200:8000 weight=1 max_fails=2 fail_timeout=30s;
         server 192.168.1.201: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;
        }
    } 
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45

4> 重启192.168.1.206 和 192.168.1.207 中的Nginx

shell> /opt/nginx/sbin/nginx -s reload
  • 1

5> 通过虚拟IP访问文件测试

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

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

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多