介紹: FastDFS:開源的高性能分散式文件系統;主要功能包括:文件存儲,文件同步和文件訪問,以及高容量和負載平衡 FastDFS:角色:跟蹤伺服器(Tracker Server)、存儲伺服器(Storage Server)和客戶端(Client) FastDfs+nginx緩存高可用集群環境流程 ...
介紹:
FastDFS:開源的高性能分散式文件系統;主要功能包括:文件存儲,文件同步和文件訪問,以及高容量和負載平衡
FastDFS:角色:跟蹤伺服器(Tracker Server)、存儲伺服器(Storage Server)和客戶端(Client)
- Tracker Server: 跟蹤伺服器,主要做調度工作,起到均衡的作用;負責管理所有的storage server和group,每個storage在啟動後會連接 Tracker,告知自己所屬 group 等信息,並保持周期性心跳。多個Tracker之間是對等關係,不存在單點故障
- Storage Server: 存儲伺服器,主要提供容量和備份服務;以 group 為單位,每個 group 內可以有多台 storage server(高可用),組內的storage server上的數據互為備份
- Client:客戶端,上傳下載數據的伺服器
FastDfs+nginx緩存高可用集群環境流程示意圖:
實驗環境機器說明:
機器名稱 | IP地址 | 應用 | 說明 |
tracker01、nginx01 | 192.168.109.137 | FastDFS,libfastcommon,nginx,keepalived | CentOS Linux release 7.6.1810 (Core) |
tracker02、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 | 無 | 無 |
軟體需求:
部署流程:
參考:
FastDFS官方網站:https://sourceforge.net/projects/fastdfs/ (Last Update: 2016-02-04)
下載地址:https://github.com/happyfish100/fastdfs/releases
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
正常