FastDFS高可用集羣架構配置搭建

 

1、基本模塊及高可用架構

FastDFS 是餘慶老師開發的一個開源的高性能分佈式文件系統(DFS)。 它的主要功能包括:文件存儲,文件同步和文件訪問,以及高容量和負載平衡。
FastDFS 系統有三個角色:跟蹤服務器(Tracker Server)、存儲服務器(Storage Server)和客戶端(Client)。html

  • Tracker Server: 跟蹤服務器,主要作調度工做,起到均衡的做用;負責管理全部的storage server和group,每一個storage在啓動後會鏈接 Tracker,告知本身所屬 group 等信息,並保持週期性心跳。多個Tracker之間是對等關係,不存在單點故障。
  • Storage Server: 存儲服務器,主要提供容量和備份服務;以 group 爲單位,每一個 group 內能夠有多臺 storage server,組內的storage server上的數據互爲備份。
  • Client:客戶端,上傳下載數據的服務器
    模塊之間的主要關係以下:
    FastDFS 架构图.png

下圖是實現統一的對外下載訪問入口的高可用架構,其中全部的Nginx只作下載用途,上傳經過tracker進行上傳。
nginx

2、環境準備工做

系統軟件說明:git

名稱 說明
CentOS 7.x(安裝系統)
libfastcommon FastDFS分離出的一些公用函數包
FastDFS FastDFS本體
fastdfs-nginx-module FastDFS和nginx的關聯模塊,解決組內同步延遲問題
nginx nginx 1.12.2(CentOS 7 下YUM能夠安裝的最新版本)

整個搭建所需環境資源及用途以下表:github

名稱 IP地址 應用
tracker01 192.168.0.1 FastDFS,libfastcommon
tracker02 192.168.0.2 FastDFS,libfastcommon
storage01 192.168.0.10 FastDFS,libfastcommon,nginx,fastdfs-nginx-module
storage02 192.168.0.11 FastDFS,libfastcommon,nginx,fastdfs-nginx-module
nginx01 192.168.0.100 nginx,keepalived
nginx02 192.168.0.101 nginx,keepalived
VIP 192.168.0.200 -

3、安裝過程vim

一、編譯環境準備

說明 位置
全部安裝包 /opt/fastdfs
數據存儲位置 /data/fastdfs
 
$ tar -zxvf libfastcommon-1.0.39.tar.gz #安裝編譯環境 $ yum groups install Development Tools -y $ yum install perl -y $ mkdir -p /opt/fastdfs /data/fastdfs $ cd /opt/fastdfs  #爲下一步下載源碼作準備

二、安裝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

三、安裝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 /opt/fastdfs/fastdfs-5.11/conf/http.conf /etc/fdfs/ #供nginx訪問使用 $ cp /opt/fastdfs/fastdfs-5.11/conf/mime.types /etc/fdfs/ #供nginx訪問使用

tracker server配置:

$ 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 #查看服務是否啓動,端口是否打開

storage server配置

$ 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和端口 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: ...

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和端口 #保存後測試,返回ID表示成功 如:group1/M00/00/00/xx.tar.gz $ fdfs_upload_file /etc/fdfs/client.conf testfile

四、安裝nginx和fastdfs-nginx-module

下載nginx module瀏覽器

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

安裝nginxbash

$ yum install nginx -y # 查看版本及編譯參數 $ nginx -v $ nginx -V

添加nginx module服務器

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=/opt/fastdfs/fastdfs-nginx-module-1.20/src/ $ make $ cp objs/nginx /usr/sbin/nginx $ systemctl enable nginx $ systemctl restart nginx

查看插件是否安裝成功:架構

$ nginx -V


配置nginx訪問app

$ vim /etc/fdfs/mod_fastdfs.conf #須要修改的內容以下 tracker_server=192.168.0.1:22122 # tracker服務器IP和端口 tracker_server=192.168.0.2: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

注:

在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
  • /configure: error: the HTTP rewrite module requires the PCRE library.
    解決方法以下:
$ yum -y install pcre pcre-devel
  • ./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
  • ./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.
    解決方法以下:
$ yum install -y zlib-devel
  • ./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
  • ./configure: error: the HTTP image filter module requires the GD library. You can either do not enable the module or install the libraries.
    解決方法以下:
$ yum -y install gd-devel
  • ./configure: error: perl module ExtUtils::Embed is required
    解決方法以下:
$ yum -y install perl-devel perl-ExtUtils-Embed
  • ./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
  • 在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,就能夠了

 

五、配置文件訪問的負載均衡和高可用

在192.168.0.100和101上安裝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 { 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 } 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,以上就是全部的安裝步驟及相關配置。
文章中有什麼疑問或者錯誤,歡迎你們提出指正。
注:借鑑文章URL以下:

注:本做品採用知識共享署名-非商業性使用-相同方式共享 4.0 國際許可協議進行許可。

相關文章
相關標籤/搜索