http://www.open-open.com/lib/view/open1435468300700.html
FastDFS是一个开源的分布式文件系统,它对文件进行管理,功能包括:文件存储、文件同步、文件访问(文件上传、文件下载)等,解决了大容量存储和负载均衡的问题。特别适合以文件为载体的在线服务,如相册网站、视频网站等等。
FastDFS服务端有两个角色:跟踪器(tracker)和存储节点(storage)。
跟踪器主要做调度工作,在访问上起负载均衡的作用。存储节点存储文件,完成文件管理的所有功能:存储、同步和提供存取接口,FastDFS同时对文件的meta data进行管理。所谓文件的meta data就是文件的相关属性,以键值对(key value pair)方式表示,如:width=1024,其中的key为width,value为1024。文件meta data是文件属性列表,可以包含多个键值对。
FastDFS系统结构如下图所示:

跟踪器和存储节点都可以由一台多台服务器构成。跟踪器和存储节点中的服务器均可以随时增加或下线而不会影响线上服务。其中跟踪器中的所有服务器都是对等的,可以根据服务器的压力情况随时增加或减少。
为了支持大容量,存储节点(服务器)采用了分卷(或分组)的组织方式。存储系统由一个或多个卷组成,卷与卷之间的文件是相互独立的,所有卷的文件容量累加就是整个存储系统中的文件容量。一个卷可以由一台或多台存储服务器组成,一个卷下的存储服务器中的文件都是相同的,卷中的多台存储服务器起到了冗余备份和负载均衡的作用。在卷中增加服务器时,同步已有的文件由系统自动完成,同步完成后,系统自动将新增服务器切换到线上提供服务。当存储空间不足或即将耗尽时,可以动态添加卷。只需要增加一台或多台服务器,并将它们配置为一个新的卷,这样就扩大了存储系统的容量。FastDFS中的文件标识
1.FastDFS上传文件交互过程:

1. client询问tracker上传到的storage,不需要附加参数;
2. tracker返回一台可用的storage;
3. client直接和storage通讯完成文件上传。
客户端client 发起对FastDFS 的文件传输动作,是通过连接到某一台Tracker Server 的指定端口来实现的,Tracker Server 根据目前已掌握的信息,来决定选择哪一台Storage Server ,然后将这个Storage Server 的地址等信息返回给client,然后client 再通过这些信息连接到这台Storage Server,将要上传的文件传送到给Storage Server上。
2.FastDFS下载文件交互过程:

1. client询问tracker下载文件的storage,参数为文件标识(卷名和文件名);
2. tracker返回一台可用的storage;
3. client直接和storage通讯完成文件下载。
FastDFS 集群规划:
跟踪服务器1:192.168.1.120 du-dfs-tracker-1
跟踪服务器2:192.168.1.80 du-dfs-tracker-2
存储服务器1:192.168.1.100 du-dfs-storage-group1-1
存储服务器2:192.168.1.101 du-dfs-storage-group1-2
环境:CentOS6.5
用户:root
数据目录:/fastdfs_data(注:数据目录按你的数据盘挂载路径而定)
所有机器均安装 所有服务器的时间必须一致
yum -y install ntpdate
crontab -e
* 4 * * * /usr/sbin/ntpdate us.pool.ntp.org > /dev/null 2>&1
yum install make cmake gcc gcc-c++ pcre-devel
安装libfastcommon
git clone https://github.com/happyfish100/libfastcommon.git
cd libfastcommon
./make.sh
./make.sh install
安装fastdfs
git clone https://github.com/happyfish100/fastdfs.git
cd fastdfs/
./make.sh
./make.sh install
配置两台 tracker 1.120 1.80
cp /etc/fdfs/tracker.conf.sample /etc/fdfs/tracker.conf
mkdir -p /home/yuqing/fastdfs 存放log的path
启动 tracker 服务:
[root@protect fastdfs]# /etc/init.d/fdfs_trackerd start
Starting FastDFS tracker server:
[root@protect fastdfs]# netstat -lptnu
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:22122 0.0.0.0:* LISTEN 16593/fdfs_trackerd
2. 配置 G1(1.100,1.101)
mkdir -p /home/yuqing/fastdfs
cp /etc/fdfs/storage.conf.sample /etc/fdfs/storage.conf
group_name=group1
store_path_count=1
store_path0=/fastdfs_data
tracker_server=192.168.1.120:22122
tracker_server=192.168.1.80:22122
查看storage是否连接到tracker
fdfs_monitor /etc/fdfs/storage.conf
storage中安装nginx,主要是为了为提供http的访问服务,同时解决group中storage服务器的同步延迟问题。
而tracker中安装nginx,主要是为了提供http访问的反向代理、负载均衡以及缓存服务
storage server 安装 nginx
nginx 新增模块(非覆盖安装)
git clone https://github.com/happyfish100/fastdfs-nginx-module.git
这四台服务器的nginx都是通过salt 一键安装的
nginx -V 可以查看原来编译时都带了哪些参数
原来的参数:
--prefix=/usr/local/nginx --user=www --group=www --with-poll_module --with-pcre --with-http_realip_module --with-http_dav_module --with-http_gzip_static_module --with-http_degradation_module --with-http_addition_module=shared --with-http_xslt_module=shared --with-http_image_filter_module=shared --with-http_geoip_module=shared --with-http_sub_module=shared --with-http_flv_module=shared --with-http_slice_module=shared --with-http_mp4_module=shared --with-http_concat_module=shared --with-http_random_index_module=shared --with-http_sysguard_module=shared --with-http_charset_filter_module=shared --with-http_userid_filter_module=shared --with-http_footer_filter_module=shared --with-http_access_module=shared --with-http_autoindex_module=shared --with-http_map_module=shared --with-http_split_clients_module=shared --with-http_referer_module=shared --with-http_rewrite_module=shared --with-http_fastcgi_module=shared --with-http_uwsgi_module=shared --with-http_scgi_module=shared --with-http_memcached_module=shared --with-http_limit_conn_module=shared --with-http_limit_req_module=shared --with-http_empty_gif_module=shared --with-http_browser_module=shared --with-http_user_agent_module=shared --with-http_upstream_ip_hash_module=shared --with-http_upstream_least_conn_module=shared --with-http_lua_module=shared
添加的参数z:--add-module=../fastdfs-nginx-module/src/
make 完后不要 make install
把之前的可执行文件nginx rm 了
cp /usr/local/src/tengine-2.1.1/objs/nginx /usr/local/nginx/sbin
配置nginx
cp /root/fastdfs-nginx-module/src/mod_fastdfs.conf /etc/fdfs/
cp /root/fastdfs/conf/{anti-steal.jpg,http.conf,mime.types} /etc/fdfs/
vim /etc/fdfs/mod_fastdfs.conf
tracker_server=192.168.1.120:22122
tracker_server=192.168.1.80:22122
group_name=group1
url_have_group_name = true
store_path_count=1
store_path0=/fastdfs_data
log_filename=/var/log/mod_fastdfs.log
group_count = 1
[group1]
group_name=group1
storage_server_port=23000
store_path_count=1
store_path0=/fastdfs_data/
ERROR
- file: ../fastdfs-nginx-module/src//common.c, line: 188, config file:
/etc/fdfs/mod_fastdfs.conf, you must set url_have_group_name to true to
support multi-group!
如查下载时如发现老报 404, 将 nginx.conf 第一行 user nobody 修改为 user root 后重新启动
常用命令:
fdfs_monitor /etc/fdfs/storage.conf|more
fdfs_monitor /etc/fdfs/storage.conf delete group1 192.168.1.14
fdfs_upload_file /etc/fdfs/client.conf /tmp/a.html
注意,有时候通过fdfs_monitor 发现store_path的配置不生效,这时,必须要删除tracker目录下的两个文件:storage_groups_new.dat,storage_servers_new.dat再重启服务。
nginx 配置查看 100,101 vhost/fastdfs.zhen.com
在tracker中安装nginx
上传ngx_cache_purge-2.3.tar.gz到/usr/local/src,解压
yum -y install pcre pcre-devel
--add-module=/usr/local/src/ngx_cache_purge-2.3
配置 Nginx, 设置负载均衡以及缓存
详情查看 nginx.cond vhost、pic.zhen.com
按以上 nginx 配置文件的要求,创建对应的缓存目录:
# mkdir -p /fastdfs/cache/nginx/proxy_cache
# mkdir -p /fastdfs/cache/nginx/proxy_cache/tmp
文件访问测试,验证
直接通过访问Storage节点:
http://192.168.1.100:8888/group1/M00/00/00/wKgBjFjSPkqAOySEAAAAF0_eDVA38.html
通过tracker 访问
http://192.168.1.120:/group1/M00/00/00/wKgBjFjSPkqAOySEAAAAF0_eDVA38.html
5. 清除缓存
若要清除该图片的缓存,可以在文件 URL 之前加上 purge 即可,如下图
http://10.10.10.123/purge/group1/M00/00/00/CgoKeFRQqaaASsgNAAEgRqu1UiU712.jpg
注意: 千万不要使用 kill -9 命令强杀 FastDFS 进程,否则可能会导致 binlog 数据丢失