Rootop 服务器运维与web架构

2014-04-27
发表者 Venus
暂无评论

fastdfs+nginx配置

这里只是将原先的fastdfs+apache改为nginx,之前的搭建过程可参考 :

https://www.rootop.org/pages/2431.html

接着上次的配置环境,现在将apache改为nginx。

在两台storage服务器上配置nginx和fastdfs-nginx模块:
storage_s1:
下载pcre、nginx、fastdfs的nginx模块,编译:

[root@centos-6.5-x64 ~]#wget -c http://fastdfs.googlecode.com/files/fastdfs-nginx-module_v1.15.tar.gz
[root@centos-6.5-x64 ~]#tar zxvf fastdfs-nginx-module_v1.15.tar.gz
[root@centos-6.5-x64 ~]#tar zxvf pcre-8.12.tar.gz
[root@centos-6.5-x64 ~]#tar zxvf nginx-1.5.10.tar.gz
[root@centos-6.5-x64 ~]#cd pcre-8.12
[root@centos-6.5-x64 pcre-8.12]#./configure --prefix=/usr/
[root@centos-6.5-x64 pcre-8.12]#make && make install
[root@centos-6.5-x64 pcre-8.12]#cd ..
[root@centos-6.5-x64 ~]#cd nginx-1.5.10
[root@centos-6.5-x64 nginx-1.5.10]#useradd www -s /sbin/nologin
[root@centos-6.5-x64 nginx-1.5.10]#yum install zlib zlib-devel -y
[root@centos-6.5-x64 nginx-1.5.10]#./configure --prefix=/usr/local/nginx --user=www --group=www --add-module=../fastdfs-nginx-module/src/
[root@centos-6.5-x64 nginx-1.5.10]#make && make install

配置nginx:
[root@centos-6.5-x64 ~]#vi /usr/local/nginx/conf/nginx.conf
在server段中添加:

location /group1/M00 {
 root /mnt/fastdfs_storage_data/data;
 ngx_fastdfs_module;
 }

配置etc/fdfs/mod_fastdfs.conf配置文件(编译时会自动拷贝过去)
[root@centos-6.5-x64 fdfs]#grep -v “#” mod_fastdfs.conf | grep -v “^$”
connect_timeout=2
network_timeout=30
base_path=/tmp
load_fdfs_parameters_from_tracker=true
storage_sync_file_max_delay = 86400
use_storage_id = false
storage_ids_filename = storage_ids.conf
tracker_server=192.168.1.60:22122
storage_server_port=23000
group_name=group1
url_have_group_name = true
store_path_count=1
store_path0=/mnt/fastdfs_storage_data
log_level=info
log_filename=
response_mode=proxy
if_alias_prefix=
http.need_find_content_type=false
flv_support = true
flv_extension = flv
group_count = 0

启动nginx:
[root@centos-6.5-x64 conf]#/usr/local/nginx/sbin/nginx &
[1] 9358
[root@centos-6.5-x64 conf]#ngx_http_fastdfs_set pid=9358
[1]+ Done /usr/local/nginx/sbin/nginx
[root@centos-6.5-x64 ~]#lsof -i:80
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
nginx 10240 root 6u IPv4 21835 0t0 TCP *:http (LISTEN)
nginx 10241 nobody 6u IPv4 21835 0t0 TCP *:http (LISTEN)
[1]+ Done /usr/local/nginx/sbin/nginx

完成

storage_s2:
配置跟storage_s1完全一样,过程略。

在客户端上传文件,访问文件的web路径,测试storage_s1、storage_s2通过。
停掉storage_s2的storage服务,客户端上传一个新文件,根据提示得知:
group_name=group1, remote_filename=M00/00/00/wKgBPVNb4hOAM7lhAAAADw_r4o417.html
source ip address: 192.168.1.61
文件上传到storage_s1服务器,现在访问storage_s2服务器的文件url。发现正常访问,表明fastdfs-nginx-module起作用了。

PS:
查看已有的nginx编译参数:
[root@centos-6.5-x64 nginx-1.5.10]#/usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.5.10
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-4) (GCC)
configure arguments: –prefix=/usr/local/nginx –user=www –group=www
nginx不像apache一样可以通过 apxs -i -a -c mod_rewrite.c 这种方式单独安装模块,需要重新编译。
所以需要知道之前nginx的编译参数。

2014-04-26
发表者 Venus
暂无评论

一次mfs恢复数据

今天web服务器发现挂载的分布式文件系统mfs无法访问,去mfsmaster检查发现服务器没有启动。
启动服务器,登陆,查看没有mfs进程,端口都没启动,手动启动服务,提示错误:
[root@centos-6.5-x64 ~]#/usr/local/mfs/sbin/mfsmaster start
working directory: /usr/local/mfs/var/mfs
lockfile created and locked
initializing mfsmaster modules …
loading sessions … ok
sessions file has been loaded
exports file has been loaded
loading metadata …
can’t open metadata file
if this is new instalation then rename metadata.mfs.empty as metadata.mfs
init: file system manager failed !!!
error occured during initialization – exiting
根据提示判断为mfs启动时认为这是一台新的mfsmaster,需要rename metadata.mfs.empty as metadata.mfs

这完全不是新服务器,赶紧恢复日志:                                                                     [root@centos-6.5-x64 var]#tar zcvf mfs.tar.gz mfs/  #防止意外,把整个数据日志信息目录备份
[root@centos-6.5-x64 mfs]#/usr/local/mfs/sbin/mfsmetarestore -m metadata.mfs.back -o metadata.mfs changelog.*.mfs
loading objects (files,directories,etc.) … loading node: read error: Success
error
can’t read metadata from file: metadata.mfs.back
看样子metadata.mfs.back文件损坏。
赶紧查资料:http://blog.ztrix.me/blog/2012/05/13/restore-mfs-metadata/
根据资料查看我本地linux文件系统确实是ext4,可以判断跟资料上的情况一样了,可是我不会像文章写的一样会编程寻找数据。

遂再次检查:
[root@centos-6.5-x64 mfs]#ll
总用量 1724
-rw-r—– 1 mfs mfs 8543 4月 26 11:09 changelog.0.mfs
-rw-r—– 1 mfs mfs 4246 4月 26 11:09 changelog.10.mfs
-rw-r—– 1 mfs mfs 3368 4月 26 11:09 changelog.11.mfs
-rw-r—– 1 mfs mfs 8543 4月 26 11:09 changelog.1.mfs
-rw-r—– 1 mfs mfs 51757 4月 26 11:09 changelog.2.mfs
-rw-r—– 1 mfs mfs 183976 4月 26 11:09 changelog.3.mfs
-rw-r—– 1 mfs mfs 4901 4月 26 11:09 changelog.4.mfs
-rw-r—– 1 mfs mfs 1796 4月 26 11:09 changelog.5.mfs
-rw-r—– 1 mfs mfs 1502 4月 26 11:09 changelog.6.mfs
-rw-r—– 1 mfs mfs 599839 4月 26 11:09 changelog.7.mfs
-rw-r—– 1 mfs mfs 4383 4月 26 11:09 changelog.8.mfs
-rw-r—– 1 mfs mfs 4988 4月 26 11:09 changelog.9.mfs
-rw-r—– 1 mfs mfs 81920 4月 26 11:09 metadata.mfs.back
-rw-r—– 1 mfs mfs 147081 4月 26 11:09 metadata.mfs.back.tmp
-rw-r–r– 1 root root 8 4月 26 11:09 metadata.mfs.empty
-rw-r—– 1 mfs mfs 10541 4月 26 11:09 sessions.mfs
-rw-r—– 1 mfs mfs 610016 4月 26 11:09 stats.mfs
[root@centos-6.5-x64 mfs]#rm -f metadata.mfs.back
[root@centos-6.5-x64 mfs]#mv metadata.mfs.back.tmp metadata.mfs.back
发现有个metadata.mfs.back.tmp文件,把之前的metadata.mfs.back删掉,metadata.mfs.back.tmp改名metadata.mfs.back
恢复日志:
[root@centos-6.5-x64 mfs]#/usr/local/mfs/sbin/mfsmetarestore -m metadata.mfs.back -o metadata.mfs changelog.*.mfs
loading objects (files,directories,etc.) … ok
loading names … ok
loading deletion timestamps … ok
checking filesystem consistency … ok
loading chunks data … ok
connecting files and chunks … ok
applying changes from file: changelog.0.mfs
meta data version: 22443
version after applying changelog: 22698
applying changes from file: changelog.10.mfs
meta data version: 22698
version after applying changelog: 22698
applying changes from file: changelog.11.mfs
meta data version: 22698
version after applying changelog: 22698
applying changes from file: changelog.1.mfs
meta data version: 22698
version after applying changelog: 22698
applying changes from file: changelog.2.mfs
meta data version: 22698
version after applying changelog: 22698
applying changes from file: changelog.3.mfs
meta data version: 22698
version after applying changelog: 22698
applying changes from file: changelog.4.mfs
meta data version: 22698
version after applying changelog: 22698
applying changes from file: changelog.5.mfs
meta data version: 22698
version after applying changelog: 22698
applying changes from file: changelog.6.mfs
meta data version: 22698
version after applying changelog: 22698
applying changes from file: changelog.7.mfs
meta data version: 22698
version after applying changelog: 22698
applying changes from file: changelog.8.mfs
meta data version: 22698
version after applying changelog: 22698
applying changes from file: changelog.9.mfs
meta data version: 22698
version after applying changelog: 22698
store metadata into file: metadata.mfs

太好了,没报错,启动服务:
[root@centos-6.5-x64 mfs]#/usr/local/mfs/sbin/mfsmaster start

working directory: /usr/local/mfs/var/mfs
lockfile created and locked
initializing mfsmaster modules …
loading sessions … ok
sessions file has been loaded
exports file has been loaded
loading metadata …
loading objects (files,directories,etc.) … ok
loading names … ok
loading deletion timestamps … ok
checking filesystem consistency … ok
loading chunks data … ok
connecting files and chunks … ok
all inodes: 1622
directory inodes: 124
file inodes: 1498
chunks: 1485
metadata file has been loaded
stats file has been loaded
master <-> metaloggers module: listen on *:9419
master <-> chunkservers module: listen on *:9420
main master server module: listen on *:9421
mfsmaster daemon initialized properly
[root@centos-6.5-x64 mfs]#
[root@centos-6.5-x64 mfs]#ps aux | grep mfs
root 1556 0.0 0.4 170416 460 ? S 10:51 0:00 python /usr/local/mfs/sbin/mfscgiserv start
mfs 1946 6.2 43.1 82056 46196 ? S< 11:16 0:00 /usr/local/mfs/sbin/mfsmaster start
root 1948 2.0 0.7 103256 844 pts/0 S+ 11:17 0:00 grep mfs
服务启动,去web服务器查看,数据出来了。

转来的丢失原因:

ext4 文件系统的 delay allocation 导致的。moosefs 在持久化 metadata 信息的时候,会重写整个文件。他会首先把之前的 metadata.mfs.back 文件重命名成 metadaba.mfs.back.tmp,然后开始写 metadata.mfs.back 文件,写完了 fclose 之后,删除 metadata.mfs.back.tmp。如果断电的时候,正常在进行这么一个过程,那么有可能 fclose 之后,metadata.mfs.back.tmp 也删除了,但是 ext4 的 delay allocation 特性导致文件还没有真正写入到磁盘。(也有可能文件写入了磁盘 block,但是文件 inode 还没有更新)。我们在网上发现有不少人也遇到过同样的断电之后文件大小变成 0 的问题,如这个:http://www.symantec.com/connect/blogs/ext4-data-recovery-how-recovery-lost-files-ext4-file-system-linux
有可能是 moosefs 写 metadata 失败导致的。阅读了相关源代码之后,我们发现 moosefs 写文件和 fclose 的时候居然从来不判断返回值!有些地方有判断返回值,但是也仅仅打印一句 log,既不做相应错误处理,也不停止。于是就有可能是写文件失败,fclose 也失败,但是它不查返回值,所以又把之前的 metadata.mfs.back.tmp 删除了。
有可能是 ext4 修复导致的。断电造成文件系统出现不一致,然后自动修复之后造成了数据丢失。

这样一来,强烈建议将mfs的文件系统改为ext3上运行。

PS:

环境中有一台mfs slave机器做备份机,发现备份机也出问题,当我加电给master时,任务计划每个一分钟同步数据日志到备机,导致备机的日志也出错。

考虑到的方案:

在一台chunkserver中添加mfslog服务,每1小时获取一次master日志,定时备份数据日志目录。把损失降到最小。

2014-04-26
发表者 Venus
暂无评论

linux配置NTP服务器(转)

实验环境:

RHEL5

Server:192.168.2.10

Client:192.168.2.120

一、搭建时间同步服务器
1、默认系统会安装ntp服务,如果没有使用yum install –y ntp安装

2、修改ntp.conf配置文件
vi /etc/ntp.conf
①、第一种配置:允许任何IP的客户机都可以进行时间同步
#restrict default kod nomodify notrap nopeer noquery

restrict default modify

②、第二种配置:只允许192.168.2.0网段的客户机进行时间同步
在restrict default nomodify notrap noquery(表示默认拒绝所有IP的时间同步)之后增加一行:
restrict 192.168.2.0 mask 255.255.255.0 nomodify notrap
3、启动ntp服务
service ntpd start
开机启动服务
chkconfig ntpd on
4、ntpd启动后,客户机要等几分钟再与其进行时间同步,否则会提示“no server suitable for synchronization found”错误。

二、配置时间同步客户机

手工执行 ntpdate <ntp server> 来同步
或者利用crontab来执行
crontab -e
*/10 * * * * /usr/sbin/ntpdate 192.168.2.10 >> /root/ntpdate.log 2>&1

表示每隔10分钟同步一次时间

三、在服务端验证:

# watch ntpq -p

Every 2.0s: ntpq -p                                                    Tue Dec  7 05:52:55 2010

remote           refid      st t when poll reach   delay   offset  jitter

==============================================================================

*LOCAL(0)        .LOCL.           8 l   22   64  377    0.000    0.000   0.001

服务正常运行

在客户端进行同步验证:

# ntpdate -d 192.168.2.10

7 Dec 05:54:42 ntpdate[3627]: ntpdate 4.2.2p1@1.1570-o Thu Nov 26 11:35:07 UTC 2009 (1)

Looking for host 192.168.2.10 and service ntp

host found : 192.168.2.10

transmit(192.168.2.10)

receive(192.168.2.10)

transmit(192.168.2.10)

receive(192.168.2.10)

transmit(192.168.2.10)

receive(192.168.2.10)

transmit(192.168.2.10)

receive(192.168.2.10)

transmit(192.168.2.10)

server 192.168.2.10, port 123

stratum 9, precision -20, leap 00, trust 000

refid [192.168.2.10], delay 0.02632, dispersion 0.00061

transmitted 4, in filter 4

reference time:    d0a890f1.b4d040ff  Tue, Dec  7 2010  5:54:41.706

originate timestamp: d0a89102.09318178  Tue, Dec  7 2010  5:54:58.035

transmit timestamp:  d0a890f2.3c8effe2  Tue, Dec  7 2010  5:54:42.236

filter delay:  0.02910  0.02632  0.02808  0.02641

0.00000  0.00000  0.00000  0.00000

filter offset: 15.80026 15.79880 15.79793 15.79837

0.000000 0.000000 0.000000 0.000000

delay 0.02632, dispersion 0.00061

offset 15.798801

7 Dec 05:54:42 ntpdate[3627]: step time server 192.168.2.10 offset 15.798801 sec

附:
当用ntpdate -d 来查询时会发现导致 no server suitable for synchronization found 的错误的信息有以下2个:
错误1.Server dropped: Strata too high
在ntp客户端运行ntpdate serverIP,出现no server suitable for synchronization found的错误。
在ntp客户端用ntpdate -d serverIP查看,发现有“Server dropped: strata too high”的错误,并且显示“stratum 16”。而正常情况下stratum这个值得范围是“0~15”。
这是因为NTP server还没有和其自身或者它的server同步上。
以下的定义是让NTP Server和其自身保持同步,如果在/ntp.conf中定义的server都不可用时,将使用local时间作为ntp服务提供给ntp客户端。

server 127.127.1.0
fudge 127.127.1.0 stratum 8

在ntp server上重新启动ntp服务后,ntp server自身或者与其server的同步的需要一个时间段,这个过程可能是5分钟,在这个时间之内在客户端运行ntpdate命令时会产生no server suitable for synchronization found的错误。
那么如何知道何时ntp server完成了和自身同步的过程呢?
在ntp server上使用命令:

# watch ntpq -p

Every 2.0s: ntpq -p                                                    Tue Dec  7 05:52:55 2010

remote           refid      st t when poll reach   delay   offset  jitter

==============================================================================

*LOCAL(0)        .LOCL.           8 l   22   64  377    0.000    0.000   0.001

注意LOCAL的这个就是与自身同步的ntp server。
注意reach这个值,在启动ntp server服务后,这个值就从0开始不断增加,当增加到17的时候,从0到17是5次的变更,每一次是poll的值的秒数,是64秒*5=320秒的时间。
如果之后从ntp客户端同步ntp server还失败的话,用ntpdate –d来查询详细错误信息,再做判断。
错误2.Server dropped: no data
从客户端执行netdate –d时有错误信息如下:

transmit(192.168.30.22)
transmit(192.168.30.22)
transmit(192.168.30.22)
transmit(192.168.30.22)
transmit(192.168.30.22)
192.168.30.22: Server dropped: no data
server 192.168.30.22, port 123

…..
28 Jul 17:42:24 ntpdate[14148]: no server suitable for synchronization found
出现这个问题的原因可能有2:
1。检查ntp的版本,如果你使用的是ntp4.2(包括4.2)之后的版本,在restrict的定义中使用了notrust的话,会导致以上错误。

使用以下命令检查ntp的版本:
# ntpq -c version
下面是来自ntp官方网站的说明:
The behavior of notrust changed between versions 4.1 and 4.2.
In 4.1 (and earlier) notrust meant “Don’t trust this host/subnet for time”.
In 4.2 (and later) notrust means “Ignore all NTP packets that are not cryptographically authenticated.” This forces remote time servers to authenticate themselves to your (client) ntpd
解决:
把notrust去掉。

2。检查ntp server的防火墙。可能是server的防火墙屏蔽了upd 123端口。
可以用命令
#service iptables stop

来关掉iptables服务后再尝试从ntp客户端的同步,如果成功,证明是防火墙的问题,需要更改iptables的设置。

#通常NTP后约15分钟内才会和上层NTP服务器顺利连接上。

#*使用中上层 上层NTP地址
#+连接成功,候选
st:stratum阶层
when:上次更新时间
poll:下次更新时间
reach:已经更新次数
delay:网络延时时间
offset:时间补偿
jitter:Linux与BIOS差异时间,单位为10的-6次方秒;

2014-04-25
发表者 Venus
暂无评论

分布式文件系统FastDFS原理介绍

什么是FastDFS?

FastDFS是一个开源的轻量级分布式文件系统。它解决了大数据量存储和负载均衡等问题。特别适合以中小文件(建议范围:4KB < file_size <500MB)为载体的在线服务,如相册网站、视频网站等等。在UC基于FastDFS开发向用户提供了:网盘,社区,广告和应用下载等业务的存储服务。

FastDFS架构:

FastDFS服务端有三个角色:跟踪服务器(tracker server)、存储服务器(storage server)和客户端(client)。

  • tracker server:跟踪服务器,主要做调度工作,起负载均衡的作用。在内存中记录集群中所有存储组和存储服务器的状态信息,是客户端和数据服务器交互的枢纽。相比GFS中的master更为精简,不记录文件索引信息,占用的内存量很少。
  • storage server:存储服务器(又称:存储节点或数据服务器),文件和文件属性(meta data)都保存到存储服务器上。Storage server直接利用OS的文件系统调用管理文件。
  • client:客户端,作为业务请求的发起方,通过专有接口,使用TCP/IP协议与跟踪器服务器或存储节点进行数据交互。

Tracker Server:跟踪服务器,主要做调度工作,在访问上起负载均衡的作用。
Storage Server:存储服务器(又称数据服务器)。

存储节点采用了分组(group)的方式。存储系统由一个或多个group组成,group与group之间的文件是相互独立的,

所有group的文件容量累加就是整个存储系统中的文件容量。一个group可以由一台或多台存储服务器组成,一个group下的存储服务器中的文件都是相同的,group中的多台存储服务器起到了冗余备份和负载均衡的作用(一个组的存储容量为该组内存储服务器容量最小的那个,不同组的Storage server之间不会相互通信,同组内的Storage server之间会相互连
接进行文件同步)。

在group中增加服务器时,同步已有的文件由系统自动完成,同步完成后,系统自动将新增服务器切换到线上提供服务。

当存储空间不足或即将耗尽时,可以动态添加group。只需要增加一台或多台服务器,并将它们配置为一个新的group,
这样就扩大了存储系统的容量。

FastDFS两个角色:Tracker server和Storage server。Tracker server作为中心结点,其主要作用是负载均衡和调度。Tracker server在内存中记录分组和Storage server的状态等信息,不记录文件索引信息,占用的内存量很少。
另外,客户端(应用)和Storage server访问Tracker server时,Tracker server扫描内存中的分组和Storage server信息,然后给出应答。由此可以看出Tracker server非常轻量化,不会成为系统瓶颈。

FastDFS中的Storage server在其他文件系统中通常称作Trunk server或Data server。Storage server直接利用OS的文件系统存储文件。FastDFS不会对文件进行分块存储,客户端上传的文件和Storage server上的文件一一对应(FastDFS中的文件标识分为两个部分:组名和文件名,二者缺一不可)。

2014-04-24
发表者 Venus
暂无评论

centos6.5下搭建fastdfs分布式存储

fastDFS下载地址:http://code.google.com/p/fastdfs/downloads/list
libevent官网: http://libevent.org/

目前最新版:https://github.com/downloads/libevent/libevent/libevent-2.0.21-stable.tar.gz
目前最新版:http://fastdfs.googlecode.com/files/FastDFS_v4.06.tar.gz

系统环境:
fastdfs_tracker       centos6.5_64 ip:192.168.1.50
fastdfs_storage_s1 centos6.5_64 ip:192.168.1.51
fastdfs_storage_s2 centos6.5_64 ip:192.168.1.52
fastdfs_client           centos6.5_64 ip:192.168.1.54

selinux关闭,iptables关闭。

配置tracker:
先装libevent,后fastdfs

[root@centos-6.5-x64 ~]#yum install -y gcc gcc-c++
[root@centos-6.5-x64 ~]#tar zxvf libevent-2.0.21-stable
[root@centos-6.5-x64 ~]#tar zxvf FastDFS_v4.06.tar.gz
[root@centos-6.5-x64 ~]#cd libevent-2.0.21-stable
[root@centos-6.5-x64 libevent-2.0.21-stable]#./configure && make && make install
[root@centos-6.5-x64 libevent-2.0.21-stable]# cd
[root@centos-6.5-x64 ~]#cd FastDFS
[root@centos-6.5-x64 FastDFS]#sh make.sh && sh make.sh install

fastdfs的配置文件路径默认在 /etc/fdfs/
[root@centos-6.5-x64 ~]#cd /etc/fdfs/
[root@centos-6.5-x64 fdfs]#ll
总用量 56
-rw-r–r– 1 root root 1463 4月 23 04:10 client.conf
-rw-r–r– 1 root root 858 4月 23 04:10 http.conf
-rw-r–r– 1 root root 31172 4月 23 04:10 mime.types
-rw-r–r– 1 root root 7460 4月 23 04:10 storage.conf
-rw-r–r– 1 root root 6621 4月 23 04:10 tracker.conf

client.conf 客户端上传配置文件
storage.conf 文件存储服务器配置文件
tracker.conf 负责均衡调度服务器配置文件
http.conf http服务器配置文件(可忽略不用)

跟fastdfs相关的命令都在 /usr/local/bin/ 下

编辑tracker配置文件:
[root@centos-6.5-x64 fdfs]#grep -v “#” tracker.conf | grep -v “^$”

#tracker.conf 配置文件是否生效,false是生效,true是屏蔽。
disabled=false

#程序的监听地址,如果不设定则监听所有地址
bind_addr=

#tracker监听的端口
port=22122

#连接超时时间
connect_timeout=30

#tracker在通过网络发送接收数据的超时时间
network_timeout=60

#数据和日志的存放地点
base_path=/home/yuqing/fastdfs

#最大连接数
max_connections=256

#工作线程数一般为cpu个数
work_threads=4

#在存储文件时选择group的策略,0:轮训策略 1:指定某一个组 2:负载均衡,选择空闲空间最大的group.
store_lookup=2

#如果上面的store_lookup选择了1,则这里需要指定一个group
store_group=group2

#在group中的哪台storage做主storage,当一个文件上传到主storage后,就由这台机器同步文件到group内的其他storage上,0:轮训策略 1:根据ip地址排序,第一个 2:根据优先级排序,第一个
store_server=0

#选择文件上传到storage中的哪个(目录/挂载点),storage可以有多个存放文件的base path 0:轮训策略 2:负载均衡,选择空闲空间最大的
store_path=0

#选择那个storage作为主下载服务器,0:轮训策略 1:主上传storage作为主下载服务器
download_server=0

#系统预留空间,当一个group中的任何storage的剩余空间小于定义的值,整个group就不能上传文件了
reserved_storage_space = 10%

#日志信息级别
log_level=info

#进程以那个用户/用户组运行,不指定默认是当前用户
run_by_group=
run_by_user=

#允许那些机器连接tracker默认是所有机器
allow_hosts=*

#设置日志信息刷新到disk的频率,默认10s
sync_log_buff_interval = 10

#检测storage服务器的间隔时间,storage定期主动向tracker发送心跳,如果在指定的时间没收到信号,tracker人为storage故障,默认120s
check_active_interval = 120

#线程栈的大小,最小64K
thread_stack_size = 64KB

#storage的ip改变后服务端是否自动调整,storage进程重启时才自动调整
storage_ip_changed_auto_adjust = true

#storage之间同步文件的最大延迟,默认1天
storage_sync_file_max_delay = 86400

#同步一个文件所花费的最大时间
storage_sync_file_max_time = 300

#是否用一个trunk文件存储多个小文件
use_trunk_file = false

#最小的solt大小,应该小于4KB,默认256bytes
slot_min_size = 256

#最大的solt大小,如果上传的文件小于默认值,则上传文件被放入trunk文件中
slot_max_size = 16MB

#trunk文件的默认大小,应该大于4M
trunk_file_size = 64MB

trunk_create_file_advance = false
trunk_create_file_time_base = 02:00
trunk_create_file_interval = 86400
trunk_create_file_space_threshold = 20G
trunk_init_check_occupying = false
trunk_init_reload_from_binlog = false
use_storage_id = false
storage_ids_filename = storage_ids.conf
id_type_in_filename = ip
store_slave_file_use_link = false
rotate_error_log = false
error_log_rotate_time=00:00
rotate_error_log_size = 0
use_connection_pool = false
connection_pool_max_idle_time = 3600

#http服务是否生效,默认不生效,此项可能在4.0版本后剔除。不起作用且没有此行。
http.disabled=false

#http服务端口
http.server_port=8080

#检测storage上http服务的时间间隔,<=0表示不检测
http.check_alive_interval=30

#检测storage上http服务时所用请求的类型,tcp只检测是否可以连接,http必须返回200
http.check_alive_type=tcp

#通过url检测storage http服务状态
http.check_alive_uri=/status.html

#用include包含进http的其他设置,此项可能在4.0版本后剔除。不起作用。
##include http.conf

需要修改的地方是 base_path 改为自己的路径,这里改为:
base_path=/mnt/fastdfs_tracker

[root@centos-6.5-x64 fdfs]#mkdir /mnt/fastdfs_tracker
启动tracker:
[root@centos-6.5-x64 fdfs]#/usr/local/bin/fdfs_trackerd /etc/fdfs/tracker.conf
/usr/local/bin/fdfs_trackerd: error while loading shared libraries: libevent-2.0.so.5: cannot open shared object file: No such file or directory
[root@centos-6.5-x64 ~]#find / -name libevent-2.0.so.5
/root/libevent-2.0.21-stable/.libs/libevent-2.0.so.5
/usr/local/lib/libevent-2.0.so.5
[root@centos-6.5-x64 ~]#ln -s /usr/local/lib/libevent-2.0.so.5 /usr/lib64/libevent-2.0.so.5 #找不到库文件,搜索一下软链接过去即可。
[root@centos-6.5-x64 ~]#/usr/local/bin/fdfs_trackerd /etc/fdfs/tracker.conf
[root@centos-6.5-x64 ~]#
[root@centos-6.5-x64 ~]#lsof -i:22122 #端口启动
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
fdfs_trac 8287 root 5u IPv4 40633 0t0 TCP *:22122 (LISTEN)

配置storage_s1:

[root@centos-6.5-x64 ~]#tar zxvf libevent-2.0.21-stable
[root@centos-6.5-x64 ~]#tar zxvf FastDFS_v4.06.tar.gz
[root@centos-6.5-x64 ~]#cd libevent-2.0.21-stable
[root@centos-6.5-x64 libevent-2.0.21-stable]#./configure && make && make install
[root@centos-6.5-x64 libevent-2.0.21-stable]#cd
[root@centos-6.5-x64 ~]#cd FastDFS
[root@centos-6.5-x64 FastDFS]#sh make.sh && sh make.sh install
[root@centos-6.5-x64 FastDFS]#cd /etc/fdfs/
[root@centos-6.5-x64 fdfs]#ll
总用量 56
-rw-r--r-- 1 root root 1463 4月 23 04:52 client.conf
-rw-r--r-- 1 root root 858 4月 23 04:52 http.conf
-rw-r--r-- 1 root root 31172 4月 23 04:52 mime.types
-rw-r--r-- 1 root root 7460 4月 23 04:52 storage.conf
-rw-r--r-- 1 root root 6621 4月 23 04:52 tracker.conf

[root@centos-6.5-x64 fdfs]#grep -v “#” storage.conf | grep -v “^$”
#是否启用配置文件
disabled=false

#这个storage服务器属于那个group
group_name=group1

#监听地址,不写,监听全部
bind_addr=

#连接其他服务器时是否绑定地址,bind_addr配置时本参数才有效
client_bind=true

#storage默认端口
port=23000

#连接超时时间
connect_timeout=30

#storage在通过网络发送接收数据的超时时间
network_timeout=60

#主动向tracker发送心跳检测的时间间隔
heart_beat_interval=30

#主动向tracker发送磁盘使用率的时间间隔
stat_report_interval=60

#数据存储路径
base_path=/home/yuqing/fastdfs

#最大连接数
max_connections=256

#接收/发送数据的buff大小,必须大于8KB
buff_size = 256KB

#工作线程数一般为cpu个数
work_threads=4

#磁盘IO是否读写分离
disk_rw_separated = true

#混合读写时的读写线程数
disk_reader_threads = 1
disk_writer_threads = 1

#同步文件时如果binlog没有要同步的文件,则延迟多少毫秒后重新读取,0表示不延迟
sync_wait_msec=50

#同步完一个文件后间隔多少毫秒同步下一个文件,0表示不休息直接同步
sync_interval=0

#表示这段时间内同步文件
sync_start_time=00:00
sync_end_time=23:59

#同步完多少文件后写mark标记
write_mark_file_freq=500

#storage在存储文件时支持多路径,默认只设置一个
store_path_count=1

#配置多个store_path路径,从0开始,如果store_path0不存在,则base_path必须存在
store_path0=/home/yuqing/fastdfs

#subdir_count * subdir_count个目录会在store_path下创建,采用两级存储
subdir_count_per_path=256

#设置fastdfs tracker
tracker_server=192.168.209.121:22122

#日志级别
log_level=info

#运行时的用户和组,为空,为当前用户
run_by_group=
run_by_user=

#允许访问网段
allow_hosts=*

#文件在数据目录下的存放策略,0:轮训 1:随机
file_distribute_path_mode=0

#当问及是轮训存放时,一个目录下可存放的文件数目
file_distribute_rotate_count=100

#写入多少字节后就开始同步,0表示不同步
fsync_after_written_bytes=0

#刷新日志信息到disk的间隔
sync_log_buff_interval=10

sync_binlog_buff_interval=10

#同步storage的状态信息到disk的间隔
sync_stat_file_interval=300

#线程栈大小
thread_stack_size=512KB

#设置文件上传服务器的优先级,值越小越高
upload_priority=10

if_alias_prefix=

#是否检测文件重复存在,1:检测 0:不检测
check_file_duplicate=0
file_signature_method=hash

#当check_file_duplicate设置为1时,次值必须设置
key_namespace=FastDFS

#与FastDHT建立连接的方式 0:短连接 1:长连接
keep_alive=0
use_access_log = false
rotate_access_log = false
access_log_rotate_time=00:00
rotate_error_log = false
error_log_rotate_time=00:00
rotate_access_log_size = 0
rotate_error_log_size = 0
file_sync_skip_invalid_record=false
use_connection_pool = false
connection_pool_max_idle_time = 3600
http.domain_name=
http.server_port=8888

其中主要修改的地方为 base_path (用于存储storage相关的log、group内的相关信息) 和 store_path0 (文件的存储位置)
这里改为:
base_path=/mnt/fastdfs_storage_info
store_path0=/mnt/fastdfs_storage_data
tracker_server=192.168.1.50:22122 #tracker服务器地址

[root@centos-6.5-x64 fdfs]#mkdir /mnt/fastdfs_storage_info #创建文件夹
[root@centos-6.5-x64 fdfs]#mkdir /mnt/fastdfs_storage_data
启动服务:
[root@centos-6.5-x64 ~]#/usr/local/bin/fdfs_storaged /etc/fdfs/storage.conf
/usr/local/bin/fdfs_storaged: error while loading shared libraries: libevent-2.0.so.5: cannot open shared object file: No such file or directory
[root@centos-6.5-x64 ~]#ln -s /usr/local/lib/libevent-2.0.so.5 /usr/lib64/libevent-2.0.so.5
[root@centos-6.5-x64 ~]#/usr/local/bin/fdfs_storaged /etc/fdfs/storage.conf

会看到好多提示:
mkdir data path: FA …
mkdir data path: FB …
mkdir data path: FC …
mkdir data path: FD …
mkdir data path: FE …
mkdir data path: FF …
data path: /mnt/fastdfs_storage_data/data, mkdir sub dir done.
[root@centos-6.5-x64 ~]#lsof -i:23000 #默认占用23000端口
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
fdfs_stor 10922 root 5u IPv4 86805 0t0 TCP *:inovaport1 (LISTEN)

配置storage_s2:
[root@centos-6.5-x64 ~]#tar zxvf libevent-2.0.21-stable
[root@centos-6.5-x64 ~]#tar zxvf FastDFS_v4.06.tar.gz
[root@centos-6.5-x64 ~]#cd libevent-2.0.21-stable
[root@centos-6.5-x64 libevent-2.0.21-stable]#./configure && make && make install
[root@centos-6.5-x64 libevent-2.0.21-stable]#cd
[root@centos-6.5-x64 ~]#cd FastDFS
[root@centos-6.5-x64 FastDFS]#sh make.sh && sh make.sh install
[root@centos-6.5-x64 FastDFS]#cd /etc/fdfs/
[root@centos-6.5-x64 fdfs]#mkdir /mnt/fastdfs_storage_info
[root@centos-6.5-x64 fdfs]#mkdir /mnt/fastdfs_storage_data
[root@centos-6.5-x64 fdfs]#vi storage.conf
修改:
base_path=/mnt/fastdfs_storage_info
store_path0=/mnt/fastdfs_storage_data
tracker_server=192.168.1.50:22122
[root@centos-6.5-x64 fdfs]#ln -s /usr/local/lib/libevent-2.0.so.5 /usr/lib64/libevent-2.0.so.5
[root@centos-6.5-x64 fdfs]#/usr/local/bin/fdfs_storaged /etc/fdfs/storage.conf
[root@centos-6.5-x64 fdfs]#lsof -i:23000
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
fdfs_stor 8277 root 5u IPv4 83248 0t0 TCP *:inovaport1 (LISTEN)

服务已经启动。
[root@centos-6.5-x64 fdfs]#netstat -anlp | grep ESTABLISHED
tcp 0 0 192.168.1.52:37220 192.168.1.50:22122 ESTABLISHED 8277/fdfs_storaged
tcp 0 0 192.168.1.52:23000 192.168.1.51:40161 ESTABLISHED 8277/fdfs_storaged
可以看到storage服务器和tracker服务器(端口22122,管理)建立了会话,与另一台storage(端口23000,数据同步)建立了会话。

安装fastdfs_client:

[root@centos-6.5-x64 ~]#tar zxvf libevent-2.0.21-stable.tar.gz
[root@centos-6.5-x64 libevent-2.0.21-stable]#./configure --prefix=/usr && make && make install
[root@centos-6.5-x64 ~]#tar zxvf FastDFS_v4.06.tar.gz
[root@centos-6.5-x64 ~]#cd FastDFS
[root@centos-6.5-x64 FastDFS]#sh make.sh && sh make.sh install

编辑fastdfs_client的配置文件:
[root@centos-6.5-x64 ~]#vi /etc/fdfs/client.conf
修改:
base_path=/mnt/fastdfs_client
tracker_server=192.168.1.50:22122

[root@centos-6.5-x64 ~]#mkdir /mnt/fastdfs_client

在fastdfs_client上上传文件测试:
[root@centos-6.5-x64 ~]#echo “that’s fastdfs_client” > index.html
[[root@centos-6.5-x64 ~]#/usr/local/bin/fdfs_upload_file /etc/fdfs/client.conf index.html
group1/M00/00/00/wKgBM1NW5JmAO_BCAAAAFhSwOfU49.html
这个是在storage服务器下的物理路径。

在storage_s1服务器查看:
[root@centos-6.5-x64 ~]#cd /mnt/fastdfs_storage_data/data/00/00
[root@centos-6.5-x64 00]#ll
总用量 20
-rw-r–r– 1 root root 22 4月 23 05:52 wKgBM1NW5JmAO_BCAAAAFhSwOfU49.html
在storage_s2服务器此目录下也会有这个文件,表明上传文件没问题。

现在我们需要通过web方式去访问这个文件。

在每台storage server上部署web server,直接对外提供HTTP服务,tracker server上不需要部署web server。
如果请求文件在当前storage上不存在,通过文件ID反解出源storage,直接请求源storage。

我这里用apache来实现访问fastdfs的文件,并且apache为yum安装。想达到访问目的需要在apache中加载fastdfs-apache-module模块。
fastdfs-apache-module模块默认不在apache中,需要自己手动编译,加载。

在storage_s1和storage_s2中都进行如下配置:

[root@centos-6.5-x64 ~]#wget -c http://fastdfs.googlecode.com/files/fastdfs-apache-module_v1.15.tar.gz
在编译fastdfs-apache-module时需要apache的某些源文件,需要安装httpd-devel开发工具包。
[root@centos-6.5-x64 ~]#yum install -y httpd-devel
[root@centos-6.5-x64 ~]#tar zxvf fastdfs-apache-module_v1.15.tar.gz
[root@centos-6.5-x64 ~]#cd fastdfs-apache-module
[root@centos-6.5-x64 fastdfs-apache-module]#ll
总用量 12
-rw-rw-r-- 1 root root 1726 1月 13 2013 HISTORY
-rw-rw-r-- 1 root root 1725 5月 22 2011 INSTALL
drwxrwxr-x 2 root root 4096 2月 19 2013 src
[root@centos-6.5-x64 fastdfs-apache-module]#cd src/
[root@centos-6.5-x64 src]#ll
总用量 56
-rw-rw-r-- 1 root root 32266 1月 13 2013 common.c
-rw-rw-r-- 1 root root 3479 1月 3 2012 common.h
-rw-rw-r-- 1 root root 1350 11月 4 2010 Makefile
-rw-rw-r-- 1 root root 5039 1月 3 2012 mod_fastdfs.c
-rw-rw-r-- 1 root root 3869 1月 13 2013 mod_fastdfs.conf
-rw-rw-r-- 1 root root 160 9月 5 2010 modules.mk

这里需要修改Makefile文件:
APACHE_BASE_PATH=/usr/local/apache2
include $(APACHE_BASE_PATH)/build/special.mk
APXS=$(APACHE_BASE_PATH)/bin/apxs
APACHECTL=$(APACHE_BASE_PATH)/bin/apachectl

改为:
APACHE_BASE_PATH=/etc/httpd #apache安装根目录,根据实际情况修改
include /usr/lib64/httpd/build/special.mk
APXS=/usr/sbin/apxs
APACHECTL=/usr/sbin/apachectl

special.mk文件路径可以通过find查找,apxs和apachectl可通过whereis查找。

[root@centos-6.5-x64 src]#make
/usr/lib64/httpd/build/special.mk:27: /etc/httpd/build/rules.mk: 没有那个文件或目录
make: *** 没有规则可以创建目标“/etc/httpd/build/rules.mk”。 停止。
[root@centos-6.5-x64 src]#

报错解决方法:

[root@centos-6.5-x64 src]#mkdir /etc/httpd/build
[root@centos-6.5-x64 src]#cp /usr/lib64/httpd/build/*.mk /etc/httpd/build/
[root@centos-6.5-x64 src]#make
/usr/lib64/apr-1/build/libtool –silent –mode=compile gcc -pthread -Wall -D_FILE_OFFSET_BITS=64 -DFDFS_OUTPUT_CHUNK_SIZE=’256*1024′ -DFDFS_MOD_CONF_FILENAME='”/etc/fdfs/mod_fastdfs.conf”‘ -DLINUX=2 -D_REENTRANT -D_GNU_SOURCE -I/usr/local/include/fastdfs -I/usr/local/include/fastcommon/ -I/usr/include/httpd -I. -I/usr/include/apr-1 -prefer-pic -c mod_fastdfs.c && touch mod_fastdfs.slo
/usr/lib64/apr-1/build/libtool –silent –mode=link gcc -pthread -Wall -Wl,-z,relro -lfastcommon -lfdfsclient -o mod_fastdfs.la -rpath /usr/lib64/httpd/modules -module -avoid-version mod_fastdfs.lo
make通过,然后make install

[root@centos-6.5-x64 src]#make install
/bin/sh: line 1: /etc/httpd/build/instdso.sh: 没有那个文件或目录
make: *** [install-modules-yes] 错误 127
[root@centos-6.5-x64 src]#

报错解决方法:

[root@centos-6.5-x64 src]#cp /usr/lib64/httpd/build/instdso.sh /etc/httpd/build/
[root@centos-6.5-x64 src]#make install

通过。

[root@centos-6.5-x64 src]#cd /etc/httpd/modules/
[root@centos-6.5-x64 modules]#ll mod_fastdfs.so
-rwxr-xr-x 1 root root 37564 4月 22 21:34 mod_fastdfs.so

mod_fastdfs.so模块编译完成。

编辑mod_fastdfs模块的配置文件:
[root@centos-6.5-x64 ~]#vi /etc/fdfs/mod_fastdfs.conf
tracker_server=192.168.1.50:22122 #修改为tracker server信息
url_have_group_name = true #改为true
store_path0=/mnt/fastdfs_storage_data #改为数据存储路径,和storage.conf一样。

编辑apache主配置文件:
[root@centos-6.5-x64 ~]#vi /etc/httpd/conf/httpd.conf
添加两行:
LoadModule fastdfs_module modules/mod_fastdfs.so #加载模块
alias /group1/M00 /mnt/fastdfs_storage_data/data #虚拟目录映射

<Location /group1/M00>    #访问/group1/M00时交给fastdfs模块处理
sethandler fastdfs
</Location>

重启httpd服务加到开机启动。
现在通过浏览器去访问storage服务器:
http://192.168.1.51/group1/M00/00/00/wKgBM1NW5JmAO_BCAAAAFhSwOfU49.html
会看到通过echo重定向的内容。

再去配置storage_s2,过程跟s1一样,完成后把ip改为192.168.1.52,后面内容不变,一样可以访问到文件。

现在我们停掉storage_s2的storaged服务,删掉此文件 再去访问http://192.168.1.52/group1/M00/00/00/wKgBM1NW5JmAO_BCAAAAFhSwOfU49.html
发现依旧可以访问到文件,这就是fastdfs模块起到了作用。这个功能就是防止因为数据同步延迟导致无法访问。而去源storage服务器查找文件。

到这里配置接近尾声,now,我把fastdfs的tracker和storage管理脚本加到系统中,方便用service管理。
进入fastdfs源码包:
[root@centos-6.5-x64 ~]#cd FastDFS/init.d/
[root@centos-6.5-x64 init.d]#ll
总用量 8
-rwxrwxr-x 1 500 500 1186 8月 15 2010 fdfs_storaged
-rwxrwxr-x 1 500 500 1186 8月 15 2010 fdfs_trackerd
不用说也能看明白。一个storaged脚本一个trackerd脚本。无需修改即可使用。
把各自脚本拷贝到/etc/init.d/下即可。然后就可以通过service fdfs_storaged/fdfs_trackerd start/stop/status管理了。

再回到我们的fastdfs_client客户端,为php添加模块:
因为fastdfs是基于应用的分布式存储,不像moosefs可以挂载为本地磁盘。fastdfs提供一个api接口实现文件操作。
所以要安装php扩展让Fastdfs支持php,在FastDFS的源码包解压后里面有个php_client目录,进入此目录,参照README进行安装:
安装此模块需要phpize命令,此命令由php-devel包提供,没有的话安装即可。
[root@centos-6.5-x64 php_client]#phpize
Configuring for:
PHP Api Version: 20090626
Zend Module Api No: 20090626
Zend Extension Api No: 220090626
[root@centos-6.5-x64 php_client]#./configure && make && make install
Installing shared extensions: /usr/lib64/php/modules/ #提示模块安装到的路径。
重定向配置文件到/etc/php.ini:
[root@centos-6.5-x64 php_client]#cat fastdfs_client.ini >> /etc/php.ini
重启apache。
[root@centos-6.5-x64 ~]#php -m | grep fast
fastdfs_client
看到php加载模块成功。

测试:
创建一个php文件,代码如下:

[root@centos-6.5-x64 html]#cat test.php
<?php
$file_info=fastdfs_get_file_info1('group1/M00/00/00/wKgBM1NW5JmAO_BCAAAAFhSwOfU49.html');
var_dump($file_info);
?>

然后执行:

[root@centos-6.5-x64 html]#php test.php
array(5) {
 ["source_id"]=>
 int(0)
 ["create_timestamp"]=>
 int(1398203545)
 ["file_size"]=>
 int(22)
 ["source_ip_addr"]=>
 string(12) "192.168.1.51"
 ["crc32"]=>
 int(347093493)
}

得到文件信息。完成!

ps:
上面重定向fastdfs_client.ini信息到php.ini,作者说需要将其拷贝到跟php.ini同一个目录,并在php.ini配置模块目录路径(extension_dir = ),结果重启apache发现模块没有加载。
所以就直接将fastdfs_client.ini内容信息全部追加到php.ini中,模块才被加载。

有关fastdfs的原理:http://os.51cto.com/art/201210/359380.htm