轉載請出自出處:http://www.cnblogs.com/hd3013779515/html
1.FastDFS簡單介紹nginx
FastDFS是由淘寶的餘慶先生所開發,是一個輕量級、高性能的開源分佈式文件系統,用純C語言開發,包括文件存儲、文件同步、文件訪問(上傳、下載)、存取負載均衡、在線擴容、相同內容只存儲一份等功能,適合有大容量存儲需求的應用或系統。作分佈式系統開發時,其中要解決的一個問題就是圖片、音視頻、文件共享的問題,分佈式文件系統正好能夠解決這個需求。同類的分佈式文件系統有谷歌的GFS、HDFS(Hadoop)、TFS(淘寶)等。c++
源碼開放下載地址:https://github.com/happyfish100
早期源碼開放下載地址:https://sourceforge.net/projects/fastdfs/files/
官網論壇:http://bbs.chinaunix.net/forum-240-1.htmlgit
一、client詢問tracker上傳到的storage,不須要附加參數;
二、tracker返回一臺可用的storage;
三、client直接和storage通信完成文件上傳。github
一、client詢問tracker下載文件的storage,參數爲文件標識(組名和文件名);
二、tracker返回一臺可用的storage;
三、client直接和storage通信完成文件下載。shell
FastDFS兩個主要的角色:Tracker Server 和 Storage Server
Tracker Server:跟蹤服務器,主要負責調度storage節點與client通訊,在訪問上起負載均衡的做用,和記錄storage節點的運行狀態,是鏈接client和storage節點的樞紐。
Storage Server:存儲服務器,保存文件和文件的meta data(元數據)
Group:文件組,也能夠稱爲卷。同組內服務器上的文件是徹底相同的,作集羣時每每一個組會有多臺服務器,上傳一個文件到同組內的一臺機器上後,FastDFS會將該文件即時同步到同組內的其它全部機器上,起到備份的做用。
meta data:文件相關屬性,鍵值對(Key Value Pair)方式,如:width=1024, height=768。和阿里雲OSS的meta data類似。vim
2.FastDFS集羣環境安裝後端
2.1集羣規劃瀏覽器
跟蹤服務器負載均衡節點1:192.168.137.160 dfs-nginx-proxy-1
跟蹤服務器負載均衡節點2:192.168.137.161 dfs-nginx-proxy-2
跟蹤服務器1:192.168.137.162 dfs-tracker-1
跟蹤服務器2:192.168.137.163 dfs-tracker-2
存儲服務器1:192.168.137.164 dfs-storage-group1-1
存儲服務器2:192.168.137.165 dfs-storage-group1-2
存儲服務器3:192.168.137.166 dfs-storage-group2-1
存儲服務器4:192.168.137.167 dfs-storage-group2-2
HA虛擬IP:192.168.137.170
HA軟件:Keepalived
操做系統:CentOS 6.8
用戶:root
數據目錄:/fastdfs緩存
2.2安裝包
FastDFS_v5.05.tar.gz:FastDFS源碼
libfastcommon-master.zip:(從 FastDFS 和 FastDHT 中提取出來的公共 C 函數庫)
fastdfs-nginx-module_v1.16.tar.gz:storage節點http服務nginx模塊
nginx-1.6.2.tar.gz:Nginx安裝包
ngx_cache_purge-2.3.tar.gz:Nginx圖片緩存清除模塊
keepalived-1.2.18.tar.gz:高可用
能夠從百度網盤下載。
連接:
http://pan.baidu.com/s/1jHIwtsq 密碼:n757
外部統一訪問192.168.137.170這個虛擬IP,來訪問集羣中各節點的文件。
在全部跟蹤服務器和存儲服務器節點上執行如下操做,即:162 ~ 167節點
shellyuminstallmakecmakegccgcc-c++>
shell> cd /home/test
shell> unzip libfastcommon-master.zip -d /usr/local/fast/
shell> cd /usr/local/fast/libfastcommon-master/
## 編譯、安裝
shell> ./make.sh
shell> ./make.sh install
FastDFS主程序設置的目錄爲/usr/local/lib/,因此須要建立/usr/lib64下的一些核心執行程序的軟鏈接文件。
shell> mkdir /usr/local/lib/
shell> ln -s /usr/lib64/libfastcommon.so /usr/local/lib/libfastcommon.so
shell> ln -s /usr/lib64/libfastcommon.so /usr/lib/libfastcommon.so
shell> ln -s /usr/lib64/libfdfsclient.so /usr/local/lib/libfdfsclient.so
shell> ln -s /usr/lib64/libfdfsclient.so /usr/lib/libfdfsclient.so
shell> cd /home/test
shell> tar -zxvf FastDFS_v5.05.tar.gz -C /usr/local/fast
shell> cd /usr/local/fast/FastDFS/
## 編譯、安裝
shell> ./make.sh
shell> ./make.sh install
由於FastDFS服務腳本設置的bin目錄爲/usr/local/bin/下,可是實際安裝在了/usr/bin/下面。因此須要修改配置文件的路徑,也就是修改下面兩個文件。
shell> vim /etc/init.d/fdfs_storaged
進行全局替換命令:%s+/usr/local/bin+/usr/bin
shell> vim /etc/init.d/fdfs_trackerd
進行全局替換命令:%s+/usr/local/bin+/usr/bin
說明:每一個節點執行相同的操做
cp.conf.sample.confshell>/etc/fdfs/tracker/etc/fdfs/tracker
shell> vim /etc/fdfs/tracker.conf
# 修改的內容以下:
disabled=false # 啓用配置文件
port=22122 # tracker服務器端口(默認22122)
base_path=/fastdfs/tracker # 存儲日誌和數據的根目錄
其它參數保留默認配置, 具體配置解釋可參考官方文檔說明:http://bbs.chinaunix.net/thread-1941456-1-1.html
shell> mkdir -p /fastdfs/tracker
shell> vi /etc/sysconfig/iptables
添加以下端口行:-A INPUT -m state --state NEW -m tcp -p tcp --dport 22122 -j ACCEPT
重啓防火牆:shell> service iptables restart
/etc/initshell>.d/fdfs_trackerd start
初次啓動,會在/fastdfs/tracker目錄下生成logs、data兩個目錄:
檢查FastDFS Tracker Server是否啓動成功:ps -ef | grep fdfs_trackerd
/etc/initshell>.d/fdfs_trackerd stop
shellon> chkconfig fdfs_trackerd
group1: 192.168.137.164,192.168.137.165
group2: 192.168.137.166,192.168.137.167
說明:每一個節點執行相同的操做
cp.conf.sample.confshell>/etc/fdfs/storage/etc/fdfs/storage
shell> vi /etc/fdfs/storage.conf
# 修改的內容以下:
disabled=false # 啓用配置文件
port=23000 # storage服務端口
group_name=group1 # 組名(第一組爲group1,第二組爲group2,依次類推...)
base_path=/fastdfs/storage # 數據和日誌文件存儲根目錄
store_path0=/fastdfs/storage # 第一個存儲目錄,第二個存儲目錄起名爲:store_path1=xxx,其它存儲目錄名依次類推...
store_path_count=1 # 存儲路徑個數,須要和store_path個數匹配
tracker_server=192.168.137.162:22122 # tracker服務器IP和端口
tracker_server=192.168.137.163:22122 # tracker服務器IP和端口
http.server_port=8888 # http訪問文件的端口
其它參數保留默認配置, 具體配置解釋可參考官方文檔說明:http://bbs.chinaunix.net/thread-1941456-1-1.html
shell> mkdir -p /fastdfs/storage
shell> vi /etc/sysconfig/iptables
添加以下端口行:-A INPUT -m state --state NEW -m tcp -p tcp --dport 23000 -j ACCEPT
重啓防火牆:shell> service iptables restart
/etc/initshell>.d/fdfs_storaged start
初次啓動,會在/fastdfs/storage目錄下生成logs、data兩個目錄。
檢查FastDFS Tracker Server是否啓動成功:ps -ef | grep fdfs_storaged
各節點啓動後,使用tail -f /fastdfs/storage/logs/storaged.log
命令監聽存儲節點的日誌,能夠看到存儲節點連接到跟蹤服務器,並提示哪個爲leader跟蹤服務器,同時也能看到同一組中其它節點加入進來的日誌信息。
全部存儲節點都啓動以後,能夠在任一存儲節點上使用以下命令查看集羣的狀態信息:
/usr/bin/fdfs_monitor /etc/fdfs/storageshell>.conf
/etc/initshell>.d/fdfs_storaged stop
shellon> chkconfig fdfs_storaged
cp.conf.sample.confshell>/etc/fdfs/client/etc/fdfs/client
.confshell> vi /etc/fdfs/client
base_path=/fastdfs/tracker
tracker_server=192.168.137.162:22122
tracker_server=192.168.137.163:22122
/usr/bin/fdfs_upload_file /etc/fdfs/clientshell>.conf /home/test/FastDFS_v5.05.tar.gz
返回如下ID號,說明文件上傳成功:
group1/M00/00/00/wKiJpFkS0ByATie5AAVFOL7FJU4.tar.gz
group2/M00/00/00/wKiJplkS0CyAJyzTAAVFOL7FJU4.tar.gz
(從返回的ID號中也能夠看出,同一個文件分別存儲在兩個組內group1和group2,但也有可能在同一組中,具體策略是由FastDFS根據服務器的存儲狀況來分配的)
說明:每一個節點執行相同的操做
FastDFS 經過 Tracker 服務器,將文件放在 Storage 服務器存儲,可是同組存儲服務器之間須要進入文件複製流程,有同步延遲的問題。假設 Tracker 服務器將文件上傳到了 192.168.137.164,上傳成功後文件 ID已經返回給客戶端。此時 FastDFS 存儲集羣機制會將這個文件同步到同組存儲 192.168.137.165,在文件尚未複製完成的狀況下,客戶端若是用這個文件 ID 在 192.168.137.165上取文件,就會出現文件沒法訪問的錯誤。而 fastdfs-nginx-module 能夠重定向文件鏈接到源服務器(192.168.137.164)上取文件,避免客戶端因爲複製延遲致使的文件沒法訪問錯誤。
安裝fastdfs-nginx-module_v1.16.tar.gz
shell> cd /home/test
shell> tar -zxvf fastdfs-nginx-module_v1.16.tar.gz -C /usr/local/fast/
shell/usr/local/fast/fastdfs-nginx-module/src/> cd
shell/usr/local/fast/fastdfs-nginx-module/src/config> vim
CORE_INCS="$CORE_INCS /usr/local/include/fastdfs /usr/local/include/fastcommon/"
改成
CORE_INCS="$CORE_INCS /usr/include/fastdfs /usr/include/fastcommon/"
## 安裝nginx所需的依賴包
shell> yum install gcc gcc-c++ make automake autoconf libtool pcre pcre-devel zlib zlib-devel openssl openssl-devel
## 編譯安裝nginx(添加fastdfs-nginx-module模塊)
shell> cd /home/test
shell> tar -zxvf nginx-1.6.2.tar.gz -C /usr/local/
shell1.6.2/> cd /usr/local/nginx-
shell--add-module=/usr/local/fast/fastdfs-nginx-module/src/> ./configure
shell> make && make install
shelllocal> cp /usr//fast/fastdfs-nginx-module/src/mod_fastdfs.conf /etc/fdfs/
shell> vi /etc/fdfs/mod_fastdfs.conf
connect_timeout=10
base_path=/tmp
tracker_server=192.168.137.162:22122
tracker_server=192.168.137.163:22122
storage_server_port=23000
group_name=group1 # 第一組storage的組名
url_have_group_name=true
store_path0=/fastdfs/storage
group_count=2
[group1]
group_name=group1
storage_server_port=23000
store_path_count=1
store_path0=/fastdfs/storage
[group2]
group_name=group2
storage_server_port=23000
store_path_count=1
store_path0=/fastdfs/storage
第二組的mod_fastdfs.confg配置與第一組的配置只有group_name不一樣:
group_name=group2
shelllocal> cd /usr//fast/FastDFS/conf
shellhttp> cp.conf mime.types /etc/fdfs/
shelldata/ /fastdfs/storage/data/M00> ln -s /fastdfs/storage/
.confshell> vi /usr/local/nginx/conf/nginx
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 localhost;
# FastDFS 文件訪問配置(fastdfs-nginx-module模塊)
location ~/group([0-9])/M00 {
ngx_fastdfs_module;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
注意:
A、8888 端口值要與/etc/fdfs/storage.conf 中的 http.server_port=8888 相對應,由於 http.server_port 默認爲 8888,若是想改爲 80,則要對應修改過來。
B、Storage 對應有多個 group 的狀況下,訪問路徑帶 group 名,如:http://xxxx/group1/M00/00/00/xxx, 對應的 Nginx 配置爲:
group09location ~/([-])/M00 {
ngx_fastdfs_module;
}
C、以下載時如發現老報 404,將nginx.conf第一行user nobody;
修改成user root;
後從新啓動。
shell> vi /etc/sysconfig/iptables
## 添加
--state NEW -m tcp -p tcp --dport 8888 -j ACCEPT-A INPUT -m state
## 重啓防火牆
shell> service iptables restart
/usr/local/nginx/sbin/nginxshell>
# fastdfs-nginx-module進程IDngx_http_fastdfs_set pid=xxx
重啓 Nginx 的命令爲:/usr/local/nginx/sbin/nginx -s reload
設置Nginx開機啓動:
shelllocal> vi /etc/rc.
# 加入
/usr/local/nginx/sbin/nginx
shelllocal> chmod +x /etc/rc.
http://192.168.137.164:8888/group1/M00/00/00/wKiJpFkS0ByATie5AAVFOL7FJU4.tar.gz
http://192.168.137.166:8888/group2/M00/00/00/wKiJplkS0CyAJyzTAAVFOL7FJU4.tar.gz
說明:每一個節點執行相同的操做
tracker節點:192.168.137.162,192.168.137.163
在 tracker 上安裝的 nginx 主要爲了提供 http 訪問的反向代理、負載均衡以及緩存服務。
>-c++-devel-devel-develshellyum install gcc gccmake automake autoconf libtool pcre pcrezlib zlibopenssl openssl
shell> cd /home/test
shell> tar -zxvf nginx-1.6.2.tar.gz -C /usr/local/
shell> tar -zxvf ngx_cache_purge-2.3.tar.gz -C /usr/local/fast/
shell1.6.2> cd /usr/local/nginx-
shell--add-module=/usr/local/fast/ngx_cache_purge-2.3> ./configure
shell> make && make install
shell> vi /usr/local/nginx/conf/nginx.conf
user nobody;
worker_processes 1;
events {
worker_connections 1024;
use epoll;
}
http {
include mime.types;
default_type application/octet-stream;
#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 logs/access.log main;
sendfile on;
tcp_nopush on;
keepalive_timeout 65;
#gzip on;
#設置緩存
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 300m;
proxy_redirect off;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 16k;
proxy_buffers 4 64k;
proxy_busy_buffers_size 128k;
proxy_temp_file_write_size 128k;
#設置緩存存儲路徑、存儲方式、分配內存大小、磁盤最大空間、緩存期限
proxy_cache_path /fastdfs/cache/nginx/proxy_cache levels=1:2 keys_zone=http-cache:500m max_size=10g inactive=30d;
proxy_temp_path /fastdfs/cache/nginx/proxy_cache/tmp;
#設置 group1 的服務器
upstream fdfs_group1 {
server 192.168.137.164:8888 weight=1 max_fails=2 fail_timeout=30s;
server 192.168.137.165:8888 weight=1 max_fails=2 fail_timeout=30s;
}
#設置 group2 的服務器
upstream fdfs_group2 {
server 192.168.137.166:8888 weight=1 max_fails=2 fail_timeout=30s;
server 192.168.137.167:8888 weight=1 max_fails=2 fail_timeout=30s;
}
server {
listen 8000;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
#設置 group 的負載均衡參數
location /group1/M00 {
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;
proxy_pass http://fdfs_group1;
expires 30d;
}
location /group2/M00 {
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;
proxy_pass http://fdfs_group2;
expires 30d;
}
#設置清除緩存的訪問權限
location ~/purge(/.*) {
allow 127.0.0.1;
allow 192.168.1.0/24;
deny all;
proxy_cache_purge http-cache $1$is_args$args;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
按以上 nginx 配置文件的要求,建立對應的緩存目錄:
>-pcacheshellmkdir/fastdfs//nginx/proxy_cache
>-pcacheshellmkdir/fastdfs//nginx/proxy_cache/tmp
shell> vi /etc/sysconfig/iptables
## 添加以下配置
--state NEW -m tcp -p tcp --dport 8000 -j ACCEPT -A INPUT -m state
shell# 從新啓動防火牆> service iptables restart
/usr/local/nginx/sbin/nginxshell>
設置開機啓動:
shelllocal> vi /etc/rc.
## 加入如下配置
/usr/local/nginx/sbin/nginx
shelllocal> chmod +x /etc/rc.
前面直接經過訪問Storage節點中的Nginx訪問文件:
http://192.168.137.164:8888/group1/M00/00/00/wKiJpFkS0ByATie5AAVFOL7FJU4.tar.gz
http://192.168.137.166:8888/group2/M00/00/00/wKiJplkS0CyAJyzTAAVFOL7FJU4.tar.gz
如今能夠經過Tracker中的Nginx來進行訪問:
(1)、經過 Tracker1 中的 Nginx 來訪問
http://192.168.137.162:8000/group1/M00/00/00/wKiJpFkS0ByATie5AAVFOL7FJU4.tar.gz
http://192.168.137.162:8000/group2/M00/00/00/wKiJplkS0CyAJyzTAAVFOL7FJU4.tar.gz
(2)、經過 Tracker2 中的 Nginx 來訪問
http://192.168.137.163:8000/group1/M00/00/00/wKiJpFkS0ByATie5AAVFOL7FJU4.tar.gz
http://192.168.137.163:8000/group2/M00/00/00/wKiJplkS0CyAJyzTAAVFOL7FJU4.tar.gz
由上面的文件訪問效果能夠看到,每個Tracker中的Nginx都單獨對後端的Storage組作了負載均衡,但整套FastDFS集羣,若是想對外提供統一的文件訪問地址,還須要對兩個Tracker中的Nginx進行HA集羣
使用Keepalived + Nginx組成的高可用負載均衡集羣,作兩個Tracker節點中Nginx的負載均衡。
分別在192.168.137.160和192.168.137.161兩個節點安裝Keepalived與Nginx。
keepalived安裝與配置:請參考本人Nginx相關blog
Nginx的安裝與配置:請參考本人Nginx相關blog
請參考本人Nginx相關blog
注意:將VIP的IP地址修改成192.168.137.170
2個節點的Nginx配置相同,以下所示:
shell> vi /usr/local/nginx/conf/nginx.conf
user root;
worker_processes 1;
events {
worker_connections 1024;
use epoll;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
## FastDFS Tracker Proxy
upstream fastdfs_tracker {
server 192.168.137.162:8000 weight=1 max_fails=2 fail_timeout=30s;
server 192.168.137.163:8000 weight=1 max_fails=2 fail_timeout=30s;
}
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
## FastDFS Proxy
location /dfs {
root html;
index index.html index.htm;
proxy_pass http://fastdfs_tracker/;
proxy_set_header Host $http_host;
proxy_set_header Cookie $http_cookie;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
client_max_body_size 300m;
}
}
}
/usr/local/nginx/sbin/nginxshell>-s reload
如今能夠經過 Keepalived+Nginx 組成的高可用負載集羣的 VIP(192.168.137.170)來訪問 FastDFS 集羣中的文件了:
http://192.168.137.170/dfs/group1/M00/00/00/wKiJpFkS0ByATie5AAVFOL7FJU4.tar.gz
http://192.168.137.170/dfs/group2/M00/00/00/wKiJplkS0CyAJyzTAAVFOL7FJU4.tar.gz
注意:千萬不要使用 kill -9 命令強殺 FastDFS 進程,不然可能會致使 binlog 數據丟失。
參考:
http://blog.csdn.net/xyang81/article/details/52928230