Mogilefs介紹與安裝步驟


MogileFS簡介:

MogileFS是一個開源的分佈式文件存儲系統,由LiveJournal旗下的Danga Interactive公司開發。Danga團隊開發了包括 Memcached、MogileFS、Perlbal 等多個知名的開源項目。目前使用MogileFS 的公司非常多,如日本排名先前的幾個互聯公司及國內的yupoo(又拍)、digg、豆瓣、1號店、大衆點評、搜狗和安居客等,分別爲所在的組織或公司管理着海量的圖片。

 

MogileFS特性
    1   工在於應用層
    2   無單點,三大組件(tracker,mogstore,database)皆可實現高可用;
    3   自動完成文件複製,制的最小單位不是文件,而是class;基於不同的class,文件可以被自動的複製到多個有足夠存儲空間的存儲節點上;
    4   傳輸無需特殊協議,可以通過NFS或HTTP協議進行通信;
    5   名稱安間簡單,文件通過一個給定的key來確定,是一個全局的命名空間;沒有目錄,基於域實現文件隔離;
    6   不共享任何數據,每個存儲節點只需維護自己所屬的存儲設備(device)即可;

 

MogileFS由部分組成:
  (1) server:主要包括mogilefsd和mogstored兩個應用程序。
    mogilefsd實現的是tracker,它通過數據庫來保存元數據信息,包括站點domain、class、host等;
    mogstored是存儲節點(store node),它其實是個WebDAV服務,默認監聽在7500端口,接受客戶端的文件存儲請求。
  (2) utils(工具集):主要是MogileFS的一些管理工具,例如mogadm等。
    在MogileFS安裝完後,要運行mogadm工具將所有的store node註冊到mogilefsd的數據庫裏,mogilefsd會對這些節點進行管理和監控。
  (3) 客戶端API:MogileFS的客戶端API很多,例如Perl、PHP、Java、Python等,用這個模塊可以編寫客戶端程序,實現文件的備份管理功能等。

  

存儲主機(節點)
    這個是 MogileFS 存儲文件存放在這些機器上,也是 mogstored 節點,也叫 Storage Server,一臺存儲主要都要啓動一個 mogstored 服務.擴容就是增加這些機器.

 

設備(device)
    一個存儲節點,以就是上面的主機,可以有多個 device, 就是用來存放文件的目錄(例如掛載的目錄),每個設備都有一個設備id,需要在 mogstored 的配置文件中的 docroot 配置的項目 指定的目錄下面創建相應的設備的目錄,目錄名爲 $docroot/dev$id,設備是不能刪除的.只能將其設備的狀態的值置爲dead,當一個設備 dead 之後,就真的 dead了,裏面的數據也無法恢復了,且這個dead了的設備的 id 也不能再用.


wKioL1aQURKiNkh8AADjM3j1jDA920.png


安裝Mogilefs簡單步驟:

準備環境:

1.創建3臺centos6.7服務器。其中1臺裝有mariadb5.5.47版本數據庫。兩外兩臺做 mogstored節點。

先安裝epel源,解決安裝時依賴關係。

wKiom1aQ9VShGaOzAABQ1hdiQaI931.jpg

1.    配置146,tracker+mysql配置

準備安裝包:

root@localhost mogilefs]# ll
total 636
-rw-r--r--. 1 root root   1916 Jan  9 03:57 MogileFS-Server-2.46-2.el6.noarch.rpm
-rw-r--r--. 1 root root 176308 Jan  9 03:57 MogileFS-Server-mogilefsd-2.46-2.el6.noarch.rpm
-rw-r--r--. 1 root root  26872 Jan  9 03:57 MogileFS-Server-mogstored-2.46-2.el6.noarch.rpm
-rw-r--r--. 1 root root  75916 Jan  9 03:57 MogileFS-Utils-2.19-1.el6.noarch.rpm
-rw-r--r--. 1 root root   5880 Jan  9 03:57 Perlbal-1.78-1.el6.noarch.rpm
-rw-r--r--. 1 root root   1624 Jan  9 03:57 Perlbal-doc-1.78-1.el6.noarch.rpm
-rw-r--r--. 1 root root  30312 Jan  9 03:57 perl-MogileFS-Client-1.14-1.el6.noarch.rpm
-rw-r--r--. 1 root root  25140 Jan  9 03:57 perl-Net-Netmask-1.9015-8.el6.noarch.rpm
-rw-r--r--. 1 root root 268620 Jan  9 03:57 perl-Perlbal-1.78-1.el6.noarch.rpm

2.安裝
yum --nogpgcheck localinstall *.rpm



3.檢查配置
[root@localhost ~]# rpm -ql MogileFS-Server          #象徵性的文檔
(contains no files)
[root@localhost ~]# rpm -ql MogileFS-Server-mogilefsd
/etc/mogilefs/mogilefsd.conf        #主配置文件
/etc/rc.d/init.d/mogilefsd
/usr/bin/mogdbsetup                 #數據庫初始化工具
/usr/bin/mogilefsd                  #守護進程對應的主文件
/usr/share/man/man1/mogilefsd.1.gz
/usr/share/man/man3/MogileFS::Checksum.3pm.gz
/usr/share/man/man3/MogileFS::Class.3pm.gz
/usr/share/man/man3/MogileFS::DevFID.3pm.gz
/usr/share/man/man3/MogileFS::Device.3pm.gz
/usr/share/man/man3/MogileFS::Domain.3pm.gz
/usr/share/man/man3/MogileFS::FID.3pm.gz
/usr/share/man/man3/MogileFS::Factory.3pm.gz
/usr/share/man/man3/MogileFS::Host.3pm.gz
/usr/share/man/man3/MogileFS::IOStatWatcher.3pm.gz
/usr/share/man/man3/MogileFS::Overview.3pm.gz



4.配置遠程用戶:

遠程配置mysql用戶:with grant option 選項表示允許該用戶有權限授權其他用戶。 

MariaDB [(none)]> GRANT ALL ON *.* TO 'root'@'192.168.%.%' IDENTIFIED BY '123..com' WITH GRANT OPTION;
MariaDB [(none)]> flush privileges;



5.初始化配置:

[root@localhost mogilefs]# mogdbsetup --help
Usage: mogdbsetup [opts] Options:                   Default      Description                  ============ =========================================== --verbose        <off>        Be verbose about what's happening.  --dbhost=        localhost    hostname or IP to database server.  --dbport=        dbd default  port number to database server.  --dbname=        mogilefs     database name to create/upgrade.  --dbrootuser=    root         Database administrator username.  Only needed                               for initial setup, not subsequent upgrades.  --dbrootpass=    <blank>      Database administrator password.  Only needed                               for initial setup, not subsequent upgrades.  --dbuser=        mogile       Regular database user to create and/or use                               for MogileFS database.  This is what the                               mogilefsd trackers connect as.  --dbpass=        <blank>      You should change this, especially if your                               database servers are accessible to other users                               on the network.  But they shouldn't be                               if you're running MogileFS, because MogileFS                               assumes your network is closed.   --type=         MySQL        Which MogileFS::Store implementation to use.                               Available: MySQL, Postgres   --yes                        Run without questions. [root@localhost mogilefs]#


配置用戶:

[root@localhost mogilefs]# mogdbsetup --dbhost=192.168.1.146 --dbrootuser=root --dbrootpass=123..com --dbname=mogdb --dbuser=moguser --dbpass=mogpass
This will attempt to setup or upgrade your MogileFS database.
It won't destroy existing data.
Run with --help for more information.  Run with --yes to shut up these prompts.
Continue? [N/y]: y
Create/Upgrade database name 'mogdb'? [Y/n]: y
Grant all privileges to user 'moguser', connecting from anywhere, to the mogilefs database 'mogdb'? [Y/n]: y
[root@localhost mogilefs]#



此時數據庫有創建好的mogdb:

MariaDB [mogdb]> show tables;
+----------------------+
| Tables_in_mogdb      |
+----------------------+
| checksum             |
| class                |
| device               |
| domain               |
| file                 |
| file_on              |
| file_on_corrupt      |
| file_to_delete       |
| file_to_delete2      |
| file_to_delete_later |
| file_to_queue        |
| file_to_replicate    |
| fsck_log             |
| host                 |
| server_settings      |
| tempfile             |
| unreachable_fids     |
+----------------------+
17 rows in set (0.00 sec)
MariaDB [mogdb]>


5.編輯配置文件:先複製一份,良好的習慣。
# 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=192.168.1.146
db_user = moguser
db_pass = mogpass
# IP:PORT to listen on for mogilefs client requests
listen = 192.168.1.146: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 = 2
# 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


配置完成,啓動服務:

[root@localhost mogilefs]# service mogilefsd start
Starting mogilefsd                                         [  OK  ]
[root@localhost mogilefs]# ss -tnl
State      Recv-Q Send-Q              Local Address:Port                Peer Address:Port 
LISTEN     0      50                              *:3306                           *:*     
LISTEN     0      128                            :::22                            :::*     
LISTEN     0      128                             *:22                             *:*     
LISTEN     0      128                 192.168.1.146:7001                           *:*     
LISTEN     0      100                           ::1:25                            :::*     
LISTEN     0      100                     127.0.0.1:25                             *:*     
[root@localhost mogilefs]#


6.配置109,101兩個節點:

安裝方法同上面,注需要單獨安裝

yum -y install perl-IO-AIO


打開配置文件:

[root@localhost ~]# vim /etc/mogilefs/mogstored.conf
maxconns = 10000     #最大併發連接數
httplisten = 0.0.0.0:7500         #http協議
mgmtlisten = 0.0.0.0:7501     #tracter來監測mogstored健康狀態的
docroot = /mogstore            #數據存放目錄,一般是新磁盤掛在目錄


兩個節點準備一個文件目錄來存放數據,這裏直接使用系統根下創建:

節點2:mkdir /var/mogdata/dev1 -p
節點3:mkdir /var/mogdata/dev2 -p
[root@localhost mogstore]# chown -R mogilefs.mogilefs /mogstore/   #一定要將目錄屬主,屬組改爲mogilefs,否則無法識別不能認到。



配置mogstored.conf,另外一個節點配置相同:

maxconns = 10000
httplisten = 0.0.0.0:7500
mgmtlisten = 0.0.0.0:7501
docroot = /mogstore


配置好後啓動,另外一個節點相同:

[root@localhost dev1]# service mogstored start
Starting mogstored                                         [  OK  ]
[root@localhost dev1]#

[root@localhost dev1]# ss -tnl
State      Recv-Q Send-Q              Local Address:Port                Peer Address:Port
 
LISTEN     0      128                            :::22                            :::*    
LISTEN     0      128                             *:22                             *:*    
LISTEN     0      64                             :::23                            :::*    
LISTEN     0      100                           ::1:25                            :::*    
LISTEN     0      100                     127.0.0.1:25                             *:*    
[root@localhost dev1]#


7.確保兩個節點加入到當前集羣中:

在節點1利用管理工具mogadm 來管理節點,首先檢查是否正常,並且用查看命令看一下:

[root@localhost dev1]# mogadm --trackers=192.168.1.146:7001 check
Checking trackers...
  192.168.1.146:7001 ... OK
Checking hosts...
No devices found on tracker(s).
[root@localhost dev1]#


詳細的查看命令:
[root@localhost dev1]# mogstats --db_dsn="DBI:mysql:mogdb:host=192.168.1.146" --db_user="moguser" --db_pass="mogpass" --stats="all"
Fetching statistics... (all)
Statistics for devices...
  device     host                   files     status
  ---------- ---------------- ------------ ----------
  ---------- ---------------- ------------ ----------
Statistics for file ids...
  Max file id: none
Statistics for files...
  domain               class           files    size (m)  fullsize (m)
  -------------------- ----------- ---------- ----------- -------------
  -------------------- ----------- ---------- ----------- -------------
Statistics for replication...
  domain               class        devcount      files
  -------------------- ----------- ---------- ----------
  -------------------- ----------- ---------- ----------
Statistics for replication queue...
  status                      count
  -------------------- ------------
  -------------------- ------------
Statistics for delete queue...
  status                      count
  -------------------- ------------
  -------------------- ------------
Statistics for general queues...
  queue           status                      count
  --------------- -------------------- ------------
  --------------- -------------------- ------------
done
[root@localhost dev1]#

mogstats --db_dsn=驅動:數據庫類型:數據庫名字:數據庫地址:用戶名:密碼 --stats狀態


添加host主機:
[root@localhost mogilefs]# mogadm --trackers=192.168.1.146:7001 host list
192.168.1.109 [1]: alive  IP:       192.168.1.109:7500
[root@localhost mogilefs]# mogadm --trackers=192.168.1.146:7001 host add 192.168.1.101 --ip=192.168.1.101 --status=alive
[root@localhost mogilefs]# mogadm --trackers=192.168.1.146:7001 host list192.168.1.109 [1]: alive  IP:       192.168.1.109:7500
192.168.1.101 [2]: alive
  IP:       192.168.1.101:7500
[root@localhost mogilefs]#


查看一下節點

現在還沒有任何主機集羣中:

[root@localhost mogilefs]# mogadm --trackers=192.168.1.146:7001 device list
192.168.1.109 [1]: alive                    used(G)    free(G)   total(G)  weight(%)
192.168.1.101 [2]: alive
                    used(G)    free(G)   total(G)  weight(%)
[root@localhost mogilefs]#


添加目錄:
[root@localhost mogilefs]# mogadm --trackers=192.168.1.146:7001 device add 192.168.1.109
 1
[root@localhost mogilefs]# mogadm --trackers=192.168.1.146:7001 device add 192.168.1.101 2
[root@localhost mogilefs]# mogadm --trackers=192.168.1.146:7001 device list
192.168.1.109 [1]: alive
                    used(G)    free(G)   total(G)  weight(%)
   dev1:   alive      2.315     13.975     16.290        100
192.168.1.101 [2]: alive
                    used(G)    free(G)   total(G)  weight(%)
   dev2:   alive      2.081     15.384     17.465        100
[root@localhost mogilefs]#


添加domain:
[root@localhost mogilefs]# mogadm --trackers=192.168.1.146:7001 domain add images[root@localhost mogilefs]# mogadm --trackers=192.168.1.146:7001 domain add conffilles[root@localhost mogilefs]# mogadm --trackers=192.168.1.146:7001 domain list domain               class                mindevcount                replpolicy   hashtype-------------------- -------------------- ------------- ------------ ------- conffilles           default          #複製數量   2        MultipleHosts() NONE    images               default                   2                       MultipleHosts() NONE   [root@localhost mogilefs]# 
添加class:
[root@localhost mogilefs]# mogadm --trackers=192.168.1.146:7001 class add images nc1 --mindevcount=2[root@localhost mogilefs]# mogadm --trackers=192.168.1.146:7001 class list domain               class                mindevcount   replpolicy   hashtype-------------------- -------------------- ------------- ------------ ------- conffilles           default                   2        MultipleHosts() NONE    images               default                   2        MultipleHosts() NONE    images               nc1                       2        MultipleHosts() NONE   [root@localhost mogilefs]#


到此配置基本完成。


測試一下上傳圖片:

[root@localhost ~]# mogupload --trackers=192.168.1.146:7001 --domain=images --key='1.jpg' --file='/root/1.jpg' 


在109節點能看到:


[root@localhost dev1]# tree 
.
├── 0
│   └── 000
│       └── 000
│           └── 0000000002.fid     #圖片文件



或者在本機通過info命令能查到

[root@localhost ~]# mogfileinfo --trackers=192.168.1.146:7001 --domain=images --key='1.jpg'
- file: 1.jpg
     class:              default
  devcount:                    2
    domain:               images
       fid:                    2
       key:                1.jpg
    length:               419640
- 
   #可以通過url能直接訪問 
- http://192.168.1.109:7500/dev1/0/000/000/0000000002.fid
[root@localhost ~]#


wKioL1aQSNWRkm8HAAINgRf1pQY707.jpg




但是這樣看圖片是不是不方便,可以用nginx+模塊實現



二.在以上環境實現高可用,三臺服務器同時爲tracker 和storage

複製tracker的配置文件到另外兩臺storage服務器:

[root@localhost ~]# scp /etc/mogilefs/mogilefsd.conf 192.168.1.109:/etc/mogilefs/

[root@localhost ~]# scp /etc/mogilefs/mogilefsd.conf 192.168.1.101:/etc/mogilefs/


將1.146創建個目錄:/mogstore,然後修改屬主,屬組:

root@localhost mogstore]# chown -R mogilefs.mogilefs /mogstore/

[root@localhost mogstore]# mogadm --tracker=192.168.1.146:7001 host add 192.168.1.146 --ip=192.168.1.146 --status=alive
[root@localhost mogstore]# mogadm --tracker=192.168.1.146:7001 host list
192.168.1.109 [1]: alive
  IP:       192.168.1.109:7500
192.168.1.101 [2]: alive
  IP:       192.168.1.101:7500
192.168.1.146 [3]: alive
  IP:       192.168.1.146:7500
[root@localhost mogstore]#
[root@localhost mogstore]# ss -tnl
State      Recv-Q Send-Q       Local Address:Port         Peer Address:Port 
LISTEN     0      50                       *:3306                    *:*     
LISTEN     0      128                      *:7500                    *:*     
LISTEN     0      128                      *:7501                    *:*     
LISTEN     0      128                     :::22                     :::*     
LISTEN     0      128                      *:22                      *:*     
LISTEN     0      128          192.168.1.146:7001                    *:*     
LISTEN     0      100                    ::1:25                     :::*     
LISTEN     0      100              127.0.0.1:25                      *:*     
[root@localhost mogstore]#
[root@localhost mogstore]# mogadm --tracker=192.168.1.146:7001 device add 192.168.1.146 3
[root@localhost mogstore]# mogadm --tracker=192.168.1.146:7001 device list
192.168.1.109 [1]: alive
                    used(G)    free(G)   total(G)  weight(%)
   dev1:   alive      2.317     13.973     16.290        100
192.168.1.101 [2]: alive
                    used(G)    free(G)   total(G)  weight(%)
   dev2:   alive      2.083     15.382     17.465        100
192.168.1.146 [3]: alive
                    used(G)    free(G)   total(G)  weight(%)
   dev3:   alive      1.607     15.857     17.465        100
[root@localhost mogstore]#


將109,101兩個節點修改:

將listen端口改爲任意,然後啓動:

# IP:PORT to listen on for mogilefs client requests

listen = 0.0.0.0:7001


[root@localhost dev1]# ss -tnl
State       Recv-Q Send-Q                                                     Local Address:Port                                                       Peer Address:Port 
LISTEN      0      128                                                                    *:7500                                                                  *:*     
LISTEN      0      128                                                                    *:7501                                                                  *:*     
LISTEN      0      128                                                                   :::22                                                                   :::*     
LISTEN      0      128                                                                    *:22                                                                    *:*     
LISTEN      0      64                                                                    :::23                                                                   :::*     
LISTEN      0      128                                                                    *:7001                                                                  *:*     
LISTEN      0      100                                                                  ::1:25                                                                   :::*     
LISTEN      0      100                                                            127.0.0.1:25                                                                    *:*     
[root@localhost dev1]#


101也同樣配置並且啓動,這樣3個節點變爲高可用了。




         




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