深入淺出分佈式文件系統MogileFS集羣

    一,簡介   

    MogileFS是一款開源的分佈式文件存儲系統,由LiveJournal旗下的Danga Interactive公司開發。Danga團隊開發了包括 Memcached、MogileFS、Perlbal 等多個知名的開源項目。目前MogileFS的日益成熟使用此解決方法的公司越來越多,例如日本的又拍、digg、中國的豆瓣、1號店、大衆點評、搜狗和安居客等,分別爲所在的組織或公司管理着海量的圖片。和傳統網絡存儲不一樣的是分佈式文件系統是將數據分散存儲至多臺服務器上,而網絡文件系統往往是將所有的數據存放在一起,存儲服務器將成爲系統可用性的瓶頸,不能滿足大規模的存放所有數據需求。分佈式文件系統採用可擴展的系統結構,利用多臺存儲設備分別存儲,分擔服務器負荷,利用位置服務器定位存儲信息,因此不僅能夠提升系統性能同時還易於擴展。


    二、實現組件

    MogileFS主要有三個部分組成:

①server端,包括兩個部分,mogilefsd和mogstored兩個應用程序,tracker是mogilefsd的實現方式,它通過數據庫來保證元數據信息,比如站點的domian、class、host等,mogstored則爲存儲節點,默認監聽7500端口,接受客服端的文件存儲請求,在MogileFS安裝完後,要運行mogadm工具將所有的store node註冊到mogilefsd的數據庫裏,mogilefsd會對這些節點進行管理和監控。


②utils工具集,主要是MogileFS的一些管理工具如mogadm等。


③客服端API,MogileFS的API接口很多,常用的的時間方式有php,perl,python,java等,使用這些語言可進行編寫客服端程序,實現文件的備份管理功能。


    三、MogileFS的特性及原理

    MogileFS特性:

    1.應用層服務,不需要使用和興組件

    2.無單點故障,tracker爲跟蹤點,mogstored爲存儲節點,mysql爲儲存元數據節點,建議使用MySQL做高可用

    3.傳輸中立,無特殊協議,可用通過nfs和http實現通信

    4.簡單的命名空間,沒有目錄,直接存儲在存儲空間上,通過域來實現,不共享任何數據

       wKiom1g9cePzlPzaAAEbzBdIMpo196.png


    MogileFS工作原理:MogileFS主要由mogilefsd和mogstored兩應用程序提供服務,tracker節點藉助數據庫保存各個節點文件的元數據信息,保存每個域中所喲的存儲位置分佈,用於實現快速定位同時監控各個節點,通知客戶端存儲區域位置並指揮storaged節點複製數據副本,進程名爲mogilefsd監聽與7001端口。storage節點爲實際存放文件的地方,存儲節點可以是http服務器,及其其他web服務,storage節點可自動維護鍵值的對應關係,storage節點前端可使用nginx進行反代,此時需要nginx-mogilefs-module-master模塊,此服務監聽與7501端口,進程名爲mogstored.一個域中的鍵值是唯一的,一個mogilefs可以有多個域,域可以有多個存儲設備,每個設備都有數據的容器,稱之爲domain.每個存儲節點爲一個主機(host),一個主機上可以有多個存儲設備,每個設備有id號,用來定位。複製的最小單元爲class,文件屬性管理,定義文件存儲在不同的設備上的份數。


    四、MogileFS安裝及其配置實現

    安裝說明:此實驗可以僅適用兩臺服務進行,一臺服務即當做server端,又當做存儲節點同時還是mysql服務器,在每臺服務器上部署tracker管理並監控節點。

#在兩服務器上均安裝如下rpm包
yum -y install per-IO-AIO    #mogilefsd和mogstored依賴於此包,先解決依賴關係。
yum -y install MogileFS-Server-2.46-2.el6.noarch.rpm  #安裝sever端
yum -y install MogileFS-server-mogilefsd-2.46-2.el6.noarch.rpm #安裝mogilefsd 
yum -y install MogileFS-Server-mogstored-2.46-2.rl6.noarch.rpm #安裝mogstored

    創建數據庫及授權修改相應的配置文件

grant all privileges on mogdb.* to 'moguser'@'127.0.0.1' identified by 'pass'; 
grant all privileges on mogdb.* to 'moguser'@'localhost' identified by 'pass'; 
flush privileges; 
#修改mogilefsd.conf配置文件
# Enable daemon mode to work in background and use syslog
daemonize = 1
# Where to store the pid of the daemon (must be the same in the init script)
pidfile = /var/run/mogilefsd/mogilefsd.pid
# Database connection information
db_dsn = DBI:mysql:mogdb:host=127.0.0.1 #修改爲上面授權的賬號密碼及其主機
db_user = moguser #修改爲上面授權的賬號密碼及其主機
db_pass = pass  #修改爲上面授權的賬號密碼及其主機
# IP:PORT to listen on for mogilefs client requests
listen = 0.0.0.0:7001  #其他參數可根據自己需求做相應的修改
# Optional, if you don't define the port above.
conf_port = 7001
# Number of query workers to start by default.
query_jobs = 10
# Number of delete workers to start by default.
delete_jobs = 1
# Number of replicate workers to start by default.
replicate_jobs = 5
# Number of reaper workers to start by default.
# (you don't usually need to increase this)
reaper_jobs = 1
# Number of fsck workers to start by default.
# (these can cause a lot of load when fsck'ing)
#fsck_jobs = 1
# Minimum amount of space to reserve in megabytes
# default: 100
# Consider setting this to be larger than the largest file you
# would normally be uploading.
#min_free_space = 200
# Number of seconds to wait for a storage node to respond.
# default: 2
# Keep this low, so busy storage nodes are quickly ignored.
#node_timeout = 2
# Number of seconds to wait to connect to a storage node.
# default: 2
# Keep this low so overloaded nodes get skipped.
#conn_timeout = 2
# Allow replication to use the secondary node get port,
# if you have apache or similar configured for GET's
#repl_use_get_port = 1
#mogstored配置文件,一般無需修改,自行根據業務需求做決定 
maxconns = 10000
httplisten = 0.0.0.0:7500
mgmtlisten = 0.0.0.0:7501
docroot = /data/mogstored

#將上訴配置考別一份至其他主機,並且啓動服務,安裝後默認在Init.d下有啓動腳本
service mogilefsd start   
service mogstored start 
[root@centos6 mogilefs]# ss -tnl |grep *.7.* #默認mogilefsd監聽7001,mogstored監聽7501及其7501
LISTEN     0      128                       *:7500                     *:*
LISTEN     0      128                       *:7501                     *:*
LISTEN     0      128                       *:7001                     *:*
[root@node mogilefs]#
#設置數據庫,存儲元數據
mogdbsetup --dbhost=127.0.0.1 --dbpass=pass 
mogdbsetup --dbname=mogdb --dbuser=moguser --dbhost=127.0.0.1 --dbpass=pass 
mogadm host add 10.1.100.1 --ip=10.1.100.1 --port=7500 --status=alive #添加host,storaged
mogadm domain add files #添加域
mogadm domain add images 

[root@centos6 ~]# mogstats --db_dsn="DBI:mysql:mogdb:host=localhost" --db_user="moguser" --db_pass="pass" --stats="all"
Fetching statistics... (all)

Statistics for devices...  #表示此storage設備存活 
  device     host                   files     status
  ---------- ---------------- ------------ ----------
  dev1       10.1.100.1          2      alive 
  dev3       10.1.100.2          1      alive
  dev4       10.1.100.2          1      alive
  ---------- ---------------- ------------ ----------

Statistics for file ids...
  Max file id: 9

Statistics for files...  #添加的域顯示結果
  domain               class           files    size (m)  fullsize (m)
  -------------------- ----------- ---------- ----------- -------------
  files                default             1           0             0
  images               default             1           0             0
  -------------------- ----------- ---------- ----------- -------------

Statistics for replication... #默認的類示結果
  domain               class        devcount      files
  -------------------- ----------- ---------- ----------
  files                default             2          1
  images               default             2          1
  -------------------- ----------- ---------- ----------

Statistics for replication queue...
  status                      count
  -------------------- ------------
  -------------------- ------------

Statistics for delete queue...
  status                      count
  -------------------- ------------
  -------------------- ------------

Statistics for general queues...
  queue           status                      count
  --------------- -------------------- ------------
  --------------- -------------------- ------------

done
[root@node ~]#
[root@node~]# mogupload --trackers=10.1.100.1 --domain=files --key='/issue' --file='/etc/issue' #上傳文件至files域內,指明可Key和上傳的文件目錄
[root@node ~]# moglistkeys --trackers=10.1.100.1 --domain=files #顯示域內文件
/fstab.txt
/issue
[root@node ~]# mogfetch --trackers=10.1.100.1 --domain=files --key='/issue' --file='/tmp/issue' #下載域內文件至某目錄 
[root@node ~]# ll /tmp/issue
-rw-r--r--. 1 root root 47 11月 25 16:48 /tmp/issue
-bash: ogfileinfo: command not found
[root@node2 ~]# mogfileinfo --tracker=10.1.100.1 --domain=files --key='/fstab.txt'
- file: /fstab.txt
     class:              default
  devcount:                    2
    domain:                files
       fid:                    5
       key:           /fstab.txt
    length:                 1115
 -http://10.1.100.2:7500/dev4/0/000/000/0000000005.fid        #可通過此URL訪問到相應的資源
 -http://10.1.100.1:7500/dev1/0/000/000/0000000005.fid        #可通過此URL訪問到相應的資源
[root@node2 ~]#


    實驗結果圖示:

   wKiom1g9hfLR3Jb5AABlKP1yDyQ907.png

    五、實戰nginx前端反代mogfilefs及負載均衡

    要求說明:在前期那配置nginx做反代將用戶的請求調度至後端的MogileFS,此實驗依賴於nginx-mogilefs-module模塊,需在編譯時加上此模塊。

#實戰配置如下所示:
#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;
    #
	upstream mogtrackers { #定義一個upstream組
		server 10.1.100.1:7001;
		server 10.1.100.2:7001;
	}

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }
        location /images/ {
			mogilefs_tracker mogtrackers; #在此調用此mogtrackers即可實現反代至mogilefs後端
		        mogilefs_domain images; #域爲images域 
			mogilefs_methods get;   #請求的方法爲get

			mogilefs_pass {
				proxy_pass $mogilefs_path;
				proxy_hide_header Content-Type;
				proxy_buffering off;
			}
			expires 1h;
        }

        #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;
    #    }
    #}

}


    圖示:此時可通過http://uri/images/aqua.jpg進行訪問,而不是原來很長的路徑名進行訪問。

wKiom1g9jpeBs6vPAAZv02hKaYM494.png




    總結:在大型站點中,mogilefs的重要性不言而喻,尤其是圖片存儲量很大的網站例如:某寶,某東等,此配置過程中,mysql易於成爲單點故障,所以可將其做主從複製,同時結合MySQL半同步複製,可降低數據的丟失風險。在配置nginx做反向代理時,大部分步驟和其他反代類是不同的是,需將模塊編譯進nginx中才能使用此模塊功能。




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