FastDFS+nginx+keepalived集群搭建


安裝環境

  nginx-1.6.2
  libfastcommon-master.zip
  FastDFS_v5.05.tar.gz(http://sourceforge.net/projects/fastdfs/files/)
  fastdfs-nginx-module_v1.16.tar.gz
  ngx_cache_purge-2.3.tar.gz(用於清除指定url的緩存

  keepalived

  VM虛擬機redhat6.5-x64:192.168.1.201、192.168.1.202、192.168.1.203、192.168.1.204、
              192.168.1.201、192.168.1.202、192.168.1.203、192.168.1.204、
  Xshell4

部署方案

  

安裝步驟

  步驟一:203、204、205、206、207、208安裝FastDFS

  參考:https://www.cnblogs.com/cac2020/p/9534967.html

  步驟二:配置跟蹤器(192.168.1.203 、192.168.1.204 節點)

  

    mkdir -p /fastdfs/tracker&& cp /etc/fdfs/tracker.conf.sample tracker.conf && vim /etc/fdfs/tracker.conf
修改內容:
    base_path=/fastdfs/tracker  
    #關鍵點:輪詢分配到各個group里
    store_lookup=0 
啟動:
    /etc/init.d/fdfs_trackerd start

  步驟三:配置FastDFS存儲(192.168.1.205-208)

  205、206作為group1 , 207、208作為group2

  

mkdir -p /fastdfs/storage && cp /etc/fdfs/storage.conf.sample storage.conf && vim /etc/fdfs/storage.conf 
修改內容:
disabled=false  
group_name=group1      #組名,第一組(205206)為 group1 第二組(207208)為 group2 
port=23000             #storage 端口號,同組的端口號必須相同 
base_path=/fastdfs/storage 
store_path_count=1     #存儲路徑個數 需要和 store_path 個數匹配 
store_path0=/fastdfs/storage #設置存儲路徑 
tracker_server=192.168.1.203:22122 #tracker 服務器的 ip 和端口 
tracker_server=192.168.1.204:22122 #多個 tracker 直接添加多條記錄 
http.server_port=8888  #設置 http 端口號
啟動:
/etc/init.d/fdfs_storaged start

  到此為止我們的 FastDFS 集群環境已經搭建完成!!!!

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

   步驟四:4個存儲節點(205 206 207 208)整合Nginx

1.安裝整合包fastdfs-nginx-module_v1.16.tar.gz
tar -zxvf /usr/local/software/fastdfs-nginx-module_v1.16.tar.gz -C /usr/local/fast/ 
vim /usr/local/fast/fastdfs-nginx-module/src/config 
修改內容:去掉第四行中的 local 文件層次

2.安裝Nginx 加載fastdfs-nginx-module模塊
tar -zxvf /usr/local/software/nginx-1.6.2.tar.gz -C /usr/local 
cd /usr/local/nginx-1.6.2 && ./configure --add-module=/usr/local/fast/fastdfs-nginx-module/src/ 
重新編譯命令: make && make install

3.修改整合模塊配置
cp /usr/local/fast/fastdfs-nginx-module/src/mod_fastdfs.conf /etc/fdfs/  && vim /etc/fdfs/mod_fastdfs.conf
修改內容:
注意:第一組(205206)和第二組(207208) 節點修改內容,只有組名不同 
connect_timeout=10 
tracker_server=192.168.1.203:22122 
tracker_server=192.168.1.204:22122 
storage_server_port=23000 
url_have_group_name = true 
store_path0=/fastdfs/storage 
group_name=group1 #第一組為 group1  第二組為 group2 
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
4.復制配置文件
cd /usr/local/fast/FastDFS/conf/ && cp http.conf mime.types /etc/fdfs/
5.創建軟連接,在/fastdfs/storage 文件存儲目錄下創建軟連接,將其鏈接到實際存放數據的目錄。 
命令: ln -s /fastdfs/storage/data/ /fastdfs/storage/data/M00 
6.修改 Nginx 配置文件4個節點 
命令:vim usr/local/nginx.conf 
修改內容:
listen 8888; 
server_name localhost; 
location ~/group([0-9])/M00 { 
    #alias /fastdfs/storage/data; 
    ngx_fastdfs_module; 
}
啟動4個節點Nginx: /usr/local/nginx/sbin/nginx

  步驟五:在2個跟蹤器節點上安裝nginx為4個存儲節點提供負載均衡

1.安裝ngx_cache_purge-2.3.tar.gz(清除指定url的緩存)
tar -zxvf /usr/local/software/ngx_cache_purge-2.3.tar.gz –C /usr/local/fast/

2.安裝Nginx  加入 ngx_cache_purge (加入清除緩存模塊)
tar -zxvf /usr/local/software/nginx-1.6.2.tar.gz -C /usr/local 
cd /usr/local/nginx-1.6.2 && ./configure --add-module=/usr/local/fast/ngx_cache_purge-2.3/src/  
重新編譯命令: make && make install

3.修改nginx配置
vim /usr/local/nginx/conf/nginx.conf
修改內容:
events { 
    worker_connections  1024;  
    use epoll; 
} 
http { 
    include       mime.types; 
    default_type  application/octet-stream; 
    sendfile        on; 
    tcp_nopush     on; 
    keepalive_timeout  65; 
    #設置緩存 
    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 Cookie $http_cookie; 
   
    proxy_set_header X-Real-IP $remote_addr; 
   
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
 
    proxy_connect_timeout 90;    
    proxy_read_timeout 90;    
    proxy_send_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-chche:200m max_size=1g inactive=30d; 
    proxy_temp_path /fastdfs/cache/nginx/proxy_cache/tmp; 
 
    # weight 權重  max_fails 失敗重試次數 fail_timeout 連接失敗超時時間 
    #設置 group1 的服務器 
    upstream fdfs_group1 { 
         server 192.168.1.205:8888 weight=1 max_fails=2 fail_timeout=30s; 
         server 192.168.1.206:8888 weight=1 max_fails=2 fail_timeout=30s; 
    } 
 
    #設置 group2 的服務器 
    upstream fdfs_group2 { 
         server 192.168.1.207:8888 weight=1 max_fails=2 fail_timeout=30s; 
         server 192.168.1.208: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;  
        } 
}


4.按以上nginx 配置文件的要求,創建對應的緩存目錄2個跟蹤器節點都需要:  
mkdir -p /fastdfs/cache/nginx/proxy_cache 
mkdir -p /fastdfs/cache/nginx/proxy_cache/tmp
啟動ngnix:/usr/local/nginx/sbin/nginx

 

  步驟六:201、202節點安裝keepalived+nginx,對203、204提供負載均衡並對外外提供統一訪問IP

1.安裝nginx
修改nginx配置
vim /usr/local/nginx/conf/nginx.conf
修改內容:
upstream fastdfs_tracker {
    server 192.168.1.203:8000 weight=1 max_fails=2 fail_timeout=30s; 
    server 192.168.1.204: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 代理路徑設置為 /fastdfs 
        location /fastdfs { 
            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; 
        } 
}

2.安裝keepalived
修改keepalived配置
參考:https://www.cnblogs.com/cac2020/p/9528106.html

 

最后注意:千萬不要使用 kill -9 命令強殺 FastDFS 進程,否則可能會導致 binlog 數據丟失  

關閉集群步驟: 
1201 202 兩台一級負載節點 
關閉 keepalived 命令:service keepalived stop 
關閉 nginx 命令:/usr/local/nginx/sbin/nginx –s stop 
 
2203 204 兩台二級負載節點,跟蹤器節點 
關閉 nginx 命令:/usr/local/nginx/sbin/nginx –s stop 
關閉 trackerd 跟蹤器命令:/etc/init.d/fdfs_trackerd stop 
 
3205 206 207 208 四台三級負載節點,存儲節點 
關閉 nginx 命令:/usr/local/nginx/sbin/nginx –s stop 
關閉 storage 存儲器命令: /etc/init.d/fdfs_storaged stop 
注意:千萬不要使用 kill -9 命令強殺 FastDFS 進程,否則可能會導致 binlog 數據丟失

 參考手冊:keepalived+nginx+fastdfs集群安裝手冊.pdf


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM