10-18FastDFS 環境搭建
一、前言
最近閒下來,整理了一下筆記,今天就分享一下 FastDFS 環境搭建吧。
二、介紹
# 2.1 FastDFS 定義
FastDFS 是用 C 語言編寫的一款開源的分散式檔案系統。FastDFS 為網際網路量身定製,充分考慮了冗餘備份、負載均衡、線性擴容等機制,並注重高可用、高效能等指標,使用 FastDFS 很容易搭建一套高效能的檔案伺服器叢集提供檔案上傳、下載等服務。
# 2.2 FastDFS 架構
FastDFS 架構包括 Tracker Server 和 Storage Server。客戶端請求 Tracker Server 進行檔案上傳、下載,通過 Tracker Server 排程最終由 Storage Server 完成檔案上傳和下載。
Tracker Server 作用是負載均衡和排程,通過 Tracker Server 在檔案上傳時可以根據一些策略找到 Storage server 提供檔案上傳服務。可以將 tracker 稱為追蹤伺服器或排程伺服器。
Storage Server 作用是檔案儲存,客戶端上傳的檔案最終儲存在 Storage 伺服器上,Storage Server 沒有實現自己的檔案系統而是利用作業系統 的檔案系統來管理檔案。可以將 storage 稱為儲存伺服器。
三、安裝
本次測試使用 ip 為 192.168.10.110 的 CentOS 7 系統的虛擬機器
# 3.1 安裝 libfastcommon
libfastcommon 是 FastDFS 官方提供的,其包含了 FastDFS 執行所需要的一些基礎庫。
- git clone https://github.com/happyfish100/libfastcommon.git
- cd libfastcommon
- ./make.sh
- ./make.sh install
# 3.2 安裝 FastDFS
- git clone https://github.com/happyfish100/fastdfs.git
- cd fastdfs
- ./make.sh
- ./make.sh install
安裝 FastDFS 成功後,會在如下目錄中生成相應的檔案。
- /etc/fdfs 生成配置檔案:
- [root@fastdfs fdfs]# pwd
- /etc/fdfs
- [root@fastdfs fdfs]# ll
- total 24
- -rw-r--r--. 1 root root 1461 Oct 16 22:26 client.conf.sample
- -rw-r--r--. 1 root root 7978 Oct 16 22:26 storage.conf.sample
- -rw-r--r--. 1 root root 105 Oct 16 22:26 storage_ids.conf.sample
- -rw-r--r--. 1 root root 7441 Oct 16 22:26 tracker.conf.sample
我們需要將“.simple”綴後刪除,這些配置檔案才能生效。
或者進入到 fdfs/conf 目錄中,如下:
- [root@fastdfs conf]# pwd
- /root/fastdfs/conf
- [root@fastdfs conf]# ll
- total 84
- -rw-r--r--. 1 root root 23981 Oct 16 22:19 anti-steal.jpg
- -rw-r--r--. 1 root root 1461 Oct 16 22:19 client.conf
- -rw-r--r--. 1 root root 955 Oct 16 22:19 http.conf
- -rw-r--r--. 1 root root 31172 Oct 16 22:19 mime.types
- -rw-r--r--. 1 root root 7978 Oct 16 22:19 storage.conf
- -rw-r--r--. 1 root root 105 Oct 16 22:19 storage_ids.conf
- -rw-r--r--. 1 root root 7441 Oct 16 22:19 tracker.conf
- [root@fastdfs conf]#
該目錄也生成好 FastDFS 執行時所需要的配置檔案,我們也可以將這些檔案一併拷貝到 /etc/fdfs 中。
- /usr/bin 中生成對應的執行命令:
- [root@fastdfs bin]# pwd
- /usr/bin
- [root@fastdfs bin]# ll fdfs*
- -rwxr-xr-x. 1 root root 317632 Oct 16 22:26 fdfs_appender_test
- -rwxr-xr-x. 1 root root 317408 Oct 16 22:26 fdfs_appender_test1
- -rwxr-xr-x. 1 root root 304264 Oct 16 22:26 fdfs_append_file
- -rwxr-xr-x. 1 root root 304008 Oct 16 22:26 fdfs_crc32
- -rwxr-xr-x. 1 root root 304320 Oct 16 22:26 fdfs_delete_file
- -rwxr-xr-x. 1 root root 305048 Oct 16 22:26 fdfs_download_file
- -rwxr-xr-x. 1 root root 304648 Oct 16 22:26 fdfs_file_info
- -rwxr-xr-x. 1 root root 322560 Oct 16 22:26 fdfs_monitor
- -rwxr-xr-x. 1 root root 1112112 Oct 16 22:26 fdfs_storaged
- -rwxr-xr-x. 1 root root 327576 Oct 16 22:26 fdfs_test
- -rwxr-xr-x. 1 root root 326784 Oct 16 22:26 fdfs_test1
- -rwxr-xr-x. 1 root root 454944 Oct 16 22:26 fdfs_trackerd
- -rwxr-xr-x. 1 root root 305248 Oct 16 22:26 fdfs_upload_appender
- -rwxr-xr-x. 1 root root 306272 Oct 16 22:26 fdfs_upload_file
四、配置
首先建立 4 個目錄:
- mkdir -p /data/fastdfs/tracker
- mkdir -p /data/fastdfs/storage
- mkdir -p /data/fastdfs/client
- mkdir -p /data/fastdfs/tmp
這些目錄用於存放 fastDFS 服務產生的資料檔案和日誌。
為了簡化步驟,進入到 fdfs/conf 目錄,將裡邊的所有檔案複製到 /etc/fdfs 中:
- cp /root/fastdfs/conf/* /etc/fdfs
# 4.1 配置 Tracker Server
修改 tracker.conf 檔案:vim /etc/fdfs/tracker.conf
- port=22122
- base_path=/data/fastdfs/tracker
啟動 Tracker Server 服務:
- /usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf start
檢測啟動情況:
- [root@fastdfs fdfs]# ps -ef | grep tracker
- root 2136 1 0 23:55 ? 00:00:00 /usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf start
- root 2147 1294 0 23:55 pts/0 00:00:00 grep --color=auto tracker
# 4.2 配置 Storage Server
修改 storage.conf 檔案:vim /etc/fdfs/storage.conf
- port=23000
- base_path=/data/fastdfs/storage
- store_path0=/data/fastdfs/storage
- tracker_server=192.168.10.110:22122
- group_name=light
- http.server_port=80
注意:tracker_server 中,ip 為本機 ip,埠為 tracker.conf 中設定的值
啟動 Storage Server 服務:
- /usr/bin/fdfs_storaged /etc/fdfs/storage.conf start
檢測啟動情況:
- [root@fastdfs fdfs]# ps -ef | grep storage
- root 2155 1 10 00:00 ? 00:00:01 /usr/bin/fdfs_storaged /etc/fdfs/storage.conf start
- root 2167 1294 0 00:00 pts/0 00:00:00 grep --color=auto storage
# 4.3 配置 client
此步驟的配置是為了測試
修改 client.conf 檔案:vim /etc/fdfs/client.conf
- base_path=/data/fastdfs/client
- tracker_server=192.168.10.110:22122
五、測試
準備圖片,名為 test.jpg
- [root@fastdfs ~]# pwd
- /root
- [root@fastdfs ~]# ll
- total 52
- drwxr-xr-x. 11 root root 270 Oct 16 22:19 fastdfs
- drwxr-xr-x. 6 root root 147 Oct 16 22:22 libfastcommon
- -rw-r--r--. 1 root root 49608 Oct 17 01:51 test.jpg
通過 fdfs 命令上傳該圖片:
- /usr/bin/fdfs_test /etc/fdfs/client.conf upload /root/test.jpg
最終,圖片會被上傳到 /data/fastdfs/storage/data/ 目錄中。
執行結果如下圖:
不過目前該圖片無法通過瀏覽器訪問,因為我們還沒有設定 web 伺服器將外部請求與本地檔案系統中的目錄的對映關聯。最簡單的實現方式就是整合 nginx 伺服器來實現 http 訪問請求,該功能將在下篇文章介紹。