FastDFS介紹與安裝


目錄

FastDFS. 1

1     介紹.... 1

2     安裝.... 4

2.1      安裝libfastcommon. 4

2.1.1      下載安裝包並解壓.... 4

2.2      安裝FastDFS. 5

2.3      安裝nginx. 8

2.3.1      更新nginx安裝庫.... 8

2.3.2      安裝nginx. 8

2.4      存儲服務器(storage server)安裝並配置nginx. 10

2.4.1      安裝fastdfs-nginx-module. 10

 

 

1     介紹

FastDFS是一款開源的、分布式文件系統(Distributed File System),由淘寶開發平台部資深架構師余慶開發。該開源項目的主頁是 http://code.google.com/p/fastdfs 。可以通過fastdfs.sourceforge.NET 下載。FastDFS孵化平台(ChinaUnix)版塊http://bbs.chinaunix.net/forum-240-1.html。

FastDFS是一個開源的輕量級分布式文件系統,它對文件進行管理,功能包括:文件存儲、文件同步、文件訪問(文件上傳、文件下載)等,解決了大容量存儲和負載均衡的問題。特別適合以文件為載體的在線服務,如相冊網站、視頻網站等等。

FastDFS為互聯網量身定制,充分考慮了冗余備份、負載均衡、線性擴容等機制,並注重高可用、高性能等指標,使用FastDFS很容易搭建一套高性能的文件服務器集群提供文件上傳、下載等服務。

FastDFS服務端有兩個角色:跟蹤器(tracker)和存儲節點(storage)。跟蹤器主要做調度工作,在訪問上起負載均衡的作用。

存儲節點存儲文件,完成文件管理的所有功能,就是這樣的存儲、同步和提供存取接口,FastDFS同時對文件的metadata進行管理。所謂文件的meta data就是文件的相關屬性,以鍵值對(key valuepair)方式表示,如:width=1024,其中的key為width,value為1024。文件metadata是文件屬性列表,可以包含多個鍵值對。

跟蹤器和存儲節點都可以由一台或多台服務器構成。跟蹤器和存儲節點中的服務器均可以隨時增加或下線而不會影響線上服務。其中跟蹤器中的所有服務器都是對等的,可以根據服務器的壓力情況隨時增加或減少。

為了支持大容量,存儲節點(服務器)采用了分卷(或分組)的組織方式。存儲系統由一個或多個卷組成,卷與卷之間的文件是相互獨立的,所有卷的文件容量累加就是整個存儲系統中的文件容量。一個卷可以由一台或多台存儲服務器組成,一個卷下的存儲服務器中的文件都是相同的,卷中的多台存儲服務器起到了冗余備份和負載均衡的作用。

在卷中增加服務器時,同步已有的文件由系統自動完成,同步完成后,系統自動將新增服務器切換到線上提供服務。

當存儲空間不足或即將耗盡時,可以動態添加卷。只需要增加一台或多台服務器,並將它們配置為一個新的卷,這樣就擴大了存儲系統的容量。

FastDFS中的文件標識分為兩個部分:卷名和文件名,二者缺一不可。

 

 

 

FastDFS架構圖

1)      Tracker cluster中各個tracker server相互獨立,不進行相互通信。

2)      Storage cluster中各個storage組(Volume1,Volume2...)相互獨立,不進行相互通信,也就是說各個組之間保存的數據是不相同的。但是各個組中的storage server之間是屬於互相備份的關系,也就是說storage server之間保存相同的數據。

3)      每個storage server會啟動一個單獨的線程主動向Tracker cluster中每個tracker server報告其狀態信息,包括磁盤使用情況,文件同步情況及文件上傳下載次數統計等信息。

 

 

上傳時序圖

1)      Client通過Tracker server將文件上傳到Storage server。

2)      Tracker server向Client返回一台可用的Storage server的IP地址和端口號。

3)      Client直接通過Tracker server返回的IP地址和端口與其中一台Storage server建立連接並進行文件上傳。

4)      上傳完成,Storage server返回Client一個文件ID,文件上傳結束。

 

 

下載時序圖

1)      Client通過Tracker server下載指定Storage組中某個Storage server上的某個文件(文件名包括Storage組名稱)。

2)      Tracker server向Client返回一台可用的Storage server的IP地址和端口號。

3)      Client直接通過Tracker server返回的IP地址和端口與其中一台Storage server建立連接並進行文件下載。

 

 

 

2     安裝

安裝FastDFS還需要安裝libfastcommon作為前提條件,首先准備安裝包FastDFS和libfastcommon。

libfastcommon下載地址: https://github.com/happyfish100/libfastcommon/releases

FastDFS的下載地址:https://github.com/happyfish100/fastdfs/releases

首先安裝libfastcommon,得到libfastcommon-1.0.35.tar.gz后解壓、編譯、安裝。

運行環境相關軟件:

²  CentOS 7.0

²  Libfastcommon-1.0.35

²  FastDFS-5.10

²  Nginx

²  Fastdfs-nginx-module

服務器規划

服務器名

備注

Fastdfs-tracker

跟蹤服務器/調度服務器

Fastdfs-storge

存儲服務器

 

2.1   安裝libfastcommon

2.1.1 下載安裝包並解壓

  • 下載libfastcommon

wget https://github.com/happyfish100/libfastcommon/archive/V1.0.35.tar.gz

  • 解壓

tar -zxvf V1.0.35.tar.gz

 

  • 安裝

cd libfastcommon-1.0.35

./make.sh

./make.sh install

 

提示:執行./make.sh命令時報如下錯誤,需要安裝gcc

 

gcc安裝命令:yum -y install gcc gcc-c++  make automake autoconf libtool perl

 

 

 

 

 

2.2   安裝FastDFS

  • 解壓

tar xvf V5.10.tar.gz

 

  • 安裝

cd fastdfs-5.10

./make.sh

./make.sh install

2.2.1.1    采用默認方式安裝后的文件及目錄

²  服務腳本

[root@fastdfs-tracker ~]# ll /etc/init.d/ |grep fdfs

-rwxr-xr-x. 1 root root   918 4月  22 22:08 fdfs_storaged

-rwxr-xr-x. 1 root root   920 4月  22 22:08 fdfs_trackerd

²  樣例配置文件

[root@fastdfs-tracker ~]# ll /etc/fdfs/

總用量 20

-rw-r--r--. 1 root root 1461 4月  22 22:08 client.conf.sample

-rw-r--r--. 1 root root 7927 4月  22 22:08 storage.conf.sample

-rw-r--r--. 1 root root 7200 4月  22 22:08 tracker.conf.sample

²  命令行工具

[root@fastdfs-tracker ~]# ll /usr/bin|grep fdfs

-rwxr-xr-x.   1 root root     252272 4月  22 22:08 fdfs_appender_test

-rwxr-xr-x.   1 root root     252225 4月  22 22:08 fdfs_appender_test1

-rwxr-xr-x.   1 root root     242449 4月  22 22:08 fdfs_append_file

-rwxr-xr-x.   1 root root     242013 4月  22 22:08 fdfs_crc32

-rwxr-xr-x.   1 root root     242508 4月  22 22:08 fdfs_delete_file

-rwxr-xr-x.   1 root root     243627 4月  22 22:08 fdfs_download_file

-rwxr-xr-x.   1 root root     243369 4月  22 22:08 fdfs_file_info

-rwxr-xr-x.   1 root root     255657 4月  22 22:08 fdfs_monitor

-rwxr-xr-x.   1 root root     863913 4月  22 22:08 fdfs_storaged

-rwxr-xr-x.   1 root root     258712 4月  22 22:08 fdfs_test

-rwxr-xr-x.   1 root root     257881 4月  22 22:08 fdfs_test1

-rwxr-xr-x.   1 root root     365232 4月  22 22:08 fdfs_trackerd

-rwxr-xr-x.   1 root root     243547 4月  22 22:08 fdfs_upload_appender

-rwxr-xr-x.   1 root root     244453 4月  22 22:08 fdfs_upload_file

2.2.1.2  配置跟蹤服務器(tracker server)

  • 拷貝tracker server和client端樣例配置文件並重命名

[root@fastdfs-tracker ~]# cp /etc/fdfs/tracker.conf.sample /etc/fdfs/tracker.conf

[root@fastdfs-storage ~]# cp /etc/fdfs/client.conf.sample /etc/fdfs/client.conf

  • 編輯tracker server配置文件tracker.conf,需要修改內容如下:

disabled=false(默認為false,表示是否無效)

port=22122(默認為22122)

base_path=/data/fastdfs/tracker

  • 編輯client端的配置文件client.conf,需要修改內容如下

base_path=/data/fastdfs/tracker

#tracker_server中IP指定的是tracker server的IP

tracker_server=192.168.128.134:22122

 

 

  • 創建tracker server數據目錄

[root@fastdfs-tracker ~]# mkdir -p /data/fastdfs/tracker

  • 測試啟動tracker server,啟動成功會自動在/data/fastdfs/tracker目錄新建data和logs目錄

[root@fastdfs-tracker ~]# cd /data/fastdfs/tracker/

[root@fastdfs-tracker tracker]# ls

[root@fastdfs-tracker tracker]# /etc/init.d/fdfs_trackerd start

Starting FastDFS tracker server:

[root@fastdfs-tracker tracker]# ss -lntup|grep 22122

Tcp LISTEN 0 128 *:22122 *:* users:(("fdfs_trackerd",3785,5))

[root@fastdfs-tracker tracker]# ls

data logs

 

 

  • 關閉tracker server

[root@fastdfs-tracker tracker]# /etc/init.d/fdfs_trackerd stop

waiting for pid [3785] exit ...

pid [3785] exit.

 

 

2.2.1.3  配置存儲服務器(storage server)

  • 拷貝storage server樣例配置文件並重命名

[root@fastdfs-storage ~]# cp /etc/fdfs/storage.conf.sample /etc/fdfs/storage.conf

  • 編輯storage server配置文件storage.conf,需要修改內容如下:

disabled=false(默認為false,表示是否無效)

port=23000(默認為23000)

base_path=/data/fastdfs/storage

tracker_server=192.168.128.135:22122

store_path0=/data/fastdfs/storage

http.server_port=8888(默認為8888,nginx中配置的監聽端口那之一致)

  • 創建storage server數據目錄

[root@fastdfs-storage ~]# mkdir -p /data/fastdfs/storage

  • 測試啟動storage server,啟動成功會自動在/data/fastdfs/tracker目錄新建data和logs目錄(啟動storage server的前提是tracker server必須事先已啟動)

[root@fastdfs-storage ~]# cd /data/fastdfs/storage/

[root@fastdfs-storage storage]# ls

[root@fastdfs-storage storage]# /etc/init.d/fdfs_storaged start

Starting FastDFS storage server:

[root@fastdfs-storage storage]# ss -lntup|grep 23000

tcp    LISTEN     0      128                    *:23000                 *:*      users:(("fdfs_storaged",3786,5))

[root@fastdfs-storage storage]# ls

data  logs

 

2.2.1.4  文件上傳測試

執行如下上傳命令:

[root@fastdfs-tracker~]# /usr/bin/fdfs_upload_file /etc/fdfs/client.conf /home/oldcat/imgs/test.jpg

group1/M00/00/00/rBIK6VcaP0aARXXvAAHrUgHEviQ394.jpg

返回文件ID即說明文件已經上傳成功

 

 

 

2.3   安裝nginx

2.3.1 更新nginx安裝庫

# rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm

# wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo

# yum makecache

# yum update

 

 

 

 

 

2.3.2 安裝nginx

yum info nginx

yum install nginx

 

 

 

2.4   存儲服務器(storage server)安裝並配置nginx

2.4.1 安裝fastdfs-nginx-module

注:FastDFS通過Tracker服務器,將文件放在Storage服務器存儲,但是同組存儲服務器之間需要進入文件復制,有同步延遲的問題。假設Tracker服務器將文件上傳到了192.168.128.135,上傳成功后文件ID已經返回給客戶端。此時FastDFS存儲集群機制會將這個文件同步到同組存儲192.168.128.136,在文件還沒有復制完成的情況下,客戶端如果用這個文件ID在192.168.128.136上取文件,就會出現文件無法訪問的錯誤。而fastdfs-nginx-module可以重定向文件連接到源服務器取文件,避免客戶端由於復制延遲導致的文件無法訪問錯誤。

2.4.1.1  下載fastdfs-nginx-moudule

[root@fastdfs-storagetools]# wget http://nchc.dl.sourceforge.net/project/fastdfs/FastDFS%20Nginx%20Module%20Source%20Code/fastdfs-nginx-module_v1.16.tar.gz

[root@fastdfs-storage tools]# tar -zxvf fastdfs-nginx-module_v1.16.tar.gz

[root@fastdfs-storage tools]# cd fastdfs-nginx-module/src/

[root@fastdfs-storage tools]# vi config

編輯config文件,執行如下命令進行批量替換並保存退出

:%s+/usr/local/+/usr/+g

 

 

修改完成后

 

 

 

2.4.1.2  拷貝fastdfs-nginx-module模塊中配置文件到/etc/fdfs目錄中並編輯

[root@fastdfs-storage~]#cp /home/centos/fastdfs-nginx-module/src/mod_fastdfs.conf /etc/fdfs/

[root@fastdfs-storage ~]# vim /etc/fdfs/mod_fastdfs.conf

修改內容如下:

connect_timeout=10

base_path=/tmp(默認為/tmp)

tracker_server=192.168.128.135:22122

storage_server_port=23000(默認配置為23000)

url_have_group_name = true

store_path0=/data/fastdfs/storage

group_name=group1(默認配置為group1)

 

[group1]

group_name=group1

storage_server_port=23000

store_path_count=1

store_path0=/data/fastdfs/storage

2.4.1.3  nginx添加fastdfs-nginx-module模塊

查看nginx版本

[root@bogon centos]# nginx -V

 

 

[root@bogon centos]# wget http://nginx.org/download/nginx-1.12.1.tar.gz

[root@bogon centos]# tar -zvxf nginx-1.12.1.tar.gz

更新一下依賴相關包

[root@bogon nginx-1.12.1]# yum -y install pcre*

[root@bogon nginx-1.12.1]# service nginx stop

#編譯./configure 后面加上 第1步nginx -V查看到的所有配置參數,后面加上你要加的模塊配置

[root@bogon nginx-1.12.1]#./configure --prefix=/etc/nginx ……(第1步nginx-V內容)……--add-module=/home/centos/fastdfs-nginx-module

 

 

[root@bogon nginx-1.12.1]#make

[root@bogon nginx-1.12.1]#make install

注:報如下錯:

 

 

需要執行yum -y install openssl-devel

2.4.1.4  拷貝FastDFS中的部分配置文件到/etc/fdfs目錄中

[root@bogon conf]# cd /home/centos/fastdfs-5.10/conf

[root@bogon conf]# cp http.conf /etc/fdfs/

[root@bogon conf]# cp mime.types /etc/fdfs/

 

2.4.1.5  配置nginx,如下所示:

[root@bogon conf.d]# cd /etc/nginx/conf.d/

[root@bogon conf.d]# vi fdfs.conf

修改配置如下:

server {

    listen 8888;

    server_name  localhost;

    location ~ /group[0-9]/ {

        root /data/fastdfs/storage/data;

        ngx_fastdfs_module;

    }

   error_page   500 502 503 504  /50x.html;

   location = /50x.html {

        root   html;

   }

}

 

3     集群配置

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM