FastDFS是一個c語言編寫的一個開源的輕量級分佈式文件系統,它對文件進程管理,功能包括:文件存儲,文件同步,文件訪問(文件上傳、文件下載)等,解決了大容量存儲和負載均衡的問題,特別適合以文件爲載體的在線服務,如相冊網站,視頻網站等等,FastDFS爲互聯網量身定製,充分考慮了冗餘備份,負載均衡,現行擴容等,並注重高可用,高性能等指標,使用FastDFS很容易搭建一套高性能的文件服務器集羣提供文件上傳、下載服務。
一、FastDFS原理及架構詳解
1、FastDFS架構
FastDFS架構包括Tracker server和Storage server,client請求Tracker server 進行文件上傳、下載,通過Tracker server調度最終由Storage server完成文件上傳和下載。
Tracker server:負責調度及負載均衡,通過Tracker server,在文件上傳時可以根據一些策略找到Storage server來提供上傳服務,可以將tracker稱爲追蹤服務器或調度服務器;
Storage server:負責文件最終存儲,客戶端上傳的文件最終存儲在storage服務器上,Storage server沒有實現自己的文件系統,而是利用操作系統的文件系統來管理文件,可以將storage稱爲存儲服務器。
2、Fast DFS系統的角色
Fast DFS系統有三個角色:跟蹤服務器(Tracker Server)、存儲服務器(Storage Server)和客戶端(Client)。
Tracker Server:跟蹤服務器,主要做調度工作,起到均衡的作用;負責管理所有的Storage server和group,每個storage在啓動後會連接Tracker,告知自己所屬group等信息,並保持週期性心跳。
Storage Server:存儲服務器,主要提供容量和備份服務;以group爲單位,每個group內部可以有多臺storage server,數據互爲備份。
Client:客戶端,上傳下載數據的服務器,也就是我們自己的項目所部署在的服務器。
3、Fast DFS原理
關於模塊:跟蹤服務器和存儲節點都可以由一臺或多臺服務器構成,跟蹤服務器和存儲節點均可以隨時增加或者下線不會影響線上服務,其中跟蹤服務器中所有服務器是對 等,可以根據服務器壓力情況隨時增加或減少
4、文件上傳流程
Storage server會連接集羣中所有的Tracker server,定時向他們報告自己的狀態,包括磁盤剩餘空間、文件同步狀況、文件上傳下載次數等統計信息。
客戶端上傳文件後存儲服務器將文件ID返回給客戶端,此文件ID用於以後訪問該文件的索引信息。
文件索引信息包括:組名,虛擬磁盤路徑,數據兩級目錄,文件名。
組名:文件上傳後所在的storage組名稱,在文件上傳成功後有storage服務器返回,需要客戶端自行保存。
虛擬磁盤路徑:storage配置的虛擬路徑,與磁盤選項store_path*對應。如果配置了store_path0則是M00,如果配置了store_path1則是M01,以此類推。
數據兩級目錄:storage服務器在每個虛擬磁盤路徑下創建的兩級目錄,用於存儲數據文件。
文件名:與文件上傳時不同。是由存儲服務器根據特定信息生成,文件名包含:源存儲服務器IP地址、文件創建時間戳、文件大小、隨機數和文件拓展名等信息。
5、文件下載流程
client發送download請求給某個tracker,必須帶上文件名信息,tracker從文件名中解析出文件的group、大小、創建時間等信息,然後爲該請求選擇一個storage用來服務讀請求,由於group內的文件同步時在後臺是異步的,所以有可能出現在讀的時候,文件還沒有同步到某些storage server上,爲了儘量避免訪問到這樣的storage,tracker按照如下規則選擇group內可讀的storage:
文件創建時間戳-storage被同步到的時間戳 且(當前時間-文件創建時間戳)>文件同步最大時間(5分鐘),說明文件創建後,認爲經過最大同步時間後,肯定已經同步到其他storage了。
二、部署Fast DFS
1、環境如下
博文中所用軟件包均可在此(提取碼:koxu)下載
由於有一些重複性的安裝操作,所以找大佬寫了個腳本,可以在進行那些重複性操作時,執行這個腳本。在上述鏈接中就有此腳本
2、配置tracker1
[root@tracker1 ~]# mkdir fastdfs
[root@tracker1 ~]# cd fastdfs/
[root@tracker1 fastdfs]# ls
fastdfs.tar.gz install.sh libfastcommon.tar.gz
[root@tracker1 fastdfs]# sh install.sh # 執行腳本
tracker server install succees .......
[root@tracker1 fastdfs]# /etc/init.d/fdfs_trackerd start # 起服務
Reloading systemd: [ OK ]
Starting fdfs_trackerd (via systemctl): [ OK ]
[root@tracker1 fastdfs]# netstat -anput | grep 22122 # 確認端口在監聽
tcp 0 0 192.168.171.134:22122 0.0.0.0:* LISTEN 3847/fdfs_trackerd
3、配置tracker2
[root@tracker2 ~]# mkdir fastdfs
[root@tracker2 ~]# cd fastdfs/
[root@tracker2 fastdfs]# ls
fastdfs.tar.gz install.sh libfastcommon.tar.gz
[root@tracker2 fastdfs]# sh install.sh
tracker server install succees .......
[root@tracker2 fastdfs]# /etc/init.d/fdfs_trackerd start
Reloading systemd: [ OK ]
Starting fdfs_trackerd (via systemctl): [ OK ]
[root@tracker2 fastdfs]# netstat -anput | grep 22122
tcp 0 0 192.168.171.135:22122 0.0.0.0:* LISTEN 4276/fdfs_trackerd
4、配置storage1
[root@storage1 ~]# mkdir fastdfs
[root@storage1 ~]# cd fastdfs/
[root@storage1 fastdfs]# tar zxf libfastcommon.tar.gz
[root@storage1 fastdfs]# tar zxf fastdfs.tar.gz
[root@storage1 libfastcommon-1.0.43]# ./make.sh && ./make.sh install
[root@storage1 libfastcommon-1.0.43]# cd ../fastdfs-6.06/
[root@storage1 fastdfs-6.06]# ./make.sh && ./make.sh install
[root@storage1 fastdfs-6.06]# cd conf/
[root@storage1 conf]# cp mime.types http.conf /etc/fdfs/
[root@storage1 conf]# cd ../../
[root@storage1 fastdfs]# tar zxf fastdfs-nginx-module.tar.gz
[root@storage1 fastdfs]# cp fastdfs-nginx-module-1.22/src/mod_fastdfs.conf /etc/fdfs/
[root@storage1 fastdfs]# cd /etc/fdfs/
[root@storage1 fdfs]# mkdir -p /data/storage-fdfs/base # 建議分開放
[root@storage1 fdfs]# mkdir -p /data/storage-fdfs/store
[root@storage1 fdfs]# cp storage.conf.sample storage.conf
[root@storage1 fdfs]# vim storage.conf
group_name = group1
bind_addr =192.168.171.140
base_path = /data/storage-fdfs/base #數據和日誌目錄地址
store_path0 = /data/storage-fdfs/store # 存儲地
tracker_server = 192.168.171.134:22122 #以下是指定tracker-server的監聽地址
tracker_server = 192.168.171.135:22122
http.server_port = 8888 #http訪問文件的端口
[root@storage1 fdfs]# vim mod_fastdfs.conf
base_path=/data/storage-fdfs/base
tracker_server=192.168.171.134:22122
tracker_server=192.168.171.135:22122
storage_server_port=23000
group_name=group1
url_have_group_name = true #當group有多個的時候需要更改爲true,以組名去訪問
store_path0=/data/storage-fdfs/store
group_count = 2
[group1]
group_name=group1
storage_server_port=23000
store_path_count=1
store_path0=/data/storage-fdfs/store
[group2]
group_name=group2
storage_server_port=23000
store_path_count=1
store_path0=/data/storage-fdfs/store
#編譯安裝nginx並添加第三方模塊
[root@storage1 fdfs]# cd ~/fastdfs/
[root@storage1 fastdfs]# yum -y install pcre-devel openssl-devel
[root@storage1 fastdfs]# tar zxf nginx-1.16.1.tar.gz
#注意下面的第三方nginx模塊路徑,要與自己的對應
[root@storage1 fastdfs]# cd nginx-1.16.1/
[root@storage1 nginx-1.16.1]# ./configure --add-module=/root/fastdfs/fastdfs-nginx-module-1.22/src && make && make install
[root@storage1 nginx-1.16.1]# vim /usr/local/nginx/conf/nginx.conf
#在原來的server字段上面(34行)添加以下內容
server {
listen 8888;
server_name localhost;
location ~/group[0-9]/M00/ {
ngx_fastdfs_module;
}
}
[root@storage1 nginx-1.16.1]# /etc/init.d/fdfs_storaged start
Starting fdfs_storaged (via systemctl): [ 確定 ]
[root@storage1 nginx-1.16.1]# /usr/local/nginx/sbin/nginx
ngx_http_fastdfs_set pid=16712
#確定相關端口在監聽
[root@storage01 conf]# netstat -anput | grep 23000
[root@storage01 conf]# netstat -anpt | grep 80
[root@storage01 conf]# netstat -anpt | grep 8888
4、配置主機storage02
[root@storage2 fastdfs]# tar zxf libfastcommon.tar.gz
[root@storage2 fastdfs]# tar zxf fastdfs.tar.gz
[root@storage2 fastdfs]# cd libfastcommon-1.0.43/
[root@storage2 libfastcommon-1.0.43]# ./make.sh && ./make.sh install
[root@storage2 libfastcommon-1.0.43]# cd ../fastdfs-6.06/
[root@storage2 fastdfs-6.06]# ./make.sh && ./make.sh install
[root@storage2 fastdfs-6.06]# cd conf/
[root@storage2 conf]# cp mime.types http.conf /etc/fdfs/
[root@storage2 conf]# cd ../../
[root@storage2 fastdfs]# tar zxf fastdfs-nginx-module.tar.gz
[root@storage2 fastdfs]# cd /etc/fdfs/
#將storage01修改後的配置文件複製到本地
[root@storage2 fdfs]# scp [email protected]:/etc/fdfs/storage.conf /etc/fdfs/
[root@storage2 fdfs]# scp [email protected]:/etc/fdfs/mod_fastdfs.conf /etc/fdfs/
[root@storage2 fdfs]# vim storage.conf # 修改複製後的配置文件
group_name = group2
bind_addr =192.168.171.143
[root@storage2 fdfs]# vim mod_fastdfs.conf
group_name=group2
[root@storage2 fdfs]# mkdir -p /data/storage-fdfs/base
[root@storage2 fdfs]# mkdir -p /data/storage-fdfs/store
#安裝nginx
[root@storage2 fdfs]# cd ~/fastdfs/
[root@storage2 fastdfs]# tar zxf nginx-1.16.1.tar.gz
[root@storage2 fastdfs]# yum -y install pcre-devel openssl-devel
[root@storage2 nginx-1.16.1]# ./configure --add-module=/root/fastdfs/fastdfs-nginx-module-1.22/src/ && make && make install
[root@storage2 nginx-1.16.1]# cd /usr/local/nginx/conf/
#將storage01主機的配置文件複製過來即可
[root@storage2 conf]# scp [email protected]:/usr/local/nginx/conf/nginx.conf ./
[root@storage2 ~]# /etc/init.d/fdfs_storaged start
Starting fdfs_storaged (via systemctl): [ 確定 ]
[root@storage2 ~]# /usr/local/nginx/sbin/nginx
ngx_http_fastdfs_set pid=5694
[root@storage02 conf]# netstat -anput | grep 23000
[root@storage02 conf]# netstat -anpt | grep 80
[root@storage02 conf]# netstat -anpt | grep 8888
5、配置主機nginx提供反向代理功能
[root@nginx ~]# tar zxf nginx-1.16.1.tar.gz
[root@nginx ~]# yum -y install pcre-devel openssl-devel
[root@nginx ~]# cd nginx-1.16.1/
[root@nginx nginx-1.16.1]# ./configure && make && make install
[root@nginx nginx-1.16.1]# vim /usr/local/nginx/conf/nginx.conf
#找到http字段
http {
include mime.types;
default_type application/octet-stream;
#添加如下內容
upstream fdfs_group1 {
server 192.168.171.140:8888 weight=1 max_fails=2 fail_timeout=30s;
}
upstream fdfs_group2 {
server 192.168.171.143:8888 weight=1 max_fails=2 fail_timeout=30s;
}
#找到server字段
server {
listen 80;
server_name localhost
#charset koi8-r
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
#添加如下內容
location ~/group1 {
proxy_pass http://fdfs_group1;
}
location ~/group2 {
proxy_pass http://fdfs_group2;
}
[root@nginx nginx-1.16.1]# /usr/local/nginx/sbin/nginx
[root@nginx nginx-1.16.1]# netstat -anput |grep 80
6、客戶端驗證服務是否可以正常使用
[root@client ~]# mkdir fastdfs
[root@client ~]# cd fastdfs/
[root@client fastdfs]# tar zxf libfastcommon.tar.gz
[root@client fastdfs]# tar zxf fastdfs.tar.gz
[root@client fastdfs]# cd libfastcommon-1.0.43/
[root@client libfastcommon-1.0.43]# ./make.sh && ./make.sh install
[root@client libfastcommon-1.0.43]# cd ../fastdfs-6.06/
[root@client fastdfs-6.06]# ./make.sh && ./make.sh install
[root@client fastdfs-6.06]# cd /etc/fdfs/
[root@client fdfs]# cp client.conf.sample client.conf
[root@client fdfs]# vim /etc/fdfs/client.conf
base_path = /data/storage-fdfs/base
tracker_server = 192.168.171.134:22122
tracker_server = 192.168.171.135:22122
[root@client ~]# mkdir -p /data/storage-fdfs/store
[root@client ~]# ls test.jpg
test.jpg
[root@client ~]# fdfs_upload_file /etc/fdfs/client.conf test.jpg # 上傳
group2/M00/00/00/wKirj15-NhGAIeapAAFEF6rSzeI524.jpg
#上面返回的信息需要保存,訪問這個圖片時需要
[root@client ~]# fdfs_download_file /etc/fdfs/client.conf group2/M00/00/00/wKirj15-NhGAIeapAAFEF6rSzeI524.jpg b.jpg
[root@client ~]# ls b.jpg
b.jpg # 可以看到已經下載並且更改名稱了
瀏覽器訪問nginx反向代理+上傳圖片時返回的ID
最後附加幾條FastDFS命令
fdfs_upload_file Usage:
fdfs_upload_file <config_file> <local_filename> [storage_ip:port] [store_path_index]
用於上傳文件
用法爲 fdfs_upload_file + 配置文件 + 文件
fdfs_download_file Usage:
fdfs_download_file <config_file> <file_id> [local_filename] [<download_offset> <download_bytes>]
用於下載文件
用法爲 fdfs_download_file + 配置文件 + 文件
fdfs_file_info Usage:
fdfs_file_info <config_file> <file_id>
用於查看文件信息
用法爲 fdfs_file_info + 配置文件 + 文件
fdfs_delete_file Usage:
fdfs_delete_file <config_file> <file_id>
用於刪除文件
用法爲 fdfs_delete_file + 配置文件 + 文件
fdfs_monitor Usage:
fdfs_monitor <config_file> [-h <tracker_server>] [list|delete|set_trunk_server <group_name> [storage_id]]
用於查看集羣信息
用法爲 fdfs_monitor + 配置文件