fastDFS系统搭建单实例

一、背景描述

    之前公司的图片服务器已经不能胜任了(直接使用tomcat),需要重新搭建图片服务器,这两天研究了下FastDFS,感觉挺好用记录下来以供日后参考。


二、FastDFS官方介绍

   FastDFS是一款类Google FS的开源分布式文件系统,它用纯C语言实现,支持Linux、FreeBSD、AIX等UNIX系统。它只能通过 专有API对文件进行存取访问,不支持POSIX接口方式,不能mount使用。准确地讲,Google FS以及FastDFS、mogileFS、 HDFS、TFS等类Google FS都不是系统级的分布式文件系统,而是应用级的分布式文件存储服务。 

    1、FastDFS架构图

    wKioL1cZmp3xhDAdAAMMQuy6qEc170.jpg

                                                            图1:FastDFS架构图

    1)Tracker cluster中各个tracker server相互独立,不进行相互通信。

    2)Storage cluster中各个storage组(Volume1,Volume2...)相互独立,不进行相互通信,也就是说各个组之间保存的数据是不相同的。但是各个组中的storage server之间是属于互相备份的关系,也就是说storage server之间保存相同的数据。

    3)每个storage server会启动一个单独的线程主动向Tracker cluster中每个tracker server报告其状态信息,包括磁盘使用情况,文件同步情况及文件上传下载次数统计等信息。


    2、文件上传和下载的时序图

         

wKioL1cZm1KwweSxAACFK-IhvCs899.jpg

                                    图2:文件上传时序图

    1)Client通过Tracker server将文件上传到Storage server。

    2)Tracker server向Client返回一台可用的Storage server的IP地址和端口号。

    3)Client直接通过Tracker server返回的IP地址和端口与其中一台Storage server建立连接并进行文件上传。

    4)上传完成,Storage server返回Client一个文件ID,文件上传结束。

                              

wKiom1cZmo-RxJsXAACFly7rbaQ068.jpg

                                     图3:文件下载时序图

    1)Client通过Tracker server下载指定Storage组中某个Storage server上的某个文件(文件名包括Storage组名称)。

    2)Tracker server向Client返回一台可用的Storage server的IP地址和端口号。

    3)Client直接通过Tracker server返回的IP地址和端口与其中一台Storage server建立连接并进行文件下载。


三、安装并配置FastDFS

    1、运行环境及相关软件

    CentOS 7.0

    FastDFS_v5.08.tar.gz

    nginx-1.8.1.tar.gz

    fastdfs-nginx-module_v1.16.tar.gz

    libfastcommon

    2、服务器规划

服务器名称
IP地址和端口
备注
fastdfs-tracker
172.16.36.158:22122
跟踪服务器/调度服务器
fastdfs-storage
172.16.36.158:23000
存储服务器

      3.安装fastDFS

      1)上传准备文件到/usr/local下面

     2) unzip libfastcommon-master.zip

     cd libfastcommon-master

     ./make.sh

    ./make.sh install

   3)tar xf FastDFS_v5.08.tar.gz

    cd FastDFS

   ./make.sh && ./make.sh install


采用默认方式安装后的文件及目录:

    1)服务脚本:

1
2
3
[root@fastdfs-tracker ~] # ll /etc/init.d/ |grep fdfs
-rwxr-xr-x. 1 root root   918 4月  22 22:08 fdfs_storaged
-rwxr-xr-x. 1 root root   920 4月  22 22:08 fdfs_trackerd

    2)样例配置文件

1
2
3
4
5
[root@fastdfs-tracker ~] # ll /etc/fdfs/
总用量 20
-rw-r--r--. 1 root root 1461 4月  22 22:08 client.conf.sample
-rw-r--r--. 1 root root 7927 4月  22 22:08 storage.conf.sample
-rw-r--r--. 1 root root 7200 4月  22 22:08 tracker.conf.sample

    3)命令行工具

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[root@fastdfs-tracker ~] # ll /usr/bin|grep fdfs
-rwxr-xr-x.   1 root root     252272 4月  22 22:08 fdfs_appender_test
-rwxr-xr-x.   1 root root     252225 4月  22 22:08 fdfs_appender_test1
-rwxr-xr-x.   1 root root     242449 4月  22 22:08 fdfs_append_file
-rwxr-xr-x.   1 root root     242013 4月  22 22:08 fdfs_crc32
-rwxr-xr-x.   1 root root     242508 4月  22 22:08 fdfs_delete_file
-rwxr-xr-x.   1 root root     243627 4月  22 22:08 fdfs_download_file
-rwxr-xr-x.   1 root root     243369 4月  22 22:08 fdfs_file_info
-rwxr-xr-x.   1 root root     255657 4月  22 22:08 fdfs_monitor
-rwxr-xr-x.   1 root root     863913 4月  22 22:08 fdfs_storaged
-rwxr-xr-x.   1 root root     258712 4月  22 22:08 fdfs_test
-rwxr-xr-x.   1 root root     257881 4月  22 22:08 fdfs_test1
-rwxr-xr-x.   1 root root     365232 4月  22 22:08 fdfs_trackerd
-rwxr-xr-x.   1 root root     243547 4月  22 22:08 fdfs_upload_appender
-rwxr-xr-x.   1 root root     244453 4月  22 22:08 fdfs_upload_file

    注意:虽然FastDFS区分tracker和storage服务器,但是安装的软件及步骤均相同,只是不同的配置文件而已,因此以上安装适用tracker server和storage server

   

4、配置跟踪服务器(tracker server)

    1)拷贝tracker server和client端样例配置文件并重命名

1
2
[root@fastdfs-tracker ~] # cp /etc/fdfs/tracker.conf.sample /etc/fdfs/tracker.conf
[root@fastdfs-storage ~] # cp /etc/fdfs/client.conf.sample /etc/fdfs/client.conf

    2)编辑tracker server配置文件tracker.conf,需要修改内容如下:

1
2
3
     disabled= false (默认为 false ,表示是否无效)
     port=22122(默认为22122)
     base_path= /data/fastdfs/tracker

    3)编辑client端的配置文件client.conf,需要修改内容如下

1
2
     base_path= /data/fastdfs/tracker
     tracker_server=172.18.10.232:22122

    4)创建tracker server数据目录

1
[root@fastdfs-tracker ~] # mkdir -p /data/fastdfs/tracker

    5)测试启动tracker server,启动成功会自动在/data/fastdfs/tracker目录新建data和logs目录

1
2
3
4
5
6
7
8
[root@fastdfs-tracker ~] # cd /data/fastdfs/tracker/
[root@fastdfs-tracker tracker] # ls
[root@fastdfs-tracker tracker] # /etc/init.d/fdfs_trackerd start
Starting FastDFS tracker server:
[root@fastdfs-tracker tracker] # ss -lntup|grep 22122
tcp    LISTEN     0      128                    *:22122                 *:*       users :(( "fdfs_trackerd" ,3785,5)) 
[root@fastdfs-tracker tracker] # ls
data  logs

    6)关闭tracker server

1
2
3
[root@fastdfs-tracker tracker] # /etc/init.d/fdfs_trackerd stop
waiting  for  pid [3785]  exit  ...
pid [3785]  exit .

    5、配置存储服务器(storage server)

    1)拷贝storage server样例配置文件并重命名

1
[root@fastdfs-storage ~] # cp /etc/fdfs/storage.conf.sample /etc/fdfs/storage.conf

    2)编辑storage server配置文件storage.conf,需要修改内容如下:

1
2
3
4
5
6
     disabled= false (默认为 false ,表示是否无效)
     port=23000(默认为23000)
     base_path= /data/fastdfs/storage
     tracker_server= 172.16.36.158 :22122
     store_path0= /data/fastdfs/storage
     http.server_port=80(默认为8888,nginx中配置的监听端口那之一致)

3)创建storage server数据目录

1
[root@fastdfs-storage ~] # mkdir -p /data/fastdfs/storage

    4)测试启动storage server,启动成功会自动在/data/fastdfs/tracker目录新建data和logs目录(启动storage server的前提是tracker server必须事先已启动)

1
2
3
4
5
6
7
8
[root@fastdfs-storage ~] # cd /data/fastdfs/storage/
[root@fastdfs-storage storage] # ls
[root@fastdfs-storage storage] # /etc/init.d/fdfs_storaged start
Starting FastDFS storage server:
[root@fastdfs-storage storage] # ss -lntup|grep 23000
tcp    LISTEN     0      128                    *:23000                 *:*       users :(( "fdfs_storaged" ,3786,5))
[root@fastdfs-storage storage] # ls
data  logs

    6、文件上传测试

    执行如下上传命令:

1
2
3
[root@fastdfs-tracker ~] # /usr/bin/fdfs_upload_file /etc/fdfs/client.conf /home/oldcat/imgs/test.jpg
group1 /M00/00/00/rBIK6VcaP0aARXXvAAHrUgHEviQ394 .jpg
返回文件ID即说明文件已经上传成功

存储服务器(storage server)安装并配置nginx

    1)下载并安装fastdfs-nginx-module模块

    注:FastDFS通过Tracker服务器,将文件放在Storage服务器存储,但是同组存储服务器之间需要进入文件复制,有同步延迟的问题。假设Tracker服务器将文件上传到了192.168.4.125,上传成功后文件ID已经返回给客户端。此时FastDFS存储集群机制会将这个文件同步到同组存储192.168.4.126,在文件还没有复制完成的情况下,客户端如果用这个文件ID在192.168.4.126上取文件,就会出现文件无法访问的错误。而fastdfs-nginx-module可以重定向文件连接到源服务器取文件,避免客户端由于复制延迟导致的文件无法访问错误。


[root@fastdfs-storage tools] # tar xf fastdfs-nginx-module_v1.16.tar.gz 
[root@fastdfs-storage tools] # cd fastdfs-nginx-module/src/
[root@fastdfs-storage src] # vim config
编辑config文件,执行如下命令进行批量替换并保存退出
:%s+ /usr/local/ + /usr/ +g

2)拷贝fastdfs-nginx-module模块中配置文件到/etc/fdfs目录中并编辑

1
2
3
4
5
6
7
8
9
10
[root@fastdfs-storage ~] # cp /home/oldcat/tools/fastdfs-nginx-module/src/mod_fastdfs.conf /etc/fdfs/
[root@fastdfs-storage ~] # vim /etc/fdfs/mod_fastdfs.conf
修改内容如下:
connect_timeout=10
base_path= /tmp (默认为 /tmp
tracker_server= 172.16.36.158 :22122
storage_server_port=23000(默认配置为23000)
url_have_group_name =  true
store_path0= /data/fastdfs/storage
group_name=group1(默认配置为group1)
3)编译设置Nginx(本机有安装好的nginx-1.13.5)
cd /usr/local/nginx-1.13.5
./configure --prefix=/usr/local/nginx/ --add-module=/usr/local/fastdfs-nginx-module/src/
make && make install

5)拷贝FastDFS中的部分配置文件到/etc/fdfs目录中

1
2
[root@fastdfs-storage ~] # cp /home/oldcat/tools/FastDFS/conf/http.conf /etc/fdfs/
[root@fastdfs-storage ~] # cp /home/oldcat/tools/FastDFS/conf/mime.types /etc/fdfs/

    6)配置nginx,如下所示:


 server {
        listen       80;
        server_name  localhost;


        #charset koi8-r;


        #access_log  logs/host.access.log  main;


      location ~/group[0-9]/ {
                ngx_fastdfs_module;
            } 


        location / {
            root   html;
            index  index.html index.htm;
        }


        #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;
        #}

    }


最后:测试

[root@izbp1eqxyns1r4n9c42c73z nginx]# /usr/bin/fdfs_upload_file /etc/fdfs/client.conf /usr/local/default.png 

group1/M00/00/00/rBAknls0UaGAZrCEAAAQP9C9yCg091.png

http://你的IP/group1/M00/00/00/rBAknls0UaGAZrCEAAAQP9C9yCg091.png

可以看到图片

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值