一、分佈式文件系統
分佈式文件系統(Distributed File
System)是指文件系統管理的物理存儲資源不一定直接連接在本地節點上,而是通過計算機網絡與節點相連,也就是集羣文件系統,可以支持大數量的節點以及PB級的數量存儲。
相對地,在一個分享的磁盤文件系統中,所有節點對數據存儲區塊都有相同的訪問權,在這樣的系統中,訪問權限就必須由客戶端程序來控制。
分佈式文件系統可能包含的功能有:透通的數據複製與容錯。也就是說,即使系統中有一小部份的節點脫機,整體來說系統仍然可以持續運作而不會有數據損失
分佈式文件系統類別
名稱
特性
MogileFS
適用於處理海量小文件Ceph
是一個 Linux PB級別的分佈式文件系統MooseFS
通用簡便,適用於研發能力不強的公司Taobao Filesystem
適用於處理海量小文件ClusterFS
適用於處理單個大文件Google Filesystem
GFS+MapReduce擅長處理單個大文件Hadoop Distributed Filesystem
GFS的山寨版+MapReduce,擅長處理單個大文件
MogileFS由3個部分組成:
第1個部分:
是server端,包括mogilefsd和mogstored兩個程序。前者即是mogilefsd的tracker,它將一些全局信息保存在數據庫
裏,例如站點domain,class,host等。後者即是存儲節點(store node),它其實是個HTTP
Daemon,默認偵聽在7500端口,接受客戶端的文件備份請求。在安裝完後,要運行mogadm工具將所有的store
node註冊到mogilefsd的數據庫裏,mogilefsd會對這些節點進行管理和監控。
第2個部分:是utils(工具集),主要是MogileFS的一些管理工具,例如mogadm等。
第3個部分:是客戶端API,目前只有Perl
API(MogileFS.pm)、PHP,用這個模塊可以編寫客戶端程序,實現文件的備份管理功能,提供MogileFS.pm
CAP理論:一致性,可用性,分區容錯性;指一個分佈式系統不可以滿足一致性,可用性和分區容錯性這三個需求,最多隻能同時滿足其中的兩個;
C(Consistency):一致性,任何一個讀操作總是能夠讀取之前完成的寫操作;就是一個數據寫入一立馬被讀到;
A(Availability):可用性,每一次操作總是能夠
在確定的時間返回;無論成功或失敗都能收到一個返回值的;
P(Tolerance of network
Partition):分區容錯性,在出現網絡分區的情況下,仍然能夠滿足一致性和可用性;
環境:3臺虛擬機:172.16.17.3調度器(tracker+MogileFS+mysql),172.16.17.11存儲節點(mogstored),172.16.17.12存儲節點(mogstored)。
172.16.17.3上安裝mysql,mysql數據庫可以安裝在這3臺主機裏的任意一臺。
[root@MinR mogilefs]# service mysqld start Starting mysqld: [ OK ] 啓動mysql [root@MinR mogilefs]# mysql Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server version: 5.1.71 Source distribution Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> GRANT ALL ON *.* TO 'root'@'172.16.%.%' IDENTIFIED BY 'mageedu'; Query OK, 0 rows affected (0.00 sec) mysql> FLUSH PRIVILEGES; Query OK, 0 rows affected (0.00 sec) 給root用戶授權 mysql> GRANT ALL ON *.* TO 'root'@'MinR' IDENTIFIED BY 'mageedu'; Query OK, 0 rows affected (0.00 sec) mysql> FLUSH PRIVILEGES; Query OK, 0 rows affected (0.00 sec) 主機名授權, 防止反向解析出錯 mysql> GRANT ALL ON mogdb.* TO 'moguser'@'172.16.%.%' IDENTIFIED BY 'mogpass'; Query OK, 0 rows affected (0.01 sec) 給 moguser用戶授權 mysql> FLUSH PRIVILEGES; Query OK, 0 rows affected (0.00 sec) mysql> GRANT ALL ON mogdb.* TO 'moguser'@'MinR' IDENTIFIED BY 'mogpass'; Query OK, 0 rows affected (0.00 sec) mysql> FLUSH PRIVILEGES; Query OK, 0 rows affected (0.00 sec)
安裝MogileFS
[root@MinR mogilefs]# yum install MogileFS-Server-2.46-2.el6.noarch.rpm MogileFS-Server-mogilefsd-2.46-2.el6.noarch.rpm MogileFS-Server-mogstored-2.46-2.el6.noarch.rpm perl-Perlbal-1.78-1.el6.noarch.rpm perl-Net-Netmask-1.9015-8.el6.noarch.rpm這個後面會用到不裝會報錯 注意這裏的perl-Perlbal是所依賴的包必須安裝。否則不成功 設定數據庫,這裏要用mogdbsetup命令,用mogdbsetup --help查看下具體用法 [root@MinR mogilefs]# mogdbsetup --dbhost=172.16.17.3 --dbname=mogdb --dbuser=moguser --dbpass=mogpass --dbrootuser=root --dbrootpass=mageedu 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 驗證下 [root@MinR mogilefs]# mysql Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 41 Server version: 5.1.71 Source distribution Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> SHOW DATABASES; +--------------------+ | Database | +--------------------+ | information_schema | | mogdb | | mysql | | test | +--------------------+ 4 rows in set (0.00 sec) mysql> use mogdb Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql> 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) mysql> \q Bye
編輯mogilefsd 的配置文件
[root@MinR mogilefs]# vim /etc/mogilefs/mogilefsd.conf db_dsn = DBI:mysql:mogdb:host=172.16.17.3 db_user = moguser db_pass = mogpass # IP:PORT to listen on for mogilefs client requests listen = 172.16.17.3:7001 這幾項是我們要改動的,其餘默認不用改
啓動mogilefs
[root@MinR mogilefs]# service mogilefsd start Starting mogilefsd [ OK ]
在另外2個節點上也安裝mogilefs
yum install MogileFS-Server-* perl-Net-Netmask-1.9015-8.el6.noarch.rpm perl-Perlbal-1.78-1.el6.noarch.rpm [root@MinR1 mogilefs]# yum install -y perl-IO-AIO 編輯配置文件 [root@MinR1 mogilefs]# cd /etc/mogilefs/ [root@MinR1 mogilefs]# cp mogstored.conf mogstored.conf.bak 備份 [root@MinR1 mogilefs]# vim mogstored.conf maxconns = 10000 最大併發連接數 httplisten = 0.0.0.0:7500 mgmtlisten = 0.0.0.0:7501 docroot = /mogdata/data 此目錄需要手動創建,並加權限 [root@MinR1 mogilefs]# chown -R mogilefs.mogilefs /mogdata /data
在tracker節點
[root@MinR mogilefs]# yum install MogileFS-Utils-2.19-1.el6.noarch.rpm perl-MogileFS-Client-1.14-1.el6.noarch.rpm 安裝所需的包 當這2個包安裝完成後,就可看到mog打頭的命令工具多了很多 [root@MinR mogilefs]# mog mogadm mogdbsetup mogfetch mogfileinfo moglistfids mogrename mogstored mogupload mogautomount mogdelete mogfiledebug mogilefsd moglistkeys mogstats mogtool [root@MinR mogilefs]# mogadm --trackers=172.16.17.3:7001 host add 查看下其具體用法 Help for 'host-add' command: mogadm host add <hostname> [opts] Add a host to MogileFS. <hostname> Hostname of machine --altip=s Alternate IP that is machine is reachable from --altmask=s Netmask which, when matches client, uses alt IP --getport=i Alternate HTTP port serving readonly traffic --ip=s IP address of machine. --port=i HTTP port of mogstored --status=s One of {alive,down}. Default 'down'. [root@MinR mogilefs]# mogadm --trackers=172.16.17.3:7001 host add node2 –ip=172.16.17.11 –status=alive 添加節點 [root@MinR mogilefs]# mogadm --trackers=172.16.17.3:7001 host add node2 –ip=172.16.17.12 –status=alive [root@MinR ~]# mogadm --trackers=172.16.17.3:7001 host listnode2 [1]: alive IP: 172.16.17.11:7500 node3 [2]: alive IP: 172.16.17.12:7500 查看 [root@MinR ~]# mogadm --trackers=172.16.17.3:7001 device add node2 1 --status=alive [root@MinR ~]# mogadm --trackers=172.16.17.3:7001 device add node3 2 --status=alive [root@MinR ~]# mogadm --trackers=172.16.17.3:7001 device add node3 3 --status=alive 注意上面的設備Id(1,2,3)都要提前在所對應的 節點上的/mogdata/data/dev1或者dev2或者dev3….創建好 [root@MinR ~]# mogadm --trackers=172.16.17.3:7001 device list 查看個節點的設備node2 [1]: alive used(G) free(G) total(G) weight(%) dev1: alive 0.926 17.760 18.686 100 node2節點的設備已添加 node3 [2]: alive used(G) free(G) total(G) weight(%) node3節點的設備未添加 添加好後 [root@MinR ~]# mogadm --trackers=172.16.17.3:7001 device listnode2 [1]: alive used(G) free(G) total(G) weight(%) dev1: alive 0.926 17.760 18.686 100 node3 [2]: alive used(G) free(G) total(G) weight(%) dev2: alive 0.419 18.267 18.686 100 dev3: alive 0.419 18.267 18.686 100 [root@MinR ~]# mogadm --trackers=172.16.17.3:7001 device summaryHostname HostID Status used(G) free(G) total(G) %Usednode2 [ 1]: alive 0.926 17.760 18.686 4.95node3 [ 2]: alive 0.840 36.531 37.371 2.25[root@MinR ~]# mogadm --trackers=172.16.17.3:7001 domain add files 默認名稱空間[root@MinR ~]# mogadm --trackers=172.16.17.3:7001 domain list domain class mindevcount replpolicy hashtype-------------------- -------------------- ------------- ------------ ------- files default 2 MultipleHosts() NONE [root@MinR ~]# mogupload --trackers=172.16.17.3:7001 --domain=files --key='/fstab.html' --file='/etc/fstab' 上傳文件 上傳成功後,就可一在另外的2個節點中的任意一個來查看上傳的文件,注意這裏的文件保存的目錄格式和我們想的不太一樣 [root@node3 data]# cat dev2/0/000/000/0000000003.fid ## /etc/fstab# Created by anaconda on Thu Aug 14 21:10:35 2014## Accessible filesystems, by reference, are maintained under '/dev/disk'# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info#/dev/mapper/vg0-root / ext4 defaults 1 1UUID=b7e0fe27-dea3-444c-9d20-4d7edb7e4570 /boot ext4 defaults 1 2/dev/mapper/vg0-usr /usr ext4 defaults 1 2/dev/mapper/vg0-var /var ext4 defaults 1 2/dev/mapper/vg0-swap swap swap defaults 0 0tmpfs /dev/shm tmpfs defaults 0 0devpts /dev/pts devpts gid=5,mode=620 0 0sysfs /sys sysfs defaults 0 0proc /proc proc defaults 0 0這的確是我們上傳的結果。 在172.16.17.3上也可查看到文件的2個副本 [root@MinR ~]# mogfileinfo --trackers=172.16.17.3:7001 --domain=files --key='/fstab.html'- file: /fstab.html class: default devcount: 2 domain: files fid: 3 key: /fstab.html length: 921 - http://172.16.17.11:7500/dev1/0/000/000/0000000003.fid 用http協議也可訪問。本來就是通過http協議共享的 - http://172.16.17.12:7500/dev2/0/000/000/0000000003.fid
再上傳一個圖片文件
[root@MinR ~]# mogupload --trackers=172.16.17.3:7001 --domain=files --key='/bg.png' --file='/usr/share/backgrounds/wallpaper-six-2048x1536.png' [root@MinR ~]# mogfileinfo --trackers=172.16.17.3:7001 --domain=files --key='/bg.png' - file: /bg.png class: default devcount: 2 domain: files fid: 4 key: /bg.png length: 608517 - http://172.16.17.12:7500/dev2/0/000/000/0000000004.fid - http://172.16.17.11:7500/dev1/0/000/000/0000000004.fid
在瀏覽器訪問