FastDFS高可用集群架构配置搭建

一、FastDFS系统介绍

FastDFS 是余庆老师开发的一个开源的高性能分布式文件系统(DFS)。 它的主要功能包括:文件存储,文件同步和文件访问,以及高容量和负载平衡。主要解决了海量数据存储问题,特别适合以中小文件(建议范围:4KB < file_size <500MB)为载体的在线服务。

FastDFS 系统有三个角色:跟踪服务器(Tracker Server)、存储服务器(Storage Server)和客户端(Client)。

  • Tracker Server: 跟踪服务器,主要做调度工作,起到均衡的作用;负责管理所有的storage server和group,每个storage在启动后会连接 Tracker,告知自己所属 group 等信息,并保持周期性心跳。多个Tracker之间是对等关系,不存在单点故障。
  • Storage Server: 存储服务器,主要提供容量和备份服务;以 group 为单位,每个 group 内可以有多台 storage server,组内的storage server上的数据互为备份。
  • Client:客户端,上传下载数据的服务器。

FastDFS系统架构图

下图是实现统一的对外下载访问入口的高可用架构,其中所有的Nginx只做下载用途,上传通过tracker进行上传。

FastDFS高可用架构图

二、设备准备

系统软件说明:

名称 说明 链接
CentOS 7.x(安装系统)
libfastcommon FastDFS分离出的一些公用函数包 libfastcommon V1.0.39
FastDFS FastDFS本体 FastDFS V5.11
fastdfs-nginx-module FastDFS和nginx的关联模块,解决组内同步延迟问题 fastdfs-nginx-module V1.20
nginx nginx 1.12.2(CentOS 7 下YUM可以安装的最新版本) nginx 1.12.2

整个搭建所需环境资源及用途如下表:

名称 IP地址 应用
tracker01 192.168.0.1 FastDFS,libfastcommon
tracker02 192.168.0.2 FastDFS,libfastcommon
tracker03 192.168.0.3 FastDFS,libfastcommon
storage01 192.168.0.10 FastDFS,libfastcommon,nginx,fastdfs-nginx-module
storage02 192.168.0.11 FastDFS,libfastcommon,nginx,fastdfs-nginx-module
storage03 192.168.0.12 FastDFS,libfastcommon,nginx,fastdfs-nginx-module
nginx01 192.168.0.100 nginx,keepalived
nginx02 192.168.0.101 nginx,keepalived
nginx03 192.168.0.102 nginx,keepalived
VIP 192.168.0.200

三、 安装流程

提前准备

1.编译环境准备

Note: 公司内部服务器,需要ops登录,使用sudo安装。

说明 位置
所有安装包 ${home}/{user}/fastdfs
数据存储位置 /data/fastdfs
# 安装编译环境
$ yum groups install Development Tools -y
$ yum install perl -y
$ mkdir -p ~/fastdfs /data/fastdfs # ~/fastdfs 可以替换为自己工作目录
$ cd ~/fastdfs  #为下一步下载源码做准备

2.安装libfastcommon

$ wget https://github.com/happyfish100/libfastcommon/archive/V1.0.39.tar.gz
$ tar -zxvf libfastcommon-1.0.39.tar.gz
$ cd libfastcommon-1.0.39/
$ ./make.sh
$ ./make.sh install

Note: libfastcommon.so 安装到了/usr/lib64/libfastcommon.so,但是FastDFS主程序设置的lib目录是/usr/local/lib,所以需要创建软链接。需要注意的是,新版本自己已创建软连接,如果没有创建,自己再添加即可。

 $ ln -s /usr/lib64/libfastcommon.so /usr/local/lib/libfastcommon.so
 $ ln -s /usr/lib64/libfastcommon.so /usr/lib/libfastcommon.so
 $ ln -s /usr/lib64/libfdfsclient.so /usr/local/lib/libfdfsclient.so
 $ ln -s /usr/lib64/libfdfsclient.so /usr/lib/libfdfsclient.so 

3.安装FastDFS

$ wget https://github.com/happyfish100/fastdfs/archive/V5.11.tar.gz
$ tar -zxvf fastdfs-5.11.tar.gz
$ cd fastdfs-5.11/
$ ./make.sh
$ ./make.sh install
$ #配置文件准备
$ cp /etc/fdfs/tracker.conf.sample /etc/fdfs/tracker.conf #tracker节点
$ cp /etc/fdfs/storage.conf.sample /etc/fdfs/storage.conf #storage节点
$ cp /etc/fdfs/client.conf.sample /etc/fdfs/client.conf #客户端文件,测试用
$ cp ./conf/http.conf /etc/fdfs/ #供nginx访问使用
$ cp ./conf/mime.types /etc/fdfs/ #供nginx访问使用

上面copy的文件,可根据设备准备中的表格中描述的功能对照着copy,无用则无需cp。编译FastDfs的时候可能会出现如下错误:

perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:

LANGUAGE = (unset),

LC_ALL = (unset),

LC_CTYPE = “UTF-8”,

LANG = “en_US.UTF-8”

are supported and installed on your system.

perl: warning: Falling back to the standard locale (“C”).

perl: warning: Setting locale failed.

解决方法:

$ vim /etc/.bashrc
 # 在~/.bashrc或者/etc/.bashrc中添加一句话https://blog.csdn.net/djh_xing/article/details/54944995
export LC_ALL=C
$ source /etc/.bashrc

3.2 TRACKER安装

$ vim /etc/fdfs/tracker.conf
$ #需要修改的内容如下
port=22122 # tracker服务器端口(默认22122,一般不修改)
base_path=/data/fastdfs # 存储日志和数据的根目录
$ #编辑启动文件
$ vim /usr/lib/systemd/system/fastdfs-tracker.service
[Unit]
Description=The FastDFS File server
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
ExecStart=/usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf start
ExecStop=/usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf stop
ExecRestart=/usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf restart

[Install]
WantedBy=multi-user.target

$ systemctl daemon-reload
$ systemctl enable fastdfs-tracker.service
$ systemctl start fastdfs-tracker.service
$ netstat -tulnp #查看服务是否启动,端口是否打开

3.2 STORAGE安装

$ vim /etc/fdfs/storage.conf
$ #需要修改的内容如下
port=23000 # storage服务端口(默认23000,一般不修改)
base_path=/data/fastdfs # 数据和日志文件存储根目录
store_path0=/data/fastdfs # 第一个存储目录
tracker_server=192.168.0.1:22122 # tracker服务器IP和端口
tracker_server=192.168.0.2:22122 # tracker服务器IP和端口
tracker_server=192.168.0.3:22122 # tracker服务器IP和端口
http.server_port=8888 # http访问文件的端口(默认8888,看情况修改,和nginx中保持一致)
$ #编辑启动文件
$ vim /usr/lib/systemd/system/fastdfs-storage.service

[Unit]
Description=The FastDFS File server
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
ExecStart=/usr/bin/fdfs_storaged /etc/fdfs/storage.conf start
ExecStop=/usr/bin/fdfs_storaged /etc/fdfs/storage.conf stop
ExecRestart=/usr/bin/fdfs_storaged /etc/fdfs/storage.conf restart

[Install]
WantedBy=multi-user.target

$ systemctl daemon-reload
$ systemctl enable fastdfs-storage.service
$ systemctl start fastdfs-storage.service
$ netstat -tulnp #查看服务是否启动,端口是否打开
#查看集群状态
$ fdfs_monitor /etc/fdfs/storage.conf list
[2018-11-06 00:00:00] DEBUG - base_path=/data/fastdfs/storage, connect_timeout=30, network_timeout=60, tracker_server_count=2, anti_steal_token=0, anti_steal_secret_key length=0, use_connection_pool=0, g_connection_pool_max_idle_time=3600s, use_storage_id=0, storage server id count: 0
server_count=2, server_index=0
tracker server is 192.168.0.1:22122
group count: 1
Group 1:
...

3.3 Client配置

$ vim /etc/fdfs/client.conf
#需要修改的内容如下
base_path=/data/fastdfs
tracker_server=192.168.0.1:22122 # tracker服务器IP和端口
tracker_server=192.168.0.2:22122 # tracker服务器IP和端口
tracker_server=192.168.0.3:22122 # tracker服务器IP和端口

#保存后测试,返回ID表示成功 如:group1/M00/00/00/xx.tar.gz
$ fdfs_upload_file /etc/fdfs/client.conf testfile

3.4 安装nginx和fastdfs-nginx-module

下载nginx module

$ wget https://github.com/happyfish100/fastdfs-nginx-module/archive/V1.20.tar.gz
$ cp ~/fastdfs/fastdfs-nginx-module-1.20/src/mod_fastdfs.conf /etc/fdfs

安装nginx

$ yum install nginx -y
# 查看版本及编译参数
$ nginx -v
$ nginx -V #注意是大写V

添加nginx module

note: 编译NGINX,可能会出现很多错误,具体的解决方法见后面。

wget http://nginx.org/download/nginx-1.12.2.tar.gz
$ tar -zxvf nginx-1.12.2.tar.gz
$ cd nginx-1.12.2/
$ ./configure --prefix=/usr/share/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/var/lib/nginx/tmp/client_body --http-proxy-temp-path=/var/lib/nginx/tmp/proxy --http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi --http-uwsgi-temp-path=/var/lib/nginx/tmp/uwsgi --http-scgi-temp-path=/var/lib/nginx/tmp/scgi --pid-path=/run/nginx.pid --lock-path=/run/lock/subsys/nginx --user=nginx --group=nginx --with-file-aio --with-ipv6 --with-http_auth_request_module --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module=dynamic --with-http_image_filter_module=dynamic --with-http_geoip_module=dynamic --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_slice_module --with-http_stub_status_module --with-http_perl_module=dynamic --with-mail=dynamic --with-mail_ssl_module --with-pcre --with-pcre-jit --with-stream=dynamic --with-stream_ssl_module --with-google_perftools_module --with-debug --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -m64 -mtune=generic' --with-ld-opt='-Wl,-z,relro -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -Wl,-E' --add-module=~/fastdfs/fastdfs-nginx-module-1.20/src
$ make
$ cp objs/nginx /usr/sbin/nginx
$ systemctl enable nginx
$ systemctl restart nginx

查看插件是否安装成功:

$ nginx -V

配置NGINX访问:

$ vim /etc/fdfs/mod_fastdfs.conf
#需要修改的内容如下
tracker_server=192.168.0.1:22122 # tracker服务器IP和端口
tracker_server=192.168.0.2:22122 # tracker服务器IP和端口
tracker_server=192.168.0.3:22122 # tracker服务器IP和端口
url_have_group_name=true
base_path=/data/fastdfs
store_path0=/data/fastdfs

#配置nginx.config
$ vim /etc/nginx/nginx.conf
#增加如下内容
server {
        listen 8888; ## 该端口为storage.conf中的http.server_port相同
        server_name localhost;
        location ~/group[0-9]/ {
            root /data/fastdfs;
            ngx_fastdfs_module;
        }
        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
            root html;
        }
    }
#测试下载,用外部浏览器访问刚才已传过的文件,引用返回的ID
http://192.168.0.10:8888/group1/M00/00/00/wKgAQ1pysxmAaqhAAA76tz-dVgg.tar.gz
#弹出下载则目前nginx已经OK

3.5 配置文件访问的负载均衡和高可用安装

在192.168.0.100、192.168.0.101和192.168.0.102上安装nginx、keepalived。

$ yum install -y nginx keepalived

nginx的配置文件如下:

upstream fdfs_group01 {
    server 192.168.0.10:8888 weight=1 max_fails=2 fail_timeout=30s;
    server 192.168.0.11:8888 weight=1 max_fails=2 fail_timeout=30s;
    server 192.168.0.12:8888 weight=1 max_fails=2 fail_timeout=30s;
}
server {
    listen 80;
    server_name localhost;
    location /group01{
        proxy_next_upstream http_502 http_504 error timeout invalid_header;
        proxy_pass http://fdfs_group01;
        expires 30d;
    }
}

keepalived配置文件如下:

global_defs {
   router_id LVS_DEVEL
}

vrrp_script chk_ngx {
    script "/etc/keepalived/check_nginx.sh"
    interval 2
    weight -5
    fall 3  
    rise 2
}


vrrp_instance VI_1 {
    interface eno16777984
    state MASTER
    priority 100
    virtual_router_id 11
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }

    unicast_src_ip 192.168.0.100

    unicast_peer {
        192.168.0.101
        192.168.0.102
    }

    virtual_ipaddress {
        192.168.0.200
    }

    track_script {
       chk_ngx
    }
    notify_master "/etc/keepalived/notify.sh master"  
    notify_backup "/etc/keepalived/notify.sh backup"  
    notify_fault "/etc/keepalived/notify.sh fault"
}

check_nginx.sh

#!/bin/bash

counter=$(ps -C nginx --no-heading|wc -l)
if [ "${counter}" = "0" ]; then
    exit 1
else
    exit 0
fi

OK,以上就是所有的安装步骤及相关配置。

错误解决

错误1: ERROR – file: connection_pool.c, line: 130, connect to 192.168.0.8:23000 fail, errno: 113, error info: No route to host

执行过程中可能出现防火墙阻止导致,检测打开端口或关闭防火墙。

systemctl stop firewalld.service #停止firewall

错误2:Fdfs错误tracker_query_storage fail, error no: 2, …

需要从新启动服务器.

错误3: ./configure: error: the invalid value in –with-ld-opt=”-Wl,-z,relro -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -Wl,-E”

解决方法:yum -y install redhat-rpm-config.noarch

错误4: ./configure: error: the HTTP image filter module requires the GD library.

解决方法: http_image_filter_module是nginx提供的集成图片处理模块,需要gd-devel的支持。

yum -y install gd-devel

错误5:./configure: error: the HTTP image filter module requires the GD library.

解决方法: yum -y install gd-devel

错误6: ./configure: error: perl module ExtUtils::Embed is required

解决方法: yum -y install perl-devel perl-ExtUtils-Embed

错误7: ./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using –without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using –with-pcre= option.

安装pcre-devel解决问题 yum -y install pcre-devel

 

错误8: ./configure: error: the HTTP cache module requires md5 functions
from OpenSSL library.   You can either disable the module by using
–without-http-cache option, or install the OpenSSL library into the system,
or build the OpenSSL library statically from the source with nginx by using
–with-http_ssl_module –with-openssl= options.

解决办法: yum -y install openssl openssl-devel

错误9: ./configure: error: the HTTP XSLT module requires the libxml2/libxslt
libraries. You can either do not enable the module or install the libraries

错误10: ./configure: error: C compiler cc is not found

解决方法:安装gcc-c++

yum -y install gcc-c++ autoconf automake
# autoconf是自动配置,automake是自动编译

错误11: 缺少zlib包,错误信息如下:
./configure: error: the HTTP gzip module requires the zlib library.
You can either disable the module by using –without-http_gzip_module
option, or install the zlib library into the system, or build the zlib
library
statically from the source with nginx by using –with-zlib= option.

解决方法:安装zlib yum install -y zlib-devel

错误12: 缺少libxml2,错误信息如下:
./configure: error: the HTTP XSLT module requires the libxml2/libxslt
libraries. You can either do not enable the module or install the libraries.

解决方法:

yum -y install libxml2 libxml2-dev
yum -y install libxslt-devel

错误13: 缺少ExtUtils,错误信息如下:
./configure: error: perl module ExtUtils::Embed is required

解决方法:yum -y install perl-devel perl-ExtUtils-Embed

错误14: 缺少GeoIP,错误信息如下:
./configure: error: the GeoIP module requires the GeoIP library.
You can either do not enable the module or install the library.

解决方法:yum -y install GeoIP GeoIP-devel GeoIP-data

错误15: 在nginx的构建中会遇到不少的报错,具体如下:
* ./configure: error: the Google perftools module requires the Google perftools library. You can either do not enable the module or install the library.
         

解决方法如下:
 yum install gperftools -y

错误16: 在make过程中会出现一个报错:/usr/include/fastdfs/fdfs_define.h:15:27: fatal error: common_define.h: No such file or directory 

解决方法如下: 
修改fastdfs-nginx-module-1.20/src/config文件,修改如下:
ngx_module_incs=”/usr/include/fastdfs /usr/include/fastcommon/”
CORE_INCS=”$CORE_INCS /usr/include/fastdfs /usr/include/fastcommon/”
然后重新./configure && make,就可以了

[ops@034057 nginx-1.12.2] ps -ef | grep nginx ops 17343 15169 0 14:19 pts/0 00:00:00 grep –color=auto nginx root 22500 1 0 Jan14 ? 00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf nginx 22501 22500 0 Jan14 ? 00:00:00 nginx: worker process nginx 22502 22500 0 Jan14 ? 00:00:00 nginx: worker process nginx 22503 22500 0 Jan14 ? 00:00:00 nginx: worker process nginx 22504 22500 0 Jan14 ? 00:00:00 nginx: worker process nginx 22505 22500 0 Jan14 ? 00:00:00 nginx: worker process nginx 22507 22500 0 Jan14 ? 00:00:00 nginx: worker process nginx 22511 22500 0 Jan14 ? 00:00:00 nginx: worker process nginx 22513 22500 0 Jan14 ? 00:00:00 nginx: worker process [ops@034057 nginx-1.12.2]
[ops@034057 nginx-1.12.2] [ops@034057 nginx-1.12.2] sudo nginx -s stop
[ops@034057 nginx-1.12.2] ps -ef | grep nginx ops 17411 15169 0 14:19 pts/0 00:00:00 grep –color=auto nginx [ops@034057 nginx-1.12.2] sudo cp objs/nginx /usr/sbin/nginx

点击量:24

发表评论