介紹:javascript
FastDFS:開源的高性能分佈式文件系統;主要功能包括:文件存儲,文件同步和文件訪問,以及高容量和負載平衡css
FastDFS:角色:跟蹤服務器(Tracker Server)、存儲服務器(Storage Server)和客戶端(Client)html
FastDfs+nginx緩存高可用集羣環境流程示意圖:java
實驗環境機器說明:node
機器名稱 | IP地址 | 應用 | 說明 |
tracker0一、nginx01 | 192.168.109.137 | FastDFS,libfastcommon,nginx,keepalived | CentOS Linux release 7.6.1810 (Core) |
tracker0二、nginx02 | 192.168.109.138 | FastDFS,libfastcommon,nginx,keepalived | CentOS Linux release 7.6.1810 (Core) |
storage01_group01 | 192.168.109.139 | FastDFS,libfastcommon,nginx,fastdfs-nginx-module | CentOS Linux release 7.6.1810 (Core) |
storage02_group01 | 192.168.109.140 | FastDFS,libfastcommon,nginx,fastdfs-nginx-module | CentOS Linux release 7.6.1810 (Core) |
storage03_group02 | 192.168.109.141 | FastDFS,libfastcommon,nginx,fastdfs-nginx-module | CentOS Linux release 7.6.1810 (Core) |
storage04_group02 | 192.168.109.142 | FastDFS,libfastcommon,nginx,fastdfs-nginx-module | CentOS Linux release 7.6.1810 (Core) |
VIP | 192.168.109.143 | 無 | 無 |
軟件需求:linux
部署流程:nginx
參考:c++
FastDFS官方網站:https://sourceforge.net/projects/fastdfs/ (Last Update: 2016-02-04)git
下載地址:https://github.com/happyfish100/fastdfs/releasesgithub
6臺機器共同操做部分:
mkdir -p /usr/local/software
cd /usr/local/software
wget https://github.com/happyfish100/fastdfs/archive/V5.11.tar.gz
wget https://github.com/happyfish100/fastdfs-client-java/archive/master.zip
mv master.zip fastdfs-client-java.zip
wget https://github.com/happyfish100/fastdfs-nginx-module/archive/master.zip
mv master.zip fastdfs-nginx-module.zip
wget https://github.com/happyfish100/libfastcommon/archive/master.zip
mv master.zip libfastcommon.zip
wget http://nginx.org/download/nginx-1.9.9.tar.gz
安裝gcc:
yum -y install make cmake gcc gcc-c++
安裝 libfastcommon:
解壓到/usr/local/fast/
unzip libfastcommon.zip -d /usr/local/fast/
編譯、安裝
cd /usr/local/fast/libfastcommon-master
./make.sh
./make.sh install
進行軟件建立,FastDFS主程序設置的目錄爲/usr/local/lib/,因此咱們須要建立/usr/lib64/下的一些核心執行程序軟連接文件
ln -s /usr/lib64/libfastcommon.so /usr/local/lib/libfastcommon.so
忽略淺色(自行查看校驗)
ln -s /usr/lib64/libfastcommon.so /usr/lib/libfastcommon.so 已存在
ln -s /usr/lib64/libfdfsclient.so /usr/local/lib/libfdfsclient.so 沒有這個文件
ln -s /usr/lib64/libfdfsclient.so /usr/lib/libfdfsclient.so 沒有這個文件
安裝 FastDFS:
tar -zxf V5.11.tar.gz -C /usr/local/fast/
cd /usr/local/fast/fastdfs-5.11
./make.sh
./make.sh install
cp -f ./conf/client.conf /etc/fdfs/
忽略,我當前版本腳本環境正確,有些版本可能須要更改,自行查看
由於 FastDFS 服務腳本設置的bin目錄爲 /usr/local/bin/下,實際安裝在/usr/bin/下面,因此須要修改配置文件中路徑
vim /etc/init.d/fdfs_storaged
進行全局替換: %s+/usr/local/bin+/usr/bin
vim /etc/init.d/fdfs_trackerd
進行全局替換: %s+/usr/local/bin+/usr/bin
如下是部分單獨操做:
tracker server:(192.168.109.137;192.168.109.138)
修改配置文件::
cd /etc/fdfs/
cp tracker.conf.sample tracker.conf
vim tracker.conf
修改成本身的路徑地址:base_path=/fastdfs/tracker
store_lookup=2 實驗能夠改成0輪詢,(0輪詢,1指定上傳組,2負載均衡選擇要上載文件的最大空閒空間組)
生成中綁定IP地址(bind_addr=)
建立:mkdir -pv /fastdfs/tracker
啓動跟蹤器:/etc/init.d/fdfs_trackerd start
tail -f /fastdfs/tracker/logs/trackerd.log
如下是部分單獨操做:
storage server:(192.168.109.139;192.168.109.140; 和 192.168.109.141;192.168.109.142;)
修改配置文件:
cd /etc/fdfs/
cp storage.conf.sample storage.conf
vim storage.conf
disabled=false #啓用配置文件
group_name=group1 #4臺機器分紅2組(group1:192.168.109.139,140和group2:192.168.109.141,142)
port=23000 #storage 端口號,同一個組的 storage 端口號必須一致
base_path=/fastdfs/storage #設置 storage 的日誌目錄
store_path_count=1 #默認就爲1,(存儲路徑個數,須要和 store_path個數匹配)
store_path0=/fastdfs/storage #設置存儲路徑
tracker_server=192.168.109.137:22122 #tracker 服務的IP和端口,多個就添加多條記錄,以下一條
tracker_server=192.168.109.138:22122
http.server_port=8888 #設置http端口號
建立目錄:mkdir -pv /fastdfs/storage
咱們這個4臺機器分紅2組(group1:192.168.109.139,140和group2:192.168.109.141,142)配置文件組名注意下
啓動 storage:
/etc/init.d/fdfs_storaged start
tail -f /fastdfs/storage/logs/storaged.log
至此FastDFS集羣已經能夠使用但不支持http
查看集羣配置信息:
命令(任意一臺storage服務器上執行):/usr/bin/fdfs_monitor /etc/fdfs/storage.conf
6臺中能夠隨意找一臺作客戶端測試下:修改客戶端配置文件
vim /etc/fdfs/client.conf 修改以下地方:
base_path=/fastdfs/tracker #tracker服務器文件路徑
tracker_server=192.168.109.137:22122 #tracker服務器IP地址和端口號
tracker_server=192.168.109.138:22122
http.tracker_server_port=8888 # tracker 服務器的 http端口號,必須和tracker的設置對應起來
/usr/bin/fdfs_upload_file /etc/fdfs/client.conf /home/111/1.jpg #你上傳的圖片路徑(linux上的)
查看:
多傳幾回能夠看到上面設置的輪詢效果
配置FastDFS集羣支持http
支持http請求,安裝nginx(4個storage節點安裝nginx,首先安裝fastdfs-nginx-module,fastdfs與nginx集成模塊)
cd /usr/local/software/
unzip fastdfs-nginx-module.zip -d /usr/local/fast/
安裝前先修改路徑,以下
cd /usr/local/fast/fastdfs-nginx-module-master/src
修改:vim /usr/local/fast/fastdfs-nginx-module-master/src/config 修改是爲nginx能編譯進這個第三方模塊,具體修改咱們先看下nginx編譯安裝報錯信息
依懶
yum -y install pcre pcre-devel
yum -y install zlib zlib-devel
yum -y install openssl openssl-devel
cd /usr/local/software
tar -zxf nginx-1.9.9.tar.gz -C /usr/local/
cd /usr/local/nginx-1.9.9
./configure --add-module=/usr/local/fast/fastdfs-nginx-module-master/src/
make
依據提示查看
make失敗是由於沒有加載到fdfs_define.h文件,這時咱們回到上面第三方模塊(fastdfs-nginx-module-master/src)咱們沒有修改的配置文件查看
vim /usr/local/fast/fastdfs-nginx-module-master/src/config
ngx_module_incs="/usr/local/include" 模塊加載的路徑是/usr/local/include,但在這個目錄下是找不到上面編譯安裝失敗的頭文件
vim /usr/local/fast/fastdfs-nginx-module-master/src/config
ngx_module_incs="/usr/local/include" 修改成 ngx_module_incs="/usr/include/fastdfs /usr/include/fastcommon"
CORE_INCS="$CORE_INCS /usr/local/include"修改成 CORE_INCS="$CORE_INCS /usr/include/fastdfs /usr/include/fastcommon"
cd /usr/local/nginx-1.9.9
再次編譯安裝前先make clean清理一下
./configure --add-module=/usr/local/fast/fastdfs-nginx-module-master/src/
make
如今就能夠make install 安裝了
cd /usr/local/fast/fastdfs-nginx-module-master/src/
cp mod_fastdfs.conf /etc/fdfs/
vim /etc/fdfs/mod_fastdfs.conf 修改
connect_timeout=20 #超時時間
#tracker_server=tracker:22122 改成下面
tracker_server=192.168.109.137:22122
tracker_server=192.168.109.138:22122
storage_server_port=23000
group_name=group1 #注意:第一組(192.168.109.139;140爲group1);第二組(192.168.109.141;142爲group2)
url_have_group_name = true #改成這裏的true
store_path0=/fastdfs/storage #修改
group_count = 2 #改成咱們這定義的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
複製FastDFS(fastdfs-5.11)裏的2個文件到/etc/fdfs/目錄下,以下:
cd /usr/local/fast/fastdfs-5.11/conf/
cp http.conf mime.types /etc/fdfs/
建立一個軟連接,在/fastdfs/storage 文件存儲目錄下建立軟連接,將其連接到實際存放數據的目錄
ln -s /fastdfs/storage/data/ /fastdfs/storage/data/M00
修改nginx配置文件(因此storage節點都一致)以下所示:
cd /usr/local/nginx/conf/
vim nginx.conf
listen 8888; #80改成8888,這裏的8888是storage.conf中配置的一致
#location / {} 修改以下:
location ~/group([0-9])/M00 {
#alias /fastdfs/storage/data;
ngx_fastdfs_module;
}
啓動nginx:
啓動命令:/usr/local/nginx/sbin/nginx
上傳圖片,網頁http測試:(橙色爲測試分析)
能夠看到數據存儲路徑在group1組:group1/M00/00/00/wKhtjF0NDviATu47AACy5BY7SZU357.jpg
用4臺storage上的nginx分別拼接地址http訪問測試:
group1組有數據正常訪問
http://192.168.109.139:8888/group1/M00/00/00/wKhtjF0NDviATu47AACy5BY7SZU357.jpg
http://192.168.109.140:8888/group1/M00/00/00/wKhtjF0NDviATu47AACy5BY7SZU357.jpg
group2組無數據沒法訪問(預想)
http://192.168.109.141:8888/group1/M00/00/00/wKhtjF0NDviATu47AACy5BY7SZU357.jpg
http://192.168.109.142:8888/group1/M00/00/00/wKhtjF0NDviATu47AACy5BY7SZU357.jpg
問題:預想的不一致
那多是緩存,查看nginx日誌有提示304,頁面緩存,但我清了頁面緩存,並取消了nginx緩存設置,繼續訪問仍是能訪問到...(鬱悶nginx並未配置代理)
group2組也能顯示與預期不符,分析:
192.168.109.141_storage_group2_01 機器抓包
tcpdump -i any -A -nn port 8888 > 123.txt
繼續往下看抓的信息,看到了服務器(192.168.109.141)向group1組的(192.168.109.140:8888)請求數據,爲何??:
繼續往下看抓取的信息,能夠看到141將從140獲取的數據,轉發給客戶端,只就是爲何group2組的(192.168.109.141)本地沒有數據,網頁訪問竟然能獲取到圖片的緣由:
分析:數據獲取是真真切切的沒毛病,但我nginx配置文件並無配置代理轉發;爲何轉發,這時想到多是第三方模塊的緣由,查看第三方模塊的配置:mod_fastdfs.conf
本地文件系統中不存在文件時的響應模式
代理:從其餘存儲服務器獲取內容,而後發送到客戶機
## redirect:重定向到原始存儲服務器(HTTP頭是位置)
綜上所述預想錯誤不成立,沒有理解透fastdfs-nginx-module模塊,fastdfs-nginx-module很好的解決了分佈式訪問問題,經過tracker獲取storage上的數據
tracker server 上安裝 nginx(緩存:加ngx_cache_purge模塊) + keepalived 作高可用:
安裝nginx:
tar -zxf ngx_cache_purge-2.3.tar.gz -C /usr/local/fast/
依懶
yum -y install pcre pcre-devel
yum -y install zlib zlib-devel
yum -y install openssl openssl-devel
cd /usr/local/software
tar -zxf nginx-1.9.9.tar.gz -C /usr/local/
cd /usr/local/nginx-1.9.9
./configure --add-module=/usr/local/fast/ngx_cache_purge-2.3
make
make install
建立緩存目錄(來自nginx配置文件):mkdir -pv /fastdfs/cache/nginx/proxy_cache
修改nginx配置文件:
cd /usr/local/nginx/conf/
vim /usr/local/nginx/conf/nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 5;
server_tokens off;
gzip on;
gzip_disable "MSIE [1-6]\.";
gzip_min_length 1100;
gzip_buffers 4 8k;
gzip_comp_level 3;
gzip_proxied any;
gzip_types text/plain text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript application/json image/jpeg image/gif imag
e/png image/jpg;
large_client_header_buffers 4 8k;
client_header_buffer_size 8k;
tcp_nopush on;
tcp_nodelay on;
server_names_hash_bucket_size 128;
client_max_body_size 300m;
client_body_buffer_size 128k;
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:200m max_size=1g inactive=30d;
proxy_temp_path /fastdfs/cache/nginx/proxy_cache/tmp;
upstream fdfs {
server 192.168.109.139:8888 weight=1 max_fails=3 fail_timeout=30s;
server 192.168.109.140:8888 weight=1 max_fails=3 fail_timeout=30s;
server 192.168.109.141:8888 weight=1 max_fails=3 fail_timeout=30s;
server 192.168.109.142:8888 weight=1 max_fails=3 fail_timeout=30s;
}
server {
listen 80;
server_name localhost;
location ~/group([0-9])/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;
expires 30d;
}
location ~/purge(/.*) {
allow 127.0.0.1;
allow 192.168.109.0/24;
deny all;
proxy_cache_purge http-cache $1$is_args$args;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
啓動nginx:/usr/local/nginx/sbin/nginx
測試:
http://192.168.109.137/group1/M00/00/00/wKhtjF0NDviATu47AACy5BY7SZU357.jpg
http://192.168.109.138/group1/M00/00/00/wKhtjF0NDviATu47AACy5BY7SZU357.jpg
下面只要配置keepalived作高可用就好:
yum -y install keepalived
刪了vrrp_strict (vip 綁上了ping不經過,抓包發現數據進來 出不去:1.防火牆(排除);2.不在一個域廣播)
配置健康檢查腳本:
賦予執行權限:chmod +x /etc/keepalived/check_nginx.sh
啓動keepalived:systemctl start keepalived
用VIP訪問測試:
http://192.168.109.143/group1/M00/00/00/wKhtjF0NDviATu47AACy5BY7SZU357.jpg
正常