FastDFS的安装和配置过程
版本V1.0
时间2012-9-3
版权GPL
作者ZERO (zerootty@https://www.sodocs.net/doc/1b13924200.html,)
本文档来自zero 对fdfs 的学习和理解,如有谬误之处,欢迎给我发邮件交流。
背景
FastDFS是一款开源的、分布式文件系统(Distributed File System),由淘宝开发平台部资深架构师余庆开发。该开源项目的主页是https://www.sodocs.net/doc/1b13924200.html,/p/fastdfs/可以通过https://www.sodocs.net/doc/1b13924200.html,下载。FastDFS论坛是https://www.sodocs.net/doc/1b13924200.html, ,目前是指向ChinaUnix开源项目孵化平台的一个板块FastDFS,网址https://www.sodocs.net/doc/1b13924200.html,/forum-240-1.html
本次实验环境说明:
服务器IP 组端口作用
Tracker1 10.1.20.201 无22122 Tracker server Tracker2 10.1.20.204 无22122 Tracker server Storage1 10.1.20.202 Group1 23000 Storage server Storage2 10.1.20.203 Group1 23000 Storage server
1,安装libevent安装libevent (作者建议使用1.4.xx稳定版本,如1.4.14b)
cd /opt/
wget https://https://www.sodocs.net/doc/1b13924200.html,/downloads/libevent/libevent/libevent-1.4.14b-stable.tar.gz
tar zvxf libevent-1.4.14b-stable.tar.gz
cd libevent-1.4.14b-stable
./configure; make; make install
为libevent创建软链接到/lib库下,64位系统对应/lib64
ln -s /usr/local/lib/libevent* /lib/
ln -s /usr/local/lib/libevent* /lib64/
2,为trackerd节点安装FastDFS,并修改配置文件/etc/fdfs/tracker.conf( 如果trackerd需要使用内置的web server,需要修改make文件,启用WITH_HTTPD=1再编译)
cd /opt
wget https://www.sodocs.net/doc/1b13924200.html,/files/FastDFS_v3.11.tar.gz
tar zvxf FastDFS_v3.11.tar.gz
cd FastDFS
./make.sh
./make.sh install
3,创建存储目录
mkdir /home/fastdfs
4,配置tracker_server:
vim /etc/fdfs/tracker.conf
设置base_path=/home/fastdfs;
启动:/usr/local/bin/fdfs_trackerd /etc/fdfs/tracker.conf
5,为storaged节点安装FastDFS,storaged节点也需要libevent,安装方法同上1-3步骤,只不过修改的文件是storage.conf
6,创建存储目录
mkdir /home/fastdfs
7,配置storage_server:
vim /etc/fdfs/storage.conf
设置base_path=/home/fastdfs;
store_path0=/home/fastdfs;
tracker_server=10.1.20.201:22122
tracker_server=10.1.20.204:22122 (可以指定多个tracker_server,换行写即可)
启动:/usr/local/bin/fdfs_storaged /etc/fdfs/storage.conf
*注:如果storage server 有多个ip,可以用bind_addr来指定某个特定ip,bind_addr地址会提交给tracker_server
8,nginx插件模块安装:
在每台storage server上部署web server,直接对外提供HTTP服务,tracker server上不需要部署web server,如果请求文件在当前storage上不存在,通过文件ID反解出源storage,直接请求源storage,FastDFS扩展模块不依赖于FastDFS server,可以独立存在。
cd /opt
wget https://www.sodocs.net/doc/1b13924200.html,/files/fastdfs-nginx-module_v1.11.tar.gz
tar zxvf fastdfs-nginx-module_v1.11.tar.gz
wget https://www.sodocs.net/doc/1b13924200.html,/download/nginx-1.0.15.tar.gz
tar zxvf nginx-1.0.15.tar.gz
./configure --prefix=/usr/local/nginx --add-module=/opt/fastdfs-nginx-module/src/
make
make install
nginx.conf添加的部分
vim /usr/local/nginx/conf/nginx.conf
添加:
location /group1/M00 {
root /home/fastdfs/data;
ngx_fastdfs_module;
}
启动nginx: /usr/local/nginx/sbin/nginx
9,mod_fastdfs模块设置
cp /opt/fastdfs-nginx-module/src/mod_fastdfs.conf /etc/fdfs/
vim /etc/fdfs/mod_fastdfs.conf
设置:base_path=/home/fastdfs
tracker_server=10.1.20.201:22122
tracker_server=10.1.20.204:22122
store_path0=/home/fastdfs
response_mode=proxy
url_have_group_name = true
log_filename=/opt/fastdfs.log
10,启动服务
Tracker Server作为系统服务
这里可以将安装包里面提供的/init.d/的fdfs_trackerd拷贝到/etc/init.d里面,后面就可以用service fdfs_trackerd start/stop/restart/status这种方式来管理服务了。
Storage Server作为系统服务
这里可以将安装包里面提供的/init.d/的fdfs_storaged拷贝到/etc/init.d里面,后面就可以用service fdfs_storaged start/stop/restart/status这种方式来管理服务了。
第一次启动的时候会去上面配置文件指定的path里面创建目录,耐心等待一下。
检查一下两台/home/fastdfs/log日志看看是否有异常,正常情况日志里面都会写,和tracker 以及storage的通讯。
11,client.conf上传配置文件修改
vim /etc/fdfs/client.conf
base_path=/home/fastdfs
tracker_server=10.1.20.201:22122 (tracker ip)
tracker_server=10.1.20.204:22122 (tracker ip)
12,测试上传文件:
fdfs_upload_file /etc/fdfs/client.conf anaconda-ks.cfg
运行完后会得到返回:
group1/M00/00/00/CgEUylA_dZvx9_4TAAAElz19TBw949.cfg
现在可以去看一下两台Storage Server是否存在这个文件,在storage的数据目录下的/00/00目录下即可看到该文件:
ll /home/fastdfs/data/00/00/CgEUylA_dZvx9_4TAAAElz19TBw949.cfg
也可以通过浏览器:
http://10.1.20.202/ group1/M00/00/00/CgEUylA_dZvx9_4TAAAElz19TBw949.cfg
http://10.1.20.203/ group1/M00/00/00/CgEUylA_dZvx9_4TAAAElz19TBw949.cfg
分别都可以得到该文件
13,删除文件
fdfs_delete_file /etc/fdfs/client.conf group1/M00/00/00/CgEUylA_dZvx9_4TAAAElz19TBw949.cfg 现在可以去看一下两台Storage Server这个文件是否已经被删掉了,通过浏览器访问就会出现404
14,冗余测试
首先先停掉一个storage server的服务,再上传一个文件,现在就只有一台机器有这个文件。但是这时候通过浏览器去两个网址请求该文件的时候,都可以拿到该文件,这个就是FastDFS 的nginx插件起作用的结果,该插件会检查本地是否有这个文件,如果没有就会向tracker询问到底同组哪台服务器有这个文件,然后作为向拥有这个文件的Nginx请求拿回文件并传回客户的浏览器,相当于一个proxy,这也就是在上面/etc/fdfs/mod_fastdfs.conf里面的response_mode=proxy。这里还有另外一个方法,是默认的redirect,这种方法是没有该文件的服务器会返回一个302,告诉客户去请求另外一个地址。
启动刚才停掉的storage server的服务,过一段时间该文件就会同步到该服务器的相应位置。
15,安装php扩展模块:
FastDFS不是通用的文件系统,只能通过专用的API来访问,目前提供了C JAVA PHP的API,下面我们来安装php扩展。让FastDFS支持php,在FastDFS的源码包解压后里面有个
php_client目录,进入此目录,参照README进行安装:
(前提是安装好php环境)
cd/opt/FastDFS/php_client
/usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config
make; make install
vim /usr/local/php/etc/php.ini
extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/"
添加:
extension = fastdfs_client.so
fastdfs_client.tracker_group_count = 1
fastdfs_client.tracker_group0 = /etc/fdfs/client.conf
fastdfs_client.base_path = /tmp
测试:/usr/local/php/bin/php -m |grep dfs如果看到fastdfs_client说明配置成功了。
此时就可以执行/usr/local/php/bin/php test.php
编写test.php测试:
$file_info=fastdfs_get_file_info1('group1/M00/00/00/CgEUylA_PUCiju7QAAEL5WT1Gu4957.jpg') ;
var_dump($file_info);
?>
测试结果如下:
全文完
FAQ:
一个tracker可以有多个stroage,一个storage也可以有多个tracker
tracker ip必须全部写进client配置文件中,同时每次改变client配置文件,必须重启php加载
新增tracker时,需PHP重启加载。
想要把fastDFS产生的记录清空,再重新搭建一次,应该删除哪些文件
停止storage server 和tracker server,删除base_path下的data 子目录即可
如何删除无效的storage server?
可以使用fdfs_monitor来删除,命令行如下:
/usr/local/bin/fdfs_monitor
例如:
/usr/local/bin/fdfs_monitor /etc/fdfs/client.conf delete group1 192.168.0.100
注意:如果被删除的storage server的状态是ACTIVE,也就是该storage server还在线上服务的情况下,是无法删除掉的。
查看tracker_server及storage_server状态
/usr/local/bin/fdfs_monitor /etc/fdfs/client.conf
重启tracker server命令:
sh /opt/FastDFS/restart.sh /usr/local/bin/fdfs_trackerd /etc/fdfs/tracker.conf
重启storage server命令:
sh /opt/FastDFS/restart.sh /usr/local/bin/fdfs_storaged /etc/fdfs/storage.conf