高可用高性能分佈式文件系統FastDFS進階keepalived+nginx對多tracker進行高可用熱備

  在上一篇 分佈式文件系統FastDFS如何作到高可用 中已經介紹了FastDFS的原理和怎麼搭建一個簡單的高可用的分佈式文件系統及怎麼訪問。html

  高可用是實現了,但因爲咱們只設置了一個group,若是如今有5臺服務器那將會出現5臺只有一個group,每臺服務器內的文件內容都相同(互備份)以下圖,會形成資源浪費。nginx

 所以下面就5臺服務器進行優化改造,進一步添加keepalived+nginx多tracker 架構,作到真正的高可用和高性能。shell

 

 FastDFS集羣服務器分佈

 

其中keepalived+nginx用做30和31 tracker服務器做熱備。用虛擬VIP 27做統一入口。vim

咱們將按上面的架構分服務器:後端

虛擬VIP用192.168.80.27centos

keepalived+nginx 熱備、負載均衡 : 2八、29 瀏覽器

tracker:緩存

30和31服務器服務器

storage:cookie

group1:3二、33

group2:3四、35

雖然每一個服務器上都有Nginx,但這裏仍是先強調一下這裏Nginx的用途:

  1. 首先全部的Nginx都是用於上傳後的文件訪問用的,跟上傳無關。
  2. 2八、29上面的就是常見的端口負載,這裏是對兩個tracker訪問文件端口8000(本身設置)負載。
  3. tracker 上安裝的 nginx 主要爲了提供 http 訪問的反向代理、負載均衡以及緩存服務。
  4. 每一臺storage服務器主機上部署Nginx及FastDFS擴展模塊,由Nginx模塊對storage存儲的文件提供http下載服務, 僅噹噹前storage節點找不到文件時會向源storage主機發起redirect或proxy動做。 

注:圖中的tracker可能爲多個tracker組成的集羣;且當前FastDFS的Nginx擴展模塊支持單機多個group的狀況

 

tracker跟蹤節點30、31

安裝配置步驟前文一致,配置tracker.conf 和client.conf,主要注意防火牆 開啓端口22122 

記得手動建立兩個文件夾:

/data/fastdfs/tracker

/data/fastdfs/client

兩個節點內容同樣。

 

storage存儲節點32到35

記得手動建立兩個文件夾:/data/fastdfs/storage

安裝步驟與上文一致,配置:

/etc/fdfs/storage.conf

group_name=group1 # 組名(第一組爲group1,第二組爲group2,依次類推...)
base_path=/data/fastdfs/storage # 數據和日誌文件存儲根目錄
store_path0=/data/fastdfs/storage # 第一個存儲目錄,第二個存儲目錄起名爲:store_path1=xxx,其它存儲目錄名依次類推...

tracker_server=192.168.80.30:22122# tracker服務器IP和端口
tracker_server=192.168.80.31:22122# tracker服務器IP和端口

 注意:

32和33  group_name=group1

34和35  group_name=group2

其餘配置同樣默認,注意防火牆 開啓端口23000。

開啓兩個tracker,並加入開機啓動:

/usr/bin/fdfs_trackerd  /etc/fdfs/tracker.conf

chkconfig fdfs_trakcerd on

開啓4個storage,並加入開機啓動:

/usr/bin/fdfs_storaged /etc/fdfs/storage.conf
chkconfig fdfs_storaged on

查看集羣狀態,會顯示總體內容:

/usr/bin/fdfs_monitor /etc/fdfs/storage.conf

測試服務

/usr/bin/fdfs_test /etc/fdfs/client.conf upload /etc/fdfs/anti-steal.jpg

以上就是FastDFS的安裝與配置。下面就與Nginx模塊結合做介紹。

將nginx和fastdfs-nginx-module上傳到 storage存儲節點32到35

fastdfs-nginx-module的做用已經在上篇介紹過,在此主要是配置。

上傳fastdfs-nginx-module.tar.gz到/opt文件夾下


解壓插件壓縮包

unzip fastdfs-nginx-module.zip

nginx編譯安裝

cd nginx

對nginx從新config

./configure \
--prefix=/opt/nginx \
--pid-path=/opt/nginx/nginx.pid \
--lock-path=/opt/nginx.lock \
--error-log-path=/opt/nginx/log/error.log \
--http-log-path=/opt/nginx/log/access.log \
--with-http_gzip_static_module \
--http-client-body-temp-path=/opt/nginx/client \
--http-proxy-temp-path=/opt/nginx/proxy \
--http-fastcgi-temp-path=/opt/nginx/fastcgi \
--http-uwsgi-temp-path=/opt/nginx/uwsgi \
--http-scgi-temp-path=/opt/nginx/scgi \
--add-module=/opt/fastdfs-nginx-module/src

 

再進行 make && make install

 

將mod_fastdfs.conf 拷貝到fdfs下進行配置

cd fastdfs-nginx-module/src
cp mod_fastdfs.conf /etc/fdfs/ #將mod_fastdfs.conf 拷貝到fdfs下進行配置

vim /etc/fdfs/mod_fastdfs.conf

base_path=/data/fastdfs/storage
tracker_server=192.168.80.30:22122
tracker_server=192.168.80.31:22122
storage_server_port=23000
group_name=group1 # 第一組storage的組名 url_have_group_name=true store_path0=/data/fastdfs/storage group_count=2 [group1] group_name=group1 storage_server_port=23000 store_path_count=1 store_path0=/data/fastdfs/storage [group2] group_name=group2 storage_server_port=23000 store_path_count=1 store_path0=/data/fastdfs/storage

 注意:

32和33  group_name=group1

34和35  group_name=group2

配置nginx

vim /opt/nginx/conf/nginx.conf

在nginx的配置文件中添加一個Server:
server {
        listen 8888;
        server_name  localhost;

        location ~/group([0-9])/M00/{
                ngx_fastdfs_module;
        }
}

 

 8888的請求且有group的都轉給 ngx_fastdfs_module 插件處理,防火牆什麼的就不說了。

啓動Nginx,並加和自啓動

shell> /opt/nginx/sbin/nginx

瀏覽器訪問測試時上傳的文件,應該都能正常訪問。
http://192.168.80.32:8888/group1/M00/00/00/CgNXGVpddOKANpzbAABdrZgsqUU369_big.jpg
http://192.168.80.34:8888/group2/M00/00/00/CgNXGVpddOKANpzbAABdrZgsqUU369_big.jpg

跟蹤節點安裝Nginx和ngx_cache_purge模塊

說明:每一個節點執行相同的操做
tracker節點:30,31
在 tracker 上安裝的 nginx 主要爲了提供 http 訪問的反向代理、負載均衡以及緩存服務。

一樣都上傳nginx和ngx_cache_purge包

解壓文件到/opt文件夾下

tar -zxvf ngx_cache_purge-2.3.tar.gz

而後對nginx進行編譯安裝

cd nginx 
./configure --prefix=/opt/nginx --sbin-path=/usr/bin/nginx --add-module=/opt/ngx_cache_purge-2.3
make && make install

 

配置Nginx,設置tracker負載均衡以及緩存

vi /opt/nginx/conf/nginx.conf

添加如下內容

    #設置緩存
    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 /data/fastdfs/cache/nginx/proxy_cache keys_zone=http-cache:100m;
#設置 group1 的服務器
upstream fdfs_group1 {
server 192.168.80.32:8888 weight=1 max_fails=2 fail_timeout=30s;
server 192.168.80.33:8888 weight=1 max_fails=2 fail_timeout=30s;
}
#設置 group2 的服務器
upstream fdfs_group2 {
server 192.168.80.34:8888 weight=1 max_fails=2 fail_timeout=30s;
server 192.168.80.35: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.80.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; 
        }
    }

 

上面配置的是當請求8000端口時根據group1仍是group2進行選擇到對應的服務器響應。

要注意的已標色。還要提早建立緩存目錄:/data/fastdfs/cache/nginx/proxy_cache

啓動Nginx

/opt/nginx/sbin/nginx
設置開機啓動: vi /etc/rc.local## 加入如下配置
/opt/nginx/sbin/nginx
chmod +x /etc/rc.local #centos7

 

文件訪問測試

前面直接經過訪問Storage節點中的Nginx訪問文件:
http://192.168.80.32:8888/group1/M00/00/00/CgNXGVpddOKANpzbAABdrZgsqUU369_big.jpg
http://192.168.80.34:8888/group2/M00/00/00/CgNXGVpddOKANpzbAABdrZgsqUU369_big.jpg
如今能夠經過Tracker中的Nginx來進行訪問:
(1)、經過 Tracker1 中的 Nginx 來訪問
http://192.168.80.30:8000/group1/M00/00/00/CgNXGVpddOKANpzbAABdrZgsqUU369_big.jpg
http://192.168.80.30:8000/group2/M00/00/00/CgNXGVpddOKANpzbAABdrZgsqUU369_big.jpg
(2)、經過 Tracker2 中的 Nginx 來訪問
http://192.168.80.31:8000/group1/M00/00/00/CgNXGVpddOKANpzbAABdrZgsqUU369_big.jpg
http://192.168.80.31:8000/group2/M00/00/00/CgNXGVpddOKANpzbAABdrZgsqUU369_big.jpg

  每個Tracker中的Nginx都單獨對後端的Storage組作了負載均衡,但整套FastDFS集羣,若是想對外提供統一的文件訪問地址,還須要對兩個Tracker中的Nginx進行HA集羣

配置Tracker服務器高可用、反向代理與負載均衡

其實上面能夠經過30或31的8000端口進行文件訪問了,下面統一端口,對30和31再進行一次負載均衡。

使用Keepalived + Nginx組成的高可用負載均衡集羣,作兩個Tracker節點中Nginx的負載均衡。

這裏Nginx也能夠用Haproxy代替。關於Keepalived和 Haproxy的安裝和介紹,在 備胎的養成記KeepAlived實現熱備負載 和 入坑系列之HAProxy負載均衡 中做過詳細描述。

這裏使用Nginx,將Keepalived 和 Nginx 安裝到 28和29上,VIP爲27。

vi /opt/nginx/conf/nginx.conf

http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
## FastDFS Tracker Proxy
upstream fastdfs_tracker {
server 192.168.80.30:8000 weight=1 max_fails=2 fail_timeout=30s; server 192.168.80.31: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 500502503504/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;
}
} 
}

 

啓動Nginx

/opt/nginx/sbin/nginx
設置開機啓動: vi /etc/rc.local## 加入如下配置 /opt/nginx/sbin/nginx chmod +x /etc/rc.local #centos7

經過VIP訪問文件

http://192.168.80.27:8000/dfs/group1/M00/00/00/CgNXGVpddOKANpzbAABdrZgsqUU369_big.jpg
http://192.168.80.27:8000/dfs/group2/M00/00/00/CgNXGVpddOKANpzbAABdrZgsqUU369_big.jpg

   以上就是整個配置過程,看起來是很是的繁雜,因此必定要先弄清整個的部署架構,而後每一個插件是用來作什麼的都要清楚,後面若是要擴展的話就有個清晰的思路。

  環境搭建只是開始,真正的使用、優化纔是重點,因此耐心的看吧。

相關文章
相關標籤/搜索