MogileFS分布式文件系统

 

         分布式存储:

                   多个节点共同提供存储空间,然后通过一个中间层抽象出一个接口,提供给其他程序调用,存储本身对于上层应用是透明的;

         存储方式:

                   单独使用一个节点存储文件的元数据:集中元数据存储;

                            存储节点专门存储数据,元数据节点专门存储元数据;

                   每个存储节点都存储文件的元数据;

                            存储节点不光存储数据,并且还存有全局的元数据;

         分布式文件系统与分布式存储的区别:

                   分布式文件系统提供文件系统接口,而分布式存储没有文件系统接口,通过API访问其中的数据;

                   常见的分布式文件系统;

                            GFS:Google File System,并行存储海量文件;

                            HDFS:储存大量较大的单个文件;

                            TFS:存储海量小文件;

                                     将元数据存储于关系型数据库或其他高性能存储中,从而能维护海量文件元数据;

                            GlusterFS:

                                     去中心化设计;

                            cehp:整合到linux内核中的分布式文件系统;

                            MogileFS:存储海量小文件;

 

                  MogileFS文件系统:使用的是HTTP协议进行数据传输,所以非常适用于WEB;

                            角色:

                                     Tracker:元数据节点;

                                               负责向Database中存储元数据或从Database中检索元数据;并且可以设置多个Tracker节点,进行高可用;

                                     Database:存储元数据节点的关系型数据库;

                                               可以使用mysql数据库,并且可以做主从复制,对数据进行高可用;

                                     Storage Node:存储节点;

                                               用于存储真正的数据;

                                     Client Library:客户端接口;

                                               通过调用API与其他角色进行通信;

 

                            基本元素:

                                     Domain:域

                                               一个MogileFS可以有多个域,每个域可以用来存放不同类型的文件,位于同一个域中的Key(文件名)必须唯一,位于不同的域则Key可以相同;

                                                        所以这就产生了一个问题:

                                                                 像淘宝这样的平台,它会为每个店铺提供上传自己商品图片的业务,但是每个店铺都是独立的,他们上传图片的时候又不会对比自己所传图片的命名名称是否与其他人的相同,这就与domain的机制相冲突了;当然我们也可以为每个店铺提供一个domain,但是这样对于管理员来说管理起来就太麻烦了不是吗?所以我们通常都是将店铺上传的图片经过系统自动随机改成另外一个名称然后存储到MogileFS中,然后像PHP页面等程序员使用的时候直接引用即可;

                                     Class:类

                                               管理文件属性,定义文件存储在不同设备上的份数,是最小的复制单元,即用于将多个小文件合并成一个单独被冗余的复制单元;

                                    

                            示例:

                                     1.Nginx+MogileFS

                                               拓扑结构:

                                                        一共四个节点:

                                                                 192.168.80.145 centos7

                                                                 192.168.80.131 clone1

                                                                 192.168.80.134 clone2

                                                                           此三个节点运行Mysql(galera cluster)、Storage Node、Tracker;

                                                                 192.168.80.136 node2

                                                                           此节点作为负载均衡器,运行Nginx;

                                              执行过程:

                                                        前端负载均衡器Nginx接收客户端请求以后,Nginx自己扮演成MogileFS的客户端,向Tracker请求元数据,当接收请求以后再跟进其响应的结果去Storage Node请求真正的数据,最后将接收到的数据响应返回给客户端;

                                               配置过程:

                                                        a.处理依赖关系

                                                                 ~]# yum install perl-Sys-Syslog perl-Net-Netmask perl-IO-AIO

                                                        b.安装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 MogileFS-Utils-2.19-1.el6.noarch.rpm perl-Danga-Socket-1.61-1.el6.rf.noarch.rpm perl-MogileFS-Client-1.14-1.el6.noarch.rpm perl-Perlbal-1.78-1.el6.noarch.rpm

                                                        c.添加相关的Mysql授权用户

                                                                 添加一个远程连接用户(可以不添加):

                                                                          MariaDB [(none)]>  grant all on *.* to 'root'@'192.168.80.%' identified by 'admin';

                                                                 创建一个MogileFS专用的数据库

                                                                           MariaDB [(none)]> create database mogilefs;

                                                                 授权一个可以完全控制MogileFS专用数据库的用户

                                                                           MariaDB [(none)]> grant all on mogilefs.* to 'moguser'@'192.168.80.%' identified by 'mogpass';

                                                                          MariaDB [(none)]> flush privileges;

                                                        d.数据库初始化

                                                                 ~]# mogdbsetup --dbhost=192.168.80.145 --dbrootpass='admin' --dbuser='moguser' --dbpass='mogpass'

                                                                 使用mogdbsetup -h查看帮助信息

                                                        e.编辑Tracker的配置文件

                                                                 ~]# vim /etc/mogilefs/mogilefsd.conf

                    daemonize = 1  以守护进程的方式运行

                    pidfile = /var/run/mogilefsd/mogilefsd.pid  pid文件

                    db_dsn = DBI:mysql:mogilefs:host=192.168.80.145  连接数据库的数据源的方式:连接方式:数据库类型:数据库名称:主机

                    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   

                                                        f.启动mogilefsd服务

                                                                 ~]# service mogilefsd start

                                                        g.编辑Storage Node的配置文件

                                                                 ~]# mkdir /data/mofilefs/dev1 -pv   路径中”dev1”是固定格式

                                                                 ~]# chown -R mogilefs.mogilefs /data/mofilefs/

                                                                 ~]# vim /etc/mogilefs/mogstored.conf

                                                                           maxconns = 10000

                                                                           httplisten = 0.0.0.0:7500

                                                                           mgmtlisten = 0.0.0.0:7501

                                                                           docroot = /data/mogilefs/

                                                        h.如果是在centos7中则需要修改一下Storage Node的服务脚本;

                                                                 ~]# cat /etc/init.d/mogstored   (考眼力,找不同

                    lockfile=${LOCKFILE-/var/lock/subsys/mogstored}

                    pidfile=/var/run/mogilefsd/mogstored.pid

                    RETVAL=0

                    start() {

                           [ $RETVAL = 0 ] && success && touch ${lockfile} && echo $(pidof mogstored) > ${pidfile}|| failure

                    }

                    stop() {

                            [ $RETVAL = 0 ] && success && rm -f ${lockfile} ${pidfile} || failure

                    }

                                                                 ~]# systemctl daemon-reload

                                                        i.启动mogstored服务

                                                                 ~]# service mogstored start

                                                        j.MogileFS关机工具:mogadm

                                                                 mogadm host:管理节点

                                                                           mogadm host list

                                                                 mogadm device:管理设备

                                                                           mogadm device list

                                                                 mogadm domain:管理域

                                                                 mogadm class:管理类

                                                                 mogadm slave:管理从节点

                                                                 mogadm fsck:管理文件系统的检测

                                                                 mogadm rebalance:管理存储均衡

                                                                 mogadm setting:设置集群属性

                                                                           查看帮助:mogadm -h或者mogadm host add 然后直接键入回车会有更详细的提示信息;

                                                        k.添加Storage Node节点到集群中

                                                                 ~]# mogadm --tracker=192.168.80.145:7001 host add 192.168.80.145(主机名) --ip=192.168.80.145(节点地址) --status=alive(节点状态)

                                                                 ~]# mogadm --tracker=192.168.80.145:7001 host add 192.168.80.131(主机名) --ip=192.168.80.131(节点地址) --status=alive(节点状态)

                                                                 ~]# mogadm --tracker=192.168.80.145:7001 host add 192.168.80.134(主机名) --ip=192.168.80.134(节点地址) --status=alive(节点状态)

                                                                 ~]# mogadm device add 192.168.80.145 1    添加设备

                                                               ~]# mogadm device add 192.168.80.131 2

                                                               ~]# mogadm device add 192.168.80.134 3

                                                                 ~]# mogadm device list

                                                                 ~]# mogadm check

                                                                 ~]# mogadm domain add images       添加域

                                                                 ~]# mogadm domain list

 

                                                                 ~]# mogadm class add images jpeg

                                                                 ~]# mogadm class add images png

                                                                 ~]# mogadm class list

 

                                                                          mindevcount:最少设备数量,也就是将相同的数据存储到几个设备上;

                                                                           replpolicy:复制策略,类似上面的选项

                                                                                    以上选项可以通过mogadm class 设置;

                                                                           参看帮助:man mogadm

                                                        l.上传文件到MogileFS中:mogupload

                                                                 Usage: /usr/bin/mogupload --trackers=host --domain=foo --key='/hello.jpg' --file='./hello.jpg'

                                                                           key:为访问路径

                                                                           file:要上传的文件

                                                                 ~]# mogupload --domain=images --key='book.png' --file='/root/book.png' --class=png --trackers=192.168.80.145:7001

                                                                 ~]# mogstats --db_dsn="DBI:mysql:mogilefs:host=192.168.80.145" --db_user="moguser" --db_pass="mogpass" --verbose --stats="devices,files"

Fetching statistics... (devices,files)

                                                        m.查看文件状态

                                                                 ~]#mogfileinfo --trackers=192.168.80.136:7001 --key='book.png' --domain=images

                                                                 ~]# mogadm check

 

                                                        Note:此处有错(devcount:1),见文末;

                                                        n.查看文件

                                                                 在浏览器键入上面http地址即可查看图片;

 

                                                        o.使用Nginx作为MogileFS的前端代理

                                                                 Mogilefs支持使用Nginx作为其前端的反向代理,Ngin可以将客来自户端的请求转发给MogileFS,并且还能将MogileFS返回的数据(通过自己的响应路径格式)响应给客户端(我们也发现了如果使用MogileFS自己的路径放文件是及其不方便的,文件名也是比较难识别);Nginx要作为MogileFS的反向代理使用还要额外加载一个模块:nginx-mogilefs-module(下载地址:https://github.com/vkholodkov/nginx-mogilefs-module

                                                                 编译安装tengine作为反向代理:

                                                                           在编译之前可以将tengine解压路径中的objs/Makefile文件中的CFLAGS =  -pipe  -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g行后面的”-Werror -g”去掉,否则会报告一个这样的错误:(也可以不设置直接编译,如果报错就改,不报就不改)

 

                                                                           ~]#unzip nginx-mogilefs-module-master.zip

                                                                           ~]#tar xf tengine-2.0.1.tar.gz

                                                                        ~]#cd tengine-2.0.1/

                                                                           ~]#./configure --prefix=/usr --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 --with-pcre --with-debug --add-module=../nginx-mogilefs-module-master

                                                                           ~]#make

                                                                           ~]#make install

                                                                           添加一个nginx启动Unit

                                                                           ~]#vim /usr/lib/systemd/system/nginx.service

                                                                                    [Unit]

                                                                                    Description=The nginx HTTP and reverse proxy server

                                                                                    After=network.target remote-fs.target nss-lookup.target

                                                                                   

                                                                                    [Service]

                                                                                    Type=forking

                                                                                    PIDFile=/var/run/nginx/nginx.pid

                                                                                    ExecStartPre=/usr/sbin/nginx -t

                                                                                    ExecStart=/usr/sbin/nginx

                                                                                    ExecReload=/bin/kill -s HUP $MAINPID

                                                                                    KillMode=process

                                                                                    KillSignal=SIGQUIT

                                                                                    TimeoutStopSec=5

                                                                                    PrivateTmp=true

                                                                                   

                                                                                    [Install]

                                                                                    WantedBy=multi-user.target

                                                                 启动nginx服务

                                                                           ~]#systemctl start nginx.service

                                                                 配置nginx作为MogileFS的反向代理

                                                                           Tracker主机可以配置多个,用于进行负载均衡

                                                                           ~]#vim /etc/nginx/nginx.conf

                                                                                    location /images/{

                                                                                             此处的路径”/images/”会根据domain中的key的不同而不同,如果资源的key名称开头为”/”则此处应该为”/images”,如果没有”/”则为”/images/”;

                                                                                             mogilefs_tracker trackers;

                                                                                                      指定tracker地址;

                                                                                             mogilefs_domain images;

                                                                                                       指定域地址,每个location中只能设置一个;

                                                                                             mogilefs_methods GET;

                                                                                                       支持的请求方法;

                                                                                             mogilefs_noverify on;

                                                                                                       是否做校验;

                                                                                            

                                                                                             mogilefs_pass {

                                                                                                       proxy_pass $mogilefs_path;

                                                                                                                自带变量,让Nginx自动构建请求报文,发送给Tracker主机;

                                                                                                       proxy_hide_header Content-Type;

                                                                                                       proxy_buffering off;

                                                                                             }

                                                                                    }

                                                                                    location /status {

                                                                        check_status;

                                                                        access_log off;

                                                                                             allow 192.168.80.0/24;

                                                                        deny all;

                                                                                    }

                                                                                   

                                                                         upstream  trackers {

                                                                        server 192.158.80.136:7001 weight=1;

                                                                        server 192.168.80.147:7001 weight=1;

                                                                        check interval=1000 rise=2 fall=5 timeout=1000;

                                                                                             #check interval=1000 rise=2 fall=5 timeout=1000 mode=http;

                                                                                             #check_keepalive_requests 100;

                                                                                             #check_http_send= “GET / HTTP/1.0\r\n\r\n”;

                                                                                             #check_http_expect_alive http_2xx http_3xx;

                                                                                                       此check关键字只在tengine中可以使用;

                                                                         }

                                                                 重启Nginx服务

                                                                           systemctl restart nginx.service

                                                                 测试访问资源

                                                                           http://192.168.80.147/images/book6.png

                                                                           http://192.168.80.147/status

 

 

 

 

         Note示例中的拓扑结构跟示例的例子有些出入,因为在做的过程中,clone1和clone2这个两个节点为centos6,因为监听mogstored服务所需要的接口,所以没有搭建成功,只是作为mysql galers cluster使用了,并且centos7也没有搭建成功,所以也是作为mysql使用了,最后将MogileFS搭建在了node2中,然后还另加了一个节点node3(192.168.80.147)也用于搭建MogileFS;其实就是换了主机,其他的没有变化,不影响实验;

出现的问题:

         1.此文章中使用的服务启动脚本,有时候在centos7中虽然做了上述的修改但是,仍然无法启动服务,但是我换了一个centos7就可以了,目前还没有找到服务,其报错为:mogstored.pid参数错误;

         2.报错:PID file /var/run/mogilefsd/mogilefsd.pid not readable (yet?) after start.

                   解决方法:创建/var/run/mogilefsd/目录,并且更改属主属组为mogilefs即可;

         3.本文使用的rpm安装包在centos6中安装以后可以启动服务,都显示成功,但是mogstored服务的接口并没有处于监听状态,所以无法作为Storage Node提供存储空间;(这就是上面换主机的原因)

                   导致的错误为:使用mogadm check以后显示请求远端节点失败

 

         4.当上传文件以后,本来设置的是文件会冗余一份,但是通过mogfileinfo查看却只有一份,也就是devcount: 1 ,这是因为perl-Sys-Syscall模块版本(0.25)过新导致的,将其降级成0.23版本即可;

                   修改步骤:

                            下载0.23的版本:https://metacpan.org/release/BRADFITZ/Sys-Syscall-0.23

                            解压以后使用Sys-Syscall-0.23/lib/Sys/Syscall.pm覆盖现有系统中的0.25版本的同名文件(可以通过rpm -ql perl-Sys-Syscall来查看现有系统的0.25版本的文件都安装在哪了);先关闭服务,然后再覆盖;

借鉴文章:http://www.bubuko.com/infodetail-2225032.html

 

    注:根据马哥视频做的学习笔记,如有错误,欢迎指正;侵删

posted @ 2019-08-19 17:59  郭伟001  阅读(311)  评论(0编辑  收藏  举报