zoukankan      html  css  js  c++  java
  • FastDFS分布式文件系统

    FastDFS分布式文件系统

    github地址:https://github.com/happyfish100

     

    FastDFS 介绍

    FastDFS是一个开源的分布式文件系统,她对文件进行管理,功能包括:文件存储、文件同步、文件访问(文件上传、文件下载)等,解决了大容量存储和负载均衡的问题。特别适合以文件为载体的在线服务,如相册网站、视频网站等等。

    FastDFS服务端有两个角色:跟踪器(tracker)和存储节点(storage)。跟踪器主要做调度工作,在访问上起负载均衡的作用。

    存储节点存储文件,完成文件管理的所有功能:存储、同步和提供存取接口,FastDFS同时对文件的meta data进行管理。

    FastDFS系统结构如下图所示:

     

    跟踪器和存储节点都可以由一台或多台服务器构成。跟踪器和存储节点中的服务器均可以随时增加或下线而不会影响线上服务。其中跟踪器中的所有服务器都是对等的,可以根据服务器的压力情况随时增加或减少。

     

    为了支持大容量,存储节点(服务器)采用了分卷(或分组)的组织方式。存储系统由一个或多个卷组成,卷与卷之间的文件是相互独立的,所有卷 的文件容量累加就是整个存储系统中的文件容量。一个卷可以由一台或多台存储服务器组成,一个卷下的存储服务器中的文件都是相同的,卷中的多台存储服务器起 到了冗余备份和负载均衡的作用。

    在卷中增加服务器时,同步已有的文件由系统自动完成,同步完成后,系统自动将新增服务器切换到线上提供服务。

    当存储空间不足或即将耗尽时,可以动态添加卷。只需要增加一台或多台服务器,并将它们配置为一个新的卷,这样就扩大了存储系统的容量。 

    上传文件交互过程: 

    1. client询问tracker上传到的storage,不需要附加参数; 
    2. tracker返回一台可用的storage; 
    3. client直接和storage通讯完成文件上传

     

    下载文件交互过程:

    1. client询问tracker下载文件的storage,参数为文件标识(卷名和文件名);
    2. tracker返回一台可用的storage;

    client直接和storage通讯完成文件下载。

    存储节点存储结构:

    storage节点按group(卷),在data下有256个1级目录,每级目录下又有256个2级子目录,总共65536个文件,新写的文件会以hash的方式被路由到其中某个子目录下,然后将文件数据直接作为一个本地文件存储到该目录中

    文件名格式:

     

     

    FastDFS为什么要结合Nginx

    .Version 4.05之前fastdfs内部绑定了libevent作为http服务器.Version 4.05之后的版本删除了内置的web http服务,内置的web http服务是个累赘,不用也罢,这里自己安装nginx做其WEB服务器

    软件准备:

    libfastcommon:
    
    https://github.com/happyfish100/libfastcommon/archive/V1.0.39.tar.gz
    
    fastdfs:
    
    https://github.com/happyfish100/fastdfs/archive/V5.11.tar.gz
    
    fastdfs-nginx-module:
    
    https://github.com/happyfish100/fastdfs-nginx-module/archive/V1.20.tar.gz
    
    nginx:
    
    http://nginx.org/download/nginx-1.14.2.tar.gz

     

    环境准备:

    服务器角色:

    192.168.1.101    tracker  nginx
    
    192.168.1.102   storage  nginx
    
    192.168.1.103    storage  nginx

    系统初始化

    关闭防火墙和selinux

    安装gcc gcc-c++

    yum -y install zlib zlib-devel pcre pcre-devel gcc gcc-c++ openssl openssl-devel libevent libevent-devel perl unzip net-tools wget

    架构图示:

         

                                     用户                                    
    
                                          nginx(upstream -->(192.168.1.102:9999,192.168.1.103:9999)   
    
                                   192.168.1.101
    
                                        tracker
    
        nginx                                                 nginx
    
    192.168.1.102                                  192.168.1.103
    
      storage                                               storage

     

     

     

    安装:

    安装libfastcommon库:

    [root@k8s-node1 libfastcommon-1.0.39]#tar xf V1.0.39.tar.gz
    
    [root@k8s-node1 libfastcommon-1.0.39]#cd /opt/libfastcommon-1.0.39/
    
    [root@k8s-node1 libfastcommon-1.0.39]#./make.sh
    
    [root@k8s-node1 libfastcommon-1.0.39]#echo $?
    
    [root@k8s-node1 libfastcommon-1.0.39]# ./make.sh install
    
    mkdir -p /usr/lib64
    
    mkdir -p /usr/lib
    
    mkdir -p /usr/include/fastcommon
    
    install -m 755 libfastcommon.so /usr/lib64
    
    install -m 644 common_define.h hash.h chain.h logger.h base64.h shared_func.h pthread_func.h ini_file_reader.h _os_define.h sockopt.h sched_thread.h http_func.h md5.h local_ip_func.h avl_tree.h ioevent.h ioevent_loop.h fast_task_queue.h fast_timer.h process_ctrl.h fast_mblock.h connection_pool.h fast_mpool.h fast_allocator.h fast_buffer.h skiplist.h multi_skiplist.h flat_skiplist.h skiplist_common.h system_info.h fast_blocked_queue.h php7_ext_wrapper.h id_generator.h char_converter.h char_convert_loader.h common_blocked_queue.h multi_socket_client.h skiplist_set.h fc_list.h /usr/include/fastcommon
    
    if [ ! -e /usr/lib/libfastcommon.so ]; then ln -s /usr/lib64/libfastcommon.so /usr/lib/libfastcommon.so; fi
    
    [root@k8s-node1 libfastcommon-1.0.39]# echo $?
    
    0

    安装fastdfs:

    [root@k8s-node1 fastdfs-5.11]#/opt/fastdfs-5.11
    
    [root@k8s-node1 fastdfs-5.11]# ./make.sh
    
    [root@k8s-node1 fastdfs-5.11]# ./make.sh install
    mkdir -p /usr/bin
    mkdir -p /etc/fdfs
    cp -f fdfs_trackerd /usr/bin
    if [ ! -f /etc/fdfs/tracker.conf.sample ]; then cp -f ../conf/tracker.conf /etc/fdfs/tracker.conf.sample; fi
    if [ ! -f /etc/fdfs/storage_ids.conf.sample ]; then cp -f ../conf/storage_ids.conf /etc/fdfs/storage_ids.conf.sample; fi
    mkdir -p /usr/bin
    mkdir -p /etc/fdfs
    cp -f fdfs_storaged /usr/bin
    if [ ! -f /etc/fdfs/storage.conf.sample ]; then cp -f ../conf/storage.conf /etc/fdfs/storage.conf.sample; fi
    mkdir -p /usr/bin
    mkdir -p /etc/fdfs
    mkdir -p /usr/lib64
    mkdir -p /usr/lib
    cp -f fdfs_monitor fdfs_test fdfs_test1 fdfs_crc32 fdfs_upload_file fdfs_download_file fdfs_delete_file fdfs_file_info fdfs_appender_test fdfs_appender_test1 fdfs_append_file fdfs_upload_appender /usr/bin
    if [ 0 -eq 1 ]; then cp -f libfdfsclient.a /usr/lib64; cp -f libfdfsclient.a /usr/lib/;fi
    if [ 1 -eq 1 ]; then cp -f libfdfsclient.so /usr/lib64; cp -f libfdfsclient.so /usr/lib/;fi
    mkdir -p /usr/include/fastdfs
    cp -f ../common/fdfs_define.h ../common/fdfs_global.h ../common/mime_file_parser.h ../common/fdfs_http_shared.h ../tracker/tracker_types.h ../tracker/tracker_proto.h ../tracker/fdfs_shared_func.h ../storage/trunk_mgr/trunk_shared.h tracker_client.h storage_client.h storage_client1.h client_func.h client_global.h fdfs_client.h /usr/include/fastdfs
    if [ ! -f /etc/fdfs/client.conf.sample ]; then cp -f ../conf/client.conf /etc/fdfs/client.conf.sample; fi
    [root@k8s-node1 fastdfs-5.11]# echo $?
    0
    [root@k8s-node1 fastdfs-5.11]# ls /etc/fdfs/
    client.conf.sample storage.conf.sample storage_ids.conf.sample tracker.conf.sample
    [root@k8s-node1 fastdfs-5.11]#

    创建数据目录:

    mkdir -p /opt/fastdfs/tarcker
    
    mkdir -p /opt/fastdfs/storage

    配置文件

     

    cd /etc/fdfs
    
    cp client.conf.sample client.conf
    
    cp storage.conf.sample storage.conf
    
    cp tracker.conf.sample tracker.conf
    
    最终文件如下:
    
    client.conf client.conf.sample http.conf mime.types mod_fastdfs.conf storage.conf storage.conf.sample storage_ids.conf.sample tracker.conf tracker.conf.sample

     

     

    配置清单:

    https://github.com/happyfish100/fastdfs/blob/master/INSTALL

    直接修改默认配置有注释

    启动:

    第一次启动要使用:
    
    service fdfs_trackerd start 
    
    service fdfs_storaged  start
    
    以后可以使用:
    
    systemctl start fdfs_trackerd
    systemctl start fdfs_storaged
    
    systemctl enable fdfs_trackerd
    systemctl enable fdfs_storaged

    安装nginx:

    cd /usr/local/nginx-1.14.2
    
    ./configure --prefix=/usr/local/nginx --add-module=/opt/fastdfs-nginx-module-1.20/src --user=nginx --group=nginx --with-http_ssl_module
    make
    
    make install

    安装完成后使用以下命令验证:

    [root@k8s-node1 nginx]# ./sbin/nginx -V
    nginx version: nginx/1.14.2
    built by gcc 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC) 
    configure arguments: --prefix=/usr/local/nginx --add-module=/opt/fastdfs-nginx-module-1.20/src --user=nginx --group=nginx
    [root@k8s-node1 nginx]#

    配置nginx:

    [root@k8s-node1 conf.d]# cat fastdfs.conf 
    upstream fdfs_group1 {
    server 192.168.1.102:9999;
    server 192.168.1.103:9999;
    }
    server {
    listen 9999;
    server_name localhost;
    
    #charset koi8-r;
    
    #access_log logs/host.access.log main;
    
    location /group1/M00 {
    proxy_pass http://fdfs_group1;
    }
    }
    
    [root@k8s-node1 conf.d]#
    
    [root@k8s-node2 conf]# cat conf.d/stoage.conf 
    server {
    listen 9999;
    server_name localhost;
    
    location / {
    root html;
    index index.html index.htm;
    }
    
    location /group1/M00 {
    root /opt/fastdfs/fastdfs/storage/data;
    ngx_fastdfs_module;
    }
    }
    
    [root@k8s-node2 conf]#

    如果使用多个group。这里要使用正则:

    location ~/group([0-9])/M00

    nginx启动及停止:

     

    systemctl start nginx
    systemctl stop nginx

     

     

    编译nginx的fastdfs模块时的小插曲:
    [解决]FastDFS安装Nginx的模块,fastdfs-nginx-module报错:fdfs_define.h:15:27: 致命错误:common_define.h:没有那个文件或目录

    vim fastdfs-nginx-module-1.20/src/config
    改变的文件内容

    ngx_module_incs="/usr/include/fastdfs /usr/include/fastcommon/"
    CORE_INCS="$CORE_INCS /usr/include/fastdfs /usr/include/fastcommon/"

    上面安装部署在所有节点都要操作,启动完成后可以开始测试。

    上传测试:

    [root@k8s-node1 fdfs]# /usr/bin/fdfs_upload_file /etc/fdfs/client.conf /root/20180108122834_41537.jpg 
    group1/M00/00/00/wKgBZlyvJCCAf8j7AABHsixzgj4521.jpg
    [root@k8s-node1 fdfs]#
    
    [root@k8s-node1 ~]# /usr/bin/fdfs_upload_file /etc/fdfs/client.conf /root/徐亮20190214.png
    group1/M00/00/00/wKgBZlyv6cGAck7aAAA_ZQ-yo84703.png
    [root@k8s-node1 ~]#

    文件访问:

    http://192.168.1.101:9999/group1/M00/00/00/wKgBZlyvJCCAf8j7AABHsixzgj4521.jpg

    复杂均衡:

     


    http://192.168.1.101:9999/group1/M00/00/00/wKgBZlyv6cGAck7aAAA_ZQ-yo84703.png

    当然也可以直接访问:

    http://192.168.1.102:9999/group1/M00/00/00/wKgBZlyvJCCAf8j7AABHsixzgj4521.jpg

     

    访问日志:

     

    http://192.168.1.103:9999/group1/M00/00/00/wKgBZlyvJCCAf8j7AABHsixzgj4521.jpg

    其他节点访问省略。。。。。

  • 相关阅读:
    cast() 函数进行类型转换
    '+' 拼接字符串引起的小事故
    shell统计ip访问情况并分析访问日志
    Windows 环境上域名配置
    WebApi中Route的作用
    Postman测试WebApi使用总结
    C# VS2017新建WepApi
    C# 反射总结
    winform--同一个项目窗体复制
    winform TextBox设置透明
  • 原文地址:https://www.cnblogs.com/xuliang666/p/11112502.html
Copyright © 2011-2022 走看看