fastdfs集群安装与配置

·  阅读 377

FastDFS安装与配置

一、Why FastDFS?

单机架构:

代码与各种静态文件放在同一台主机,通过目录结构直接访问静态资源。代码与文件耦合度高。

分层架构:

使用独立图片服务器,单机扩展性差。代表软件:nfs

分布式文件系统:

扩展性强、高可用性。

缺点:需要的服务器资源多。

二、What is FastDFS?

开源、轻量、分布式文件系统。适用于中小文件(4KB ~~ 500MB)。

纯C实现,支持Linux、FreeBSD等类UNIX

三、相关概念

1、tracker server

  • 调度器
  • 负责维持集群信息,这些信息由storage node报告生成
  • 本地不需要持久存储数据
  • storage node需要周期性向tracker发送心跳信息

2、storage server

  • 实现文件上传、下载、文件同步

  • 以group为单元进行组织

  • 任何一个storage server都应该属于某个group,一个group应该包含至少两个storage server.

  • 在同一个group内部,各storage server的数据互相冗余。

  • 存储空间以最小storage server空间为准。

3、base_path 基路径

  • FastDFS基路径,不会自动创建,需事先建好。
  • $base_path/data:数据文件存储位置,包括PID、节点状态信息、数据同步文件
  • $base_path/logs:日志文件存储位置
  • $base_path/data/sync:storage server存放文件上传、删除等更改操作binglog的路径。

4、store_path 存储路径

  • 通常应对应一块硬盘或RAID盘
  • 支持多存储路径,最多256个
  • 多个store_path时,顺序不可随意调整(文件ID中包含store_path顺序号)

5、文件ID

  • 由storage server生成的访问文件的凭证
  • 包括group名称(group1)、存储路径顺序号(M00)、两级目录的文件名(00/00)、文件名
  • 文件名:由源头storage server ip 、创建时的时间戳、文件大小、文件的校验码和一个随机数进行hash计算之后生成
  • 示例:group1/M00/01/32/wKgCaFx1982ENxLWAAAAAA0e5-o982.txt

四、制作rpm包并安装

项目地址:

gitee.com/fastdfs100/…

github.com/happyfish10…

FastDFS v5.12、libfastcommon V1.0.43 及以上版本

github.com/happyfish10…

github.com/happyfish10…

tracker server:

主机名IP地址
tracker1192.168.100.10

storage server:

所属组主机名IP地址
group1storage11192.168.100.11
group1storage12192.168.100.12
group2storage21192.168.100.21
group2storage22192.168.100.22

1 安装包组

~]# yum groupinstall -y "Development Tools" -y
复制代码

2 制作libfastcommon包并安装

[root@tracker1 ~]# mkdir -pv rpmbuild/{SOURCES,SPECS}
[root@tracker1 ~]# tar xf libfastcommon-1.0.48.tar.gz 
[root@tracker1 ~]# cp libfastcommon-1.0.48/libfastcommon.spec rpmbuild/SPECS/
[root@tracker1 ~]# cd rpmbuild/SPECS/
[root@tracker1 SPECS]# rpmbuild -bb libfastcommon.spec 
# 制作成功的rpm包位置:/root/rpmbuild/RPMS/x86_64/,debug包无需安装
[root@tracker1 ~]# ls rpmbuild/RPMS/x86_64/
libfastcommon-1.0.48-1.el7.x86_64.rpm  libfastcommon-debuginfo-1.0.48-1.el7.x86_64.rpm  libfastcommon-devel-1.0.48-1.el7.x86_64.rpm
复制代码

安装libfastcommon

[root@tracker1 ~]# mkdir rpms
[root@tracker1 ~]# cp rpmbuild/RPMS/x86_64/libfastcommon-devel-1.0.48-1.el7.x86_64.rpm rpms/
[root@tracker1 ~]# cp rpmbuild/RPMS/x86_64/libfastcommon-1.0.48-1.el7.x86_64.rpm rpms/
[root@tracker1 ~]# rpm -ivh rpms/*.rpm
Preparing...                          ################################# [100%]
	package libfastcommon-1.0.48-1.el7.x86_64 is already installed
	package libfastcommon-devel-1.0.48-1.el7.x86_64 is already installed
复制代码

3 制作fastdfs安装包

[root@tracker1 ~]# wget https://github.com/happyfish100/fastdfs/archive/refs/tags/V5.12.tar.gz
[root@tracker1 ~]# cp fastdfs-5.12/fastdfs.spec rpmbuild/SPECS/
# 注意:需修改fastdfs.spec文件中FDFSVersion为5.12,原值为5.0.12,制作rpm包时会报找不到fastdfs-5.0.12目录
[root@tracker1 ~]# cd rpmbuild/SPECS/
[root@tracker1 SPECS]# rpmbuild -bb fastdfs.spec
# 注意:制作fastdfs安装包前,必须先安装libfastcommon包(libfastcommon为fastdfs依赖包)
复制代码

各包说明:

包名用途
fastdfs-server提供tracker及storage
fastdfs-5.12server及client
fastdfs-tool提供fastdfs各种工具
libfdfsclient客户端动态库
libfdfsclient-devel客户端开发包

安装fastdfs软件包:

[root@tracker1 ~]# cd /root/rpmbuild/RPMS/x86_64
[root@tracker1 x86_64]# cp fastdfs-5.12-1.el7.x86_64.rpm fastdfs-server-5.12-1.el7.x86_64.rpm fastdfs-tool-5.12-1.el7.x86_64.rpm libfdfsclient-* /root/rpms/
[root@tracker1 x86_64]# cd /root/rpms/
[root@tracker1 rpms]# rpm -ivh fastdfs-* libfdfs*
Preparing...                          ################################# [100%]
	package libfdfsclient-5.12-1.el7.x86_64 is already installed
	package libfdfsclient-devel-5.12-1.el7.x86_64 is already installed
	package fastdfs-tool-5.12-1.el7.x86_64 is already installed
	package fastdfs-server-5.12-1.el7.x86_64 is already installed
	package fastdfs-5.12-1.el7.x86_64 is already installed
复制代码

五、配置tracker并启动

提供配置文件:

~]# cp /etc/fdfs/tracker.conf.sample /etc/fdfs/tracker.conf
复制代码

编辑配置文件,修改以下项:

base_path=/data/tracker/fastdfs
复制代码

创建文件:

~]# mkdir -pv /data/tracker/fastdfs
复制代码

启动服务:

[root@tracker1 ~]# /etc/init.d/fdfs_trackerd start
复制代码

查看服务:

[root@tracker1 ~]# /etc/init.d/fdfs_trackerd status
● fdfs_trackerd.service - LSB: FastDFS tracker server
   Loaded: loaded (/etc/rc.d/init.d/fdfs_trackerd; bad; vendor preset: disabled)
   Active: active (running) since Sat 2021-08-14 02:07:44 CST; 4min 27s ago
     Docs: man:systemd-sysv-generator(8)
  Process: 22916 ExecStop=/etc/rc.d/init.d/fdfs_trackerd stop (code=exited, status=0/SUCCESS)
  Process: 22933 ExecStart=/etc/rc.d/init.d/fdfs_trackerd start (code=exited, status=0/SUCCESS)
   CGroup: /system.slice/fdfs_trackerd.service
           └─22938 /usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf

Aug 14 02:07:44 tracker1 systemd[1]: Starting LSB: FastDFS tracker server...
Aug 14 02:07:44 tracker1 fdfs_trackerd[22933]: Starting FastDFS tracker server:
Aug 14 02:07:44 tracker1 systemd[1]: Started LSB: FastDFS tracker server.
复制代码

六、配置storage

storage11

group_name=group1
base_path=/data/fastdfs
store_path0=/data/fastdfs/fdfs
tracker_server=192.168.100.10:22122
复制代码

storage12

group_name=group1
base_path=/data/fastdfs
store_path0=/data/fastdfs/fdfs
tracker_server=192.168.100.10:22122
复制代码

storage21

group_name=group2
base_path=/data/fastdfs
store_path0=/data/fastdfs/fdfs
tracker_server=192.168.100.10:22122
复制代码

storage22

group_name=group2
base_path=/data/fastdfs
store_path0=/data/fastdfs/fdfs
tracker_server=192.168.100.10:22122
复制代码

storage节点创建目录:

~]# mkdir -pv /data/fastdfs/fdfs
复制代码

启动服务:

~]# /etc/init.d/fdfs_storaged start
复制代码

七、测试

测试工具的使用需配置/etc/fdfs/client.conf

base_path=/data/fastdfs/client
tracker_server=192.168.100.10:22122
复制代码

1、查看集群状态

命令:fdfs_monitor

Usage:fdfs_monitor <config_file> [-h <tracker_server>] [list|delete|set_trunk_server <group_name> [storage_id]]

示例:

# 查看集群整体状态
~]# fdfs_monitor /etc/fdfs/client.conf list group1

# 查看group1状态
~]# fdfs_monitor /etc/fdfs/client.conf list group1
复制代码

2、上传、下载文件测试

命令:fdfs_upload_file

Usage: fdfs_upload_file <config_file> <local_filename> [storage_ip:port] [store_path_index]

# 上传文件,上传后storage server会返回文件的FID
[root@tracker1 ~]# fdfs_upload_file /etc/fdfs/client.conf fastdfs-5.12/conf/anti-steal.jpg 
group1/M00/00/00/wKhkC2EXhDaAWXVIAABdreSfEnY603.jpg
复制代码

命令:fdfs_download_file

Usage: fdfs_download_file <config_file> <file_id> [local_filename] [<download_offset> <download_bytes>]

[root@tracker1 ~]# fdfs_download_file /etc/fdfs/client.conf group1/M00/00/00/wKhkC2EXhDaAWXVIAABdreSfEnY603.jpg
复制代码

查看是否是一个文件:

[root@tracker1 ~]# md5sum wKhkC2EXhDaAWXVIAABdreSfEnY603.jpg 
2a5af127e0e1188e73012cd5cc69d0f4  wKhkC2EXhDaAWXVIAABdreSfEnY603.jpg
[root@tracker1 ~]# md5sum fastdfs-5.12/conf/anti-steal.jpg 
2a5af127e0e1188e73012cd5cc69d0f4  fastdfs-5.12/conf/anti-steal.jpg
复制代码

八、通过nginx访问

fastdsf-nginx-module 1.20

nginx 1.18.0

主机上需已安装FastDFS storage server和client libray.

FastDFS 版本需>=5.11

1、编译安装nginx

~]# wget http://nginx.org/download/nginx-1.18.0.tar.gz
~]# tar xf nginx-1.18.0.tar.gz
~]# yum -y install gcc redhat-rpm-config perl-devel perl-ExtUtils-Embed gd-devel libxml2-devel libxslt-devel openssl-devel openssl pcre pcre-devel gperftools

[root@storage11 nginx-1.18.0]# ./configure --prefix=/usr/share/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/var/lib/nginx/tmp/client_body --http-proxy-temp-path=/var/lib/nginx/tmp/proxy --http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi --http-uwsgi-temp-path=/var/lib/nginx/tmp/uwsgi --http-scgi-temp-path=/var/lib/nginx/tmp/scgi --pid-path=/run/nginx.pid --lock-path=/run/lock/subsys/nginx --user=nginx --group=nginx --with-compat --with-debug --with-file-aio --with-google_perftools_module --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_degradation_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_image_filter_module=dynamic --with-http_mp4_module --with-http_perl_module=dynamic --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-http_xslt_module=dynamic --with-mail=dynamic --with-mail_ssl_module --with-pcre --with-pcre-jit --with-stream=dynamic --with-stream_ssl_module --with-stream_ssl_preread_module --with-threads --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -m64 -mtune=generic' --with-ld-opt='-Wl,-z,relro -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -Wl,-E' --add-module=/root/fastdfs-nginx-module-1.20/src
[root@storage11 nginx-1.18.0]# make -j 4 && make install
复制代码

2、配置

创建nginx用户

~]# useradd -r nginx
复制代码

创建所需目录并授权给nginx用户

~]# mkdir -pv /var/lib/nginx/tmp/
~]# chown -R nginx.nginx /var/lib/nginx
复制代码

nginx.conf添加以下配置:

location /M00 {
    root /data/fastdfs/fdfs/data;
    ngx_fastdfs_module;
}
复制代码

/etc/fdfs/mod_fastdfs.conf修改:

base_path=/data/nginx/
tracker_server=192.168.100.10:22122
store_path0=/data/fastdfs/fdfs		# 与当前节点storage.conf中store_path0保持一致
复制代码

提供http.conf mime.type文件(fastdfs源码包conf目录下)

~]# scp http.conf mime.types root@storage11:/etc/fdfs/
复制代码

启动nginx

~]# nginx -c /etc/nginx/nginx.conf
复制代码

访问图片:

http://192.168.100.11/M00/00/00/wKhkC2EXhDaAWXVIAABdreSfEnY603.jpg

分类:
开发工具
标签:
分类:
开发工具
标签:
收藏成功!
已添加到「」, 点击更改