分佈式存儲 CentOS6.5虛擬機環境搭建FastDFS-5.0.5集羣

正文:

      本次的目的是利用VMware搭建一個屬於自己的hadoop集羣。本次我們選擇的是VMware10,具體的安裝步驟大家可以到網上搜索,資源很多。

      在閱讀本篇文章之前,你最好對FastDFS有一個初步的瞭解,我建議可以看看下面的鏈接文章中的介紹:

      http://blog.csdn.net/poechant/article/details/6977407

      第一步,確定目標:

      Tracker  192.168.224.20:22122  CentOS

      Group1-Storage11   192.168.224.25:23000  CentOS

      Group1-Storage12   192.168.224.26:23000  CentOS

      Group2-Storage21   192.168.224.28:23001  CentOS

      Group2-Storage22   192.168.224.29:23001  CentOS

      雖然上述集羣結構中tracker是單一節點,如果在生產環境中,這是不正確的,但現在我們主要是想通過這次的搭建熟悉基本的搭建流程,所以此處就暫時忽略吧。

 

      第二步,確定安裝版本:

    

      如上圖可以看出,截止到2015年5月17日,fastdfsz的最新版本是5.05,區別於以往版本,這個版本將公共的一些函數等單獨封裝成了libfastcommon這個jar包,所以在安裝fastdfs之前,我們必須先安裝libfastcommon。

      同時,我們搭建完的分佈式環境,還需要nginx幫忙實現http訪問以及負載均衡、緩存等功能,涉及了nginx-1.7.8.tar.gz,fastdfs-nginx-module_v1.16.tar.gz,ngx_cache_purge-2.1.tar.gz

      PS:順道分享一個網址,這個網址中有很多nginx的模塊包,可查詢下載:  http://labs.frickle.com/files/

 

      第三步,安裝虛擬機環境:

      如果沒有搭建過虛擬機環境,可以參考我的另外兩篇博客http://www.cnblogs.com/PurpleDream/p/4263465.htmlhttp://www.cnblogs.com/PurpleDream/p/4263421.html ,主要講解了如何安裝一個CentOS6.5的虛擬機以及如何配置虛擬機的網絡,還有如何根據已有的虛擬機,克隆多臺虛擬機。

     

      第四步,文件夾初始化: 

      按照我們第一步確認的目標,我們可以現在自由選擇一個路徑,創建一些文件夾目錄,這些目錄後邊會在配置tracker和storage的過程中被用到。我在用虛擬機創建的時候,是在/opt這個目錄下面創建相關的文件夾,詳細如下,僅供參考:

      1. 配置tracker所需的base_path: /opt/fastdfs_tracker。

      2. 配置storage所需的日誌目錄: /opt/fastdfs_storage_info。 備註: 這個目錄是用來存儲storage之間同步文件等日誌的

      3. 配置storage所需的存儲文件目錄: /opt/fastdfs_storage_data。備註: 這個目錄是用來存儲文件的

      其實,我們在使用過程中並不是每個物理機上都會同時部署tracker和storage,那麼根據自己的實際情況,如果只部署storage,則就創建storage的那兩個文件夾;反之則只創建tracker的那個文件夾。後邊在解釋配置文件如何配置的過程中我將會按照上邊的目錄路徑做配置,大家可以比對着看每個路徑對應哪個配置。

 

      第五步,安裝libfastcommon-1.0.7.zip:

      在安裝libfastcommon的過程中,涉及瞭解壓縮、make安裝等過程,第一次安裝的時候,可能由於環境的原因,會提示比如沒有安裝unzip、zip;沒有安裝perl;沒有安裝gcc等,逐一安裝即可。

      1. 利用unzip解壓縮,如果提示沒有安裝 ,則按照下圖中的命令安裝即可:

      

      2. 解壓完成後,進入文件夾,會看到make.sh文件,執行./make.sh命令,可能會報沒有安裝gcc和perl的錯誤,截圖如下:

      

      3. 爲了解決2中的問題,安裝gcc和perl:

          a. 執行 yum -y install gcc 和 yum -y install gcc-c++ 這了兩個命令安裝

          b. 安裝perl時,我們可以直接用wget命令安裝,我這裏安裝的版本是perl-5.20.2。

              可以參考這篇文章安裝perl http://blog.sina.com.cn/s/blog_68158ebf0100ndes.html 

              wget http://www.cpan.org/src/5.0/perl-5.20.2.tar.gz

              tar zxvf perl-5.20.2.tar.gz               

              mkdir /usr/local/perl

              ./Configure -des -Dprefix=/usr/local/perl -Dusethreads -Uversiononly

              make

              make install

              perl -version   

       4.  安裝完perl後,重新依次執行 ./make.sh 和 ./make.sh install 這兩個命令,完成libfastcommon的安裝。

             

       5.  注意,上述安裝的路徑在/usr/lib64/,但是FastDFS主程序設置的lib目錄是/usr/local/lib,所以需要創建軟連接如下: 

             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-5.05.tar.gz:

      前邊幾步如果安裝的都正確的話,這一步就比較簡單了,解壓縮fastdfs-5.05.tar.gz,然後依次執行 ./make.sh 和 ./make.sh install 這兩個命令,過程中沒有報錯,並且打開 /etc/fdfs 這個目錄發現有配置文件存在,就說明安裝成功了。

             

 

 

      第七步,配置tracker:

      前邊的這六步無論是配置tracker還是配置storage都是必須的,而tracker和storage的區別主要是在安裝完fastdfs之後的配置過程中。我們在第一步確認目標時,計劃在192.168.224.20這臺虛擬機上配置tracker,所以我們就在這臺機器上演示。

      1. 進入/etc/fdfs文件夾,執行命令: cp  tracker.conf.sample  tracker.conf。

      2. 編輯tracker.conf,執行命令: vi  tracker.conf ,將以下幾個選項進行編輯:

          a. disabled=false            #啓用配置文件

          b. port=22122                #設置tracker的端口號,一般採用22122這個默認端口

          c. base_path=/opt/fastdfs_tracker   #設置tracker的數據文件和日誌目錄(預先創建)

          d. http.server_port=8080     #設置http端口號   注意,這個配置在fastdfs5.05這個版本中已經不用配置,不用管這個!

          

      3. 啓動tracker,執行如下命令: /usr/local/bin/fdfs_trackerd  /etc/fdfs/tracker.conf  restart

          注意,一般fdfs_trackerd等命令在/usr/local/bin中沒有,而是在/usr/bin路徑下,所以命令修改如下: /usr/bin/fdfs_trackerd  /etc/fdfs/tracker.conf  restart

      4. 啓動完畢後,可以通過以下兩個方法查看tracker是否啓動成功:

          a. netstat -unltp|grep fdfs,查看22122端口監聽情況 

          b. 通過以下命令查看tracker的啓動日誌,看是否有錯誤: tail -100f  /opt/fastdfs_tracker/logs/trackerd.log

      5. 如果啓動沒有問題,可以通過以下步驟,將tracker的啓動添加到服務器的開機啓動中:

          a. 打開文件 vi /etc/rc.d/rc.local

          b. 將如下命令添加到該文件中 /usr/bin/fdfs_trackerd  /etc/fdfs/tracker.conf  restart

 

 

      第八步,配置storage:

      其實配置storage和配置tracker類似,只不過配置文件和配置內容不一樣。我們以配置192.168.224.29配置storage爲例。

      1. 進入/etc/fdfs文件夾,執行命令: cp  storage.conf.sample  storage.conf。

      2. 編輯storage.conf,執行命令: vi  storage.conf ,將以下幾個選項進行編輯:

           a. disabled=false            #啓用配置文件

           b. group_name=group2    #組名,根據實際情況修改

           c. port=23001     #設置storage的端口號,默認是23000,同一個組的storage端口號必須一致

           d. base_path=/opt/fastdfs_storage_info    #設置storage的日誌目錄(需預先創建)

           e. store_path_count=1   #存儲路徑個數,需要和store_path個數匹配

           f.  store_path0=/opt/fastdfs_storage_data   #存儲路徑

           g. tracker_server=192.168.224.20:22122   #tracker服務器的IP地址和端口號

           h. http.server_port=8080     #設置http端口號   注意,這個配置在fastdfs5.05這個版本中已經不用配置,不用管這個!

                      

           

           

      3. 啓動storage,執行如下命令: /usr/local/bin/fdfs_storage  /etc/fdfs/storage.conf  restart

          注意,一般fdfs_storage等命令在/usr/local/bin中沒有,而是在/usr/bin路徑下,所以命令修改如下: /usr/bin/fdfs_storage  /etc/fdfs/storage.conf  restart

      4. 啓動完畢後,可以通過以下兩個方法查看storage是否啓動成功:

          a. netstat -unltp|grep fdfs,查看23001端口監聽情況 

          b. 通過以下命令查看storage的啓動日誌,看是否有錯誤: tail -100f  /opt/fastdfs_storage_info/logs/storage.log

      5. 啓動成功後,可以通過fdfs_monitor查看集羣的情況,即storage是否已經註冊到tracker服務器中

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

          查看192.168.224.29:23001 是ACTIVE狀態即可

      6. 如果啓動沒有問題,可以通過以下步驟,將storage的啓動添加到服務器的開機啓動中:

          a. 打開文件 vi /etc/rc.d/rc.local

          b. 將如下命令添加到該文件中 /usr/bin/fdfs_storage  /etc/fdfs/storage.conf  restart

      

===============================================================長長的分割線====================================================================   

      以上八步其實已經完成了fastdfs的配置,如果此時你用java等api編輯客戶端,實際上就可以完成文件的上傳、同步和下載。但是爲什麼網上還會有很多人說需要nginx呢???其實主要原因時因爲,我們可以通過配置nginx爲下載提供基於http協議的下載等功能。

      其實,storage中安裝nginx,主要是爲了爲提供http的訪問服務,同時解決group中storage服務器的同步延遲問題。而tracker中安裝nginx,主要是爲了提供http訪問的反向代理、負載均衡以及緩存服務 

 

      第九步,安裝nginx的準備:

      不管是在tracker中還是storage中安裝nginx,前提都需要安裝一些基礎軟件。一些大公司的服務器默認都會初始化這些軟件,但是你在配置的時候可能還是最好自己利用命令確認一下。

      1. yum install -y gcc  這個前邊在安裝libfastcommon之前已經安裝了

      2. yum install -y gcc-c++ 這個前邊在安裝libfastcommon之前已經安裝了

      3. yum install -y pcre pcre-devel

      4. yum install -y zlib zlib-devel

      5. yum install -y openssl openssl-devel

      

      第十步,在storage中安裝nginx:

      1. 創建nginx默認的安裝文件夾: mkdir /usr/local/nginx

      2. 提前將 nginx1.7.8.tar.gz 和 fastdfs-nginx-module_v1.16.tar.gz 解壓縮,然後進入nginx1.7.8的文件夾目錄,執行如下命令:

          ./configure  --prefix=/usr/local/nginx  --add-module=/myself_settings/fastdfs-nginx-module/fastdfs-nginx-module/src

      3. 執行完上述命令,如果沒有報錯的話,我們繼續執行 make 命令,此時編譯有可能會報錯,那是因爲我們忘了做一項重要的工作,我們可以參考下面這篇文章http://bbs.chinaunix.net/thread-4163021-1-1.html中的解答,其實彙總就是下面兩個意思:

          a. 我們在解壓縮fastdfs-nginx-module_v1.16.tar.gz之後,需要進入fastdfs-nginx-module/src目錄,編輯config文件,找到包含CORE_INCS這個一行,將路徑中local全部去掉,變爲CORE_INCS="$CORE_INCS /usr/include/fastdfs /usr/include/fastcommon/"

          b. 建立軟連接,這個軟連接我們在第五步安裝libfastcommon時的最後一個操作就已經建立了,所以此處不用再建立了。

      4. 執行完3個步驟後,我們在重複執行2中的命令,然後再依次執行 make 和 make install 這兩個命令,沒有報錯誤就是安裝成功了。

      5. 執行命令 cd /usr/local/nginx/conf,編輯 nginx.conf 這個文件,編輯如下:

          listen       8080;

          在server段中添加: 

          location ~/group[1-3]/M00{

                 root/fdfs/storage/data;

                 ngx_fastdfs_module;

          }

          注意,如果配置的storage是在group2組,則下面的location應該是 ~/group2/M00

          

      6. 執行命令 cd /myself_settings/fastdfs5.0.5/fastdfs-5.05/conf,即進入fastdfs5.0.5的安裝文件夾的conf目錄下,將目錄下面的http.conf和mime.types拷貝到/etc/fdfs/下,如果不執行這一步,後邊在啓動nginx時會報錯。

      7. 執行命令 cd /myself_settings/fastdfs_nginx_module/fastdfs-nginx-module/src,即進入fastdfs-nginx-module_v1.16的安裝文件夾的src目錄下,將目錄下面的mod_fastdfs.conf這個文件拷貝到 /etc/fdfs 目錄下。

      8. 打開 /etc/fdfs 這個目錄,編輯 mod_fastdfs.conf 這個文件,如下:

          注意以下配置是group2的第二個storage 192.168.224.29的配置,按照第一步確定的集羣目標來配置的,供大家參考。

          a. base_path=/opt/fastdfs_storage_info   #保存日誌目錄

          b. tracker_server=192.168.224.20:22122   #tracker服務器的IP地址以及端口號

          c. storage_server_port=23001   #storage服務器的端口號

          d. group_name=group2  #當前服務器的group名

          c. url_have_group_name= true        #文件url中是否有group名

          d. store_path_count=1          #存儲路徑個數,需要和store_path個數匹配

          e. store_path0=/opt/fastdfs_storage_data         #存儲路徑

          f.  http.need_find_content_type=true     #從文件擴展名查找文件類型(nginx時爲true)  注意:這個配置網上一般都會列出,但是在fastdfs5.05的版本中是沒有的

          h. group_count= 2       #設置組的個數

          i.  在文件的末尾,按照第一步確定的集羣目標,追加如下圖的配置:

           

      9. 建立軟連接 ln  -s  /opt/fastdfs_storage_data/data  /opt/fastdfs_storage_data/data/M00

      10. 執行命令啓動nginx: /usr/local/nginx/sbin/nginx ,如果/usr/local/nginx/logs/error.log中沒有報錯,同時訪問192.168.224.29:8080這個url能看到nginx的歡迎頁面。

 

      第十一步,在tracker中安裝nginx:

      注意,tracker中nginx安裝時比storage中的nginx安裝時多安裝一個nginx的緩存模塊,同時在配置的時候有很大的不同。

      1. 創建nginx默認的安裝文件夾: mkdir /usr/local/nginx

      2. 提前將 nginx1.7.8.tar.gz、fastdfs-nginx-module_v1.16.tar.gz、ngx_cache_purge-2.1.tar.gz解壓縮,然後進入nginx1.7.8的文件夾目錄,執行如下命令:

          ./configure  --prefix=/usr/local/nginx  --add-module=/myself_settings/fastdfs-nginx-module/fastdfs-nginx-module/src  --add-module=/myself_settings/ngx_cache_purge/ngx_cache_purge-2.1

      3. 執行完上述命令,如果沒有報錯的話,我們繼續執行 make 命令,此時編譯有可能會報錯,那是因爲我們忘了做一項重要的工作,我們可以參考下面這篇文章http://bbs.chinaunix.net/thread-4163021-1-1.html中的解答,其實彙總就是下面兩個意思:

          a. 我們在解壓縮fastdfs-nginx-module_v1.16.tar.gz之後,需要進入fastdfs-nginx-module/src目錄,編輯config文件,找到包含CORE_INCS這個一行,將路徑中local全部去掉,變爲CORE_INCS="$CORE_INCS /usr/include/fastdfs /usr/include/fastcommon/"

          b. 建立軟連接,這個軟連接我們在第五步安裝libfastcommon時的最後一個操作就已經建立了,所以此處不用再建立了。

      4. 執行完3個步驟後,我們在重複執行2中的命令,然後再依次執行 make 和 make install 這兩個命令,沒有報錯誤就是安裝成功了。

      5. 執行命令 cd /usr/local/nginx/conf,編輯 nginx.conf 這個文件,編輯如下: 

複製代碼
#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;
    
    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 /opt/cache/nginx/proxy_cache levels=1:2 
    keys_zone=http-cache:500m max_size=10g inactive=30d;
    proxy_temp_path /opt/cache/nginx/proxy_cache/tmp;

    upstream fdfs_group1 {
         server 192.168.224.25:8080 weight=1 max_fails=2 fail_timeout=30s;
         server 192.168.224.26:8080 weight=1 max_fails=2 fail_timeout=30s;
    }
    
    upstream fdfs_group2 {
         server 192.168.224.28:8080 weight=1 max_fails=2 fail_timeout=30s;
         server 192.168.224.29:8080 weight=1 max_fails=2 fail_timeout=30s;
    }

    server {
        listen       8080;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        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.224.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;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }

    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}
複製代碼

      6. 執行命令啓動nginx: /usr/local/nginx/sbin/nginx ,如果/usr/local/nginx/logs/error.log中沒有報錯,同時訪問192.168.224.20:8080這個url能看到nginx的歡迎頁面。

 

      第十二步,測試上傳:

      1. 打開 /etc/fdfs 文件夾,編輯 client.conf 文件,編輯內容如下:

          a. base_path=/opt/fastdfs_tracker      #存放路徑

          b. tracker_server=192.168.224.20:22122          #tracker服務器IP地址和端口號

          c. http.tracker_server_port=8080              #tracker服務器的http端口號,注意,這個配置在fastdfs5.0.5中已經沒有用了

      2. 模擬上傳文件,執行如下命令: /usr/bin/fdfs_upload_file  /etc/fdfs/client.conf  /opt/1.txt

          使用瀏覽器訪問返回的url: http://192.168.224.20:8080/group1/M00/00/00/wKjgGlVYgi6AAv3tAAAADv4ZzcQ572.txt

          也可以直接訪問文件所在的storage: http://192.168.224.25:8080/group1/M00/00/00/wKjgGlVYgi6AAv3tAAAADv4ZzcQ572.txt

          上述如果訪問成功,會在 tracker 192.168.224.20 的 /opt/cache中產生緩存

          我們還可以通過在url中添加purge清除緩存,例如: http://192.168.224.20:8080/purge/group1/M00/00/00/wKjgGlVYgi6AAv3tAAAADv4ZzcQ572.txt

 

       參考資料:

       http://blog.csdn.net/lynnlovemin/article/details/39398043    fastdfs集羣的配置教程

       http://blog.csdn.net/poechant/article/details/6977407   fastdfs系列教程  

       http://m.blog.csdn.net/blog/hfty290/42030339    tracker-leader的選舉

 

       轉自 http://www.cnblogs.com/PurpleDream/p/4510279.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章