声明:
MogileFS-Server:包含了mogilefs的tracker和mogstored;
MogileFS-Utils:包含了个命令行工具;
perl-MogileFS-Client:客户端,Perl语言的API;
项目地址:
实验环境:
tracker+mysql:192.168.85.129
storage node2:192.168.85.130
storage node3:192.168.85.131
一.安装mogilefs
1.安装mysql
[root@localhost ~]# yum install mysql mysql-server mysql-devel
2.安装CPANM
安装CPANM的原因是为了可以方便使用CPANM安装mogilefs和相关的Perl模块,当然也可以下载各个包源码安装;
wget --no-check-certificate -O /sbin/cpanm && chmod +x /sbin/cpanma
在
.bashrc文件中添加如下一行:
alias cpanm='cpanm --sudo --mirror --mirror-only'
当然,如果实在不想用这种方法安装的话,也有做好的全面的mogilefs的RPM格式安装包,直接安装就行;
下载地址:
3.使用CPANM安装相关模块
如果出错请看第四步,因为之前安装了CPAN,但是不好用,解决了一些错误,但是最后还是不知道什么原因失败了,所以改用了CPANM,有些东西CPAN可能已经装了,所以看起来cpanm能直接安装成功,我不知道是不是CPAN的原因;
cpanm相关:
cpanm MogileFS::Server
cpanm MogileFS::Utils
cpanm MogileFS::Client
二.配置数据库
1.创建数据库和用户
mysql> create database mogilefs
mysql> grant all privileges on mogilefs.* to 'moguser'@'192.168.85.%' identified by '123456';
2.创建和mysql的连接(详细用法mogdbsetup --help)
[root@localhost ~]# mogdbsetup --dbhost=192.168.85.129 --dbname=mogilefs --dbuser=moguser --dbpass=123456
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
三.配置tracker
1.创建tracker配置文件
[root@localhost ~]# mkdir /etc/mogilefs
[root@localhost ~]# vim /etc/mogilefs/mogilefsd.conf
db_dsn = DBI:mysql:mogilefs:host=192.168.85.129;port=3306
db_user = moguser
db_pass = 123456
conf_port = 7001
listener_jobs = 5
node_timeout = 5
rebalance_ignore_missing = 1
2.mogilefs启动/停止
(控制脚本见后面附录)
由于mogilefs不能以root用户启动,所以创建一个用户以后用来控制mogilefs
[root@localhost ~]# useradd mogilefs
[root@localhost ~]#
su mogilefs -c "mogilefsd -c /etc/mogilefs/mogilefsd.conf --daemon"
[root@localhost ~]# netstat -ntlp | grep mogilefsd
tcp 0 0 0.0.0.0:7001 0.0.0.0:* LISTEN 34099/mogilefsd
停止mogilefsd:
kill mogilefsd_pid 或 pkill mogilefsd
mogilefsd启动的进程:
[root@localhost ~]# ps -ef |grep mogilefsd
mogilefs 34125 1 0 00:08 ? 00:00:09 mogilefsd
mogilefs 34126 34125 0 00:08 ? 00:00:05 mogilefsd [monitor]
mogilefs 34128 34125 0 00:08 ? 00:00:01 mogilefsd [replicate]
mogilefs 34129 34125 0 00:08 ? 00:00:01 mogilefsd [delete]
mogilefs 34130 34125 0 00:08 ? 00:00:00 mogilefsd [queryworker]
mogilefs 34131 34125 0 00:08 ? 00:00:00 mogilefsd [queryworker]
mogilefs 34132 34125 0 00:08 ? 00:00:00 mogilefsd [queryworker]
mogilefs 34133 34125 0 00:08 ? 00:00:00 mogilefsd [queryworker]
mogilefs 34134 34125 0 00:08 ? 00:00:00 mogilefsd [queryworker]
mogilefs 34135 34125 0 00:08 ? 00:00:00 mogilefsd [reaper]
mogilefs 34136 34125 0 00:08 ? 00:00:01 mogilefsd [fsck]
mogilefs 34137 34125 0 00:08 ? 00:00:18 mogilefsd [job_master]
至此,tracker和mysql已经安装配置完成了;
四.安装配置mogstored
另外两台主机(130和131)都要安装MogileFS::Server MogileFS::Utils MogileFS::Client并配置mogstored
1.安装CPANM
wget --no-check-certificate -O /sbin/cpanm && chmod +x /sbin/cpanma
在.bashrc文件中添加如下一行:
alias cpanm='cpanm --sudo --mirror --mirror-only'
执行cpanm命令时,如果提示无权限,/sbin/cpanm加上可执行权限即可;
2.安装MogileFS:Server MogileFS:Utils MogileFS:Client
安装之前最好yum install perl perl-CPAN perl-YAML perl-Time-HiRes
声明:Perl的东西我一点也不懂,所以在安装的时候总是有些东西装不上,反反复复试了好多次(--forcehe 和--reinstall等),虽然最后还是装上了,但是我不知道具体是怎么回事,如果遇到错误了,还是自行上网查看吧!
[root@localhost ~]# cpam MogileFS:Server
[root@localhost ~]# cpam MogileFS:Utils
[root@localhost ~]# cpam MogileFS:Client
提示错误:
Configuring ExtUtils-MakeMaker-7.18 ... OK
Can't locate ExtUtils/Manifest.pm in @INC (@INC contains: FatPacked::33428208=HASH(0x1fe12f0) /usr/local/lib64/perl5 /usr/local/share/perl5 /usr/lib64/perl5/vendor_perl /usr/share/perl5/vendor_perl /usr/lib64/perl5 /usr/share/perl5 .) at /sbin/cpanm line 132.
解决办法:
cd /root
wget
tar xvzf ExtUtils-MakeMaker-7.18.tar.gz
cd ExtUtils-MakeMaker-7.18
perl Makefile.PL
make
make install
然后继续上一步(注意看安装过程,有依赖的包会自己安装)
3.配置mogstroed
先创建数据存储位置和控制用户并进行权限配置:
[root@localhost ~]# mkdir -pv /data/mogdata
[root@localhost ~]# useradd mogilefs
[root@localhost ~]# chown -R mogilefs.mogilefs /data/mogdata/
然后为mogilefs storage创建配置文件(具体参数可以mogstored --help查看):
[root@localhost ~]# mkdir /etc/mogilefs
[root@localhost ~]# vim /etc/mogilefs/mogstroed.conf
maxconns=1024 #最大并发连接数
httplisten=192.168.85.130:7500 #监听地址,不同的节点要更改配置
mgmtlisten=192.168.85.130:7501 #管理接口地址
docroot=/data/mogdata #数据存放位置
4.启动mogstroed服务(控制脚本见后面附录)
192.168.85.130节点:
[root@localhost ~]#
su - mogilefs -c "mogstored -c /etc/mogilefs/mogstored.conf --daemonize"
[root@localhost mogilefs]# netstat -ntlp | grep mogstored
tcp 0 0 192.168.85.130:7500 0.0.0.0:* LISTEN 40867/mogstored
tcp 0 0 192.168.85.130:7501 0.0.0.0:* LISTEN 40867/mogstored
192.168.85.131节点:
[root@localhost ~]# netstat -ntlp | grep mogstored
tcp 0 0 192.168.85.131:7500 0.0.0.0:* LISTEN 35866/mogstored
tcp 0 0 192.168.85.131:7501 0.0.0.0:* LISTEN 35866/mogstored
五.管理配置MogileFS-Server
1.添加mogstored节点t到tracker
格式:
mogadm host add [opts] Add a host to MogileFS.
节点的主机名,也可为IP
--altip=s 如果节点故障,以谁为备用节点
--altmask=s Netmask which, when matches client, uses alt IP
--getport=i Alternate HTTP port serving readonly traffic
--ip=s 指定添加的主机的IP
--port=i 主机端口号,默认为7500
--status=s 节点见进来后所处的状态(alive,down),默认为down
[root@localhost ~]#
mogadm --trackers=192.168.85.129:7001 host add 192.168.85.130 --ip=192.168.85.130 --port=7500 --status=alive
[root@localhost ~]# mogadm --trackers=192.168.85.129:7001 host add 192.168.85.131 --ip=192.168.85.131 --port=7500 --status=alive
查看节点状态:
[root@localhost ~]# mogadm --trackers=192.168.85.129:7001 host list
192.168.85.130 [1]: alive
IP: 192.168.85.130:7500
192.168.85.131 [2]: alive
IP: 192.168.85.131:7500
2.在mogstored中添加设备device
格式:
mogadm device add [opts] Add a device to a host.
device的id号,唯一,不可重用
设备的主机名,也可为IP
--status=s 添加设备的状态(alive,down),默认为alive
其他用法:
mogadm device add [opts] Add a device to a host.
mogadm device list [opts] List all devices, for each host.
mogadm device mark Mark a device as {alive,dead,down,drain,readonly}
mogadm device modify [opts] Modify a device's properties.
mogadm device next Show the next available devid.
mogadm device summary [opts] List the summary of devices, for each host.
添加设备:
[root@localhost ~]#
mogadm --trackers=192.168.85.129:7001 device add 192.168.85.130 1
[root@localhost ~]# mogadm --trackers=192.168.85.129:7001 device add 192.168.85.131 2
查看设备信息:
[root@localhost ~]# mogadm --trackers=192.168.85.129:7001 device list
192.168.85.130 [1]: alive
used(G) free(G) total(G) weight(%)
dev1: alive 0.000 0.000 0.000 100
192.168.85.131 [2]: alive
used(G) free(G) total(G) weight(%)
dev2: alive 0.000 0.000 0.000 100
dev1和dev2的可以空间都为0,那是因为我们还有没有创建,下面分别在各节点的存储目录mogdata下面创建dev1和dev2,并设定好属主属组权限:
[root@localhost ~]# mkdir /data/mogdata/dev1
[root@localhost ~]# chown -R mogilefs.mogilefs /data/mogdata/dev1/
[root@localhost ~]# mkdir /data/mogdata/dev2
[root@localhost ~]# chown -R mogilefs.mogilefs /data/mogdata/dev2/
再次查看:
[root@localhost ~]# mogadm --trackers=192.168.85.129:7001 device list
192.168.85.130 [1]: alive
used(G) free(G) total(G) weight(%)
dev1: alive 0.000 0.000 0.000 100
192.168.85.131 [2]: alive
used(G) free(G) total(G) weight(%)
dev2: alive 3.292 13.184 16.476 100
dev1的空间利用仍然为0,这个问题,下面会解决,先用着吧;
3.创建domain
格式:
mogadm domain add Add a domain (namespace)
Domain (namespace) to add.
[root@localhost ~]#
mogadm --trackers=192.168.85.129:7001 domain add files
查看domain信息:
[root@localhost ~]# mogadm --trackers=192.168.85.129:7001 domain list
domain class mindevcount replpolicy hashtype
-------------------- -------------------- ------------- ------------ -------
files default 2 MultipleHosts() NONE
其中的mindevcount表示最小文件复制数;
4.测试
上传文件
[root@localhost ~]#
mogupload --trackers=192.168.85.129:7001 --domain=files --key='/fstab.html' --file='/etc/fstab'
查看可用的key:
[root@localhost ~]#
moglistkeys --trackers=192.168.85.129:7001 --domain=files
/fstab.html
浏览器直接访问192.168.85.129:7001/fstab.html是无法显示的,因为只能基于客户端API而不是HTTP,如要访问,需要先找出可用的地址:
[root@localhost ~]#
mogfileinfo --trackers=192.168.85.129:7001 --domain=files --key='/fstab.html'
- file: /fstab.html
class: default
devcount: 1
domain: files
fid: 4
key: /fstab.html
length: 805
-
http://192.168.85.131:7500/dev2/0/000/000/0000000004.fid #因为只有一个可用的storage,所以只有一个,上面不出现问题的话应为两个
访问:
5.解决只有一个可用的storage节点问题(摘自网上文章)
可以看到,上面上传的文件仅在一个主机上存放,实际上应该是两个节点都存放才对。我从网上查到出错的原因可能是由于
Sys::Syscall这个模块造成的。我们看到现在此模块的版本为0.25。我上网搜索其解决办法,发现如果将此软件降为0.23版本可能会修复此问题:
cpan[1]> i /Sys::Syscall/
CPAN: Storable loaded ok (v2.20)
Going to read '/root/.cpan/Metadata'
Database was generated on Wed, 25 May 2016 03:53:54 GMT
Module id = Sys::Syscall
CPAN_USERID BRADFITZ (Brad Fitzpatrick )
CPAN_VERSION 0.25
CPAN_FILE B/BR/BRADFITZ/Sys-Syscall-0.25.tar.gz
UPLOAD_DATE 2012-12-17
MANPAGE Sys::Syscall - access system calls that Perl doesn't normally provide access to
INST_FILE /usr/local/share/perl5/Sys/Syscall.pm
INST_VERSION
0.25
自己手动编译安装0.23版本的Sys::Syscall:
[root@localhost ~]# wget
[root@localhost ~]# tar xf Sys-Syscall-0.23.tar.gz
[root@localhost ~]# cd Sys-Syscall-0.23
[root@localhost Sys-Syscall-0.23]# perl Makefile.PL
[root@localhost Sys-Syscall-0.23]# make && make install
重启mogilefsd进程后查看Sys::Syscall版本:
cpan[2]> i /Sys::Syscall/
Module id = Sys::Syscall
CPAN_USERID BRADFITZ (Brad Fitzpatrick )
CPAN_VERSION 0.25
CPAN_FILE B/BR/BRADFITZ/Sys-Syscall-0.25.tar.gz
UPLOAD_DATE 2012-12-17
MANPAGE Sys::Syscall - access system calls that Perl doesn't normally provide access to
INST_FILE /usr/local/share/perl5/Sys/Syscall.pm
INST_VERSION
0.23
重新上传一个文件:
[root@localhost ~]# mogupload --trackers=192.168.85.129:7001 --domain=files
--key='/pass.html' --file='/etc/passwd'
列出可用文件的key:
[root@localhost ~]# moglistkeys --trackers=192.168.85.129:7001 --domain=files
/fstab.html
/pass.html
查看可用文件的访问地址:
[root@localhost ~]# mogfileinfo --trackers=192.168.85.129:7001 --domain=files --key='/pass.html'
- file: /pass.html
class: default
devcount: 2
domain: files
fid: 9
key: /pass.html
length: 1527
- http://192.168.85.131:7500/dev2/0/000/000/0000000009.fid
- http://192.168.85.130:7500/dev1/0/000/000/0000000009.fid
六.最后说明
可以看到,访问时的地址看起来很怪异,而且不友好,如果想以正常的URL格式访问,可以在整个mogilefs前端加上一个Nginx(安装了相关模块)作为反向代理,让Nginx去访问tracker,tracker再查询mysql,如果有该文件的话返回可用的地址返回给Nginx,Nginx以友好的URL格式返回给用户,这样才是完美的;
这个配置在:
http://blog.chinaunix.net/uid-30212356-id-5735013.html
附录:
mogilefsd的控制脚本:
[root@localhost ~]# cat /etc/rc.d/init.d/mogilefsd
#!/bin/bash
#
# mogilefsd - Startup script for the MogileFS tracker
#
# chkconfig: - 85 15
# description: MogileFS tracker
# processname: mogilefsd
# config: /etc/mogilefs/mogilefsd.conf
# pidfile: /var/run/mogilefsd/mogilefsd.pid
# Source function library.
. /etc/rc.d/init.d/functions
# Path to the apachectl script, server binary, and short-form for messages.
lockfile=${LOCKFILE-/var/lock/subsys/mogilefsd}
RETVAL=0
#prog=$(which mogilefsd)
prog=/usr/local/bin/mogilefsd
start() {
ulimit -n 65535
echo -n $"Starting mogilefsd"
su - mogilefs -c "$prog -c /etc/mogilefs/mogilefsd.conf --daemon"
RETVAL=$?
[ $RETVAL = 0 ] && success && touch ${lockfile} || failure
echo
return $RETVAL
}
stop() {
echo -n $"Stopping mogilefsd"
netstat -nlp|grep "mogilefsd"|grep -v grep|awk '{print $7}'|awk -F"/" '{print $1}'|xargs kill -9
RETVAL=$?
[ $RETVAL = 0 ] && success && rm -f ${lockfile} || failure
echo
}
reload() {
echo -n $"Reloading mogilefsd: "
killall mogilefsd -HUP
RETVAL=$?
[ $RETVAL = 0 ] && success || failure
echo
}
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status mogilefsd
RETVAL=$?
;;
restart)
stop
sleep 1
start
;;
reload)
reload
;;
*)
echo $"Usage: mogilefsd {start|stop|restart|reload|status}"
exit 1
esac
exit $RETVAL
mogstored的控制脚本:
[root@localhost ~]# cat /etc/rc.d/init.d/mogstored
#!/bin/bash
#
# mogstored - Startup script for the MogileFS storage
#
# chkconfig: - 86 14
# description: MogileFS storage
# processname: mogstored
# config: /etc/mogilefs/mogstored.conf
# pidfile: /var/run/mogilefsd/mogstored.pid
# Source function library.
. /etc/rc.d/init.d/functions
# Path to the apachectl script, server binary, and short-form for messages.
lockfile=${LOCKFILE-/var/lock/subsys/mogstored}
RETVAL=0
configfile='/etc/mogilefs/mogstored.conf'
#prog=$(which mogstored)
prog=/usr/local/bin/mogstored
start() {
ulimit -n 65535
echo -n $"Starting mogstored"
su - mogilefs -c "$prog -c $configfile --daemon" &> /dev/null
RETVAL=$?
[ $RETVAL = 0 ] && success && touch ${lockfile} || failure
echo
return $RETVAL
}
stop() {
echo -n $"Stopping mogstored"
netstat -nlp|grep "mogstored"|grep -v grep|awk '{print $7}'|awk -F"/" '{print $1}'|xargs kill -9
RETVAL=$?
[ $RETVAL = 0 ] && success && rm -f ${lockfile} || failure
echo
}
reload() {
echo -n $"Reloading mogstored: "
killall mogstored -HUP
RETVAL=$?
[ $RETVAL = 0 ] && success || failure
echo
}
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status mogstored
RETVAL=$?
;;
restart)
stop
sleep 1
start
;;
reload)
reload
;;
*)
echo $"Usage: mogstored {start|stop|restart|reload|status}"
exit 1
esac
exit $RETVAL
参考文章:
http://my.oschina.net/u/1259000/blog/182277