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 也不能再用.
安装Mogilefs简单步骤:
准备环境:
1.创建3台centos6.7服务器。其中1台装有mariadb5.5.47版本数据库。两外两台做 mogstored节点。
先安装epel源,解决安装时依赖关系。
准备安装包:
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
yum --nogpgcheck localinstall *.rpm
[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
远程配置mysql用户:with grant option 选项表示允许该用户有权限授权其他用户。
MariaDB [(none)]> GRANT ALL ON *.* TO 'root'@'192.168.%.%' IDENTIFIED BY '123..com' WITH GRANT OPTION; MariaDB [(none)]> flush privileges;
[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]>
# 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]#
安装方法同上面,注需要单独安装
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]#
在节点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状态
[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]#
[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 ~]#
但是这样看图片是不是不方便,可以用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个节点变为高可用了。