分布式文件系统 fastdfs

Posted 从零开始的linux

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了分布式文件系统 fastdfs相关的知识,希望对你有一定的参考价值。

fastdfs

FastDFS是一款开源的轻量级分布式文件系统纯C实现,支持Linux、FreeBSD等UNIX系统类google FS,不是通用的文件系统,只能通过专有API访问,目前提供了C、Java和php API为互联网应用量身定做,解决大容量文件存储问题,追求高性能和高扩展性FastDFS可以看做是基于文件的key value pair存储系统,称作分布式文件存储服务更为合适。

主机 ip 端口(tracker) 端口(storage)
node1(tracker) 128.0.0.81 22122
node2(storage) 128.0.0.82
23000
node3(storage) 128.0.0.83
23000

安装tracker

 
   
   
 
  1. [root@node1 ~]# wget https://github.com/happyfish100/libfastcommon/archive/V1.0.7.tar.gz

  2. [root@node1 ~]# tar -zxvf V1.0.7.tar.gz

  3. [root@node1 ~]# cd libfastcommon-1.0.7/

  4. [root@node1 libfastcommon-1.0.7]# ls

  5. HISTORY  INSTALL  libfastcommon.spec  make.sh  README  src

  6. [root@node1 libfastcommon-1.0.7]# ./make.sh

  7. [root@node1 libfastcommon-1.0.7]# ./make.sh install

  8. [root@node1 ~]# wget https://github.com/happyfish100/fastdfs/archive/V5.05.tar.gz

  9. [root@node1 ~]# tar -zxvf V5.05.tar.gz

  10. [root@node1 ~]# cd fastdfs-5.05/

  11. [root@node1 fastdfs-5.05]# ./make.sh

  12. [root@node1 fastdfs-5.05]# ./make.sh install

  13. [root@node1 ~]# cd /etc/fdfs/

  14. [root@node1 fdfs]# ls

  15. client.conf.sample  storage.conf.sample  tracker.conf.sample

  16. [root@node1 fdfs]# cp tracker.conf.sample tracker.conf

  17. #修改目录和端口

  18. [root@node1 fdfs]# vim tracker.conf

  19. base_path=/data/fastdfs/tracker

  20. http.server_port=80

  21. #创建目录

  22. [root@node1 fdfs]# mkdir -pv /data/fastdfs/tracker

  23. #做软链接

  24. [root@node1 fdfs]# ln -s /usr/bin/fdfs_trackerd /usr/local/bin

  25. [root@node1 fdfs]# ln -s /usr/bin/stop.sh /usr/local/bin

  26. [root@node1 fdfs]# ln -s /usr/bin/restart.sh /usr/local/bin

  27. #启动

  28. [root@node1 fdfs]# service fdfs_trackerd start

  29. Unit fdfs_trackerd.service could not be found.

  30. Reloading systemd:                                         [  OK  ]

  31. Starting fdfs_trackerd (via systemctl):                    [  OK  ]

  32. #查看端口

  33. [root@node1 fdfs]# netstat -nltp

  34. Active Internet connections (only servers)

  35. Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    

  36. tcp        0      0 0.0.0.0:22122           0.0.0.0:*               LISTEN      22547/fdfs_trackerd

storage的安装与tracker安装类似,首先编译安装fastdfs,然后修改配置文件。

 
   
   
 
  1. [root@node2 ~]# cd /etc/fdfs/

  2. [root@node2 fdfs]# cp storage.conf.sample storage.conf

  3. [root@node3 fdfs]# cp storage.conf.sample storage.conf

  4. [root@node2 fdfs]# vim storage.conf

  5. disabled=false #启用配置文件

  6. group_name=group1 #组名,根据实际情况修改

  7. port=23000 #设置 storage 的端口号

  8. base_path=/data/fastdfs/storage #设置 storage 的日志目录(需预先创建)

  9. store_path_count=1 #存储路径个数,需要和 store_path 个数匹配 store_path0=/data/fastdfs/storage #存储路径

  10. #创建目录

  11. [root@node2 bin]# mkdir -pv /data/fastdfs/storage

  12. #软链接

  13. [root@node2 fdfs]# ln -s /usr/bin/fdfs_storaged /usr/local/bin

  14. [root@node2 bin]# ln -s /usr/bin/fdfs_trackerd /usr/local/bin

  15. [root@node2 bin]# ln -s /usr/bin/stop.sh /usr/local/bin

  16. [root@node2 bin]# ln -s /usr/bin/restart.sh /usr/local/bin

  17. #启动

  18. [root@node2 bin]# systemctl start fdfs_storaged.service

  19. #查看端口

  20. [root@node2 bin]# netstat -nlatp|grep fdfs

  21. tcp        0      0 0.0.0.0:23000           0.0.0.0:*               LISTEN      22431/fdfs_storaged

  22. tcp        0      0 128.0.0.82:53724        128.0.0.81:22122        ESTABLISHED 22431/fdfs_storaged

  23. [root@node2 bin]# ll /data/fastdfs/storage

  24. total 12

  25. drwxr-xr-x 259 root root 8192 Dec  3 13:45 data

  26. drwxr-xr-x   2 root root   26 Dec  3 13:41 logs

查看Tracker 和 Storage 服务的通信

 
   
   
 
  1. [root@node2 ~]# /usr/bin/fdfs_monitor /etc/fdfs/storage.conf

  2. [2017-12-03 14:29:19] DEBUG - base_path=/data/fastdfs/storage, connect_timeout=30, network_timeout=60, tracker_server_count=1, anti_steal_token=0, anti_steal_secret_key length=0, use_connection_pool=0, g_connection_pool_max_idle_time=3600s, use_storage_id=0, storage server id count: 0

  3. server_count=1, server_index=0

  4. tracker server is 128.0.0.81:22122

  5. group count: 1

  6. Group 1:

  7. group name = group1

  8. disk total space = 92115 MB

  9. disk free space = 90561 MB

  10. trunk free space = 0 MB

  11. storage server count = 1

  12. active server count = 1

  13. storage server port = 23000

  14. storage HTTP port = 8888

  15. store path count = 1

  16. subdir count per path = 256

  17. current write server index = 0

  18. current trunk file id = 0

  19.    Storage 1:

  20.        id = 128.0.0.82

  21.        ip_addr = 128.0.0.82 (node2)  ACTIVE

  22.        http domain =

  23.        version = 5.05

  24.        join time = 2017-12-03 13:41:51

  25.        up time = 2017-12-03 13:45:35

  26.        total storage = 92115 MB

  27.        free storage = 90561 MB

  28.        upload priority = 10

  29.        store_path_count = 1

  30.        subdir_count_per_path = 256

  31.        storage_port = 23000

  32.        storage_http_port = 8888

  33.        current_write_path = 0

  34.        source storage id =

  35.        if_trunk_server = 0

  36.        connection.alloc_count = 256

  37.        connection.current_count = 0

  38.        connection.max_count = 0

  39.        total_upload_count = 0

  40.        success_upload_count = 0

  41.        total_append_count = 0

  42.        success_append_count = 0

  43.        total_modify_count = 0

  44.        success_modify_count = 0

  45.        total_truncate_count = 0

  46.        success_truncate_count = 0

  47.        total_set_meta_count = 0

  48.        success_set_meta_count = 0

  49.        total_delete_count = 0

  50.        success_delete_count = 0

  51.        total_download_count = 0

  52.        success_download_count = 0

  53.        total_get_meta_count = 0

  54.        success_get_meta_count = 0

  55.        total_create_link_count = 0

  56.        success_create_link_count = 0

  57.        total_delete_link_count = 0

  58.        success_delete_link_count = 0

  59.        total_upload_bytes = 0

  60.        success_upload_bytes = 0

  61.        total_append_bytes = 0

  62.        success_append_bytes = 0

  63.        total_modify_bytes = 0

  64.        success_modify_bytes = 0

  65.        stotal_download_bytes = 0

  66.        success_download_bytes = 0

  67.        total_sync_in_bytes = 0

  68.        success_sync_in_bytes = 0

  69.        total_sync_out_bytes = 0

  70.        success_sync_out_bytes = 0

  71.        total_file_open_count = 0

  72.        success_file_open_count = 0

  73.        total_file_read_count = 0

  74.        success_file_read_count = 0

  75.        total_file_write_count = 0

  76.        success_file_write_count = 0

  77.        last_heart_beat_time = 2017-12-03 14:29:06

  78.        last_source_update = 1970-01-01 08:00:00

  79.        last_sync_update = 1970-01-01 08:00:00

  80.        last_synced_timestamp = 1970-01-01 08:00:00

查看端口

 
   
   
 
  1. [root@node3 ~]# netstat -nlatp | grep fdfs

  2. tcp        0      0 0.0.0.0:23000           0.0.0.0:*               LISTEN      22381/fdfs_storaged

  3. tcp        0      0 128.0.0.83:23000        128.0.0.82:60548        ESTABLISHED 22381/fdfs_storaged

  4. tcp        0      0 128.0.0.83:60226        128.0.0.81:22122        ESTABLISHED 22381/fdfs_storaged

  5. tcp        0      0 128.0.0.83:54014        128.0.0.82:23000        ESTABLISHED 22381/fdfs_storaged

查看Tracker 和 Storage 服务的通信

 
   
   
 
  1. [root@node3 ~]# /usr/bin/fdfs_monitor /etc/fdfs/storage.conf

  2. [2017-12-03 14:46:15] DEBUG - base_path=/data/fastdfs/storage, connect_timeout=30, network_timeout=60, tracker_server_count=1, anti_steal_token=0, anti_steal_secret_key length=0, use_connection_pool=0, g_connection_pool_max_idle_time=3600s, use_storage_id=0, storage server id count: 0

  3. server_count=1, server_index=0

  4. tracker server is 128.0.0.81:22122

  5. group count: 1

  6. Group 1:

  7. group name = group1

  8. disk total space = 92115 MB

  9. disk free space = 90560 MB

  10. trunk free space = 0 MB

  11. storage server count = 2

  12. active server count = 2

  13. storage server port = 23000

  14. storage HTTP port = 8888

  15. store path count = 1

  16. subdir count per path = 256

  17. current write server index = 0

  18. current trunk file id = 0

  19.    Storage 1:

  20.        id = 128.0.0.82

  21.        ip_addr = 128.0.0.82 (node2)  ACTIVE

  22.        http domain =

  23.        version = 5.05

  24.        join time = 2017-12-03 13:41:51

  25.        up time = 2017-12-03 13:45:35

  26.        total storage = 92115 MB

  27.        free storage = 90561 MB

  28.        upload priority = 10

  29.        store_path_count = 1

  30.        subdir_count_per_path = 256

  31.        storage_port = 23000

  32.        storage_http_port = 8888

  33.        current_write_path = 0

  34.        source storage id =

  35.        if_trunk_server = 0

  36.        connection.alloc_count = 256

  37.        connection.current_count = 1

  38.        connection.max_count = 1

  39.        total_upload_count = 0

  40.        success_upload_count = 0

  41.        total_append_count = 0

  42.        success_append_count = 0

  43.        total_modify_count = 0

  44.        success_modify_count = 0

  45.        total_truncate_count = 0

  46.        success_truncate_count = 0

  47.        total_set_meta_count = 0

  48.        success_set_meta_count = 0

  49.        total_delete_count = 0

  50.        success_delete_count = 0

  51.        total_download_count = 0

  52.        success_download_count = 0

  53.        total_get_meta_count = 0

  54.        success_get_meta_count = 0

  55.        total_create_link_count = 0

  56.        success_create_link_count = 0

  57.        total_delete_link_count = 0

  58.        success_delete_link_count = 0

  59.        total_upload_bytes = 0

  60.        success_upload_bytes = 0

  61.        total_append_bytes = 0

  62.        success_append_bytes = 0

  63.        total_modify_bytes = 0

  64.        success_modify_bytes = 0

  65.        stotal_download_bytes = 0

  66.        success_download_bytes = 0

  67.        total_sync_in_bytes = 0

  68.        success_sync_in_bytes = 0

  69.        total_sync_out_bytes = 0

  70.        success_sync_out_bytes = 0

  71.        total_file_open_count = 0

  72.        success_file_open_count = 0

  73.        total_file_read_count = 0

  74.        success_file_read_count = 0

  75.        total_file_write_count = 0

  76.        success_file_write_count = 0

  77.        last_heart_beat_time = 2017-12-03 14:46:05

  78.        last_source_update = 1970-01-01 08:00:00

  79.        last_sync_update = 1970-01-01 08:00:00

  80.        last_synced_timestamp = 1970-01-01 08:00:00

  81.    Storage 2:

  82.        id = 128.0.0.83

  83.        ip_addr = 128.0.0.83 (node3)  ACTIVE

  84.        http domain =

  85.        version = 5.05

  86.        join time = 2017-12-03 14:44:08

  87.        up time = 2017-12-03 14:44:31

  88.        total storage = 92115 MB

  89.        free storage = 90560 MB

  90.        upload priority = 10

  91.        store_path_count = 1

  92.        subdir_count_per_path = 256

  93.        storage_port = 23000

  94.        storage_http_port = 8888

  95.        current_write_path = 0

  96.        source storage id = 128.0.0.82

  97.        if_trunk_server = 0

  98.        connection.alloc_count = 256

  99.        connection.current_count = 1

  100.        connection.max_count = 1

  101.        total_upload_count = 0

  102.        success_upload_count = 0

  103.        total_append_count = 0

  104.        success_append_count = 0

  105.        total_modify_count = 0

  106.        success_modify_count = 0

  107.        total_truncate_count = 0

  108.        success_truncate_count = 0

  109.        total_set_meta_count = 0

  110.        success_set_meta_count = 0

  111.        total_delete_count = 0

  112.        success_delete_count = 0

  113.        total_download_count = 0

  114.        success_download_count = 0

  115.        total_get_meta_count = 0

  116.        success_get_meta_count = 0

  117.        total_create_link_count = 0

  118.        success_create_link_count = 0

  119.        total_delete_link_count = 0

  120.        success_delete_link_count = 0

  121.        total_upload_bytes = 0

  122.        success_upload_bytes = 0

  123.        total_append_bytes = 0

  124.        success_append_bytes = 0

  125.        total_modify_bytes = 0

  126.        success_modify_bytes = 0

  127.        stotal_download_bytes = 0

  128.        success_download_bytes = 0

  129.        total_sync_in_bytes = 0

  130.        success_sync_in_bytes = 0

  131.        total_sync_out_bytes = 0

  132.        success_sync_out_bytes = 0

  133.        total_file_open_count = 0

  134.        success_file_open_count = 0

  135.        total_file_read_count = 0

  136.        success_file_read_count = 0

  137.        total_file_write_count = 0

  138.        success_file_write_count = 0

  139.        last_heart_beat_time = 2017-12-03 14:46:00

  140.        last_source_update = 1970-01-01 08:00:00

  141.        last_sync_update = 1970-01-01 08:00:00

  142.        last_synced_timestamp = 1970-01-01 08:00:00

  143. #都是active通信成功

  144. ip_addr = 128.0.0.82 (node2)  ACTIVE

  145. ip_addr = 128.0.0.83 (node3)  ACTIVE

安装nginx

 
   
   
 
  1. yum -y install gcc pcre pcre-devel zlib openssl

  2. wget https://github.com/happyfish100/fastdfs-nginx-module/archive/master.zip

  3. wget -c https://nginx.org/download/nginx-1.10.1.tar.gz

  4. #配置/usr/local/src/fastdfs-nginx-module/src/目录下的config文件,把CORE_INCS和CORE_LIBS的所有路径都修改为/usr/include和/usr/lib

  5. vim /root/fastdfs-nginx-module/src/config

  6. CORE_INCS="$CORE_INCS /usr/include/fastdfs /usr/include/fastcommon/"

  7. CORE_LIBS="$CORE_LIBS -L/usr/lib -lfastcommon -lfdfsclient"

  8. make && make install

  9. /usr/local/nginx/sbin/nginx -V

  10. /usr/local/nginx/sbin/nginx

配置fastdfs模块

 
   
   
 
  1. [root@node2 ~]# cd fastdfs-nginx-module/src/

  2. [root@node2 src]# ls

  3. common.c  config            ngx_http_fastdfs_module.c

  4. common.h  mod_fastdfs.conf

  5. [root@node2 src]# cp mod_fastdfs.conf /etc/fdfs

  6. [root@node2 src]# cd /etc/fdfs/

  7. [root@node2 fdfs]# vim mod_fastdfs.conf

  8.  #保存日志目录

  9.  base_path=/data/fastdfs/storage

  10.  tracker_server=128.0.0.81:22122

  11.  #当前服务器的 group 名

  12.  group_name=group1

  13.  #文件 url 中是否有 group 名

  14.  url_have_group_name = true

  15.  #存储路径个数,需要和 store_path 个数匹配

  16.  store_path0=/data/fastdfs/storage #存储路径

  17.  store_path_count=1

  18.  group_count = 2#设置组的个数

  19.  # 从文件扩展名查找文件类型( nginx时为true)

  20.  http.need_find_content_type=true

  21.  [group1]

  22.  group_name=group1

  23.  storage_server_port=23000

  24.  store_path_count=1

  25.  store_path0=/data/fastdfs/storage

配置nginx

 
   
   
 
  1. [root@node2 ~]# cd fastdfs-5.05/

  2. [root@node2 fastdfs-5.05]# cd conf/

  3. [root@node2 conf]# cp anti-steal.jpg http.conf mime.types /etc/fdfs/

  4. [root@node2 conf]# cd /usr/local/nginx/conf

  5. [root@node2 conf]# vim nginx.conf

  6.  location /group1/M00 {

  7.            root /data/fastdfs/storage/;

  8.            ngx_fastdfs_module;

  9.      }

  10. [root@node2 conf]# mkdir /data/fastdfs/storage/data/group1

  11. [root@node2 conf]# ln -s /data/fastdfs/storage/data /data/fastdfs/storage/data/group1/M00

配置客户端

 
   
   
 
  1. [root@node2 conf]# cd /etc/fdfs

  2. [root@node2 fdfs]# cp client.conf.sample client.conf

  3. base_path=/data/fastdfs/client

  4. tracker_server=192.168.2.231:22122

  5. mkdir /data/fastdfs/client

上传

 
   
   
 
  1. [root@node2 ~]# /usr/bin/fdfs_test /etc/fdfs/client.conf upload timg.jpg

  2. This is FastDFS client test program v5.05

  3. Copyright (C) 2008, Happy Fish / YuQing

  4. FastDFS may be copied only under the terms of the GNU General

  5. Public License V3, which may be found in the FastDFS source kit.

  6. Please visit the FastDFS Home Page http://www.csource.org/

  7. for more detail.

  8. [2017-12-03 18:21:45] DEBUG - base_path=/data/fastdfs/client, connect_timeout=30, network_timeout=60, tracker_server_count=1, anti_steal_token=0, anti_steal_secret_key length=0, use_connection_pool=0, g_connection_pool_max_idle_time=3600s, use_storage_id=0, storage server id count: 0

  9. tracker_query_storage_store_list_without_group:

  10.    server 1. group_name=, ip_addr=128.0.0.82, port=23000

  11.    server 2. group_name=, ip_addr=128.0.0.83, port=23000

  12. group_name=group1, ip_addr=128.0.0.82, port=23000

  13. storage_upload_by_filename

  14. group_name=group1, remote_filename=M00/00/00/gAAAUloj0DmATy21AAMuQuM_dQs658.jpg

  15. source ip address: 128.0.0.82

  16. file timestamp=2017-12-03 18:21:45

  17. file size=208450

  18. file crc32=3812586763

  19. example file url: http://128.0.0.82/group1/M00/00/00/gAAAUloj0DmATy21AAMuQuM_dQs658.jpg

  20. storage_upload_slave_by_filename

  21. group_name=group1, remote_filename=M00/00/00/gAAAUloj0DmATy21AAMuQuM_dQs658_big.jpg

  22. source ip address: 128.0.0.82

  23. file timestamp=2017-12-03 18:21:45

  24. file size=208450

  25. file crc32=3812586763

  26. example file url: http://128.0.0.82/group1/M00/00/00/gAAAUloj0DmATy21AAMuQuM_dQs658_big.jpg


以上是关于分布式文件系统 fastdfs的主要内容,如果未能解决你的问题,请参考以下文章

FastDFS+docker建立分布式文件系统

FastDFS分布文件系统Java客户端使用

FastDFS分布文件系统Java客户端使用

Linux 下 FastDFS v5.08 分布式文件系统的安装

高可用高性能分布式文件系统FastDFS实践Java程序

Go语言实现FastDFS分布式存储系统WebAPI网关