一、MogileFS简要说明
1、相关概念介绍
MogileFS是一个开源的分布式文件存储系统,MogileFS适用于存储海量小文件的工作场景,由LiveJournal旗下的Danga Interactive公司开发,该团队开发了包括 Memcached、MogileFS、Perlbal 等多个知名的开源项目。
- tracker节点:借助数据库保存各节点文件的元数据信息保存每个域中所有键的存储位置分布,方便检索定位数据位置的同时监控各节点,告诉客户端存储区位置并指挥storage节点复制数据副本,进程名为mogilefsd(7001)。
- database节点:为tracker节点提供数据存取服务。
- storage节点:将指定域中的键转换为其特有的文件名存储在指定的设备文件中,转换后的文件名为值,storage节点自动维护键值的对应关系,storage节点由于使用http进行数据传输,因此依赖于perlbal,storage节点前端可以使用nginx进行反向代理,但需要安装nginx-mogilefs-module-master模块进行名称转换,进程名mogstored(7501),perbal(7500)。
- Domain:一个域中的键值是惟一的,一个MogileFS可以有多个域,域可以用来存储不同应用类型的数据的容器。
- Host:每一个存储节点称为一个主机,一个主机上可以有多个存储设备(单独的硬盘),每个设备都有ID号,Domain+Fid用来定位文件。
- Class:复制最小单位,文件属性管理,定义文件存储在不同设备上份数。
2、实验说明
在前面的试验中我们发现在请求资源时的路径是以mogilefs系统的命名方式存储的,但是用户在请求资源时肯定不会使用此种方法输入URL,故需要一个反向代理来将其封装成与用户使用习惯一样的URL。故在其前端使用nginx做反代,但是使用nginx做反代时需要编译安装nginx并加上第三方模块nginx-mogilefs-module-master。同时试验中将各节点即当tracker又当storage,这样nginx就可以使用upstream来进行轮询调用后端存储节点的内容。
3、实验环境介绍
4、实验拓扑图
5、工作流程
- 客户端向服务器端发送请求
- nginx通过调度将请求转达给其中一个mogilefs的tracker
- tracker接收到请求向后端数据库获取存储位置并返回给nginx
- nginx接到存储位置再到mogilefs的存储上获取实际存储数据并返回给客户端
二、搭建环境
1、同步时间
[[email protected] ~]# ntpdate 202.120.2.101 [[email protected] ~]# ntpdate 202.120.2.101 [[email protected] ~]# ntpdate 202.120.2.101 [[email protected] ~]# ntpdate 202.120.2.101 [[email protected] ~]# ntpdate 202.120.2.101
2、数据库授权配置
[[email protected] ~]# mysql mysql> grant all privileges on *.* to ‘root‘@‘192.168.%.%‘ identified by ‘redhat‘; Query OK, 0 rows affected (0.00 sec) mysql> grant all on mogilefs.* to ‘moguser‘@‘192.168.%.%‘ identified by ‘mogpass‘; Query OK, 0 rows affected (0.05 sec) mysql> flush privileges; Query OK, 0 rows affected (0.03 sec)
3、mogilefs安装配置
[[email protected] ~]# ls *.rpm 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 MogileFS-Utils-2.19-1.el6.noarch.rpm perl-MogileFS-Client-1.14-1.el6.noarch.rpm perl-Net-Netmask-1.9015-8.el6.noarch.rpm perl-Perlbal-1.78-1.el6.noarch.rpm [[email protected] ~]# yum install -y *.rpm perl-IO-AIO
tracker配置:
[[email protected] ~]# vim /etc/mogilefs/mogilefsd.conf daemonize = 1 pidfile = /var/run/mogilefsd/mogilefsd.pid db_dsn = DBI:mysql:mogilefs:host=172.16.10.211 #定义数据库名及主机 db_user = moguser #数据库用户名 db_pass = mogpass #数据库用户密码 listen = 0.0.0.0:7001 #监听地址 conf_port = 7001 query_jobs = 10 delete_jobs = 1 replicate_jobs = 5 reaper_jobs = 1
mogstored配置:
[[email protected] ~]# vim /etc/mogilefs/mogstored.conf maxconns = 10000 httplisten = 0.0.0.0:7500 mgmtlisten = 0.0.0.0:7501 docroot = /mogdata
创建设备挂载点:
[[email protected] ~]# mkdir /mogdata/dev1 -pv #最好将数据存放在独立磁盘设备中,这里就直接存放在本地了 [[email protected] ~]# chown -R mogilefs.mogilefs /mogdata/dev1/
授权pid目录权限:
[[email protected] ~]# chown -R mogilefs.mogilefs /var/run/mogilefsd/
初始化数据库:
[[email protected] ~]# mogdbsetup --dbhost=192.168.1.6 --dbrootuser=root --dbrootpass=redhat --dbuser=moguser --dbpass=mogpass --dbname=mogilefs --yes
查看数据库是否初始化成功:
mysql> use mogilefs Database changed mysql> show tables; +----------------------+ | Tables_in_mogilefs | +----------------------+ | 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)
启动服务:
[[email protected] ~]# service mogstored start Starting mogstored [确定] [[email protected] ~]# service mogilefsd start Starting mogilefsd [确定]
查看服务是否启动:
[[email protected] ~]# ss -tnlp |grep mog LISTEN 0 128 *:7001 *:* users:(("mogilefsd",1602,6)) LISTEN 0 128 *:7500 *:* users:(("mogstored",1565,4)) LISTEN 0 128 *:7501 *:* users:(("mogstored",1565,9))
增加节点:
[[email protected] ~]# mogadm --trackers=192.168.1.9:7001 host add node1 --ip=192.168.1.9 --status=alive [[email protected] ~]# mogadm --trackers=192.168.1.9:7001 host add node2 --ip=192.168.1.10 --status=alive [[email protected] ~]# mogadm --trackers=192.168.1.9:7001 host add node3 --ip=192.168.1.11 --status=alive
添加设备:
[[email protected] ~]# mogadm --trackers=192.168.1.9:7001 device add node1 1 [[email protected] ~]# mogadm --trackers=192.168.1.9:7001 device add node2 2 [[email protected] ~]# mogadm --trackers=192.168.1.9:7001 device add node3 3 #添加设备时所使用的设备ID号必须要/mogdata中的dev编号一致
状态检查:
[[email protected] ~]# mogadm --trackers=192.168.1.9:7001 check Checking trackers... 192.168.1.9:7001 ... OK Checking hosts... [ 1] node1 ... OK [ 2] node2 ... REQUEST FAILURE FETCHING: http://192.168.1.10:7500/ [ 3] node3 ... REQUEST FAILURE FETCHING: http://192.168.1.11:7500/ Checking devices... host device size(G) used(G) free(G) use% ob state I/O% ---- ------------ ---------- ---------- ---------- ------ ---------- ----- [ 1] dev1 11.990 2.860 9.130 23.86% writeable 0.0 ---- ------------ ---------- ---------- ---------- ------ total: 11.990 2.860 9.130 23.86%
从上面看到还要两个节点未发现,下面就将node2、node3节点安装mogilefs。安装配置方法和node1节点一样这里就不介绍了。
将node2及node3安装mogilefs并启动后在检测:
[[email protected] ~]# mogadm --trackers=192.168.1.9:7001 check Checking trackers... 192.168.1.9:7001 ... OK Checking hosts... [ 1] node1 ... OK [ 2] node2 ... OK [ 3] node3 ... OK Checking devices... host device size(G) used(G) free(G) use% ob state I/O% ---- ------------ ---------- ---------- ---------- ------ ---------- ----- [ 1] dev1 11.990 2.863 9.127 23.87% writeable 0.0 [ 2] dev2 11.990 2.862 9.128 23.87% writeable 0.0 [ 3] dev3 11.990 2.862 9.127 23.87% writeable 0.0 ---- ------------ ---------- ---------- ---------- ------ total: 35.970 8.587 27.382 23.87%
创建domain:
[[email protected] ~]# mogadm --trackers=192.168.1.9:7001 domain add files [[email protected] ~]# mogadm --trackers=192.168.1.9:7001 domain add html [[email protected] ~]# mogadm --trackers=192.168.1.9:7001 domain add images
查看domain信息:
[[email protected] ~]# mogadm --trackers=192.168.1.9:7001 domain list domain class mindevcount replpolicy hashtype -------------------- -------------------- ------------- ------------ ------- files default 2 MultipleHosts() NONE html default 2 MultipleHosts() NONE images default 2 MultipleHosts() NONE
上传文件:
[[email protected] ~]# mogupload --trackers=192.168.1.9:7001 --domain=html --key=‘issue.html‘ --file=‘/etc/issue‘ [[email protected] ~]# mogupload --trackers=192.168.1.9:7001 --domain=images --key=‘test.jpeg‘ --file=‘/root/test.jpeg‘ [[email protected] ~]# mogupload --trackers=192.168.1.9:7001 --domain=html --key=‘fsttab.html‘ --file=‘/etc/fstab‘
查看是否上传成功:
[[email protected] ~]# moglistkeys --trackers=192.168.1.9:7001 --domain=html fsttab.html issue.html [[email protected] ~]# moglistkeys --trackers=192.168.1.9:7001 --domain=images test.jpeg [[email protected] ~]# mogfileinfo --trackers=192.168.1.9:7001 --domain=html --key=‘issue.html‘ - file: issue.html class: default devcount: 2 domain: html fid: 4 key: issue.html length: 47 - http://192.168.1.11:7500/dev3/0/000/000/0000000004.fid - http://192.168.1.9:7500/dev1/0/000/000/0000000004.fid [[email protected] ~]# mogfileinfo --trackers=192.168.1.9:7001 --domain=images --key=‘test.jpeg‘ - file: test.jpeg class: default devcount: 2 domain: images fid: 5 key: test.jpeg length: 49770 - http://192.168.1.9:7500/dev1/0/000/000/0000000005.fid - http://192.168.1.11:7500/dev3/0/000/000/0000000005.fid [[email protected] ~]# mogfileinfo --trackers=192.168.1.9:7001 --domain=html --key=‘fsttab.html‘ - file: fsttab.html class: default devcount: 2 domain: html fid: 8 key: fsttab.html length: 961 - http://192.168.1.10:7500/dev2/0/000/000/0000000008.fid - http://192.168.1.9:7500/dev1/0/000/000/0000000008.fid
状态信息查看:
[[email protected] ~]# mogstats --config=/etc/mogilefs/mogilefsd.conf Fetching statistics... (all) Statistics for devices... device host files status ---------- ---------------- ------------ ---------- dev1 node1 3 alive dev2 node2 1 alive dev3 node3 2 alive ---------- ---------------- ------------ ---------- Statistics for file ids... Max file id: 7 Statistics for files... domain class files size (m) fullsize (m) -------------------- ----------- ---------- ----------- ------------- html default 2 0 0 images default 1 0 0 -------------------- ----------- ---------- ----------- ------------- Statistics for replication... domain class devcount files -------------------- ----------- ---------- ---------- html default 2 2 images default 2 1 -------------------- ----------- ---------- ---------- Statistics for replication queue... status count -------------------- ------------ -------------------- ------------ Statistics for delete queue... status count -------------------- ------------ -------------------- ------------ Statistics for general queues... queue status count --------------- -------------------- ------------ --------------- -------------------- ------------ done
浏览器测试:
4、安装配置nginx
[[email protected] ~]# groupadd -r nginx [[email protected] ~]# useradd -r -g nginx nginx [[email protected] ~]# tar xf nginx-1.8.0.tar.gz [[email protected] ~]# unzip nginx-mogilefs-module-master.zip [[email protected] ~]# cd nginx-1.8.0 [[email protected] nginx-1.8.0]# ./configure --prefix=/usr/local/ --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx/nginx.pid --lock-path=/var/lock/nginx.lock --user=nginx --group=nginx --with-http_ssl_module --with-http_flv_module --with-http_stub_status_module --with-http_gzip_static_module --http-client-body-temp-path=/var/tmp/nginx/client/ --http-proxy-temp-path=/var/tmp/nginx/proxy/ --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi --http-scgi-temp-path=/var/tmp/nginx/scgi --with-pcre --with-debug --add-module=../nginx-mogilefs-module-master [[email protected] nginx-1.8.0]# make && make install
查看nginx是否安装成功:
[[email protected] nginx-1.8.0]# nginx -v nginx version: nginx/1.8.0
提供启动脚本:
[[email protected] nginx-1.8.0]# vim /etc/init.d/nginx #!/bin/sh # # nginx - this script starts and stops the nginx daemon # # chkconfig: - 85 15 # description: Nginx is an HTTP(S) server, HTTP(S) reverse # proxy and IMAP/POP3 proxy server # processname: nginx # config: /etc/nginx/nginx.conf # config: /etc/sysconfig/nginx # pidfile: /var/run/nginx.pid # Source function library. . /etc/rc.d/init.d/functions # Source networking configuration. . /etc/sysconfig/network # Check that networking is up. [ "$NETWORKING" = "no" ] && exit 0 nginx="/usr/sbin/nginx" prog=$(basename $nginx) NGINX_CONF_FILE="/etc/nginx/nginx.conf" [ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx lockfile=/var/lock/subsys/nginx make_dirs() { # make required directories user=`nginx -V 2>&1 | grep "configure arguments:" | sed ‘s/[^*]*--user=\([^ ]*\).*/\1/g‘ -` options=`$nginx -V 2>&1 | grep ‘configure arguments:‘` for opt in $options; do if [ `echo $opt | grep ‘.*-temp-path‘` ]; then value=`echo $opt | cut -d "=" -f 2` if [ ! -d "$value" ]; then # echo "creating" $value mkdir -p $value && chown -R $user $value fi fi done } start() { [ -x $nginx ] || exit 5 [ -f $NGINX_CONF_FILE ] || exit 6 make_dirs echo -n $"Starting $prog: " daemon $nginx -c $NGINX_CONF_FILE retval=$? echo [ $retval -eq 0 ] && touch $lockfile return $retval } stop() { echo -n $"Stopping $prog: " killproc $prog -QUIT retval=$? echo [ $retval -eq 0 ] && rm -f $lockfile return $retval } restart() { configtest || return $? stop sleep 1 start } reload() { configtest || return $? echo -n $"Reloading $prog: " killproc $nginx -HUP RETVAL=$? echo } force_reload() { restart } configtest() { $nginx -t -c $NGINX_CONF_FILE } rh_status() { status $prog } rh_status_q() { rh_status >/dev/null 2>&1 } case "$1" in start) rh_status_q && exit 0 $1 ;; stop) rh_status_q || exit 0 $1 ;; restart|configtest) $1 ;; reload) rh_status_q || exit 7 $1 ;; force-reload) force_reload ;; status) rh_status ;; condrestart|try-restart) rh_status_q || exit 0 ;; *) echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}" exit 2 esac [[email protected] nginx-1.8.0]# chmod +x /etc/init.d/nginx[[email protected] nginx-1.8.0]# chkconfig --add nginx[[email protected] nginx-1.8.0]# chkconfig nginx on
配置nginx:
[[email protected] ~]# cat /etc/nginx/nginx.conf worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; upstream trackers { server 192.168.1.9:7001; server 192.168.1.10:7001; server 192.168.1.11:7001; } server { listen 80; location ~* /images/ { mogilefs_tracker trackers; mogilefs_domain images; mogilefs_pass { proxy_pass $mogilefs_path; proxy_hide_header Content-Type; proxy_buffering off; } } location ~* /html/ { mogilefs_tracker trackers; mogilefs_domain html; mogilefs_pass { proxy_pass $mogilefs_path; proxy_hide_header Content-Type; proxy_buffering off; } } location ~* /files/ { allow 192.168.0.0/16; deny all; mogilefs_tracker trackers; mogilefs_domain files; mogilefs_methods PUT DELETE; mogilefs_pass { proxy_pass $mogilefs_path; proxy_hide_header Content-Type; proxy_buffering off; } } } } [[email protected] ~]# mkdir /var/tmp/nginx/client -pv [[email protected] ~]# chown -R nginx.nginx /var/tmp/nginx/
测试配置文件是否正确:
[[email protected] ~]# nginx -t nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful
启动服务:
[[email protected] ~]# service nginx start 正在启动 nginx: [确定]
浏览器测试:
模拟节点故障:
[[email protected] ~]# service mogilefsd stop Stopping mogilefsd [确定] [[email protected] ~]# service mogstored stop Stopping mogstored [确定]