1. FastDFS介绍
FastDFS是一个开源的分布式文件系统,它对文件进行管理。功能包括:文件存储、文件同步、文件访问(文件上传、文件下载)等,解决了大容量存储和负载均衡的问题。特别适合以文件为载体的在线服务,如相册网站、视频网站等。
FastDFS服务端有两个角色:跟踪器(tracker)和存储节点(storage)。跟踪器主要做调度工作,在访问上起负载均衡的作用。存储节点存储文件,完成文件管理的功能:存储、同步和提供存取接口,FastDFS同时对文件的meta data进行管理。所谓文件的meta data就是文件的相关属性,以键值对(key value pair)方式表示,如:width=1024,其中的key为width,value为1024.文件的metadata是文件属性列表,可以包含多个键值对。FastDFS系统结构如下图所示:
跟踪器和存储节点都可以由一台或多台服务器构成。跟踪器和存储节点中的服务器均可以随时增加或下线而不会影响线上服务。其中跟踪器中的所有服务器都是对等的,可以根据服务器的压力情况随时增加或减少。
为了支持大容量,存储节点(服务器)采用了分卷(或分组)的组织方式。存储系统由一个或多个卷组成,卷与卷之间的文件是相互独立的,所有卷的文件容量累加就是整个存储系统中的文件容量。一个卷可以由一台或多台存储服务器组成,一个卷下的存储服务器中的文件都是相同的,卷中的多台存储服务器起到了冗余备份和负载均衡的作用。在卷中增加服务器时,同步已有的文件由系统自动完成,同步完成后,系统自动将新增服务器切换到线上提供服务。当存储空间不足或即将耗尽时,可以动态添加卷。只需要增加一台或多台服务器,并将它们配置为一个新的卷,这样就扩大了存储系统的容量。FastDFS中的文件标识分为两部分:卷名和文件名,二者缺一不可。
2. FastDFS文件上传交互过程
1. Client询问tracker上传到storage,不需要附加参数。
2. tracker返回一台可用的storage。
3. client直接和storage通宵完成文件上传
客户端client发起对FastDFS的文件传输动作,是通过连接到某一台Tracker Server的指定端口来实现的,Tracker Server根据目前已掌握的信息,来决定选择那一台Storage Service,然后将这个Storage Server的地址等信息返回给client,然后client再通过这些信息连接到这台Storage Server,将要上传的文件传送到Storage Server上。
3. FastDFS文件下载交互过程
1. client询问tracker下载文件的storage,参数为文件标识(卷名和文件名)。
2. tracker返回一台可用的storage。
3. client直接和storage通讯完成文件下载。
4. FastDFS主从搭建
4.1 服务器及文件准备
跟踪服务器1 : 192.168.2.222 master
跟踪服务器2 : 192.168.2.223 slave
存储服务器1 : 192.168.2.222 master
存储服务器2 : 192.168.2.223 slave
环境:CentOS 6.8
用户:root
所需文件:见附件
fastdfs-5.05.tar.gz : FastDFS安装主文件包
libfastcommon-master.zip: FastDFS文件系统依赖包
nginx-1.8.1.tar.gz:nginx安装包
fastdfs-nginx-module_v1.16.tar.gz: nginx下fastdfs模块包
4.2 创建用户组及用户
创建用户所属组
# groupadd dfs
# grep dfs /etc/group
dfs:x:500:
创建fastdfs用户
# useradd -g 500 fastdfs
# id fastdfs
uid=500(fastdfs) gid=500(dfs) 组=500(dfs)
指定用户密码
# echo "123456" | passwd --stdin fastdfs
更改用户 fastdfs 的密码 。
passwd: 所有的身份验证令牌已经成功更新。
# cd /home/
# ls
fastdfs
4.3 上传所有安装包到服务器
# pwd
/home/fastdfs
# tree
.
├── fastdfs-5.05.tar.gz
├──fastdfs-nginx-module_v1.16.tar.gz
├── libfastcommon-master.zip
└── nginx-1.8.1.tar.gz
0 directories, 4 files
[[email protected]]#
4.4 创建文件夹
# cd /home/fastdfs/
创建nginx安装文件夹
# mkdir /usr/local/nginx
创建tracker相关文件夹
# mkdir /home/fastdfs/tracker
创建storage相关文件夹
# mkdir /home/fastdfs/storage
创建storage数据存储文件夹
# mkdir /home/fastdfs/storage_data
4.5 安装依赖包
需要配置网络yum源
[[email protected]]# yum -y install gcc gcc-c++ pcre pcre-develzlib zlib-devel openssl openssl-devel
4.6 安装libfastcommon-master
解压
# unzip libfastcommon-master.zip
# cd libfastcommon-master
# ls
HISTORY INSTALL libfastcommon.spec make.sh php-fastcommon README src
编译
# ./make.sh
安装
默然安装到了
/usr/lib64/libfastcommon.so
# ./make.sh install
配置软连接
因为FastDFS主程序设置的lib目录是/usr/local/lib,所以需要创建软连接。
# ln -s /usr/lib64/libfastcommon.so/usr/local/lib/libfastcommon.so
# ln -s /usr/lib64/libfastcommon.so /usr/lib/libfastcommon.so
ln: 创建符号链接 "/usr/lib/libfastcommon.so": 文件已存在
# ln -s /usr/lib64/libfdfsclient.so/usr/local/lib/libfdfsclient.so
# ln -s /usr/lib64/libfdfsclient.so /usr/lib/libfdfsclient.so
4.7 安装fastdfs-5.05
解压
# tar zxvf fastdfs-5.05.tar.gz
# cd fastdfs-5.05
编译(编译前确保已经安装了libfastcommon-master)
# ./make.sh
安装
# ./make.sh install
采用默然安装的方式安装,安装后的相应文件和目录:
A、安装后的配置文件(样例配置文件)
# ls /etc/fdfs/
client.conf.sample # 客户端配置文件
storage.conf.sample # 存储节点配置文件
tracker.conf.sample #跟踪节点配置文件
B、安装后的脚本
# ll /etc/init.d/fdfs*
/etc/init.d/fdfs_storaged # 存储节点脚本文件
/etc/init.d/fdfs_trackerd # 跟踪节点脚本文件
C、命令工具在/usr/bin/目录下的:
/usr/bin/fdfs_appender_test
/usr/bin/fdfs_delete_file
/usr/bin/fdfs_storaged # storage启动命令
/usr/bin/fdfs_upload_appender
/usr/bin/fdfs_appender_test1
/usr/bin/fdfs_download_file
/usr/in/fdfs_test
/usr/bin/fdfs_upload_file # 客户端上传文件命令
/usr/bin/fdfs_append_file
/usr/bin/fdfs_file_info
/usr/in/fdfs_test1
/usr/bin/fdfs_crc32
/usr/bin/fdfs_monitor # fdfs集群监控命令
/usr/bin/fdfs_trackerd # tracker启动命令
修改脚本
因为FastDFS服务脚本设置的bin目录是/usr/local/bin,但实际命令安装在/usr/bin。因此需要修改FastDFS服务脚本中相应的命令路径,也就是把/etc/init.d/fdfs_storaged和/etc/init.d/fdfs_trackerd两个脚本中的/usr/local/bin修改成/usr/bin
# vim /etc/init.d/fdfs_trackerd
使用查找替换命令统一修改 :%s+/usr/local/bin+/usr/bin
# vim /etc/init.d/fdfs_storaged
使用查找替换命令统一修改 :%s+/usr/local/bin+/usr/bin
4.8 配置FastDFS跟踪器Tracker
# cd /etc/fdfs/
# cp tracker.conf.sample tracker.conf
# vim tracker.conf
修改以下内容
disabled=false #启动配置文件
port=22122 #tracker的端口号,一般采用22122这个默认的端口
base_path=/home/fastdfs/tracker #tracker的数据文件和日志目录
启动tracker
# /etc/init.d/fdfs_trackerd start
查看tracker是否启动成功
(1)查看22122端口监听情况
# netstat -antpu |grep fdfs
tcp 0 0 0.0.0.0:22122 0.0.0.0:* LISTEN 51641/fdfs_trackerd
(2)通过查看tracker启动日志
# tail -100f /home/fastdfs/tracker/logs/trackerd.log
防火墙中打开跟踪器Tracker端口
# iptables -I INPUT -p tcp --dport 22122 -j ACCEPT
保存防火墙规则
# /etc/init.d/iptables save
关闭tracker
# /etc/init.d/fdfs_trackerd stop
设置FastDFS跟踪器开机启动
# echo ‘/etc/init.d/fdfs_trackerd start‘ >>/etc/rc.d/rc.local
4.9 配置FastDFS存储节点Storage
# cd /etc/fdfs/
# cp storage.conf.sample storage.conf
# vim storage.conf
修改以下内容
disabled=false # 启动配置文件
group_name=group1 # 组名
port=23000 # storage的端口号
base_path=/home/fastdfs/storage # 设置storage的日志路径
store_path0=/home/fastdfs/storage_data # 存储路径
store_path_count=1 #存储路径的个数,需要和storage_path个数相同
tracker_server=192.168.2.222:22122 # tracker服务器的IP地址和端口
tracker_server=192.168.2.223:22122 # 多个tracker直接添加多条配置
启动storage
# /etc/init.d/fdfs_storaged start
查看storage是否启动成功
(1) 查看监听端口
# netstat -antpu | grepfdfs
tcp 0 00.0.0.0:22122 0.0.0.0:* LISTEN 51641/fdfs_trackerd
tcp 0 00.0.0.0:23000 0.0.0.0:* LISTEN 51798/fdfs_storaged
tcp 0 0192.168.2.222:22122 192.168.2.222:54181 ESTABLISHED 51641/fdfs_trackerd
tcp 0 0 192.168.2.222:54181 192.168.2.222:22122 ESTABLISHED 51798/fdfs_storaged
(2)查看日志,初次启动成功会在/home/fastdfs/storage /下创建data和log目录
# tail-100f /home/fastdfs/storage/logs/storaged.log
防火墙中打开存储节点端口
# iptables -I INPUT -p tcp --dport 23000 -j ACCEPT
保存防火墙规则
# /etc/init.d/iptables save
关闭storage
# /etc/init.d/fdfs_storaged stop
设置FastDFS存储节点开机启动
# echo ‘/etc/init.d/fdfs_storaged start‘ >>/etc/rc.d/rc.local
查看FastDFS监控集群信息
# /usr/bin/fdfs_monitor /etc/fdfs/storage.conf
[2016-08-01 12:49:39] DEBUG -base_path=/home/fastdfs/storage, connect_timeout=30, network_timeout=60,tracker_server_count=2, 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 server_count=2, server_index=1 tracker server is192.168.2.223:22122 group count: 1 Group 1: group name = group1 disk total space = 9951 MB disk free space = 4890 MB trunk free space = 0 MB storage server count = 2 active server count = 0 storage server port = 23000 storage HTTP port = 8888 store path count = 1 subdir count per path = 256 current write server index = 0 current trunk file id = 0 Storage 1: id = 192.168.2.222 ip_addr = 192.168.2.222 WAIT_SYNC http domain = version = 5.05 join time = 2016-08-01 12:11:49 up time = 2016-08-01 12:11:49 total storage = 9951 MB free storage = 4890 MB upload priority = 10 store_path_count = 1 subdir_count_per_path = 256 storage_port = 23000 storage_http_port = 8888 current_write_path = 0 source storage id =192.168.2.223 if_trunk_server = 0 connection.alloc_count = 256 connection.current_count = 0 connection.max_count = 0 total_upload_count = 0 success_upload_count = 0 total_append_count = 0 success_append_count = 0 total_modify_count = 0 success_modify_count = 0 total_truncate_count = 0 success_truncate_count = 0 total_set_meta_count = 0 success_set_meta_count = 0 total_delete_count = 0 success_delete_count = 0 total_download_count = 0 success_download_count = 0 total_get_meta_count = 0 success_get_meta_count = 0 total_create_link_count = 0 success_create_link_count = 0 total_delete_link_count = 0 success_delete_link_count = 0 total_upload_bytes = 0 success_upload_bytes = 0 total_append_bytes = 0 success_append_bytes = 0 total_modify_bytes = 0 success_modify_bytes = 0 stotal_download_bytes = 0 success_download_bytes = 0 total_sync_in_bytes = 0 success_sync_in_bytes = 0 total_sync_out_bytes = 0 success_sync_out_bytes = 0 total_file_open_count = 0 success_file_open_count = 0 total_file_read_count = 0 success_file_read_count = 0 total_file_write_count = 0 success_file_write_count = 0 last_heart_beat_time =2016-08-01 12:49:28 last_source_update = 1970-01-0108:00:00 last_sync_update = 1970-01-0108:00:00 last_synced_timestamp = 1970-01-0108:00:00 Storage 2: id = 192.168.2.223 ip_addr = 192.168.2.223 OFFLINE http domain = version = 5.05 join time = 2016-08-01 12:12:06 up time = total storage = 9951 MB free storage = 4889 MB upload priority = 10 store_path_count = 1 subdir_count_per_path = 256 storage_port = 23000 storage_http_port = 8888 current_write_path = 0 source storage id = if_trunk_server = 0 connection.alloc_count = 0 connection.current_count = 0 connection.max_count = 0 total_upload_count = 0 success_upload_count = 0 total_append_count = 0 success_append_count = 0 total_modify_count = 0 success_modify_count = 0 total_truncate_count = 0 success_truncate_count = 0 total_set_meta_count = 0 success_set_meta_count = 0 total_delete_count = 0 success_delete_count = 0 total_download_count = 0 success_download_count = 0 total_get_meta_count = 0 success_get_meta_count = 0 total_create_link_count = 0 success_create_link_count = 0 total_delete_link_count = 0 success_delete_link_count = 0 total_upload_bytes = 0 success_upload_bytes = 0 total_append_bytes = 0 success_append_bytes = 0 total_modify_bytes = 0 success_modify_bytes = 0 stotal_download_bytes = 0 success_download_bytes = 0 total_sync_in_bytes = 0 success_sync_in_bytes = 0 total_sync_out_bytes = 0 success_sync_out_bytes = 0 total_file_open_count = 0 success_file_open_count = 0 total_file_read_count = 0 success_file_read_count = 0 total_file_write_count = 0 success_file_write_count = 0 last_heart_beat_time =2016-08-01 12:49:18 last_source_update = 1970-01-0108:00:00 last_sync_update = 1970-01-0108:00:00 last_synced_timestamp = 1970-01-01 08:00:00 |
4.10 文件上传测试
修改Tracker服务器中的客户端配置文件
# cd /etc/fdfs/
# cp client.conf.sample client.conf
# vim /etc/fdfs/client.conf
修改以下内容
base_path=/home/fastdfs/tracker #tracker日志存储路径
tracker_server=192.168.2.222:22122 #tracker服务器的IP地址和端口
tracker_server=192.168.2.223:22122 # 多个tracker直接添加多条配置
测试文件上传,返回下班文件ID,说明文件上传成功
# /usr/bin/fdfs_upload_file /etc/fdfs/client.conf/home/fastdfs/fastdfs-5.05.tar.gz
group1/M00/00/00/wKgC3lee2O6AYhpdAAUggSnIHZU.tar.gz
5. 在存储节点上安装nginx
5.1 安装fastdfs-nginx-module
解压
# tar zxvf fastdfs-nginx-module_v1.16.tar.gz
修改配置文件
# vim /home/fastdfs/fastdfs-nginx-module/src/config
修改内容(这个路径修改很重要,不然在nginx编译时会报错)
CORE_INCS="$CORE_INCS/usr/local/include/fastdfs /usr/local/include/fastcommon/" 修改为:
CORE_INCS="$CORE_INCS /usr/include/fastdfs/usr/include/fastcommon/"
5.2 安装nginx
解压
# tar zxvf nginx-1.8.1.tar.gz
# cd nginx-1.8.1
配置安装信息
# ./configure --prefix=/usr/local/nginx/--add-module=/home/fastdfs/fastdfs-nginx-module/src/
编译安装
# make && make install
复制配置文件
复制fastdfs-nginx-module源码中的配置文件到/etc/fdfs目录
# cp /home/fastdfs/fastdfs-nginx-module/src/mod_fastdfs.conf/etc/fdfs/
复制FastDFS的部分配置文件到/etc/fdfs目录
# cp /home/fastdfs/fastdfs-5.05/conf/mime.types /etc/fdfs/
# cp /home/fastdfs/fastdfs-5.05/conf/http.conf /etc/fdfs/
修改mod_fastdfs.conf
# vim /etc/fdfs/mod_fastdfs.conf
修改内容
connect_timeout=10
base_path=/home/fastdfs/storage
tracker_server=192.168.2.222:22122
tracker_server=192.168.2.223:22122
storage_server_port=23000
group_name=group1
url_have_group_name = true
store_path_count=1
store_path0=/home/fastdfs/storage_data
group_count = 1
[group1]
group_name=group1
storage_server_port=23000
store_path_count=1
store_path0=/home/fastdfs/storage_data
修改nginx配置文件
# vim /usr/local/nginx/conf/nginx.conf
修改内容
user root; # nginx访问用户
listen 80; # nginx监听端口
location ~/group([0-9])/M00 {
root/home/fastdfs/storage_data/data;
ngx_fastdfs_module;
}
防火墙放开80端口
# iptables -I INPUT -p tcp --dport 80 -j ACCEPT
保存防火墙规则链,重启电脑后设置的规则继续生效
# /etc/init.d/iptables save
启动nginx
# /usr/local/nginx/sbin/nginx
nginx 重启命令
# /usr/local/nginx/sbin/nginx -s reload
设置nginx开机启动
# echo /usr/local/nginx/sbin/nginx >> /etc/rc.d/rc.local