FastDFS介绍
fastdfs是一个开源的,高性能的的分布式文件系统,他主要的功能包括:文件存储,同步和访问,设计基于高可用和负载均衡,fastfd非常适用于基于文件服务的站点,例如图片分享和视频分享网站。
fastfds有两个角色:跟踪服务(tracker)和存储服务(storage),跟踪服务控制,调度文件以负载均衡的方式访问;存储服务包括:文件存储,文件同步,提供文件访问接口,同时以key value的方式管理文件的元数据。
跟踪和存储服务可以由1台或者多台服务器组成,同时可以动态的添加,删除跟踪和存储服务而不会对在线的服务产生影响,在集群中,tracker服 务是对等的。 存储系统由一个或多个卷(group)组成,卷与卷之间的文件是相互独立的,所有卷的文件容量累加就是整个存储系统中的文件容量。一个卷可以由一台或多台 存储服务器组成,一个卷下的存储服务器中的文件都是相同的,卷中的多台存储服务器起到了冗余备份和负载均衡的作用。在卷中增加服务器时,同步已有的文件由 系统自动完成,同步完成后,系统自动将新增服务器切换到线上提供服务。当存储空间不足或即将耗尽时,可以动态添加卷。只需要增加一台或多台服务器,并将它 们配置为一个新的卷,这样就扩大了存储系统的容量。
FastDFS文件上传及下载过程
文件上传过程
1. Client询问Tracker server上传到的Storage server;
2. Tracker server返回一台可用的Storage server,返回的数据为该Storage server的IP地址和端口;
3. Client直接和该Storage server建立连接,进行文件上传,Storage server返回新生成的文件ID,文件上传结束。
文件下载过程
1. Client询问Tracker server可以下载指定文件的Storage server,参数为文件ID(包含组名和文件名);
2. Tracker server返回一台可用的Storage server;
3. Client直接和该Storage server建立连接,完成文件下载。
基础环境
VMware虚拟机 中 CentOS 6.5 32位 最小化安装
# yum install gcc-c gcc-c++ perl
下载FastDFS
FastDFS5.03下载地址:http://jaist.dl.sourceforge.net/project/fastdfs/FastDFS Server Source Code/FastDFS Server with PHP Extension Source Code V5.03
安装FastDFS
# tar zxvf FastDFS_v5.03.tar.gz # mv FastDFS /usr/local/ # cd /usr/local/FastDFS/ # vi make.sh 修改如下: TARGET_PREFIX=/usr/local/FastDFS TARGET_CONF_PATH=/etc/fdfs WITH_LINUX_SERVICE=1 # ./make.sh # ./make.sh install
本台服务器作为TrackerA来配置
配置TrackerA
本机IP:192.168.3.75
# cd /etc/fdfs/ # ls client.conf http.conf mime.types storage.conf tracker.conf # vi tracker.conf bind_addr=192.168.3.75 base_path=/home/data/fastdfs # mkdir -p /home/data/fastdfs 启动Tracker进程: # /usr/local/FastDFS/tracker/fdfs_trackerd /etc/fdfs/tracker.conf 查看Tracker进程: # netstat -ntpl|grep fdfs tcp 0 0 192.168.3.75:22122 0.0.0.0:* LISTEN 1668/fdfs_trackerd
开启防火墙端口:
# vi /etc/sysconfig/iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 22122 -j ACCEPT # service iptables restart
配置TrackerA上的nginx + ngx_fastdfs_module
# tar zxvf fastdfs-nginx-module_v1.16.tar.gz # mv fastdfs-nginx-module /usr/local/ # cp /usr/local/fastdfs-nginx-module/src/mod_fastdfs.conf /etc/fdfs/ 配置mod_fastdfs.conf: # vi /etc/fdfs/mod_fastdfs.conf tracker_server=192.168.3.75:22122 tracker_server=192.168.3.74:22122 url_have_group_name = true group_count = 2 [group1] group_name=group1 storage_server_port=23000 store_path_count=1 store_path0=/home/data/fastdfs [group2] group_name=group2 storage_server_port=23000 store_path_count=1 store_path0=/home/data/fastdfs 安装nginx: # vi /usr/local/fastdfs-nginx-module/src/config ngx_addon_name=ngx_http_fastdfs_module HTTP_MODULES="$HTTP_MODULES ngx_http_fastdfs_module" NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/ngx_http_fastdfs_module.c" CORE_INCS="$CORE_INCS /usr/local/FastDFS/include/fastdfs /usr/local/FastDFS/include/fastcommon/" CORE_LIBS="$CORE_LIBS -L/usr/local/FastDFS/lib -lfastcommon -lfdfsclient" CFLAGS="$CFLAGS -D_FILE_OFFSET_BITS=64 -DFDFS_OUTPUT_CHUNK_SIZE='256*1024' -DFDFS_MOD_CONF_FILENAME='\"/etc/fdfs/mod_fastdfs.conf\"'" # yum install pcre-devel zlib-devel # tar zxvf nginx-1.6.1.tar.gz # ./configure --prefix=/usr/local/nginx --sbin-path=/usr/local/nginx --add-module=/usr/local/fastdfs-nginx-module/src/ # make # make install 修改nginx配置: # vi /usr/local/nginx/conf/nginx.conf listen 8080; location ~ /group([0-9])/M00 { ngx_fastdfs_module; } # ln -s /usr/local/FastDFS/lib/libfdfsclient.so /usr/lib/libfdfsclient.so # ln -s /usr/local/FastDFS/lib/libfastcommon.so /usr/lib/libfastcommon.so # vi /etc/sysconfig/iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 8080 -j ACCEPT # service iptables restart
至此完成了FastDSF TrackerA服务器的配置 将虚拟机再复制三份 分别为TrackerB(192.168.3.74)、StorageA(192.168.3.77)、StorageB(192.168.3.76)
配置TrackerB
# cd /etc/fdfs/ # ls client.conf http.conf mime.types storage.conf tracker.conf # vi tracker.conf bind_addr=192.168.3.74 # /usr/local/FastDFS/tracker/fdfs_trackerd /etc/fdfs/tracker.conf # netstat -ntpl|grep fdfs tcp 0 0 192.168.3.74:22122 0.0.0.0:* LISTEN 1440/fdfs_trackerd
配置StorageA
# cd /etc/fdfs/ # ls client.conf http.conf mime.types storage.conf tracker.conf # vi storage.conf group_name=group1 bind_addr=192.168.3.77 base_path=/home/data/fastdfs store_path0=/home/data/fastdfs tracker_server=192.168.3.75:22122 tracker_server=192.168.3.74:22122 # /usr/local/FastDFS/storage/fdfs_storaged /etc/fdfs/storage.conf # netstat -ntpl|grep fdfs tcp 0 0 192.168.3.77:23000 0.0.0.0:* LISTEN 1595/fdfs_storaged
开启防火墙
# vi /etc/sysconfig/iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 23000 -j ACCEPT # service iptables restart
配置StorageB
# cd /etc/fdfs/ # ls client.conf http.conf mime.types storage.conf tracker.conf # vi storage.conf group_name=group2 bind_addr=192.168.3.76 base_path=/home/data/fastdfs store_path0=/home/data/fastdfs tracker_server=192.168.3.75:22122 tracker_server=192.168.3.74:22122 # /usr/local/FastDFS/storage/fdfs_storaged /etc/fdfs/storage.conf # netstat -ntpl|grep fdfs tcp 0 0 192.168.3.76:23000 0.0.0.0:* LISTEN 1558/fdfs_storaged
开启防火墙
# vi /etc/sysconfig/iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 23000 -j ACCEPT # service iptables restart
到此4台服务器配置完成
测试文件上传及下载
四台服务器启动nginx进程:
/usr/local/nginx/nginx
以在TrackerA服务器上传为例。 修改client.conf配置
# vi /etc/fdfs/client.conf base_path=/home/data/fastdfs tracker_server=192.168.3.75:22122 http.tracker_server_port=8080
TrackerA上第一次上传:
# /usr/local/FastDFS/client/fdfs_test /etc/fdfs/client.conf upload /home/abel/upload/nginx-1.6.1.tar.gz This is FastDFS client test program v5.03 Copyright (C) 2008, Happy Fish / YuQing FastDFS may be copied only under the terms of the GNU General Public License V3, which may be found in the FastDFS source kit. Please visit the FastDFS Home Page http://www.csource.org/ for more detail. [2014-09-05 19:32:44] DEBUG - base_path=/home/data/fastdfs, 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 tracker_query_storage_store_list_without_group: server 1. group_name=, ip_addr=192.168.3.76, port=23000 group_name=group2, ip_addr=192.168.3.76, port=23000 storage_upload_by_filename group_name=group2, remote_filename=M00/00/00/wKgDTFQJn1uACkjfAAxB5Q0Pr6s.tar.gz source ip address: 192.168.3.76 file timestamp=2014-09-05 19:32:43 file size=803301 file crc32=219131819 example file url: http://192.168.3.76:8080/group2/M00/00/00/wKgDTFQJn1uACkjfAAxB5Q0Pr6s.tar.gz storage_upload_slave_by_filename group_name=group2, remote_filename=M00/00/00/wKgDTFQJn1uACkjfAAxB5Q0Pr6s_big.tar.gz source ip address: 192.168.3.76 file timestamp=2014-09-05 19:32:44 file size=803301 file crc32=219131819 example file url: http://192.168.3.76:8080/group2/M00/00/00/wKgDTFQJn1uACkjfAAxB5Q0Pr6s_big.tar.gz 在浏览器中打开以下网址均可下载刚上传的文件: http://192.168.3.74:8080/group2/M00/00/00/wKgDTFQJn1uACkjfAAxB5Q0Pr6s_big.tar.gz http://192.168.3.75:8080/group2/M00/00/00/wKgDTFQJn1uACkjfAAxB5Q0Pr6s_big.tar.gz http://192.168.3.76:8080/group2/M00/00/00/wKgDTFQJn1uACkjfAAxB5Q0Pr6s_big.tar.gz http://192.168.3.77:8080/group2/M00/00/00/wKgDTFQJn1uACkjfAAxB5Q0Pr6s_big.tar.gz
过一会儿TrackerA上第二次:
# /usr/local/FastDFS/client/fdfs_test /etc/fdfs/client.conf upload /home/abel/upload/nginx-1.6.1.tar.gz This is FastDFS client test program v5.03 Copyright (C) 2008, Happy Fish / YuQing FastDFS may be copied only under the terms of the GNU General Public License V3, which may be found in the FastDFS source kit. Please visit the FastDFS Home Page http://www.csource.org/ for more detail. [2014-09-05 19:36:15] DEBUG - base_path=/home/data/fastdfs, 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 tracker_query_storage_store_list_without_group: server 1. group_name=, ip_addr=192.168.3.77, port=23000 group_name=group1, ip_addr=192.168.3.77, port=23000 storage_upload_by_filename group_name=group1, remote_filename=M00/00/00/wKgDTVQJoC6AcaHrAAxB5Q0Pr6s.tar.gz source ip address: 192.168.3.77 file timestamp=2014-09-05 19:36:14 file size=803301 file crc32=219131819 example file url: http://192.168.3.77:8080/group1/M00/00/00/wKgDTVQJoC6AcaHrAAxB5Q0Pr6s.tar.gz storage_upload_slave_by_filename group_name=group1, remote_filename=M00/00/00/wKgDTVQJoC6AcaHrAAxB5Q0Pr6s_big.tar.gz source ip address: 192.168.3.77 file timestamp=2014-09-05 19:36:14 file size=803301 file crc32=219131819 example file url: http://192.168.3.77:8080/group1/M00/00/00/wKgDTVQJoC6AcaHrAAxB5Q0Pr6s_big.tar.gz 在浏览器中打开以下网址均可下载刚上传的文件: http://192.168.3.74:8080/group1/M00/00/00/wKgDTVQJoC6AcaHrAAxB5Q0Pr6s_big.tar.gz http://192.168.3.75:8080/group1/M00/00/00/wKgDTVQJoC6AcaHrAAxB5Q0Pr6s_big.tar.gz http://192.168.3.76:8080/group1/M00/00/00/wKgDTVQJoC6AcaHrAAxB5Q0Pr6s_big.tar.gz http://192.168.3.77:8080/group1/M00/00/00/wKgDTVQJoC6AcaHrAAxB5Q0Pr6s_big.tar.gz