1、背景描述php
以前公司的圖片服務器已經不能勝任了(直接使用tomcat),須要從新搭建圖片服務器,這兩天研究了下FastDFS,感受挺好用記錄下來以供往後參考。html
2、FastDFS官方介紹nginx
FastDFS是一款類Google FS的開源分佈式文件系統,它用純C語言實現,支持Linux、FreeBSD、AIX等UNIX系統。它只能經過 專有API對文件進行存取訪問,不支持POSIX接口方式,不能mount使用。準確地講,Google FS以及FastDFS、mogileFS、 HDFS、TFS等類Google FS都不是系統級的分佈式文件系統,而是應用級的分佈式文件存儲服務。 git
一、FastDFS架構圖github

圖1:FastDFS架構圖vim
1)Tracker cluster中各個tracker server相互獨立,不進行相互通訊。瀏覽器
2)Storage cluster中各個storage組(Volume1,Volume2...)相互獨立,不進行相互通訊,也就是說各個組之間保存的數據是不相同的。可是各個組中的storage server之間是屬於互相備份的關係,也就是說storage server之間保存相同的數據。tomcat
3)每一個storage server會啓動一個單獨的線程主動向Tracker cluster中每一個tracker server報告其狀態信息,包括磁盤使用狀況,文件同步狀況及文件上傳下載次數統計等信息。服務器
二、文件上傳和下載的時序圖架構

圖2:文件上傳時序圖
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,文件上傳結束。

圖3:文件下載時序圖
1)Client經過Tracker server下載指定Storage組中某個Storage server上的某個文件(文件名包括Storage組名稱)。
2)Tracker server向Client返回一臺可用的Storage server的IP地址和端口號。
3)Client直接經過Tracker server返回的IP地址和端口與其中一臺Storage server創建鏈接並進行文件下載。
3、安裝並配置FastDFS
一、運行環境及相關軟件
CentOS 6.5
FastDFS_v5.08.tar.gz
nginx-1.8.1.tar.gz
fastdfs-nginx-module_v1.16.tar.gz
libfastcommon
二、服務器規劃
服務器名稱 |
IP地址和端口 |
備註 |
fastdfs-tracker |
172.18.10.232:22122 |
跟蹤服務器/調度服務器 |
fastdfs-storage |
172.18.10.233:23000 |
存儲服務器 |
三、安裝FastDFS
1)首先建立工具目錄(非必須)
1 2 |
[root@fastdfs-storage ~] # mkdir -p /home/oldcat/tools [root@fastdfs-storage ~] # cd /home/oldcat/tools/ |
2) 下載並安裝FastDFS依賴包libfastcommon
1 2 3 4 5 6 7 |
[root@fastdfs-storage tools] # wget https://codeload.github.com/happyfish100/libfastcommon/zip/master [root@fastdfs-storage tools] # unzip master [root@fastdfs-storage tools] # cd libfastcommon-master/ [root@fastdfs-storage libfastcommon-master] # ls HISTORY INSTALL libfastcommon.spec make .sh php-fastcommon README src [root@fastdfs-storage libfastcommon-master] # ./make.sh [root@fastdfs-storage libfastcommon-master] # ./make.sh install |
3)下載並安裝FastDFS
1 2 3 4 |
[root@fastdfs-tracker tools] # wget http://jaist.dl.sourceforge.net/project/fastdfs/FastDFS%20Server%20Source%20Code/FastDFS%20Server%20with%20PHP%20Extension%20Source%20Code%20V5.08/FastDFS_v5.08.tar.gz [root@fastdfs-tracker tools] # tar xf FastDFS_v5.08.tar.gz [root@fastdfs-tracker tools] # cd FastDFS [root@fastdfs-tracker FastDFS] # ./make.sh && ./make.sh install |
採用默認方式安裝後的文件及目錄:
1)服務腳本:
1 2 3 |
[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 |
2)樣例配置文件
1 2 3 4 5 |
[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 |
3)命令行工具
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
[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 |
注意:雖然FastDFS區分tracker和storage服務器,可是安裝的軟件及步驟均相同,只是不一樣的配置文件而已,所以以上安裝適用tracker server和storage server
四、配置跟蹤服務器(tracker server)
1)拷貝tracker server和client端樣例配置文件並重命名
1 2 |
[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 |
2)編輯tracker server配置文件tracker.conf,須要修改內容以下:
1 2 3 |
disabled= false (默認爲 false ,表示是否無效) port=22122(默認爲22122) base_path= /data/fastdfs/tracker |
3)編輯client端的配置文件client.conf,須要修改內容以下
1 2 |
base_path= /data/fastdfs/tracker tracker_server=172.18.10.232:22122 |
4)建立tracker server數據目錄
1 |
[root@fastdfs-tracker ~] # mkdir -p /data/fastdfs/tracker |
5)測試啓動tracker server,啓動成功會自動在/data/fastdfs/tracker目錄新建data和logs目錄
1 2 3 4 5 6 7 8 |
[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 |
6)關閉tracker server
1 2 3 |
[root@fastdfs-tracker tracker] # /etc/init.d/fdfs_trackerd stop waiting for pid [3785] exit ... pid [3785] exit . |
五、配置存儲服務器(storage server)
1)拷貝storage server樣例配置文件並重命名
1 |
[root@fastdfs-storage ~] # cp /etc/fdfs/storage.conf.sample /etc/fdfs/storage.conf |
2)編輯storage server配置文件storage.conf,須要修改內容以下:
1 2 3 4 5 6 |
disabled= false (默認爲 false ,表示是否無效) port=23000(默認爲23000) base_path= /data/fastdfs/storage tracker_server=172.18.10.232:22122 store_path0= /data/fastdfs/storage http.server_port=8888(默認爲8888,nginx中配置的監聽端口那之一致) |
3)建立storage server數據目錄
1 |
[root@fastdfs-storage ~] # mkdir -p /data/fastdfs/storage |
4)測試啓動storage server,啓動成功會自動在/data/fastdfs/tracker目錄新建data和logs目錄(啓動storage server的前提是tracker server必須事先已啓動)
1 2 3 4 5 6 7 8 |
[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 |
六、文件上傳測試
執行以下上傳命令:
1 2 3 |
[root@fastdfs-tracker ~] # /usr/bin/fdfs_upload_file /etc/fdfs/client.conf /home/oldcat/imgs/test.jpg group1 /M00/00/00/rBIK6VcaP0aARXXvAAHrUgHEviQ394 .jpg 返回文件ID即說明文件已經上傳成功 |
4、存儲服務器(storage server)安裝並配置nginx
1)下載並安裝fastdfs-nginx-module模塊
注:FastDFS經過Tracker服務器,將文件放在Storage服務器存儲,可是同組存儲服務器之間須要進入文件複製,有同步延遲的問題。假設Tracker服務器將文件上傳到了192.168.4.125,上傳成功後文件ID已經返回給客戶端。此時FastDFS存儲集羣機制會將這個文件同步到同組存儲192.168.4.126,在文件尚未複製完成的狀況下,客戶端若是用這個文件ID在192.168.4.126上取文件,就會出現文件沒法訪問的錯誤。而fastdfs-nginx-module能夠重定向文件鏈接到源服務器取文件,避免客戶端因爲複製延遲致使的文件沒法訪問錯誤。
1 2 3 4 5 6 |
[root@fastdfs-storage tools] # 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 xf fastdfs-nginx-module_v1.16.tar.gz [root@fastdfs-storage tools] # cd fastdfs-nginx-module/src/ [root@fastdfs-storage src] # vim config 編輯config文件,執行以下命令進行批量替換並保存退出 :%s+ /usr/local/ + /usr/ +g |
2)拷貝fastdfs-nginx-module模塊中配置文件到/etc/fdfs目錄中並編輯
1 2 3 4 5 6 7 8 9 10 |
[root@fastdfs-storage ~] # cp /home/oldcat/tools/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=172.18.10.232:22122 storage_server_port=23000(默認配置爲23000) url_have_group_name = true store_path0= /data/fastdfs/storage group_name=group1(默認配置爲group1) |
3)安裝nginx依賴庫
1 |
[root@fastdfs-storage nginx-1.8.1] # yum install -y pcre-devel zlib-devel |
4)下載並安裝nginx
1 2 3 4 5 |
[root@fastdfs-storage tools] # wget http://nginx.org/download/nginx-1.8.1.tar.gz [root@fastdfs-storage tools] # tar xf nginx-1.8.1.tar.gz [root@fastdfs-storage tools] # cd nginx-1.8.1 [root@fastdfs-storage nginx-1.8.1] # ./configure --prefix=/application/nginx/ --add-module=../fastdfs-nginx-module/src/ [root@fastdfs-storage nginx-1.8.1] # make && make install |
5)拷貝FastDFS中的部分配置文件到/etc/fdfs目錄中
1 2 |
[root@fastdfs-storage ~] # cp /home/oldcat/tools/FastDFS/conf/http.conf /etc/fdfs/ [root@fastdfs-storage ~] # cp /home/oldcat/tools/FastDFS/conf/mime.types /etc/fdfs/ |
6)配置nginx,以下所示:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
[root@fastdfs-storage ~] # vim /application/nginx/conf/nginx.conf user root; 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; location ~ /group [0-9]/ { ngx_fastdfs_module; } error_page 500 502 503 504 /50x .html; location = /50x .html { root html; } } } |
說明:
a、"user root"是解決下載操做時報404的問題
b、8888端口號與/etc/fdfs/storage.conf中的http.server_port=8888相對應
c、storage對應有多個group的狀況下,訪問路徑帶group名稱,例如:/group1/M00/00/00/**,對應nginx配置:
location ~/group[0-9]/ {
ngx_fastdfs_module;
}
7)拷貝nginx服務到/etc/init.d/目錄下並啓動
1 2 3 4 |
[root@fastdfs-storage ~] # cp /application/nginx/sbin/nginx /etc/init.d/ [root@fastdfs-storage ~] # /etc/init.d/nginx [root@fastdfs-storage ~] # ss -lntup|grep 8888 tcp LISTEN 0 128 *:8888 *:* users :(( "nginx" ,7308,6),( "nginx" ,7309,6)) |
8)經過瀏覽器訪問以前已經上傳的文件
瀏覽器能夠成功訪問到上傳的圖片,說明使用FastDFS搭建的圖片服務器成功完成!