FastDFS集羣搭建與使用(CentOS 7)

部署

環境準備

集羣架構

服務器 IP 服務(端口)
服務器1 192.168.2.130 tracker0(22122)
服務器2 192.168.2.150 tracker1(22122)
服務器3 192.168.2.124 storage0(23000)、nginx(8888)
服務器4 192.168.2.119 storage1(23000)、nginx(8888)
對外開放的服務器 公網IP/域名 nginx(80/443)

所需文件

文件 描述 下載
libfastcommon_V1.0.43.tar.gz FastDFS分離出的一些公用函數包 下載
fastdfs_V6.06.tar.gz FastDFS本體 下載
fastdfs-nginx-module_V1.22.tar.gz FastDFS和nginx的關聯模塊,解決組內同步延遲問題 下載
nginx-1.16.1.tar.gz 下載

官方文檔

服務器1:Tracker0

1. 安裝編譯環境

yum install git gcc gcc-c++ make automake autoconf libtool pcre pcre-devel zlib zlib-devel openssl-devel wget vim -y

2. 安裝libfastcommon

tar -zxvf libfastcommon_V1.0.43.tar.gz
cd libfastcommon-1.0.43/
./make.sh
./make.sh install
cd ..

3. 安裝FastDFS

安裝

tar -zxvf fastdfs_V6.06.tar.gz
cd fastdfs-6.06/
./make.sh
./make.sh install
cd ..

配置

tracker.conf
cp /etc/fdfs/tracker.conf.sample /etc/fdfs/tracker.conf
vim /etc/fdfs/tracker.conf
#需要修改的內容如下
port=22122 # tracker服務器端口(默認22122,一般不修改)
base_path=/data/fastdfs # 存儲日誌和數據的根目錄
use_storage_id = true # 使用id來區分storage,方便遷移
storage_ids_filename = storage_ids.conf
id_type_in_filename = id

確保配置文件的目錄都存在

mkdir -p /data/fastdfs
storage_ids.conf
cp /etc/fdfs/storage_ids.conf.sample /etc/fdfs/storage_ids.conf
vim /etc/fdfs/storage_ids.conf
# 將storage的ip添加進去
100001   group1  172.18.0.2
100002   group1  172.18.0.3

啓動

# 啓動
fdfs_trackerd /etc/fdfs/tracker.conf start
# 停止
fdfs_trackerd /etc/fdfs/tracker.conf stop
# 重啓
fdfs_trackerd /etc/fdfs/tracker.conf restart

服務器2:Tracker1

部署方式同服務器1

服務器3:Storage0 + Nginx

1. 安裝編譯環境

yum install git gcc gcc-c++ make automake autoconf libtool pcre pcre-devel zlib zlib-devel openssl-devel wget vim -y

2. 安裝libfastcommon

tar -zxvf libfastcommon_V1.0.43.tar.gz
cd libfastcommon-1.0.43/
./make.sh
./make.sh install
cd ..

3. 安裝FastDFS

安裝

tar -zxvf fastdfs_V6.06.tar.gz
cd fastdfs-6.06/
./make.sh
./make.sh install
cd ..

配置

storage.conf
cp /etc/fdfs/storage.conf.sample /etc/fdfs/storage.conf
vim /etc/fdfs/storage.conf
#需要修改的內容如下
port=9000 # storage服務端口(默認23000,一般不修改)
base_path=/data/fastdfs # 數據和日誌文件存儲根目錄
store_path0=/data/fastdfs/store_path0 # 文件存儲目錄,可根據磁盤設置
tracker_server = 192.168.2.130:22122 # tracker服務器IP和端口
tracker_server = 192.168.2.150:22122 # tracker服務器IP和端口
http.server_port=8888 # http訪問文件的端口(默認8888,看情況修改,和nginx中保持一致)

確保配置文件中設置的目錄都存在

mkdir -p /data/fastdfs
mkdir -p /data/fastdfs/store_path0

啓動

# 啓動
fdfs_storaged /etc/fdfs/storage.conf start
# 停止
fdfs_storaged /etc/fdfs/storage.conf stop
# 重啓
fdfs_storaged /etc/fdfs/storage.conf restart
#查看集羣狀態
fdfs_monitor /etc/fdfs/storage.conf list

4. 安裝Nginx

安裝

tar -zxvf fastdfs-nginx-module_V1.22.tar.gz
tar -zxvf nginx-1.16.1.tar.gz
cd nginx-1.16.1
./configure --add-module=../fastdfs-nginx-module-1.22/src/
make
make install
cd ..

配置

mod_fastdfs.conf
cp fastdfs-6.06/conf/http.conf /etc/fdfs/
cp fastdfs-6.06/conf/mime.types /etc/fdfs/
cp fastdfs-nginx-module-1.22/src/mod_fastdfs.conf /etc/fdfs/
vim /etc/fdfs/mod_fastdfs.conf
#需要修改的內容如下
tracker_server = 192.168.2.130:22122 # tracker服務器IP和端口
tracker_server = 192.168.2.150:22122 # tracker服務器IP和端口
storage_server_port=23000
url_have_group_name=true
store_path0=/data/fastdfs/store_path0
nginx.config
vim /usr/local/nginx/conf/nginx.conf
#添加如下配置
server {
    listen       8888;    ## 該端口爲storage.conf中的http.server_port相同
    server_name  localhost;
    location ~/group1/ {
        ngx_fastdfs_module;
    }
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
    root   html;
    }
}

啓動

# 啓動
/usr/local/nginx/sbin/nginx
# 停止
/usr/local/nginx/sbin/nginx -s stop
# 重啓
/usr/local/nginx/sbin/nginx -s reload

服務器4:Storage1+ Nginx

部署方式同服務器3

服務器1-4:Client(僅供測試)

可在任意安裝了FastDFS上的服務器上運行測試

配置

cp /etc/fdfs/client.conf.sample /etc/fdfs/client.conf
vim /etc/fdfs/client.conf
#需要修改的內容如下
base_path=/data/fastdfs_client # 數據和日誌文件存儲根目錄
tracker_server = 192.168.2.130:22122 # tracker服務器IP和端口
tracker_server = 192.168.2.150:22122 # tracker服務器IP和端口
use_storage_id = true # 使用id來區分storage,方便遷移

確保配置文件中的目錄都存在

mkdir -p /data/fastdfs_client

啓動

# 測試上傳,上傳成功後返回文件路徑:group1/M00/00/00/rBIAAV6ZN62AKxKOAA_ZI6CZBeI.tar.gz
fdfs_upload_file /etc/fdfs/client.conf /root/fastdfs/nginx-1.17.8.tar.gz

配置外部訪問的Nginx

參考配置

upstream fdfs_group01 {
        server 192.168.2.124:8888 weight=1 max_fails=2 fail_timeout=30s;
        server 192.168.2.119:8888 weight=1 max_fails=2 fail_timeout=30s;
    }

server {
   listen       80;
   server_name  localhost;
   
   location / {
       root   html;
       index  index.html index.htm;
   }

   location /files {
       proxy_next_upstream http_502 http_504 error timeout invalid_header;
       proxy_pass http://fdfs_group01/;
       expires 30d;
   }
}

嘗試在瀏覽器中訪問

http://127.0.0.1/files/group1/M00/00/00/rBIAAV6ZN62AKxKOAA_ZI6CZBeI.tar.gz

這裏127.0.0.1應換成外網可訪問的nginx的地址

一切正常時,會彈出下載框

開啓token防盜鏈

開啓後訪問文件需要一個定期失效的token,以防止鏈接被濫用。

配置

vim /etc/fdfs/http.conf
# 需要配置的內容如下
# if use token to anti-steal
# default value is false (0)   
# 是否做token檢查,缺省值爲false。
http.anti_steal.check_token=true
 
# token TTL (time to live), seconds
# default value is 600
# TTL,即生成token的有效時長(秒)
http.anti_steal.token_ttl=3600
 
# secret key to generate anti-steal token
# this parameter must be set when http.anti_steal.check_token set to true
# the length of the secret key should not exceed 128 bytes
# 生成token的密鑰,儘量設置得長一些,千萬不要泄露出去
http.anti_steal.secret_key=14789632
 
# return the content of the file when check token fail
# default value is empty (no file sepecified)
# 檢查失敗,返回的文件內容,需指定本地文件名
http.anti_steal.token_check_fail=

啓動

# 重啓tracker
fdfs_trackerd /etc/fdfs/tracker.conf restart
# 重啓storage
fdfs_storaged /etc/fdfs/storage.conf restart
# 重啓nginx
./nginx -s reload
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章