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

1、tracker:是一个调度器,Fastdfs进程就是Fastdfs进程程序,trackers做了很多工作,replication(节点复制),deletion(文件删除),reaper(存储失败重制),monitor(健康检测)等等,这个是基于时间的eventbasesd父进程/消息总线来管理所有来治愈客户端的交互(requesting operations to be performed)包括将请求负载平衡到多个‘query workers’中,然后让Fastdfs的子进程去处理。

2、group:实际文件存放位置,存储节点是一个文件存储节点服务器,用来做删除,存放,重命名等事情,读取可用web服务来实现,任何web服务都可以。

实验环境4台服务器node1,node2,node3,node4
node1:client,tracker,store
group1:node1,node2
group2:node3,node4

1、依赖

yum -y groupinstall "Development tools"

2、包下载

[root@node4 fastdfs]# git clone https://github.com/happyfish100/libfastcommon.git
[root@node4 fastdfs]# git clone https://github.com/happyfish100/fastdfs.git

3、libfastcommon依赖

[root@node2 fastdfs]# cd libfastcommon/
[root@node2 libfastcommon]# ./make.sh
[root@node2 libfastcommon]# ./make.sh install

4、fastdfs安装

[root@node1 fastdfs]# cp -r fastdfs /www/server/source/fastdfs-5.0.12   
[root@node1 fastdfs]# ln -sv /www/server/source/fastdfs-5.0.12  /www/server/fastdfs
[root@node1 fastdfs]# cd /www/server/source/fastdfs-5.0.12
[root@node1 fastdfs-5.0.12]# ./make.sh
[root@node1 fastdfs-5.0.12]# ./make.sh install
[root@node1 fastdfs-5.0.12]# cp conf/{http.conf,mime.types,anti-steal.jpg} /etc/fdfs/

5、存储目录

[root@node1 fastdfs-5.0.12]# mkdir /www/data/fastdfs/{tracker,store,client}/{data,base} -pv

6、tracker配置文件:

[root@node1 fastdfs]# cd /etc/fdfs/
[root@node1 fdfs]# cp tracker.conf.sample tracker.conf

[root@node1 fdfs]# vim tracker.conf
base_path=/www/data/fastdfs/tracker/base    #临时数据  注意权限

#选组
# 0: round robin
# 1: specify group
# 2: load balance, select the max free space group to upload file
store_lookup=2
# which group to upload file
# when store_lookup set to 1, must set store_group to the group name
store_group=group2   只有指定组才生效

#选组中的节点
# 0: round robin (default)
# 1: the first server order by ip address
# 2: the first server order by priority (the minimal)
# Note: if use_trunk_file set to true, must set store_server to 1 or 2
store_server=0

# which path(means disk or mount point) of the storage server to upload file
# 0: round robin
# 2: load balance, select the max free space path to upload file
store_path=0

# 读
# 0: round robin (default)
# 1: the source storage server which the current file uploaded to
download_server=0

#预留百分10
reserved_storage_space = 10%

run_by_group=
run_by_user=
allow_hosts=*

7、启动trackerd

[root@node1 fdfs]# /etc/init.d/fdfs_trackerd start
Reloading systemd:                                         [  OK  ]
Starting fdfs_trackerd (via systemctl):                    [  OK  ]
[root@node1 fdfs]# ss -lntup|grep 22122
tcp    LISTEN     0      128       *:22122                 *:*                   users:(("fdfs_trackerd",pid=30355,fd=5))

8、storage配置

[root@node1 fdfs]# cp storage.conf.sample  storage.conf
[root@node1 fdfs]# vim storage.conf
base_path=/www/data/fastdfs/store/base
group_name=group1  #组号

store_path_count=1                   #存储个数
store_path0=/www/data/fastdfs/store/data    #存储位置
#store_path1=/home/yuqing/fastdfs2
tracker_server=192.168.1.201:22122

node3,node4的组id

group_name=group2

9、启动 store

[root@node1 fdfs]# /etc/init.d/fdfs_storaged start
Starting fdfs_storaged (via systemctl):                    [  OK  ]
[root@node1 fdfs]# ss -lntup|grep fs
tcp    LISTEN     0      128       *:23000                 *:*                   users:(("fdfs_storaged",pid=30850,fd=5))

文件存储目录结构
Fastdfs文件存储系统

10、客户端配置

[root@node1 fdfs]# cp client.conf.sample client.conf
[root@node1 fdfs]# vim client.conf
base_path=/www/data/fastdfs/client/base
tracker_server=192.168.1.200:22122

11、上传文件

[root@node1 ~]# fdfs_upload_file /etc/fdfs/client.conf a.jpg
group1/M00/00/00/wKgByFsvVkKANyY1AAAQhCu9IQw489.jpg
[root@node1 ~]# fdfs_upload_file /etc/fdfs/client.conf a.jpg
group2/M00/00/00/wKgBylsvVkmAFdD7AAAQhCu9IQw868.jpg

配置nginx代理

1、编译nginx

name: nginx
linkname: nginx
version: 1.14.0
download: http://nginx.org/download/nginx-{version}.tar.gz
cmds:
  - id nginx ||  useradd -r -M -s /sbin/nologin nginx
yum:
  - yum -y groupinstall "Development tools"
  - yum -y install pcre-devel openssl-devel zlib-devel

uncompress: tar xf nginx-{version}.tar.gz
uncompress_dir: nginx-{version}
compile: ./configure --prefix={app_source_path}
                              --conf-path={app_source_path}/conf/nginx.conf
                              --error-log-path=/var/log/nginx/error.log
                              --http-log-path=/var/log/nginx/access.log
                              --pid-path=/var/run/nginx.pid
                              --lock-path=/var/run/nginx.lock
                              --user=nginx
                              --group=nginx
                              --with-http_ssl_module
                              --with-http_v2_module
                              --with-http_dav_module
                              --with-http_stub_status_module
                              --with-threads
                              --with-file-aio
                              --with-http_gzip_static_module
                              --with-stream
                              --add-module=../nginx-http-auth-digest-master
                              --add-module=../fastdfs-nginx-module-master/src
                              && make && make install
depends:
  - name: nginx-http-auth-digest
    version: ''
    download: 'https://github.com/atomx/nginx-http-auth-digest/archive/master.zip -O nginx-http-auth-digest-master.zip'
    uncompress: unzip -o nginx-http-auth-digest-master.zip
    uncompress_dir: ''
    compile: ''
  - name: nginx-fastdfs
    version: ''
    download: 'https://github.com/happyfish100/fastdfs-nginx-module/archive/master.zip -O nginx-fastdfs.zip'
    uncompress: unzip -o nginx-fastdfs.zip
    uncompress_dir: ''
    compile: ''
init:
    cmds:
      - cat {app_source_path}/conf/nginx.conf|egrep 'include conf\.d/\*\.conf' || sed -i '$s#}#\tinclude conf.d/*.conf;\n}#' {app_source_path}/conf/nginx.conf
      - '[ -d {app_source_path}/conf/conf.d ] || mkdir {app_source_path}/conf/conf.d'
      - echo 'export PATH={app_link_path}/sbin:$PATH'>/etc/profile.d/nginx.sh

    systemctl:
          path: /etc/systemd/system/mynginx.service
          content:
              Unit:
                  Description: The Nginx Server
                  After: network.target remote-fs.target nss-lookup.target
              Service:
                  Type: forking
                  PIDFILE: '/var/run/nginx.pid'
                  ExecStart: '{app_link_path}/sbin/nginx'
                  ExecReload: '{app_link_path}/sbin/nginx  -s reload'
                  ExecStop: '{app_link_path}/sbin/nginx -s stop'
                  PrivateTmp: true
              Install:
                  WantedBy: multi-user.target

2、配置nginx

[root@node1 src]# cp /root/fs/fastdfs-nginx-module-master/src/mod_fastdfs.conf   /etc/fdfs/
[root@node1 fdfs]# vim mod_fastdfs.conf
base_path=/www/data/fastdfs/client/base
group_name=group1
tracker_server=192.168.1.200:22122
store_path0=/www/data/fastdfs/store/data
log_filename=/www/data/fastdfs/tracker/base/logs/mod_fastdfs.log
url_have_group_name = false  #说明在反代时候不用加group

3、日志配置

[root@node1 fdfs]# touch /www/data/fastdfs/tracker/base/logs/mod_fastdfs.log
[root@node1 fdfs]# chown -R nginx.nginx  /www/data/fastdfs/tracker/base/logs/mod_fastdfs.log

4、nginx配置

        location /M00 {
            alias /www/data/fastdfs/store/data/data/;
            ngx_fastdfs_module;
        }

Fastdfs文件存储系统

5、日志

[root@node1 fdfs]#  cat /www/data/fastdfs/tracker/base/logs/mod_fastdfs.log
[2018-06-24 17:31:47] INFO - fastdfs apache / nginx module v1.15, response_mode=proxy, base_path=/www/data/fastdfs/client/base, url_have_group_name=0, group_name=group1, storage_server_port=23000, path_count=1, store_path0=/www/data/fastdfs/store/data, connect_timeout=2, network_timeout=30, tracker_server_count=1, if_alias_prefix=, local_host_ip_count=3, anti_steal_token=0, token_ttl=0s, anti_steal_secret_key length=0, token_check_fail content_type=, token_check_fail buff length=0, load_fdfs_parameters_from_tracker=1, storage_sync_file_max_delay=86400s, use_storage_id=0, storage server id count=0, flv_support=1, flv_extension=flv