1. 程式人生 > >高可用高性能分布式文件系統FastDFS進階keepalived+nginx對多tracker進行高

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

type module chmod def expire 空間 根據 權限 原理

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

  高可用是實現了,但由於我們只設置了一個group,如果現在有5臺服務器那將會出現5臺只有一個group,每臺服務器內的文件內容都相同(互備份)如下圖,會造成資源浪費。

技術分享圖片

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

FastDFS集群服務器分布

技術分享圖片

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

我們將按上面的架構分服務器:

虛擬VIP用192.168.80.27

keepalived+nginx 熱備、負載均衡 : 28、29

tracker:

30和31服務器

storage:

group1:32、33

group2:34、35

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

  1. 首先所有的Nginx都是用於上傳後的文件訪問用的,跟上傳無關。

  2. 28、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=.:tracker_server=.:storage_server_port=url_have_group_name=store_path0=/data/fastdfs/storage
group_count=[group1]
group_name=group1
storage_server_port=store_path_count=store_path0=/data/fastdfs/storage
[group2]
group_name=group2
storage_server_port=store_path_count=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.3make && 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  
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

  以上就是整個配置過程,看起來是非常的繁雜,所以一定要先弄清整個的部署架構,然後每個插件是用來做什麽的都要清楚,後面如果要擴展的話就有個清晰的思路。

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


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