1、背景
最近由于某些原因接觸到了分布式存儲(chǔ),而像阿里云的OSS等都是需要付費(fèi)的,那么有沒有免費(fèi)的呢?fastdfs就是一個(gè)免費(fèi)的,此處記錄一下如何搭建一個(gè)單機(jī)版的 fastdfs 環(huán)境。
2、fastdfs的一些知識(shí)
2.1 fastdfs的特點(diǎn)
- 分組存儲(chǔ),靈活簡(jiǎn)潔
- 對(duì)等結(jié)構(gòu),不存在單點(diǎn)
- 文件ID由FastDFS生成,作為文件訪問憑證。FastDFS不需要傳統(tǒng)的name server
- 和流行的web server無(wú)縫銜接,F(xiàn)astDFS已提供apache和nginx擴(kuò)展模塊
- 大、中、小文件均可以很好支持,支持海量小文件存儲(chǔ)
- 支持多塊磁盤,支持單盤數(shù)據(jù)恢復(fù)
- 支持相同文件內(nèi)容只保存一份,節(jié)省存儲(chǔ)空間
- 存儲(chǔ)服務(wù)器上可以保存文件附加屬性
- 下載文件支持多線程方式,支持?jǐn)帱c(diǎn)續(xù)傳
2.2 架構(gòu)圖

2.2.1 client 介紹
客戶端,即文件上傳或下載的服務(wù)器,也就是我們自己項(xiàng)目部署的服務(wù)器。
2.2.2 tracker-server 介紹
-
tracker-server是跟蹤服務(wù)器,主要起調(diào)度作用。負(fù)責(zé)管理所有storage server的元數(shù)據(jù)信息,比如:storage ip、port、group等信息。每個(gè)storage server在啟動(dòng)的時(shí)候,會(huì)向tracker server連接,上報(bào)自己的元數(shù)據(jù)信息,并與之保持周期性的心跳。 -
tracker-server之間通常不會(huì)相互通訊
2.2.3 storage-server 介紹
-
storage-server是存儲(chǔ)服務(wù)器,主要用來(lái)存儲(chǔ)各種文件。 -
storage-server是以Group為單位,每個(gè)Group內(nèi)可以有多臺(tái)storage server,同一個(gè)組內(nèi)多個(gè)storage server為互為備份關(guān)系。 - 每個(gè)
Group中的機(jī)器的存儲(chǔ)建議配置成一樣大,否則以這個(gè)Group內(nèi)容量最小的storage為準(zhǔn)。 - 由
storage server主動(dòng)向tracker server報(bào)告狀態(tài)信息
2.3 fastdfs需要安裝的軟件
| 依賴庫(kù) | 備注 |
|---|---|
| libfatscommon | 基礎(chǔ)庫(kù)-fastdfs分離出來(lái)的公共函數(shù) |
| libserverframe | 基礎(chǔ)庫(kù)-網(wǎng)絡(luò)框架庫(kù) |
| FastDFS | fastdfs本地 |
| fastdfs-nginx-module | fastdfs與nginx整合模塊 |
| nginx | nginx |
2.4 為什么需要fastdfs-nginx-module
其實(shí)這個(gè)主要是因?yàn)?,同一個(gè)group中存在多個(gè)storage server,而多個(gè)storage server之間會(huì)存在文件同步,那么可能就會(huì)出現(xiàn) 同步延遲 問題。
比如:
我們存在2臺(tái)storage server,分別是 storageA 和 storageB。
FastDFS通過Tracker服務(wù)器,將文件放入到了storageA服務(wù)器存儲(chǔ)。上傳成功后將 文件ID 返回給客戶端。此時(shí)FastDFS存儲(chǔ)集群機(jī)制會(huì)將這個(gè)文件同步到同組存儲(chǔ)storageB,在文件還沒有復(fù)制完成的情況下,客戶端如果用這個(gè)文件ID在storageB上取文件,就會(huì)出現(xiàn)文件無(wú)法訪問的錯(cuò)誤。而fastdfs-nginx-module可以重定向文件連接到源服務(wù)器取文件,避免客戶端由于同步延遲導(dǎo)致的文件無(wú)法訪問錯(cuò)誤。
2.5 fastdfs的一些資料
- fastdfs 源碼地址 https://github.com/happyfish100/
- fastdfs官方論壇 http://bbs.chinaunix.net/forum.php?mod=forumdisplay&fid=240&filter=typeid&typeid=424
- fastdfs 配置文件解釋 http://bbs.chinaunix.net/thread-1941456-1-1.html
- fastdfs ppt介紹 http://bbs.chinaunix.net/thread-1958475-1-1.html
- fastdfs faq http://bbs.chinaunix.net/thread-1920470-1-1.html
3、fastdfs軟件安裝
3.1 前置條件
3.1.1 依賴庫(kù)安裝
yum install git gcc gcc-c++ make automake autoconf libtool pcre pcre-devel zlib zlib-devel openssl-devel -y
3.1.2 fastdfs源碼所在目錄
[root@fastdfs fastdfs]# pwd
/opt/fastdfs
[root@fastdfs fastdfs]# ls
fastdfs-6.09.tar.gz fastdfs-nginx-module-1.23.tar.gz libfastcommon-1.0.61.tar.gz libserverframe-1.1.20.tar.gz nginx-1.22.0.tar.gz
[root@fastdfs fastdfs]#
3.1.3 tracker 和 storage server
此處因?yàn)槭莊astdfs單機(jī)安裝,因此 tracker server 和 storage server 安裝到同一臺(tái)機(jī)器上。
3.2 安裝基礎(chǔ)庫(kù)-libfatscommon
[root@fastdfs fastdfs]# cd /opt/fastdfs
[root@fastdfs fastdfs]# tar -zxvf libfastcommon-1.0.61.tar.gz
[root@fastdfs fastdfs]# cd libfastcommon-1.0.61
[root@fastdfs libfastcommon-1.0.61]# ./make.sh && ./make.sh install
[root@fastdfs fdfs]# ls /usr/lib64 | grep libfastcommon.so
libfastcommon.so
[root@fastdfs fdfs]# ls /usr/lib | grep libfastcommon.so
libfastcommon.so
3.3 安裝基礎(chǔ)庫(kù)-libserverframe
[root@fastdfs fastdfs]# cd /opt/fastdfs
[root@fastdfs fastdfs]# tar -zxvf libserverframe-1.1.20.tar.gz
[root@fastdfs fastdfs]# cd libserverframe-1.1.20
[root@fastdfs libfastcommon-1.0.61]# ./make.sh && ./make.sh install
[root@fastdfs fdfs]# ls /usr/lib | grep libserverframe.so
libserverframe.so
[root@fastdfs fdfs]# ls /usr/lib64 | grep libserverframe.so
libserverframe.so
3.4 安裝fastdfs
[root@fastdfs fastdfs]# cd /opt/fastdfs
[root@fastdfs fastdfs]# tar -zxvf fastdfs-6.09.tar.gz
[root@fastdfs fastdfs]# cd fastdfs-6.09
[root@fastdfs fastdfs-6.09]# ./make.sh && ./make.sh install
3.4.1 默認(rèn)配置文件位置
[root@fastdfs fdfs]# cd /etc/fdfs/
[root@fastdfs fdfs]# ll
total 32
-rw-r--r--. 1 root root 1909 Oct 6 22:30 client.conf
-rw-r--r--. 1 root root 10246 Oct 6 22:30 storage.conf
-rw-r--r--. 1 root root 620 Oct 6 22:30 storage_ids.conf
-rw-r--r--. 1 root root 9138 Oct 6 22:30 tracker.conf
[root@fastdfs fdfs]#
3.4.2 默認(rèn)命令工具
[root@fastdfs bin]# cd /usr/bin/ && ll | grep fdfs
-rwxr-xr-x. 1 root root 438264 Oct 6 22:30 fdfs_appender_test
-rwxr-xr-x. 1 root root 438048 Oct 6 22:30 fdfs_appender_test1
-rwxr-xr-x. 1 root root 429064 Oct 6 22:30 fdfs_append_file
-rwxr-xr-x. 1 root root 427736 Oct 6 22:30 fdfs_crc32
-rwxr-xr-x. 1 root root 429088 Oct 6 22:30 fdfs_delete_file
-rwxr-xr-x. 1 root root 429816 Oct 6 22:30 fdfs_download_file
-rwxr-xr-x. 1 root root 429848 Oct 6 22:30 fdfs_file_info
-rwxr-xr-x. 1 root root 440816 Oct 6 22:30 fdfs_monitor
-rwxr-xr-x. 1 root root 429344 Oct 6 22:30 fdfs_regenerate_filename
-rwxr-xr-x. 1 root root 1364800 Oct 6 22:30 fdfs_storaged
-rwxr-xr-x. 1 root root 444152 Oct 6 22:30 fdfs_test
-rwxr-xr-x. 1 root root 443336 Oct 6 22:30 fdfs_test1
-rwxr-xr-x. 1 root root 571736 Oct 6 22:30 fdfs_trackerd
-rwxr-xr-x. 1 root root 430048 Oct 6 22:30 fdfs_upload_appender
-rwxr-xr-x. 1 root root 431080 Oct 6 22:30 fdfs_upload_file
[root@fastdfs bin]#
3.4.3 fastdfs默認(rèn)啟動(dòng)腳本
[root@fastdfs init.d]# cd /opt/fastdfs/fastdfs-6.09/init.d/ && ll
total 8
-rwxrwxr-x. 1 root root 961 Sep 14 16:33 fdfs_storaged
-rwxrwxr-x. 1 root root 963 Sep 14 16:33 fdfs_trackerd
[root@fastdfs init.d]#
3.5 創(chuàng)建fastdfs數(shù)據(jù)根目錄
mkdir -p /data/fastdfs
mkdir -p /data/fastdfs/tracker
mkdir -p /data/fastdfs/storage
mkdir -p /data/fastdfs/client
3.6 配置tracker server
3.6.1 修改配置文件
vim /etc/fdfs/tracker.conf
# 配置文件是否不生效 false為生效
disabled = false
# tracker 服務(wù)的端口
port = 22122
# tracker 存儲(chǔ)數(shù)據(jù)和日志文件的根目錄,根目錄需要提前創(chuàng)建好
base_path = /data/fastdfs/tracker
3.6.2 防火墻放行端口
[root@fastdfs fdfs]# firewall-cmd --zone=public --add-port=22122/tcp --permanent
success
[root@fastdfs fdfs]# firewall-cmd --reload
success
[root@fastdfs fdfs]#
3.6.3 啟動(dòng)tracker server
1、復(fù)制啟動(dòng)腳本
[root@fastdfs logs]# cp /opt/fastdfs/fastdfs-6.09/init.d/fdfs_trackerd /etc/init.d/
2、啟動(dòng)tracker server
[root@fastdfs logs]# systemctl start fdfs_trackerd
3、檢測(cè)tracker server 是否啟動(dòng)
[root@fastdfs logs]# ps aux | grep fd
root 6686 0.0 0.3 79556 5900 ? Sl 05:38 0:00 /usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf start
root 6694 0.0 0.1 106180 1892 pts/0 R+ 05:39 0:00 grep --color=auto fd
4、檢測(cè)22122端口是否被監(jiān)聽
[root@fastdfs logs]# netstat -anp | grep 22122
tcp 0 0 0.0.0.0:22122 0.0.0.0:* LISTEN 6686/fdfs_trackerd
5、關(guān)閉tracker server
[root@fastdfs logs]# /etc/init.d/fdfs_trackerd stop
Stopping fdfs_trackerd (via systemctl): [ OK ]
3.7 配置storage server
3.7.1 修改配置文件
/etc/fdfs/storage.conf
# 配置文件是否不生效,false 為生效
disabled=false
# 指定此 storage server 所在 組
group_name=group1
# storage server 服務(wù)的端口
port=23000
# Storage 數(shù)據(jù)和日志目錄地址,根目錄必須提前創(chuàng)建好
base_path=/data/fastdfs/storage
# 文件存儲(chǔ)路徑的個(gè)數(shù)。
store_path_count=1
# 配置 store_path_count 個(gè)路徑,索引號(hào)從 0 開始。
# 如果不配置 store_path0,那它就和 base_path 的路徑一樣。
store_path0=/data/fastdfs/storage
# FastDFS 存儲(chǔ)文件時(shí),采用了兩級(jí)目錄。這里配置存放文件的目錄個(gè)數(shù)。
# tracker_server 的列表 ,會(huì)主動(dòng)連接 tracker_server
# 當(dāng)存在多個(gè) tracker server 時(shí),每個(gè) tracker server 寫一行
tracker_server=192.168.121.137:22122
# 此存儲(chǔ)服務(wù)器上web服務(wù)器的端口
http.server_port=8888
3.6.2 防火墻放行端口
[root@fastdfs fdfs]# firewall-cmd --zone=public --add-port=23000/tcp --permanent
success
[root@fastdfs fdfs]# firewall-cmd --zone=public --add-port=8888/tcp --permanent
success
[root@fastdfs fdfs]# firewall-cmd --reload
success
[root@fastdfs fdfs]#
3.6.3 啟動(dòng)storage server
1、復(fù)制啟動(dòng)腳本
[root@fastdfs logs]# cp /opt/fastdfs/fastdfs-6.09/init.d/fdfs_storaged /etc/init.d/
2、啟動(dòng)storage server
[root@fastdfs logs]# systemctl start fdfs_storaged
3、檢測(cè)storage server 是否啟動(dòng)
[root@fastdfs logs]# ps aux | grep fd
ps aux | grep fd
root 6723 0.0 0.6 145092 8980 ? Sl 05:55 0:00 /usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf start
root 6774 1.8 4.5 139948 68364 ? Sl 06:12 0:00 /usr/bin/fdfs_storaged /etc/fdfs/storage.conf start
root 6785 0.0 0.1 106180 1864 pts/0 R+ 06:12 0:00 grep --color=auto fd
4、檢測(cè)23000端口是否被監(jiān)聽
[root@fastdfs logs]# netstat -anp | grep 23000
tcp 0 0 0.0.0.0:22122 0.0.0.0:* LISTEN 6686/fdfs_trackerd
5、關(guān)閉storage server
[root@fastdfs logs]# /etc/init.d/fdfs_storaged stop
Stopping fdfs_trackerd (via systemctl): [ OK ]
3.8 查看storage和tracker是否在通訊
/usr/bin/fdfs_monitor /etc/fdfs/storage.conf

3.9 文件上傳測(cè)試
3.9.1 修改配置文件
vim /etc/fdfs/client.conf
# 存儲(chǔ)client的日志文件
base_path = /data/fastdfs/client
# tracker服務(wù)器的地址,多個(gè)寫多行
tracker_server = 192.168.121.137:22122
3.9.2 上傳文件
[root@fastdfs fastdfs]# /usr/bin/fdfs_upload_file /etc/fdfs/client.conf /opt/fastdfs/小紅帽.jpg
group1/M00/00/00/wKh5iWM_VlCAUwAtAAAwWD4VeAg204.jpg
[root@fastdfs fastdfs]#
[root@fastdfs 00]# pwd
/data/fastdfs/storage/data/00/00
[root@fastdfs 00]# ls
wKh5iWM_VlCAUwAtAAAwWD4VeAg204.jpg
[root@fastdfs 00]#
文件id解釋

3.10 storage服務(wù)安裝nginx
[root@fastdfs fastdfs]# tar -zxf nginx-1.22.0.tar.gz
[root@fastdfs fastdfs]# cd nginx-1.22.0
[root@fastdfs fastdfs]# ./configure
[root@fastdfs fastdfs]# make
[root@fastdfs fastdfs]# make install
[root@fastdfs sbin]# /usr/local/nginx/sbin/nginx -v
nginx version: nginx/1.22.0
1、啟動(dòng)nginx
[root@fastdfs sbin]# /usr/local/nginx/sbin/nginx
[root@fastdfs sbin]# ps aux | grep nginx
root 9460 0.0 0.1 4436 1528 ? Ss 06:40 0:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody 9461 0.0 0.1 4880 2260 ? S 06:40 0:00 nginx: worker process
root 9463 0.0 0.1 106180 1884 pts/0 R+ 06:40 0:00 grep --color=auto nginx
2、停止nginx
[root@fastdfs sbin]# /usr/local/nginx/sbin/nginx -s stop
[root@fastdfs sbin]# ps aux | grep nginx
root 9466 0.0 0.1 106180 1860 pts/0 R+ 06:41 0:00 grep --color=auto nginx
3.11 安裝fastdfs-nginx-module
3.11.1 安裝
[root@fastdfs sbin]# /usr/local/nginx/sbin/nginx -s stop
[root@fastdfs sbin]# cd /opt/fastdfs/
[root@fastdfs sbin]# tar -zxf fastdfs-nginx-module-1.23.tar.gz
[root@fastdfs sbin]# cd /opt/fastdfs/nginx-1.22.0
[root@fastdfs sbin]# ./configure --add-module=../fastdfs-nginx-module-1.23/src/
[root@fastdfs sbin]# make && make install
[root@fastdfs nginx-1.22.0]# /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.22.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
configure arguments: --add-module=../fastdfs-nginx-module-1.23/src/
3.11.2 復(fù)制mod_fastdfs.conf文件
復(fù)制 fastdfs-nginx-module-1.23 源碼中的配置文件到/etc/fdfs 目錄
cp /opt/fastdfs/fastdfs-nginx-module-1.23/src/mod_fastdfs.conf /etc/fdfs/
3.11.3 編輯mod_fastdfs.conf文件
vim /etc/fdfs/mod_fastdfs.conf
# tracker server 的地址,多個(gè)寫多行
tracker_server=192.168.121.137:22122
# storage server的端口
storage_server_port=23000
# storage server的組名
group_name=group1
# url上是否有組名
url_have_group_name = true
# 和 storage.conf配置文件中配置的一樣
store_path_count=1
# 和 storage.conf配置文件中配置的一樣
store_path0=/data/fastdfs/storage
3.11.5 拷貝mime.types和http.conf文件
[root@fastdfs nginx-1.22.0]# cp /opt/fastdfs/fastdfs-6.09/conf/mime.types /etc/fdfs/
[root@fastdfs nginx-1.22.0]# cp /opt/fastdfs/fastdfs-6.09/conf/http.conf /etc/fdfs/
不拷貝的話,可能文件不能訪問
3.11.4 編輯nginx配置文件
vim /usr/local/nginx/conf/nginx.conf
server {
listen 8888;
server_name 192.168.121.137;
location ~/group([0-9])/ {
ngx_fastdfs_module;
}
}
此處的8888和/etc/fdfs/storage.conf中的http.server_port=8888值要一致。
3.11.5 啟動(dòng)nginx
[root@fastdfs nginx-1.22.0]# /usr/local/nginx/sbin/nginx
ngx_http_fastdfs_set pid=12113
3.11.6 訪問之前上傳的文件
