手把手教你搭建FastDFS集羣(下)

  因爲博客圖片量大,篇幅太長,所以須要分上,中,下三篇博客來寫,上篇和中篇咱們已經一塊兒學習完了,這篇博客咱們一塊兒學習下剩餘部分。php

一,配置反向代理html

       咱們須要在兩個跟蹤器上安裝的nginx(也就是192.168.156.5和192.168.156.6)以提供反向代理服務,目的是使用統一的一個IP地址對外提供服務。爲了不一些沒必要要的錯誤,咱們先把其它四臺虛擬機的窗口關掉。nginx

       1.解壓ngx_cache_purge-2.3.tar.gz,解壓命令:tar -zxvf ngx_cache_purge-2.3.tar.gz -C / usr / local / fast /,以下圖所示(另外一臺設備就不粘貼圖片了)。緩存

         解壓完以後咱們在在/ usr /本地/快/目錄下能夠看到多了一個ngx_cache_purge-2.3文件夾。以下圖所示。服務器

      2.下載須要的依賴庫,在兩臺設備上依次執行下面四條命令。cookie

 

[html] 查看純文本 session

  1. yum安裝pcre  
  2. yum安裝pcre-devel  
  3. yum安裝zlib  
  4. yum安裝zlib-devel  

     3.爲兩臺設備都安裝nginx,咱們在XShell的下方的輸入框中輸入命令:cd / usr / local / software /並敲回車,兩個窗口都會進入/ usr / local / software目錄下,而後在下面的輸入框再輸入 「LL」 來查看在/ usr /本地/軟件目錄下的文件,以下圖所示(只有輸入框左邊的圖標是多窗口的狀況下才能一次做用全部窗口,若是當前是單窗口圖標,就以下圖那樣選擇所有XSHELL)。app

 

      接着,咱們在下面的輸入框中輸入:tar -zxvf nginx-1.6.2.tar.gz -C / usr / local /並按回車,會在兩個窗口同時執行解壓操做以下圖所示。
 負載均衡

      接下來咱們在下面的輸入框中輸入:cd / usr / local並按回車,兩臺設備都進入/usr/local/nginx-1.6.2目錄下。以下圖所示。tcp

        接着,在下面的輸入框中加入模塊命令:./configure --add-module = / usr / local / fast / ngx_cache_purge-2.3,回車就會在兩臺設備上都執行添加緩存模塊並會檢查環境。

         接着在下面的輸入框中輸入命令:make && make install,回車就會在兩臺設備上都執行編譯安裝以下圖所示。

       下面咱們須要修改下兩臺設備在/ usr /本地/ nginx的/ conf目錄/目錄下的nginx.conf文件,你們能夠直接把下面代碼替換這個目錄下的該文件,也能夠直接到:HTTP://下載。 csdn .NET / detail / u012453843 / 9803673這個地址下載nginx.conf文件來替換。不過因爲咱們搭建環境的虛擬機IP可能不同,所以,咱們須要根據實際狀況修改下IP等信息(注意192.168.156.5和192.168.156.6這兩臺設備的在/ usr /本地/ nginx的/ CONF /目錄下的nginx.conf都要修改

 

[html] 查看純文本 

  1. #user nobody  
  2. worker_processes 1;  
  3.   
  4. #error_log logs / error.log;  
  5. #error_log logs / error.log notice;  
  6. #error_log logs / error.log info;  
  7.   
  8. #pid logs / nginx.pid;  
  9.   
  10.   
  11. 事件{  
  12.     worker_connections 1024;  
  13.     使用epoll  
  14. }  
  15.   
  16.   
  17. http {  
  18.     包括mime.types;  
  19.     default_type application / octet-stream;  
  20.   
  21.     #log_format main'$ remote_addr - $ remote_user [$ time_local]「$ request」'  
  22.     #'$ status $ body_bytes_sent「$ http_referer」'  
  23.     #'「$ http_user_agent」「$ http_x_forwarded_for」';  
  24.   
  25.     #access_log logs / access.log main;  
  26.   
  27.     發送文件;  
  28.     tcp_nopush on;  
  29.     #tcp_nopush on  
  30.   
  31.     #keepalive_timeout 0;  
  32.     keepalive_timeout 65;  
  33.   
  34.     #gzip on;  
  35.     #設置緩存  
  36.     server_names_hash_bucket_size 128;  
  37.     client_header_buffer_size 32k;  
  38.     large_client_header_buffers 4 32k;  
  39.     client_max_body_size 300m;  
  40.   
  41.     proxy_redirect關閉  
  42.     proxy_set_header主機$ http_host;  
  43.     proxy_set_header X-Real-IP $ remote_addr;  
  44.     proxy_set_header X-Forwarded-For $ proxy_add_x_forwarded_for;  
  45.     proxy_connect_timeout 90;  
  46.     proxy_send_timeout 90;  
  47.     proxy_read_timeout 90;  
  48.     proxy_buffer_size 16k;  
  49.     proxy_buffers 4 64k;  
  50.     proxy_busy_buffers_size 128k;  
  51.     proxy_temp_file_write_size 128k;  
  52.     #設置緩存存儲路徑,存儲方式,分別內存大小,磁盤最大空間,緩存期限  
  53.     proxy_cache_path / fastdfs / cache / nginx / proxy_cache  levels = 1 :2  
  54.     keys_zone = http -cache:200m  max_size = 1g inactive = 30d ;   
  55.     proxy_temp_path / fastdfs / cache / nginx / proxy_cache / tmp;  
  56.     #1組的服務設置  
  57.     上游fdfs_group1 {  
  58.          服務器192.168.156.7:8888  weight = 1 max_fails = 2 fail_timeout = 30s ;    
  59.          服務器192.168.156.8:8888  weight = 1 max_fails = 2 fail_timeout = 30s ;    
  60.     }  
  61.     #第2組的服務設置  
  62.     上游fdfs_group2 {  
  63.          服務器192.168.156.9:8888  weight = 1 max_fails = 2 fail_timeout = 30s ;    
  64.          服務器192.168.156.10:8888  weight = 1 max_fails = 2 fail_timeout = 30s ;    
  65.     }  
  66.   
  67.     服務器{  
  68.         聽8000;  
  69.         server_name localhost;  
  70.   
  71.         #charset koi8-r  
  72.   
  73.         #access_log logs / host.access.log main;  
  74.         #1組的負載均衡配置  
  75.         位置/ group1 / M00 {  
  76.             proxy_next_upstream http_502 http_504錯誤超時invalid_header;  
  77.             proxy_cache http-cache;  
  78.             proxy_cache_valid 200 304 12h;  
  79.             proxy_cache_key $ uri $ is_args $ args;  
  80.             #對應組1的服務設置  
  81.             proxy_pass http:// fdfs_group1;  
  82.             到期30d;  
  83.         }  
  84.   
  85.         位置/ group2 / M00 {  
  86.             proxy_next_upstream http_502 http_504錯誤超時invalid_header;  
  87.             proxy_cache http-cache;  
  88.             proxy_cache_valid 200 304 12h;  
  89.             proxy_cache_key $ uri $ is_args $ args;  
  90.             #對應組2的服務設置  
  91.             proxy_pass http:// fdfs_group2;  
  92.             到期30d;  
  93.          }  
  94.   
  95.         位置〜/ purge(/.*){  
  96.             容許127.0.0.1;  
  97.             容許192.168.156.0/24;  
  98.             拒絕全部  
  99.             proxy_cache_purge http-cache $ 1 $ is_args $ args;  
  100.         }  
  101.   
  102.         位置 / {  
  103.             根html;  
  104.             index index.html index.htm;  
  105.         }  
  106.   
  107.         #error_page 404 /404.html;  
  108.   
  109.         #將服務器錯誤頁面重定向到靜態頁面/50x.html  
  110.         #  
  111.         error_page 500 502 503 504 /50x.html;  
  112.         location  = /50x.html {  
  113.             根html;  
  114.         }  
  115.   
  116.         #代碼PHP腳本到Apache在127.0.0.1:80上監聽  
  117.         #  
  118.         #location〜\ .php $ {  
  119.         #proxy_pass http://127.0.0.1;  
  120.         #}  
  121.   
  122.         #將PHP腳本傳遞給FastCGI服務器,偵聽127.0.0.1:9000  
  123.         #  
  124.         #location〜\ .php $ {  
  125.         #root html;  
  126.         #fastcgi_pass 127.0.0.1:9000;  
  127.         #fastcgi_index index.php;  
  128.         #fastcgi_param SCRIPT_FILENAME / scripts $ fastcgi_script_name;  
  129.         #include fastcgi_params;  
  130.         #}  
  131.   
  132.         #deny訪問.htaccess文件,若是是Apache的文檔根  
  133.         #贊成nginx的  
  134.         #  
  135.         #location〜/\.ht {  
  136.         #deny all;  
  137.         #}  
  138.     }  
  139.   
  140.   
  141.     #另外一個使用IP-,基於名稱和端口配置的虛擬主機  
  142.     #  
  143.     #server {  
  144.     #聽8000;  
  145.     #聽somename:8080;  
  146.     #server_name somename alias another.alias;  
  147.   
  148.     # 位置 / {  
  149.     #root html;  
  150.     #index index.html index.htm;  
  151.     #}  
  152.     #}  
  153.   
  154.   
  155.     #HTTPS服務器  
  156.     #  
  157.     #server {  
  158.     #listen 443 ssl;  
  159.     #server_name localhost;  
  160.   
  161.     #ssl_certificate cert.pem;  
  162.     #ssl_certificate_key cert.key;  
  163.   
  164.     #ssl_session_cache shared:SSL:1m;  
  165.     #ssl_session_timeout 5m;  
  166.   
  167.     #ssl_ciphers HIGH:!aNULL:!MD5;  
  168.     #ssl_prefer_server_ciphers on;  
  169.   
  170.     # 位置 / {  
  171.     #root html;  
  172.     #index index.html index.htm;  
  173.     #}  
  174.     #}  
  175.   
  176. }                               

      修改完nginx.conf文件以後,咱們下面須要建立/ fastdfs /緩存/ nginx的/ proxy_cache和/ fastdfs /緩存/ nginx的/ proxy_cache的/ tmp目錄,這是由於咱們在nginx.conf文件中配置緩存路徑時指定了該目錄,可是這兩個目錄目前還不存在,所以咱們須要在192.168.156.5和192.168.156.6這兩臺設備上都建立下這兩個目錄,因爲涉及到多級,所以須要遞歸建立目錄,使用命令:mkdir -p / fastdfs / cache / nginx / proxy_cache和mkdir -p / fastdfs / cache / nginx / proxy_cache / tmp,以下圖所示。

 

       因爲咱們配置了兩個跟蹤器的訪問端口是8000,而咱們的防火牆是不容許訪問該端口的,所以咱們須要修改下防火牆,使其容許訪問8000端口,這個操做我在上篇和中篇都介紹過了,這裏就不囉嗦了。

       下面咱們便來啓動這兩臺設備上的nginx。啓動所用的命令是/ usr / local / nginx / sbin / nginx。啓動完後以後,可使用ps -ef | grep nginx命令來查看nginx是否正常啓動,若是看到根4027 1 0 08:18?00:00:00 nginx:master進程/ usr / local / nginx / sbin / nginx這條信息,說明正常啓動了。

 

[html] 查看純文本 

  1. [root @ itcast05 conf]#/ usr / local / nginx / sbin / nginx  
  2. [root @ itcast05 conf]#ps -ef | grep nginx  
  3. 根4027 1 0 08:18?00:00:00 nginx:主進程/ usr / local / nginx / sbin / nginx  
  4. 沒有人4028 4027 0 08:18?00:00:00 nginx:worker進程        
  5. 沒人4029 4027 0 08:18?00:00:00 nginx:緩存管理器進程  
  6. 沒有人4030 4027 0 08:18?00:00:00 nginx:緩存加載器進程  
  7. root 4032 1522 0 08:18 pts / 0 00:00:00 grep nginx  

        兩臺設備都啓動完nginx的以後,咱們再在192.168.156.5上上傳兩次次圖片,第一次返回的路徑是在組1下,第二次返回的路徑是在組2下。

 

 

[html] 查看純文本 

  1. [root @ itcast05 conf]#/ usr / bin / fdfs_upload_file /etc/fdfs/client.conf /usr/local/3.jpg  
  2. 組1 / M00 / 00/00 / wKicCFjkOVGAMlQvAAHk-VzqZ6w757.jpg  
  3. [root @ itcast05 conf]#/ usr / bin / fdfs_upload_file /etc/fdfs/client.conf /usr/local/3.jpg  
  4. 組2 / M00 / 00/00 / wKicCVjkOeaAVb0dAAHk-VzqZ6w123.jpg  
  5. [root @ itcast05 conf]#   

        因爲咱們在192.168.156.5和192.168.156.6上配置了代理,代理端口是8000,因此咱們能夠訪問這兩個IP的8000端口來訪問咱們剛纔上傳的圖片,以下圖所示(咱們訪問的http:// 192.168.156.5:8000/group1/M00/00/00/wKicCFjkOVGAMlQvAAHk-VzqZ6w757.jpg也能訪問到該圖片)。這說明咱們配置的代理徹底沒問題。

       咱們知道,nginx的對外提供服務有可能碰到服務掛掉的時候,這時候高可用就顯得異常重要了,所以如今咱們搭建一個nginx的和KEEPALIVED結合實現的nginx的集羣高可用的環境,你們能夠參考的http: //blog.csdn .net / u012453843 / article / details / 69668663這篇博客進行學習。

       咱們如今要把KEEPALIVED實現的nginx的集羣高可用應用到咱們的FastDFS集羣當中,如今用於搭建nginx的集羣高可用的設備是192.168.156.11和192.168.156.12,咱們只須要修改下這兩臺設備的nginx的。的conf文件,配置文件以下

 

[html] 查看純文本 

  1. #user nobody  
  2. worker_processes 1;  
  3.   
  4. #error_log logs / error.log;  
  5. #error_log logs / error.log notice;  
  6. #error_log logs / error.log info;  
  7.   
  8. #pid logs / nginx.pid;  
  9.   
  10.   
  11. 事件{  
  12.     worker_connections 1024;  
  13. }  
  14.   
  15.   
  16. http {  
  17.     包括mime.types;  
  18.     default_type application / octet-stream;  
  19.   
  20.     #log_format main'$ remote_addr - $ remote_user [$ time_local]「$ request」'  
  21.     #'$ status $ body_bytes_sent「$ http_referer」'  
  22.     #'「$ http_user_agent」「$ http_x_forwarded_for」';  
  23.   
  24.     #access_log logs / access.log main;  
  25.   
  26.     發送文件;  
  27.     #tcp_nopush on  
  28.   
  29.     #keepalive_timeout 0;  
  30.     keepalive_timeout 65;  
  31.   
  32.     #gzip on;  
  33.       
  34.     上游fastdfs_tracker {  
  35.        服務器192.168.156.5:8000  weight = 1 max_fails = 2 fail_timeout = 30s ;    
  36.        服務器192.168.156.6:8000  weight = 1 max_fails = 2 fail_timeout = 30s ;    
  37.     }  
  38.   
  39.     服務器{  
  40.         聽80  
  41.         server_name localhost;  
  42.   
  43.         #charset koi8-r  
  44.   
  45.         #access_log logs / host.access.log main;  
  46.   
  47.         location / fastdfs {  
  48.            根html;  
  49.            index index.html index.htm;  
  50.            proxy_pass http:// fastdfs_tracker /;  
  51.            proxy_set_header主機$ http_host;  
  52.            proxy_set_header Cookie $ http_cookie;  
  53.            proxy_set_header X-Real-IP $ remote_addr;  
  54.            proxy_set_header X-Forwarded-For $ proxy_add_x_forwarded_for;  
  55.            proxy_set_header X-Forwarded-Proto $ scheme;  
  56.            client_max_body_size 300m;  
  57.         }  
  58.   
  59.         #error_page 404 /404.html;  
  60.   
  61.         #將服務器錯誤頁面重定向到靜態頁面/50x.html  
  62.         #  
  63.         error_page 500 502 503 504 /50x.html;  
  64.         location  = /50x.html {  
  65.             根html;  
  66.         }  
  67.     }  
  68. }  


 

 

      咱們對配置文件作了兩處修改,一處是添加了負載均衡上游fastdfs_tracker,以下所示咱們是把192.168.156.5和192.168.156.6兩臺設備做爲追蹤器,如今咱們加了一層nginx來代理這兩個跟蹤器。

 

[html] 查看純文本 

  1. 上游fastdfs_tracker {  
  2.    服務器192.168.156.5:8000  weight = 1 max_fails = 2 fail_timeout = 30s ;    
  3.    服務器192.168.156.6:8000  weight = 1 max_fails = 2 fail_timeout = 30s ;    
  4. }  

      第二處修改是,添加了一個位置,而且匹配規則是路徑當中有fastdfs以下所示。

 

 

[html] 查看純文本 

  1. location / fastdfs {  
  2.            根html;  
  3.            index index.html index.htm;  
  4.            proxy_pass http:// fastdfs_tracker /;  
  5.            proxy_set_header主機$ http_host;  
  6.            proxy_set_header Cookie $ http_cookie;  
  7.            proxy_set_header X-Real-IP $ remote_addr;  
  8.            proxy_set_header X-Forwarded-For $ proxy_add_x_forwarded_for;  
  9.            proxy_set_header X-Forwarded-Proto $ scheme;  
  10.            client_max_body_size 300m;  
  11.         }  

       作好了修改以後,咱們只需從新啓動192.168.156.11和192.168.156.12這兩臺設備的nginx的便可。

 

 

[html] 查看純文本 

  1. [root @ nginx1 conf]#/ usr / local / nginx / sbin / nginx -s reload  
  2. [root @ nginx1 conf]#  

[html] 查看純文本 

  1. [root @ nginx2 conf]#/ usr / local / nginx / sbin / nginx -s reload  
  2. [root @ nginx2 conf]#  

       這樣咱們便配置好了虛擬IP,如今咱們從192.168.156.5再上傳一張圖片,以下所示。

 

 

[html] 查看純文本 

  1. [root @ itcast05 conf]#/ usr / bin / fdfs_upload_file /etc/fdfs/client.conf /usr/local/3.jpg   
  2. 組1 / M00 / 00/00 / wKicB1jqnPqARiT6AAHk-VzqZ6w956.jpg  
  3. [root @ itcast05 conf]#   

       咱們如今就用虛擬IP192.168.156.110來訪咱們剛纔上傳的圖片,只是注意在地址欄中要記得輸入fastdfs(這是咱們nginx.conf文件中位置/ fastdfs {}規則規定的)以下圖所示,發現,咱們經過虛擬IP即可以訪問咱們上傳的圖片了。這樣的好處是,對用戶來講,只須要訪問這個虛擬IP就能夠了,不用關心FastDFS集羣內部的轉發機制。

 



       這樣咱們的FastDFS集羣便搭建完了,搭建完後的集羣圖以下圖所示。這個集羣當中192.168.156.7,19​​2.168.156.8,192.168.156.9,192.168.156.10這四臺設備以8888端口對外提供服務,咱們使用追蹤器跟蹤器管理這四臺存儲設備,兩個跟蹤器的nginx的對外提供的端口號是8000,也就是咱們可使用兩臺跟蹤的任何一臺設備的IP而且使用端口8000來訪問存儲在存儲上的資源文件。其實咱們徹底能夠在兩臺跟蹤設備上搭建KEEPALIVED和nginx的相結合的高可用環境而且對外提供虛擬IP192.168.156.110和端口80來訪問資源文件。只不過這裏爲了展現多層nginx的負載均衡因此纔在192.168.156.11和192.168.156.12上專門搭建了KEEPALIVED和nginx的相結合的高可用環境,由這兩臺設備對外提供虛擬IP服務,因爲端口使用了默認的80,所以咱們在使用虛擬IP192。 168.156.110訪問圖片的時候纔不用輸入端口號的。

       備註:啓動集羣步驟

        1.啓動6臺設備(192.168.156.5,192.168.156.6,192.168.156.7,19​​2.168.156.8,192.168.156.9,192.168.156.10)的nginx的(其中192.168.156.11和192.168.156.12配置了KEEPALIVED開機自啓動,順帶會啓動nginx的,所以這兩臺設備不用啓動nginx的)

        2.啓動跟蹤器(192.168.156.5和192.168.156.6,啓動命令:/etc/init.d/fdfs_trackerd start)

        啓動存儲(192.168.156.7,19​​2.168.156.8,192.168.156.9,192.168.156.10,啓動命令:/etc/init.d/fdfs_storaged start)

        這樣FastDFS集羣便都啓動完了。

相關文章
相關標籤/搜索