且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

深入浅出分布式文件系统MogileFS集群

更新时间:2022-09-27 14:14:10

   一,简介   

    MogileFS是一款开源的分布式文件存储系统,由LiveJournal旗下的Danga Interactive公司开发。Danga团队开发了包括 Memcached、MogileFS、Perlbal 等多个知名的开源项目。目前MogileFS的日益成熟使用此解决方法的公司越来越多,例如日本的又拍、digg、中国的豆瓣、1号店、大众点评、搜狗和安居客等,分别为所在的组织或公司管理着海量的图片。和传统网络存储不一样的是分布式文件系统是将数据分散存储至多台服务器上,而网络文件系统往往是将所有的数据存放在一起,存储服务器将成为系统可用性的瓶颈,不能满足大规模的存放所有数据需求。分布式文件系统采用可扩展的系统结构,利用多台存储设备分别存储,分担服务器负荷,利用位置服务器定位存储信息,因此不仅能够提升系统性能同时还易于扩展。


    二、实现组件

    MogileFS主要有三个部分组成:

①server端,包括两个部分,mogilefsd和mogstored两个应用程序,tracker是mogilefsd的实现方式,它通过数据库来保证元数据信息,比如站点的domian、class、host等,mogstored则为存储节点,默认监听7500端口,接受客服端的文件存储请求,在MogileFS安装完后,要运行mogadm工具将所有的store node注册到mogilefsd的数据库里,mogilefsd会对这些节点进行管理和监控。


②utils工具集,主要是MogileFS的一些管理工具如mogadm等。


③客服端API,MogileFS的API接口很多,常用的的时间方式有php,perl,python,java等,使用这些语言可进行编写客服端程序,实现文件的备份管理功能。


    三、MogileFS的特性及原理

    MogileFS特性:

    1.应用层服务,不需要使用和兴组件

    2.无单点故障,tracker为跟踪点,mogstored为存储节点,mysql为储存元数据节点,建议使用MySQL做高可用

    3.传输中立,无特殊协议,可用通过nfs和http实现通信

    4.简单的命名空间,没有目录,直接存储在存储空间上,通过域来实现,不共享任何数据

       深入浅出分布式文件系统MogileFS集群


    MogileFS工作原理:MogileFS主要由mogilefsd和mogstored两应用程序提供服务,tracker节点借助数据库保存各个节点文件的元数据信息,保存每个域中所哟的存储位置分布,用于实现快速定位同时监控各个节点,通知客户端存储区域位置并指挥storaged节点复制数据副本,进程名为mogilefsd监听与7001端口。storage节点为实际存放文件的地方,存储节点可以是http服务器,及其其他web服务,storage节点可自动维护键值的对应关系,storage节点前端可使用nginx进行反代,此时需要nginx-mogilefs-module-master模块,此服务监听与7501端口,进程名为mogstored.一个域中的键值是唯一的,一个mogilefs可以有多个域,域可以有多个存储设备,每个设备都有数据的容器,称之为domain.每个存储节点为一个主机(host),一个主机上可以有多个存储设备,每个设备有id号,用来定位。复制的最小单元为class,文件属性管理,定义文件存储在不同的设备上的份数。


    四、MogileFS安装及其配置实现

    安装说明:此实验可以仅适用两台服务进行,一台服务即当做server端,又当做存储节点同时还是mysql服务器,在每台服务器上部署tracker管理并监控节点。

1
2
3
4
5
#在两服务器上均安装如下rpm包
yum -y install per-IO-AIO    #mogilefsd和mogstored依赖于此包,先解决依赖关系。
yum -y install MogileFS-Server-2.46-2.el6.noarch.rpm  #安装sever端
yum -y install MogileFS-server-mogilefsd-2.46-2.el6.noarch.rpm #安装mogilefsd 
yum -y install MogileFS-Server-mogstored-2.46-2.rl6.noarch.rpm #安装mogstored

    创建数据库及授权修改相应的配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
grant all privileges on mogdb.* to 'moguser'@'127.0.0.1' identified by 'pass'
grant all privileges on mogdb.* to 'moguser'@'localhost' identified by 'pass'
flush privileges; 
#修改mogilefsd.conf配置文件
# 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=127.0.0.1 #修改为上面授权的账号密码及其主机
db_user = moguser #修改为上面授权的账号密码及其主机
db_pass = pass  #修改为上面授权的账号密码及其主机
# IP:PORT to listen on for mogilefs client requests
listen = 0.0.0.0: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 = 1
# 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
# Number of fsck workers to start by default.
# (these can cause a lot of load when fsck'ing)
#fsck_jobs = 1
# Minimum amount of space to reserve in megabytes
# default: 100
# Consider setting this to be larger than the largest file you
# would normally be uploading.
#min_free_space = 200
# Number of seconds to wait for a storage node to respond.
# default: 2
# Keep this low, so busy storage nodes are quickly ignored.
#node_timeout = 2
# Number of seconds to wait to connect to a storage node.
# default: 2
# Keep this low so overloaded nodes get skipped.
#conn_timeout = 2
# Allow replication to use the secondary node get port,
# if you have apache or similar configured for GET's
#repl_use_get_port = 1
#mogstored配置文件,一般无需修改,自行根据业务需求做决定 
maxconns = 10000
httplisten = 0.0.0.0:7500
mgmtlisten = 0.0.0.0:7501
docroot = /data/mogstored
 
#将上诉配置考别一份至其他主机,并且启动服务,安装后默认在Init.d下有启动脚本
service mogilefsd start   
service mogstored start 
[root@centos6 mogilefs]# ss -tnl |grep *.7.* #默认mogilefsd监听7001,mogstored监听7501及其7501
LISTEN     0      128                       *:7500                     *:*
LISTEN     0      128                       *:7501                     *:*
LISTEN     0      128                       *:7001                     *:*
[root@node mogilefs]#
#设置数据库,存储元数据
mogdbsetup --dbhost=127.0.0.1 --dbpass=pass 
mogdbsetup --dbname=mogdb --dbuser=moguser --dbhost=127.0.0.1 --dbpass=pass 
mogadm host add 10.1.100.1 --ip=10.1.100.1 --port=7500 --status=alive #添加host,storaged
mogadm domain add files #添加域
mogadm domain add images 
 
[root@centos6 ~]# mogstats --db_dsn="DBI:mysql:mogdb:host=localhost" --db_user="moguser" --db_pass="pass" --stats="all"
Fetching statistics... (all)
 
Statistics for devices...  #表示此storage设备存活 
  device     host                   files     status
  ---------- ---------------- ------------ ----------
  dev1       10.1.100.1          2      alive 
  dev3       10.1.100.2          1      alive
  dev4       10.1.100.2          1      alive
  ---------- ---------------- ------------ ----------
 
Statistics for file ids...
  Max file id: 9
 
Statistics for files...  #添加的域显示结果
  domain               class           files    size (m)  fullsize (m)
  -------------------- ----------- ---------- ----------- -------------
  files                default             1           0             0
  images               default             1           0             0
  -------------------- ----------- ---------- ----------- -------------
 
Statistics for replication... #默认的类示结果
  domain               class        devcount      files
  -------------------- ----------- ---------- ----------
  files                default             2          1
  images               default             2          1
  -------------------- ----------- ---------- ----------
 
Statistics for replication queue...
  status                      count
  -------------------- ------------
  -------------------- ------------
 
Statistics for delete queue...
  status                      count
  -------------------- ------------
  -------------------- ------------
 
Statistics for general queues...
  queue           status                      count
  --------------- -------------------- ------------
  --------------- -------------------- ------------
 
done
[root@node ~]#
[root@node~]# mogupload --trackers=10.1.100.1 --domain=files --key='/issue' --file='/etc/issue' #上传文件至files域内,指明可Key和上传的文件目录
[root@node ~]# moglistkeys --trackers=10.1.100.1 --domain=files #显示域内文件
/fstab.txt
/issue
[root@node ~]# mogfetch --trackers=10.1.100.1 --domain=files --key='/issue' --file='/tmp/issue' #下载域内文件至某目录 
[root@node ~]# ll /tmp/issue
-rw-r--r--. 1 root root 47 11月 25 16:48 /tmp/issue
-bash: ogfileinfo: command not found
[root@node2 ~]# mogfileinfo --tracker=10.1.100.1 --domain=files --key='/fstab.txt'
file/fstab.txt
     class:              default
  devcount:                    2
    domain:                files
       fid:                    5
       key:           /fstab.txt
    length:                 1115
 -http://10.1.100.2:7500/dev4/0/000/000/0000000005.fid        #可通过此URL访问到相应的资源
 -http://10.1.100.1:7500/dev1/0/000/000/0000000005.fid        #可通过此URL访问到相应的资源
[root@node2 ~]#


    实验结果图示:

   深入浅出分布式文件系统MogileFS集群

    五、实战nginx前端反代mogfilefs及负载均衡

    要求说明:在前期那配置nginx做反代将用户的请求调度至后端的MogileFS,此实验依赖于nginx-mogilefs-module模块,需在编译时加上此模块。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#实战配置如下所示:
#user  nobody;
worker_processes  1;
 
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
 
#pid        logs/nginx.pid;
 
 
events {
    worker_connections  1024;
}
 
 
http {
    include       mime.types;
    default_type  application/octet-stream;
 
    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';
 
    #access_log  logs/access.log  main;
 
    sendfile        on;
    #tcp_nopush     on;
 
    #keepalive_timeout  0;
    keepalive_timeout  65;
 
    #gzip  on;
    #
    upstream mogtrackers { #定义一个upstream组
        server 10.1.100.1:7001;
        server 10.1.100.2:7001;
    }
 
    server {
        listen       80;
        server_name  localhost;
 
        #charset koi8-r;
 
        #access_log  logs/host.access.log  main;
 
        location / {
            root   html;
            index  index.html index.htm;
        }
        location /images/ {
            mogilefs_tracker mogtrackers; #在此调用此mogtrackers即可实现反代至mogilefs后端
                mogilefs_domain images; #域为images域 
            mogilefs_methods get;   #请求的方法为get
 
            mogilefs_pass {
                proxy_pass $mogilefs_path;
                proxy_hide_header Content-Type;
                proxy_buffering off;
            }
            expires 1h;
        }
 
        #error_page  404              /404.html;
 
        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
 
        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}
 
        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}
 
        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }
 
 
    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;
 
    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
 
 
    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;
 
    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;
 
    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;
 
    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;
 
    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
 
}


    图示:此时可通过http://uri/images/aqua.jpg进行访问,而不是原来很长的路径名进行访问。

深入浅出分布式文件系统MogileFS集群




    总结:在大型站点中,mogilefs的重要性不言而喻,尤其是图片存储量很大的网站例如:某宝,某东等,此配置过程中,mysql易于成为单点故障,所以可将其做主从复制,同时结合MySQL半同步复制,可降低数据的丢失风险。在配置nginx做反向代理时,大部分步骤和其他反代类是不同的是,需将模块编译进nginx中才能使用此模块功能。



本文转自chengong1013 51CTO博客,原文链接:http://blog.51cto.com/purify/1878002,如需转载请自行联系原作者