以前介紹過關於FastDFS單機部署,詳見博文:FastDFS+Nginx(單點部署)事例html
下面來玩下FastDFS集羣部署,實現高可用(HA)java
跟蹤服務器1【主機】(Tracker Server):192.100.139.121nginx
跟蹤服務器2【備機】(Tracker Server):192.100.139.122c++
存儲服務器1(Storage Server):192.100.139.121git
存儲服務器2(Storage Server):192.100.139.123github
存儲服務器3(Storage Server):192.100.139.124vim
操做系統:CentOS7 瀏覽器
用戶:root 緩存
數據存儲目錄:服務器
應用 |
安裝目錄 |
Nginx |
/opt/nginx |
Fastdfs |
/usr/bin |
Keepalived |
/usr/local |
安裝包 |
/home/yxgly/resources è /usr/local/src |
Tracker_data |
/fastdfs/tracker |
Storage_data |
/fastdfs/storage |
點擊這裏下載全部安裝包
下載完成後,將壓縮包解壓到/usr/local/src目錄下
yum install make cmake gcc gcc-c++
cd /usr/local/src #安裝unzip 命令: yum install -y unzip zip unzip libfastcommon-master.zip cd libfastcommon-master ## 編譯、安裝 ./make.sh ./make.sh install
cd /usr/local/src tar -xzvf FastDFS_v5.08.tar.gz cd FastDFS ./make.sh ./make.sh install
採用默認安裝方式,相應的文件與目錄檢查以下:
1> 服務腳本:
/etc/init.d/fdfs_storaged
/etc/init.d/fdfs_trackerd
2> 配置文件(示例配置文件):
ll /etc/fdfs/ -rw-r--r-- 1 root root 1461 1月 4 14:34 client.conf.sample -rw-r--r-- 1 root root 7927 1月 4 14:34 storage.conf.sample -rw-r--r-- 1 root root 7200 1月 4 14:34 tracker.conf.sample
3> 命令行工具(/usr/bin目錄下)
ll /usr/bin/fdfs_* -rwxr-xr-x 1 root root 260584 1月 4 14:34 fdfs_appender_test -rwxr-xr-x 1 root root 260281 1月 4 14:34 fdfs_appender_test1 -rwxr-xr-x 1 root root 250625 1月 4 14:34 fdfs_append_file -rwxr-xr-x 1 root root 250045 1月 4 14:34 fdfs_crc32 -rwxr-xr-x 1 root root 250708 1月 4 14:34 fdfs_delete_file -rwxr-xr-x 1 root root 251515 1月 4 14:34 fdfs_download_file -rwxr-xr-x 1 root root 251273 1月 4 14:34 fdfs_file_info -rwxr-xr-x 1 root root 266401 1月 4 14:34 fdfs_monitor -rwxr-xr-x 1 root root 873233 1月 4 14:34 fdfs_storaged -rwxr-xr-x 1 root root 266952 1月 4 14:34 fdfs_test -rwxr-xr-x 1 root root 266153 1月 4 14:34 fdfs_test1 -rwxr-xr-x 1 root root 371336 1月 4 14:34 fdfs_trackerd -rwxr-xr-x 1 root root 251651 1月 4 14:34 fdfs_upload_appender -rwxr-xr-x 1 root root 252781 1月 4 14:34 fdfs_upload_file
cp /etc/fdfs/tracker.conf.sample /etc/fdfs/tracker.conf
vim /etc/fdfs/tracker.conf # 修改的內容以下: disabled=false # 啓用配置文件 port=22122 # tracker服務器端口(默認22122) base_path=/fastdfs/tracker # 存儲日誌和數據的根目錄 store_group=group1 其它參數保留默認配置, 具體配置解釋可參考官方文檔說明:http://bbs.chinaunix.net/thread-1941456-1-1.html
mkdir -p /fastdfs/tracker
vi /etc/sysconfig/iptables 附加:若/etc/sysconfig 目錄下沒有iptables文件可隨便寫一條iptables命令配置個防火牆規則:如: iptables -P OUTPUT ACCEPT 而後用命令:service iptables save 進行保存,默認就保存到 /etc/sysconfig/iptables 文件裏。這時既有了這個文件。防火牆也能夠啓動了。接下來要寫策略,也能夠直接寫在/etc/sysconfig/iptables 裏了。 添加以下端口行: -A INPUT -m state --state NEW -m tcp -p tcp --dport 22122 -j ACCEPT 重啓防火牆 service iptables restart
/etc/init.d/fdfs_trackerd start 初次啓動,會在/fastdfs/tracker目錄下生成logs、data兩個目錄。 drwxr-xr-x 2 root root 4096 1月 4 15:00 data drwxr-xr-x 2 root root 4096 1月 4 14:38 logs 檢查FastDFS Tracker Server是否啓動成功: ps -ef | grep fdfs_trackerd
cp /etc/fdfs/storage.conf.sample /etc/fdfs/storage.conf
vi /etc/fdfs/storage.conf # 修改的內容以下: disabled=false # 啓用配置文件 port=23000 # storage服務端口 base_path=/fastdfs/storage # 數據和日誌文件存儲根目錄 store_path0=/fastdfs/storage # 第一個存儲目錄 tracker_server=192.100.139.121:22122 # tracker服務器IP和端口 tracker_server=192.100.139.122:22122 #tracker服務器IP2和端口[Microsof1] http.server_port=8888 # http訪問文件的端口 其它參數保留默認配置, 具體配置解釋可參考官方文檔說明:http://bbs.chinaunix.net/thread-1941456-1-1.html
mkdir -p /fastdfs/storage
vi /etc/sysconfig/iptables #添加以下端口行: -A INPUT -m state --state NEW -m tcp -p tcp --dport 23000 -j ACCEPT 重啓防火牆 service iptables restart
追蹤+存儲節點操做步驟1、步驟2、步驟三
存儲節點只作存儲則只操做步驟三
/etc/init.d/fdfs_storaged start 初次啓動,會在/fastdfs/storage目錄下生成logs、data兩個目錄。 drwxr-xr-x 259 root root 4096 Mar 31 06:22 data drwxr-xr-x 2 root root 4096 Mar 31 06:22 logs
檢查FastDFS Tracker Server是否啓動成功:
[root@gyl-test-t9 ~]# ps -ef | grep fdfs_storaged root 1336 1 3 06:22 ? 00:00:01 /usr/bin/fdfs_storaged /etc/fdfs/storage.conf root 1347 369 0 06:23 pts/0 00:00:00 grep fdfs_storaged
cp /etc/fdfs/client.conf.sample /etc/fdfs/client.conf vim /etc/fdfs/client.conf # 修改如下配置,其它保持默認 base_path=/fastdfs/tracker tracker_server=192.100.139.121:22122 # tracker服務器IP和端口 tracker_server=192.100.139.122:22122 #tracker服務器IP2和端口
#/usr/local/src/test.png 是須要上傳文件路徑 /usr/bin/fdfs_upload_file /etc/fdfs/client.conf /usr/local/src/test.png 返回文件ID號:group1/M00/00/00/tlxkwlhttsGAU2ZXAAC07quU0oE095.png (能返回以上文件ID,說明文件已經上傳成功) Or : /usr/bin/fdfs_test /etc/fdfs/client.conf upload client.conf
FastDFS 經過 Tracker 服務器,將文件放在 Storage 服務器存儲,可是同組存儲服務器之間須要進入文件複製,有同步延遲的問題。假設 Tracker 服務器將文件上傳到了 ip01,上傳成功後文件 ID 已經返回給客戶端。此時 FastDFS 存儲集羣機制會將這個文件同步到同組存儲 ip02,在文件還沒有複製完成的狀況下,客戶端若是用這個文件 ID 在 ip02 上取文件,就會出現文件沒法訪問的錯誤。而 fastdfs-nginx-module 能夠重定向文件鏈接到源服務器取文件,避免客戶端因爲複製延遲致使的文件沒法訪問錯誤。(解壓後的 fastdfs-nginx-module 在 nginx 安裝時使用)
cd /usr/local/src tar -xzvf fastdfs-nginx-module_v1.16.tar.gz
cd fastdfs-nginx-module/src vim config 將 CORE_INCS="$CORE_INCS /usr/local/include/fastdfs /usr/local/include/fastcommon/" 修改成: CORE_INCS="$CORE_INCS /usr/include/fastdfs /usr/include/fastcommon/"
yum install gcc gcc-c++ make automake autoconf libtool pcre* zlib openssl openssl-devel
cd /usr/local/src/ tar -zxvf nginx-1.10.0.tar.gz tar –zxvf ngx_cache_purge_2.3.tar.gz cd nginx-1.10.0 ./configure --prefix=/opt/nginx --add-module=/usr/local/src/fastdfs-nginx-module/src --add-module=/usr/local/src/ngx_cache_purge-2.3 make && make install
cp /usr/local/src/fastdfs-nginx-module/src/mod_fastdfs.conf /etc/fdfs/ vi /etc/fdfs/mod_fastdfs.conf 修改如下配置: connect_timeout=10 base_path=/tmp tracker_server=192.100.139.121:22122 # tracker服務器IP和端口 tracker_server=192.100.139.122:22122 #tracker服務器IP2和端口 url_have_group_name=true #url中包含group名稱 #在最後添加 [group1] group_name=group1 storage_server_port=23000 store_path_count=1 store_path0=/fastdfs/storage
八、複製 FastDFS 的部分配置文件到/etc/fdfs 目錄
cd /usr/local/src/FastDFS/conf
cp http.conf mime.types /etc/fdfs/
ln -s /fastdfs/storage/data/ /fastdfs/storage/data/M00
user nobody; worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { listen 8888; server_name 192.100.139.121; location ~/group1/M00 { ngx_fastdfs_module; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } upstream storage_server_group1{ server 192.100.139.121:8888 weight=10; server 192.100.139.123:8888 weight=10; server 192.100.139.124:8888 weight=10; } }
說明:
A、8888 端口值是要與/etc/fdfs/storage.conf 中的 http.server_port=8888 相對應, 由於 http.server_port 默認爲 8888,若是想改爲 80,則要對應修改過來。
B、Storage 對應有多個 group 的狀況下,訪問路徑帶 group 名,如/group1/M00/00/00/xxx, 對應的 Nginx 配置爲:
location ~/group([0-9])/M00 {
ngx_fastdfs_module;
}
C、如查下載時如發現老報 404,將 nginx.conf 第一行 user nobody 修改成 user root 後從新啓動。
vi /etc/sysconfig/iptables 添加: -A INPUT -m state --state NEW -m tcp -p tcp --dport 8888 -j ACCEPT #重啓防火牆 service iptables restart 啓動nginx : /opt/nginx/sbin/nginx (重啓 Nginx 的命令爲:/opt/nginx/sbin/nginx -s reload)
切換追蹤服務器IP一樣能夠訪問
http://192.100.139.121:8888/group1/M00/00/00/CmSKtFj13gyAen4oAAH0yXi-HW8296.png
http://192.100.139.122:8888/group1/M00/00/00/CmSKtFj13gyAen4oAAH0yXi-HW8296.png
注意修改tracker服務器Ip地址
connect_timeout = 2 network_timeout = 30 charset = ISO8859-1 http.tracker_http_port = 8888 http.anti_steal_token = no tracker_server=192.100.139.121:22122 tracker_server=192.100.139.122:22122 default_group_name=group1