主要内容
什么是FastDFS
全称fast distributed file system,由国人开发,它功能强大,且简单易用。
FastDFS包括两个核心组件:tracker和storage:
- tracker 类似一个调度器,它并不用于文件的存储,作用是把文件分发到不同的storage储存服务器
- storage 是最终的文件存储服务器
tracker和storage都可以由一台或者多台服务器组成。storage服务器中的所有文件名称都会进行hash操作,因此,我们无法查看原始的文件名称。
tracker和storage的关系如下图:
安装FastDFS
系统平台是centos7,3台主机,一台部署tracker,两台部署storage
- centos7-server (192.168.11.3) 用于部署tracker
- centos7-host1 (192.168.11.4) 用于部署storage
- centos7-host2 (192.168.11.5) 用于部署storage
注意:关闭selinux和防火墙,避免tracker和storage之间无法通讯
下面的所有步骤都必须在3台主机中执行
准备编译环境
由于FastDFS不提供二进制安装包,因此只能通过编译源码的方式安装
1 2 |
yum install git gcc gcc-c++ make automake autoconf libtool pcre pcre-devel zlib zlib-devel openssl-devel -y |
创建数据保存目录,这些目录用于保存tracker或storage的状态信息文件,例如日志等。
1 2 3 |
mkdir -p /home/fastdfs/tracker mkdir -p /home/fastdfs/storage |
另外创建一个用于保存FastDFS源码的目录(也可以使用现有的目录)
1 2 |
mkdir -p /home/fastdfs/src |
安装依赖包
FastDFS依赖于libfatscommon,这个包也是作者自己开发的,安装FastDFS之前先把这个依赖装上
1 2 3 4 5 |
cd /home/fastdfs/src git clone https://github.com/happyfish100/libfastcommon.git --depth 1 cd libfastcommon/ ./make.sh && ./make.sh install |
如果没有错误,表示安装成功
安装FastDFS
1 2 3 4 |
cd /home/fastdfs/src cd fastdfs/ ./make.sh && ./make.sh install |
安装过程也非常快,没有出现错误,安装成功之后,在/usr/bin/
目录下面有一些fdfs开头的二进制文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
[root@centos7-server ~]# ls -l /usr/bin/fdfs* -rwxr-xr-x 1 root root 317536 Sep 6 16:43 /usr/bin/fdfs_appender_test -rwxr-xr-x 1 root root 317312 Sep 6 16:43 /usr/bin/fdfs_appender_test1 -rwxr-xr-x 1 root root 304160 Sep 6 16:43 /usr/bin/fdfs_append_file -rwxr-xr-x 1 root root 303872 Sep 6 16:43 /usr/bin/fdfs_crc32 -rwxr-xr-x 1 root root 304200 Sep 6 16:43 /usr/bin/fdfs_delete_file -rwxr-xr-x 1 root root 304960 Sep 6 16:43 /usr/bin/fdfs_download_file -rwxr-xr-x 1 root root 304552 Sep 6 16:43 /usr/bin/fdfs_file_info -rwxr-xr-x 1 root root 322456 Sep 6 16:43 /usr/bin/fdfs_monitor -rwxr-xr-x 1 root root 1111688 Sep 6 16:43 /usr/bin/fdfs_storaged -rwxr-xr-x 1 root root 327440 Sep 6 16:43 /usr/bin/fdfs_test -rwxr-xr-x 1 root root 326648 Sep 6 16:43 /usr/bin/fdfs_test1 -rwxr-xr-x 1 root root 454728 Sep 6 16:43 /usr/bin/fdfs_trackerd -rwxr-xr-x 1 root root 305152 Sep 6 16:43 /usr/bin/fdfs_upload_appender -rwxr-xr-x 1 root root 306176 Sep 6 16:43 /usr/bin/fdfs_upload_file |
部署FastDFS
第一步:部署tracker,fastdfs安装好后在/etc/fdfs/目录下已经有一些配置示例,把对应的文件重命名
1 2 3 4 5 |
[root@centos7-server ~]# cd /etc/fdfs [root@centos7-server fdfs]# ls client.conf.sample storage.conf.sample storage_ids.conf.sample tracker.conf.sample [root@centos7-server fdfs]# cp tracker.conf.sample tracker.conf |
第二步:编辑配置文件
需要修改的地方不多,大部分保持默认即可
1 2 3 4 5 6 |
# vim tracker.conf # the base path to store data and log files port=22122 #保持默认即可 base_path=/home/fastdfs/tracker |
修改完毕启动fdfs_trackerd
服务
1 2 |
/etc/init.d/fdfs_trackerd start |
第三步:部署storage
(两台storage服务器的配置一样)先准备storage的配置文件
1 2 3 |
cd /etc/fdfs/ cp storage.conf.sample storage.conf |
编辑storage.conf,同样大部分保持默认
1 2 3 4 5 6 |
group_name=group1 #分组名称 port=23000 base_path=/home/fastdfs/storage #tracer服务器的地址,一定要填 tracker_server=192.168.11.3:22122 |
之后启动fdfs_storaged
服务
1 2 |
/etc/init.d/fdfs_storaged start |
测试文件上传
使用fdfs_upload_file
测试文件上传功能,需要先配置好client设置
1 2 3 4 5 6 7 8 9 10 |
# client的数据储存路径 mkdir -p /home/fastdfs/client cd /etc/fdfs/ cp client.conf.sample client.conf # vim client.conf base_path=/home/fastdfs/client #tracker服务器的地址 tracker_server=192.168.11.3:22122 |
配置好后准备测试
1 2 3 4 5 6 7 8 9 |
[root@centos7-server ~]# pwd /root [root@centos7-server ~]# dd if=/dev/zero of=./test.img bs=100M count=2 2+0 records in 2+0 records out 209715200 bytes (210 MB) copied, 4.05501 s, 51.7 MB/s [root@centos7-server ~]# fdfs_upload_file /etc/fdfs/client.conf ./test.img group2/M00/00/00/wKgLBVuQ8X6Ab2sYDIAAAFNPCGE705.img #返回了文件的ID,证明上传成功 |
FastDFS工作模式
FastDFS根据不同的组名称来保存文件,相同的组下所有的服务器都保存相同的文件,相同组中的主机相当于GlusterFS中的Replicated Volume,不同组下的主机相当于GlusterFS的Distributed Volume。
横向扩展
当储存空间不足,只需要添加新主机并创建新的组,即可完成空间的扩容,相对来说还是非常简单的。
转载请注明:Pure nonsense » 分布式文件系统FastDFS