FastDFS
FastDFS的作者余慶在其 GitHub 上是這樣描述的:“FastDFS is an open source high performance distributed file system. It's major functions include: file storing, file syncing and file accessing (file uploading and file downloading), and it can resolve the high capacity and load balancing problem. FastDFS should meet the requirement of the website whose service based on files such as photo sharing site and video sharing site” ,意思說,FastDFS是一個開源的高性能分布式檔案系統,其主要功能包括:檔案存盤、檔案同步和檔案訪問(檔案上傳和檔案下載),它可以解決高容量和負載平衡問題,FastDFS應滿足基于照片共享站點和視頻共享站點等檔案的網站的服務要求,
FastDFS 有兩個角色:跟蹤器(Tracker)和存盤器(Storage),Tracker 負責檔案訪問的調度和負載平衡,Storage 存盤檔案及其功能是檔案管理,包括:檔案存盤、檔案同步、提供檔案訪問介面,它還管理元資料,這些元資料是表示為檔案的鍵值對的屬性,Tracker 和 Storage 節點都可以由一臺或多臺服務器構成,這些服務器均可以隨時增加或下線而不會影響線上服務,當然各個節點集群至少需要一臺服務 Running,注意,其中 Tracker 集群中的所有服務器都是對等的(P2P),可以根據服務器的壓力情況隨時增加或減少,
此外,官網還存盤系統做了詳盡的說明,為了支持大容量,Storage 節點采用了分卷(或分組)的組織方式,存盤系統由一個或多個卷組成,卷與卷之間的檔案是相互獨立的,所有卷的檔案容量累加就是整個存盤系統中的檔案容量,一個卷可以由一臺或多臺 Storage 服務器組成,一個卷下的存盤服務器中的檔案都是相同的,卷中的多臺存盤服務器起到了冗余備份和負載均衡的作用,在卷中增加服務器時,同步已有的檔案由系統自動完成,同步完成后,系統自動將新增服務器切換到線上提供服務,當存盤空間不足或即將耗盡時,可以動態添加卷,只需要增加一臺或多臺服務器,并將它們配置為一個新的卷,這樣就擴大了存盤系統的容量,關于卷或組的概念暫且不要過多深入,后面的安裝部署中會后詳細的說明,
在FastDFS中,檔案的標識由兩部分組成:卷名和檔案名,
環境說明
- 作業系統:Centos Linux release 7.2.1511
- 系統磁盤: 274G
- 掛在磁盤:3.7T * 12
- CPU:32 (Intel(R) Xeon(R) )
- Memory:8G
架構設計

圖1 DFS架構圖
作業方式:客戶端向 tracker 發出請求,然后 tracker 從 storage 節點拿到源資料,返還給客戶端,然后客戶端根據源資料再去請求 storage 節點,

圖2 高可用DFS架構圖(紅色虛線部分另外討論)
概要精簡
1.核心系統只有兩個角色,tracker server和storage server,
2.所有跟蹤器服務器都是對等的,不存在 Master-Slave 關系(圖中 Tracker-leader 需另行討論,目前只需要明白 tracker 是 P2P),
3.存盤服務器采用分組方式,同組記憶體儲服務器上的檔案完全相同,
4.不同組的 storage server 之間不會相互通信,相同組之間會進行同步,
5.由 storage server 主動向 tracker server 報告狀態資訊,每個 tracker server 都會記錄 storage server 的資訊,
6.如果開啟了 trunk 功能,tracker 服務協調 storage 選舉 trunk-server,
集群部署
準備環境
表1 軟體串列及版本
| 名稱 | 說明 | 鏈接 |
|---|---|---|
| CentOS | 7.x(安裝系統) | |
| libfastcommon | FastDFS分離出的一些公用函式包 | libfastcommon V1.0.39 |
| FastDFS | FastDFS本體 | FastDFS V5.11 |
| fastdfs-nginx-module | FastDFS和nginx的關聯模塊,解決組內同步延遲問題 | fastdfs-nginx-module V1.20 |
| nginx | nginx 1.12.2(CentOS 7 下YUM可以安裝的最新版本) | nginx 1.12.2 |
Note: 在google瀏覽器中,點擊上面鏈接下載和復制鏈接下載的檔案名稱不相同,google會根據上面的文本資訊重命名,
表2 服務器IP、服務分配、埠規劃表
| 名稱 | IP地址 | 應用服務 | 埠 |
|---|---|---|---|
| A機器 | |||
| 10.58.10.136 | tracker | 22122 | |
| 10.58.10.136 | storage-group1 | 23000 | |
| 10.58.10.136 | storage-group2 | 33000 | |
| 10.58.10.136 | libfastcommon | - | |
| 10.58.10.136 | nginx | 8888 | |
| 10.58.10.136 | fastdfs-nginx-module | - | |
| B機器 | |||
| 10.58.10.137 | tracker | 22122 | |
| 10.58.10.137 | storage-group1 | 23000 | |
| 10.58.10.137 | storage-group3 | 43000 | |
| 10.58.10.137 | libfastcommon | - | |
| 10.58.10.137 | nginx | 8888 | |
| 10.58.10.137 | fastdfs-nginx-module | - | |
| C機器 | |||
| 10.58.10.138 | tracker | 22122 | |
| 10.58.10.138 | storage-group2 | 33000 | |
| 10.58.10.138 | storage-group23 | 43000 | |
| 10.58.10.138 | libfastcommon | - | |
| 10.58.10.138 | nginx | 8888 | |
| 10.58.10.138 | fastdfs-nginx-module | - |
在安裝之前必須說明:
1.記得給下面用到的存盤目錄(日志、資料、pid檔案等)授權可讀可寫權限;
2.下面所有的組態檔中由于說明,加了注釋,請務必在配置的時候把配置項后面以“#”開頭的注釋刪掉,
初始化環境
# 安裝編譯環境
$ yum groups install Development Tools -y
$ yum install perl -y
$ yum -y install redhat-rpm-config.noarch
$ yum -y install gd-devel
$ yum -y install perl-devel perl-ExtUtils-Embed
$ yum -y install pcre-devel
$ yum -y install openssl openssl-devel
$ yum -y install gcc-c++ autoconf automake
$ yum install -y zlib-devel
$ yum -y install libxml2 libxml2-dev
$ yum -y install libxslt-devel
$ yum -y install GeoIP GeoIP-devel GeoIP-data
$ yum install gperftools -y
安裝libfastcommon
在A、B、C三臺機器上分別執行下面的操作:
$ tar -zxvf libfastcommon-1.0.39.tar.gz
$ cd libfastcommon-1.0.39/
$ ./make.sh
$ ./make.sh install
libfastcommon 安裝在了 /usr/lib64/libfastcommon.so,需要注意新老版本,新版本在安裝之后自動會將 libfastcommon.so 通過軟連接鏈接到 /usr/local/lib 目錄下面,如果是老版本,則需要手動建立:
ln -s /usr/lib64/libfastcommon.so /usr/local/lib/libfastcommon.so ln -s /usr/lib64/libfastcommon.so /usr/lib/libfastcommon.so
如果有 libfdfsclient.so,則也可以把libfdfsclient.so加入到 /usr/local/lib 中:
ln -s /usr/lib64/libfdfsclient.so /usr/local/lib/libfdfsclient.so ln -s /usr/lib64/libfdfsclient.so /usr/lib/libfdfsclient.so
Note:最好自己通過命令
ls | grep libfastcommon在/usr/lib/和/usr/local/lib查看確認是否鏈接成功,
安裝tracker
在 A、B、C 三臺機器上分別執行下面操作:
$ mkdir -p /data/fastdfs/tracker
$ tar -zxvf fastdfs-5.11.tar.gz
$ cd fastdfs-5.11/
$ ./make.sh
$ ./make.sh install
$ # 組態檔準備
$ cp /etc/fdfs/tracker.conf.sample /etc/fdfs/tracker.conf #tracker節點
修改tracker的組態檔:
$ vim /etc/fdfs/tracker.conf
$ #需要修改的內容如下
max_connections=1024 #默認256,最大連接數
port=22122 # tracker 服務器埠(默認 22122,一般不修改)
base_path=/data/fastdfs/tracker # 存盤日志和資料的根目錄
增加 tracker.service 服務,可使用 systemctl 執行服務的啟動、重啟、停止等操作,也可設定開機自動重啟,
$ #編輯啟動檔案
$ vim /usr/lib/systemd/system/fastdfs-tracker.service
[Unit]
Description=The FastDFS File server
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
ExecStart=/usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf start
ExecStop=/usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf stop
ExecRestart=/usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf restart
[Install]
WantedBy=multi-user.target
保存 /usr/lib/systemd/system/fastdfs-tracker.service 檔案并退出 vim 編輯器,我們執行下面操作啟動 Fastdfs Tracker 服務:
$ systemctl daemon-reload
$ systemctl enable fastdfs-tracker.service
$ systemctl start fastdfs-tracker.service
tracker 服務啟動結束后,我們通過下面命令查看埠是否正常:
$ netstat -tulnp|grep 22122 #查看服務是否啟動,埠是否打開
安裝sorage
安裝 tracker 的時候已經解壓過了則可以不執行此步驟,
$ tar -zxvf fastdfs-5.11.tar.gz
$ cd fastdfs-5.11/
$ ./make.sh
$ ./make.sh install
A 機器(group1/group2)
在 fastdfs-5.11 目錄下面復制 storage 的組態檔(復制兩份):
$ sudo mkdir -p /data/fastdfs/storage/group1
$ sudo mkdir -p /data/fastdfs/storage/group2
$ sudo cp /etc/fdfs/storage.conf.sample /etc/fdfs/storage-group1.conf #storage節點的group1組
$ sudo cp /etc/fdfs/storage.conf.sample /etc/fdfs/storage-group2.conf #storage節點的group1組
$ sudo cp /etc/fdfs/client.conf.sample /etc/fdfs/client.conf #客戶端檔案,測驗用
根據架構設計,我們依次修改上面的三個檔案:
- 修改 group1 的組態檔:
/etc/fdfs/storage-group1.conf
$ sudo vim /etc/fdfs/storage-group1.conf
#需要修改的內容如下
group_name=group1
port=23000 # storage 服務埠(默認 23000)
base_path=/data/fastdfs/storage/group1 # 資料和日志檔案存盤根目錄
store_path_count=6
store_path0=/data01/fastdfs # group1 的第一個存盤目錄
store_path1=/data02/fastdfs # group1 的第二個存盤目錄
store_path2=/data03/fastdfs # group1 的第三個存盤目錄
store_path3=/data04/fastdfs # group1 的第四個存盤目錄
store_path4=/data05/fastdfs # group1 的第五個存盤目錄
store_path5=/data06/fastdfs # group1 的第六個存盤目錄
tracker_server=10.58.10.136:22122 # tracker 服務器IP和埠
tracker_server=10.58.10.137:22122 # tracker 服務器IP和埠
tracker_server=10.58.10.138:22122 # tracker 服務器IP和埠
http.server_port=8888 # http 訪問檔案的埠(默認 8888,看情況修改,和nginx 中保持一致)
- 修改 group2 的組態檔:
/etc/fdfs/storage-group2.conf
$ sudo vim /etc/fdfs/storage-group2.conf
#需要修改的內容如下
group_name=group2
port=33000 # storage 服務埠(默認 23000,修改為 33000)
base_path=/data/fastdfs/storage/group2 # 資料和日志檔案存盤根目錄
store_path_count=6
store_path0=/data07/fastdfs # group2 的第一個存盤目錄
store_path1=/data08/fastdfs # group2 的第二個存盤目錄
store_path2=/data09/fastdfs # group2 的第三個存盤目錄
store_path3=/data10/fastdfs # group2 的第四個存盤目錄
store_path4=/data11/fastdfs # group2 的第五個存盤目錄
store_path5=/data12/fastdfs # group2 的第六個存盤目錄
tracker_server=10.58.10.136:22122 # tracker 服務器 IP 和埠
tracker_server=10.58.10.137:22122 # tracker 服務器 IP 和埠
tracker_server=10.58.10.138:22122 # tracker 服務器 IP 和埠
http.server_port=8888 # http 訪問檔案的埠(默認 8888,看情況修改,和nginx 中保持一致)
- 修改client的組態檔:
/etc/fdfs/client.conf
$ sudo vim /etc/fdfs/client.conf
#需要修改的內容如下
base_path=/data/fastdfs/client
tracker_server=10.58.10.136:22122 # tracker 服務器 IP 和埠
tracker_server=10.58.10.137:22122 # tracker 服務器 IP 和埠
tracker_server=10.58.10.138:22122 # tracker 服務器 IP 和埠
- 修改好以上三個組態檔之后,我們制作啟動 service:
fastdfs-storage-group1.service
$ vim /usr/lib/systemd/system/fastdfs-storage-group1.service
# 編輯啟動檔案
[Unit]
Description=The FastDFS File server
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
ExecStart=/usr/bin/fdfs_storaged /etc/fdfs/storage-group1.conf start
ExecStop=/usr/bin/fdfs_storaged /etc/fdfs/storage-group1.conf stop
ExecRestart=/usr/bin/fdfs_storaged /etc/fdfs/storage-group1.conf restart
[Install]
WantedBy=multi-user.target
fastdfs-storage-group2.service
$ vim /usr/lib/systemd/system/fastdfs-storage-group2.service
# 編輯啟動檔案
[Unit]
Description=The FastDFS File server
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
ExecStart=/usr/bin/fdfs_storaged /etc/fdfs/storage-group2.conf start
ExecStop=/usr/bin/fdfs_storaged /etc/fdfs/storage-group2.conf stop
ExecRestart=/usr/bin/fdfs_storaged /etc/fdfs/storage-group2.conf restart
[Install]
WantedBy=multi-user.target
- Storage 的 service 制作好只好,我們啟動 Storage 的兩個服務:
$ systemctl daemon-reload
$ systemctl enable fastdfs-storage-group1.service
$ systemctl start fastdfs-storage-group1.service
啟動程序可能因為某些權限或配置書寫錯誤的問題,會出現啟動失敗的情況,可通過 systemctl status fastdfs-storage-group1.service 查看該服務的狀態,同時結合logs(日志檔案在 /data/fastdfs/storage/group1/logs/目錄下面)排查,可以快速定位問題,如果出現一些例外情況,也可以看看后面的采坑部分,看看有沒有期望的結果,
$ netstat -tulnp #查看服務是否啟動,埠是否打開(23000,33000)
- 服務起來之后,我們可以查看Fastdfs的集群狀態:
#查看集群狀態
$ fdfs_monitor /etc/fdfs/storage-group1.conf list
控制臺列印出如下資訊,說明成功了:
[2018-11-06 00:00:00] DEBUG - base_path=/data/fastdfs/storage/group1, connect_timeout=30, network_timeout=60, tracker_server_count=2, anti_steal_token=0, anti_steal_secret_key length=0, use_connection_pool=0, g_connection_pool_max_idle_time=3600s, use_storage_id=0, storage server id count: 0
server_count=3, server_index=0
tracker server is 10.58.10.136:22122,10.58.10.137:22122,10.58.10.138:22122
group count: 2
Group 1:
...
- 通過客戶端測驗上傳檔案
#保存后測驗,回傳 ID 表示成功 如:group1/M00/00/00/xx.txt
$ fdfs_upload_file /etc/fdfs/client.conf test.txt
B 機器(group1/group3)
配置程序與 A 機器類似,需要修改以下幾個點:
- 創建目錄及復制組態檔,
$ sudo mkdir -p /data/fastdfs/storage/group1
$ sudo mkdir -p /data/fastdfs/storage/group3
$ sudo cp /etc/fdfs/storage.conf.sample /etc/fdfs/storage-group1.conf #storage節點的group1組
$ sudo cp /etc/fdfs/storage.conf.sample /etc/fdfs/storage-group3.conf #storage節點的group3組
$ sudo cp /etc/fdfs/client.conf.sample /etc/fdfs/client.conf #客戶端檔案,測驗用
- 修改 group3 的組態檔
/etc/fdfs/storage-group3.conf,group1 的配置內容和 A 的保持一致,
$ sudo vim /etc/fdfs/storage-group3.conf
#需要修改的內容如下
group_name=group3
port=43000 # storage服務埠(默認23000,修改為43000)
base_path=/data/fastdfs/storage/group3 # 資料和日志檔案存盤根目錄
store_path_count=6
store_path0=/data07/fastdfs # group3的第一個存盤目錄
store_path1=/data08/fastdfs # group3的第二個存盤目錄
store_path2=/data09/fastdfs # group3的第三個存盤目錄
store_path3=/data10/fastdfs # group3的第四個存盤目錄
store_path4=/data11/fastdfs # group3的第五個存盤目錄
store_path5=/data12/fastdfs # group3的第六個存盤目錄
tracker_server=10.58.10.136:22122 # tracker服務器IP和埠
tracker_server=10.58.10.137:22122 # tracker服務器IP和埠
tracker_server=10.58.10.138:22122 # tracker服務器IP和埠
http.server_port=8888 # http訪問檔案的埠(默認8888,看情況修改,和nginx中保持一致)
- client 的組態檔和 A 機器一致,此處不做重復,
- 制作啟動 group3 的 service,
fastdfs-storage-group1.service和 A 機器一致,復制即可,
$ vim /usr/lib/systemd/system/fastdfs-storage-group3.service
# 編輯啟動檔案
[Unit]
Description=The FastDFS File server
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
ExecStart=/usr/bin/fdfs_storaged /etc/fdfs/storage-group3.conf start
ExecStop=/usr/bin/fdfs_storaged /etc/fdfs/storage-group3.conf stop
ExecRestart=/usr/bin/fdfs_storaged /etc/fdfs/storage-group3.conf restart
[Install]
WantedBy=multi-user.target
- 執行啟動腳本,直到
fastdfs-storage-group1.service和fastdfs-storage-group3.service服務起來即可,
C 機器(group2/group3)
配置程序與 A 機器類似,需要修改以下幾個點:
- 創建目錄及復制組態檔,
$ sudo mkdir -p /data/fastdfs/storage/group2
$ sudo mkdir -p /data/fastdfs/storage/group3
$ sudo cp /etc/fdfs/storage.conf.sample /etc/fdfs/storage-group2.conf #storage節點的group1組
$ sudo cp /etc/fdfs/storage.conf.sample /etc/fdfs/storage-group3.conf #storage節點的group3組
$ sudo cp /etc/fdfs/client.conf.sample /etc/fdfs/client.conf #客戶端檔案,測驗用
- 修改 group2 的組態檔
/etc/fdfs/storage-group2.conf,group3 的配置內容和 B 機器的保持一致,
$ sudo vim /etc/fdfs/storage-group2.conf
#需要修改的內容如下
group_name=group2
port=33000 # storage服務埠(默認23000,修改為33000)
base_path=/data/fastdfs/storage/group2 # 資料和日志檔案存盤根目錄
store_path_count=6
store_path0=/data01/fastdfs # group2的第一個存盤目錄
store_path1=/data02/fastdfs # group2的第二個存盤目錄
store_path2=/data03/fastdfs # group2的第三個存盤目錄
store_path3=/data04/fastdfs # group2的第四個存盤目錄
store_path4=/data05/fastdfs # group2的第五個存盤目錄
store_path5=/data06/fastdfs # group2的第六個存盤目錄
tracker_server=10.58.10.136:22122 # tracker服務器IP和埠
tracker_server=10.58.10.137:22122 # tracker服務器IP和埠
tracker_server=10.58.10.138:22122 # tracker服務器IP和埠
http.server_port=8888 # http訪問檔案的埠(默認8888,看情況修改,和nginx中保持一致)
- client 的組態檔和 A 機器一致,此處不做重復,
- 制作啟動 group2 和 group3 的 service,上面已有,直接復制即可,
- 執行啟動腳本,直到
fastdfs-storage-group2.service和fastdfs-storage-group3.service服務起來即可,
安裝nginx和fastdfs的nginx模塊
step1 在 fastdfs 的目錄中,將 http.conf 和 mime.types 檔案拷貝到 /etc/fdfs 目錄下面,支持 nginx 訪問 storage 服務,
# 在三臺機器上都需要執行
$ cp ./conf/http.conf /etc/fdfs/ #供 nginx 訪問使用
$ cp ./conf/mime.types /etc/fdfs/ #供 nginx 訪問使用
step2 安裝 fastdfs 的 nginx 模塊:
# 在三臺機器上都需要執行
$ tar -zxvf V1.20.tar.gz
$ cp fastdfs-nginx-module-1.20/src/mod_fastdfs.conf /etc/fdfs/mod_fastdfs.conf
step3 然后修改 fastdfs-nginx-module-1.20/src/config 檔案,找到 ngx_module_incs 和 CORE_INCS 兩處,修改如下:
ngx_module_incs=”/usr/include/fastdfs /usr/include/fastcommon/”
CORE_INCS=”$CORE_INCS /usr/include/fastdfs /usr/include/fastcommon/”
如果不修改,在編譯 nginx 的時候,會出這種錯誤:/usr/include/fastdfs/fdfs_define.h:15:27: fatal error: common_define.h: No such file or directory,
step4 之后我們解壓 nginx,安裝 nginx 服務:
$ tar -zxvf nginx-1.12.2.tar.gz
$ cd nginx-1.12.2
$ ./configure --prefix=/usr/share/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/var/lib/nginx/tmp/client_body --http-proxy-temp-path=/var/lib/nginx/tmp/proxy --http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi --http-uwsgi-temp-path=/var/lib/nginx/tmp/uwsgi --http-scgi-temp-path=/var/lib/nginx/tmp/scgi --pid-path=/run/nginx.pid --lock-path=/run/lock/subsys/nginx --user=nginx --group=nginx --with-file-aio --with-ipv6 --with-http_auth_request_module --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module=dynamic --with-http_image_filter_module=dynamic --with-http_geoip_module=dynamic --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_slice_module --with-http_stub_status_module --with-http_perl_module=dynamic --with-mail=dynamic --with-mail_ssl_module --with-pcre --with-pcre-jit --with-stream=dynamic --with-stream_ssl_module --with-google_perftools_module --with-debug --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -m64 -mtune=generic' --with-ld-opt='-Wl,-z,relro -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -Wl,-E' --add-module=${YOUR_PATH}/fastdfs-nginx-module-1.20/src
$ make
$ make install
注意,上面 ${YOUR_PATH} 改為 fastdfs-nginx-module-1.20 的父目錄,保證路徑正確,
- 修改A機器
/etc/fdfs/mod_fastdfs.conf組態檔:
connect_timeout=2
network_timeout=30
base_path=/data/fastdfs/ngx_mod
load_fdfs_parameters_from_tracker=true
storage_sync_file_max_delay = 86400
use_storage_id = false
storage_ids_filename = storage_ids.conf
tracker_server=10.58.10.136:22122 # tracker服務器IP和埠
tracker_server=10.58.10.137:22122 # tracker服務器IP和埠
tracker_server=10.58.10.138:22122 # tracker服務器IP和埠
group_name=group1/group2 # 全域
url_have_group_name = true
log_level=info
log_filename=
response_mode=proxy
if_alias_prefix=
flv_support = true
flv_extension = flv
group_count = 2
[group1]
group_name=group1 #group中
storage_server_port=23000
store_path_count=6
store_path0=/data01/fastdfs
store_path1=/data02/fastdfs
store_path2=/data03/fastdfs
store_path3=/data04/fastdfs
store_path4=/data05/fastdfs
store_path5=/data06/fastdfs
[group2]
group_name=group2
storage_server_port=33000
store_path_count=6
store_path0=/data07/fastdfs
store_path1=/data08/fastdfs
store_path2=/data09/fastdfs
store_path3=/data10/fastdfs
store_path4=/data11/fastdfs
store_path5=/data12/fastdfs
- 配置 nginx 的組態檔,以便訪問 storage
$ sudo vim /etc/nginx/nginx.conf
user nginx;
worker_prosesses on;
worker_rlimit_nofile 65535;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 65535;
use epoll;
accept_mutex off;
}
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
gzip on;
server_names_bucket_hash_size 128;
client_header_buffer_size 32k;
larger_client_header_buffers 4 32k;
client_max_body_size 300m;
proxy_redirect off;
proxy_http_version 1.1;
proxy_set_header Connection '';
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header HOST $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_connet_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 16k;
proxy_buffers 8 64k;
proxy_busy_buffers_size 128k;
proxy_temp_file_write_size 128k;
proxy_cache_path /data/fastdfs/cache/nginx/proxy_cache levels=1:2
keys_zone=http-cache:200m max_size=1g inactive=30d;
proxy_temp_path /data/fastdfs/cache/nginx/proxy_cache/temp;
include /etc/nginx/mime.types;
default_type application/octet-stream;
include /etc/nginx/conf.d/*.conf;
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
#include /etc/nginx/default.d/*.conf;
location ~ ^/ok(\..*)?$ {
return 200 "OK";
}
location /nginx {
stub_status on;
}
location /healthcheck {
check_status on;
}
location ^~ /group1/ {
proxy_next_upstream http_502 http_504 error timeout invalid_header;
proxy_cache http-cache;
proxy_cache_valid 200 304 12h;
proxy_cache_key $uri$is_args$args;
add_header 'Access-Control-Allow-Origin' $http_origin;
add_header 'Access-Control-Allow-Credentials' 'true';
add_header "Access-Control-Allow-Methods" "GET, POST, HEAD, PUT, DELETE, OPTIONS, PATCH";
add_header "Access-Control-Allow-Headers" "Origin, No-Cache, Authorization, X-Requested-With, If-Modified-Since, Pragma, Last-Modified, Cache-Control, Expires, Content-Type";
if ($request_method = 'OPTIONS') {
return 200 'OK';
}
proxy_pass http://fdfs_group1;
expire 30d;
}
location ^~ /group2/ {
proxy_next_upstream http_502 http_504 error timeout invalid_header;
proxy_cache http-cache;
proxy_cache_valid 200 304 12h;
proxy_cache_key $uri$is_args$args;
add_header 'Access-Control-Allow-Origin' $http_origin;
add_header 'Access-Control-Allow-Credentials' 'true';
add_header "Access-Control-Allow-Methods" "GET, POST, HEAD, PUT, DELETE, OPTIONS, PATCH";
add_header "Access-Control-Allow-Headers" "Origin, No-Cache, Authorization, X-Requested-With, If-Modified-Since, Pragma, Last-Modified, Cache-Control, Expires, Content-Type";
if ($request_method = 'OPTIONS') {
return 200 'OK';
}
proxy_pass http://fdfs_group2;
expire 30d;
}
location ^~ /group3/ {
proxy_next_upstream http_502 http_504 error timeout invalid_header;
proxy_cache http-cache;
proxy_cache_valid 200 304 12h;
proxy_cache_key $uri$is_args$args;
add_header 'Access-Control-Allow-Origin' $http_origin;
add_header 'Access-Control-Allow-Credentials' 'true';
add_header "Access-Control-Allow-Methods" "GET, POST, HEAD, PUT, DELETE, OPTIONS, PATCH";
add_header "Access-Control-Allow-Headers" "Origin, No-Cache, Authorization, X-Requested-With, If-Modified-Since, Pragma, Last-Modified, Cache-Control, Expires, Content-Type";
if ($request_method = 'OPTIONS') {
return 200 'OK';
}
proxy_pass http://fdfs_group3;
expire 30d;
}
location ~/purge(/.*) {
allow 127.0.0.1;
allow 192.168.1.0/24;
allow 10.58.1.0/24;
deny all;
proxy_cache_purge http-cache $1$is_args$args;
}
}
server {
listen 8888;
server_name localhost;
location /ok.htm {
return 200 "OK";
}
location ~/group[0-9]/ {
ngx_fastdfs_module;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
upstream fdfs_group1 {
server 10.58.10.136:8888 max_fails=0;
server 10.58.10.137:8888 max_fails=0;
keepalive 10240;
check interval=2000 rise=2 fall=3 timeout=1000 type=http default_down=false;
check_http_send "GET /ok.htm HTTP/1.0\r\nConnection:keep-alive\r\n\r\n";
check_keepalive_requests 100;
}
upstream fdfs_group2 {
server 10.58.10.136:8888 max_fails=0;
server 10.58.10.138:8888 max_fails=0;
keepalive 10240;
check interval=2000 rise=2 fall=3 timeout=1000 type=http default_down=false;
check_http_send "GET /ok.htm HTTP/1.0\r\nConnection:keep-alive\r\n\r\n";
check_keepalive_requests 100;
}
upstream fdfs_group3 {
server 10.58.10.137:8888 max_fails=0;
server 10.58.10.138:8888 max_fails=0;
keepalive 10240;
check interval=2000 rise=2 fall=3 timeout=1000 type=http default_down=false;
check_http_send "GET /ok.htm HTTP/1.0\r\nConnection:keep-alive\r\n\r\n";
check_keepalive_requests 100;
}
}
- 啟動 nginx 服務:
sudo nginx -c /etc/nginx/nginx.conf
訪問http://localhost/ok.htm看看是否回傳 200 的狀態,并有 OK 內容,如果啟動失敗,可以查看nginx的日志,日志在/var/log/nginx/error.log檔案中,這個也是 nginx 在 Centos 中的默認錯誤日志路徑,如果修改過 error_log,那么在 error_log 指定的檔案中查看錯誤資訊,
- B 和 C 機器的配置基本一樣,nginx 的組態檔完全一樣,唯一需要修改的是
/etc/fdfs/mod_fastdfs.conf,在 group_name 中根據節點中 storage 的配置修改對應的名字即可,但是需要注意的是方括號[groupX]中X一定是順序遞增的,從1開始,
問題總結
遇到問題,第一,我們通過 Console 列印的資訊排查服務啟動失敗的原因;第二,通過日志排查,大多數時候,日志是最有效的排查方式,沒有之一,tracker 或 storage 服務的日志存放在其服務對應組態檔中 base_path 配置項指定路徑的 logs 目錄下面, nginx的日志,如果開啟了自己指定日志路徑,那么就是自己指定的路徑下查找,如果默認,在 Centos 下面大多數在 /var/log/nginx/ 目錄下面,當然,在這種環境中我們排查的不單單是 nginx,還有 fastdfs 的對 nginx 的擴展模塊,這時候,要考慮 fastdfs-nginx-module 的日志的排查,其日志存放路徑為 /etc/fdfs/mod_fastdfs.conf 配置中 base_path 指定的路徑,
集群中多個 group 配置埠
每個 group 都需要獨立一個 storage 服務,因此同一臺主機上 storage group 的埠不能沖突,這里我把 group1 的規劃為 23000,group2 的規劃為 33000,group3 的規劃為43000.
相同名稱的 group 不能在同一臺機器上,因為 tracker 調度時,相同名稱的 group 之間會自動進行同步,需要相同的埠號,故此,兩臺主機上統一名稱的 group 必須保證 storage 的埠一致,
集群中多個group存盤路徑的配置
同一主機不同的 group 存盤路徑不要放在一起,需要分開配置,
不同主機相同的 group 存盤路徑可以不一致,但是數量必須保證相同,大小也應該基本一致,
同一節點多個 storage 的配置
在同一個機器上,我們部署了兩個 group,三臺機器組成的集群,每個 group 在另外一個節點中保存一份副本,使用命令systemctl start fastdfs-storage-groupx.service啟動的時候,另外一個 group 無法啟動(groupx 為 group1、group2、group3),一直顯示為 Loaded: loaded (/usr/lib/systemd/system/fastdfs-storage-group1.service; enabled; vender preset: disabled),直到最好失敗 Active: inactive 變為 Active: exited (下圖為tracker的,storage 的問題類似),
當時的解決方法:每個 storage 啟動時都需要 systemctl daemon-reload 一下,然后啟動即可,
假如要啟動A機器中的 fastdfs-storage-group1.service 和 fastdfs-storage-group2.service,則需要執行如下操作:
- 先啟動 fastdfs-storage-group1.service
sudo systemctl daemon-reload
sudo systemctl enable fastdfs-storage-group1.service
sudo systemctl start fastdfs-storage-group1.service
- 觀察 fastdfs-storage-group1.service 的狀態:
systemctl status fastdfs-storage-group1.service
- 當看到 active 為 running 的時候,說明啟動成功了,然后啟動 fastdfs-storage-group2.service;否則沒啟動成功,查看日志尋找問題即可:
sudo systemctl daemon-reload
sudo systemctl enable fastdfs-storage-group2.service
sudo systemctl start fastdfs-storage-group3.service
- 觀察 fastdfs-storage-group2.service 的狀態,直到起來為止,
可能會出現 Unkown lvalue 'ExecRestart' in section 'Service' 的警告日志,目前未找到解決方法,當時執行了 yum install systemd-* 的命令,也沒能解決這個問題(有知道解決方案的朋友麻煩告知一下),
fastdfs的nginx模塊配置
在所有 storage 機器上,修改 /etc/fdfs/mod_fastdfs.conf 時,根據不同的 storage 中的 group 修改 group_name 中的值,但是需要注意兩個配置專案,下面就 group2 和 group3 為例說明一下:
- 全域的 group_name
全域配置中的 group_name 必須用”/“分割,值為區域中的 group_name 的值,比如:
group_name=group2/group3,
- group 中的 group_name
group 標識一定是從 group1 開始,依次為 group2,這里的標識就是方括號中的字串,類似 mysql 組態檔中的 [Mysql],
group_name=group2/group3 # 全域
...
group_count = 2
[group1] # 一定要注意這個地方
group_name=grou2 #group中
storage_server_port=33000
store_path_count=6
......
[group2] # 還有這個地方,都是有序的
group_name=group3
storage_server_port=43000
store_path_count=6
......
最后,個人理解,錯誤在所難免,歡迎各位朋友批評指正,如有不對的地方,煩請指出,后續我就 FastDFS 的 Tracker 和 Storage 之間的關系做一個詳細的解釋,
該文首發《虛懷若谷》個人博客,轉載前請務必署名,轉載請標明出處,
古之善為道者,微妙玄通,深不可識,夫唯不可識,故強為之容:
豫兮若冬涉川,猶兮若畏四鄰,儼兮其若客,渙兮若冰之釋,敦兮其若樸,曠兮其若谷,混兮其若濁,
孰能濁以靜之徐清?孰能安以動之徐生?
保此道不欲盈,夫唯不盈,故能敝而新成,
請關注我的微信公眾號:下雨就像彈鋼琴,Thanks?(?ω?)?

轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/160490.html
標籤:Linux
上一篇:Windows系統下安裝VirtualBox及安裝Ubuntu16.04
下一篇:Linux的一些常用命令(一)
