由于网站使用nfs共享方式保存用户上传的图片,附件等资料,然后通过apache下载的方式供用户访问,在网站架构初期,使用这 种简单的方式实现了静态资源的读写分离,但随着网站数据量的增加,图片服务器渐渐成为整个网站的短板,缘次催生了使用fastfds的想法,故而先进行一 番简单的测试!在开始之前还是先来看看fastfds的介绍信息:
fastdfs是一个开源的,高性能的的分布式文件系统,他主要的功能包括:文件存储,同步和访问,设计基于高可用和负载均衡,fastfd非常适用于基于文件服务的站点,例如图片分享和视频分享网站
fastfds有两个角色:跟踪服务和存储服务,跟踪服务控制,调度文件以负载均衡的方式访问;存储服务包括:文件存储,文件同步,提供文件访问接口,同时以key value的方式管理文件的元数据
跟踪和存储服务可以由1台或者多台服务器组成,同时可以动态的添加,删除跟踪和存储服务而不会对在线的服务产生影响,在集群中,tracker服务是对等的
存储系统由一个或多个卷组成,卷与卷之间的文件是相互独立的,所有卷的文件容量累加就是整个存储系统中的文件容量。一个卷可以由一台或多台存储服务 器组成,一个卷下的存储服务器中的文件都是相同的,卷中的多台存储服务器起到了冗余备份和负载均衡的作用。在卷中增加服务器时,同步已有的文件由系统自动 完成,同步完成后,系统自动将新增服务器切换到线上提供服务。当存储空间不足或即将耗尽时,可以动态添加卷。只需要增加一台或多台服务器,并将它们配置为 一个新的卷,这样就扩大了存储系统的容量。
下面几张图可以清楚的说明fastfds的架构和文件上传和下载流程等:
下面将介绍下fastdfs在rhel上的部署过程
192.168.0.205 storage tracker
1, 安装依赖包,添加fastDFS运行用户
yum install -y zlib zlib-devel pcre pcre-devel gcc gcc-c++ openssl openssl-devel libevent libevent-devel perl unzip
useradd -s /sbin/nologin fastdfs
2,创建数据存储目录
mkdir -p /data/fastdfs/{storage,tracker,client}
[root@localhost src]# ll /data/fastdfs
total 0
drwxr-xr-x 2 root root 6 Dec 10 15:18 storage #Storage目录保存运行日志及其data数据
drwxr-xr-x 2 root root 6 Dec 10 15:17 tracker #tracker目录保存运行日志
drwxr-xr-x 2 root root 6 Dec 10 15:17 client
3,安装libfastcommon
下载最新版本: libfastcommon
wget https://github.com/happyfish100/libfastcommon/archive/master.zip
unzip master.zip
cd libfastcommon-master/
./make.sh
./make.sh install
4, 安装Fastdfs
wget http://sourceforge.net/projects/fastdfs/files/FastDFS%20Server%20Source%20Code/FastDFS%20Server%20with%20PHP%20Extension%20Source%20Code%20V5.05/FastDFS_v5.05.tar.gz/download
tar zxf FastDFS_v5.05.tar.gz && cd FastDFS
./make.sh
./make.sh install
\cp conf/*.conf /etc/fdfs/
tar zxf fastdfs-nginx-module_v1.16.tar.gz
cp fastdfs-nginx-module/src/mod_fastdfs.conf /etc/fdfs/
cd /etc/fdfs/
rm -rf *.sample
chown -R fastdfs: /data/fastdfs (不用改权限貌似也可以)
5, 配置tracker 和 storage 配置文件, 对应修改
vi storage.conf
group_name=group1
base_path=/data/fastdfs/storage
store_path0=/data/fastdfs/storage
tracker_server=192.168.0.205:22122
http.server_port=80
vi tracker.conf
base_path=/data/fastdfs/tracker
vi mod_fastdfs.conf
group_name=group1
base_path=/data/fastdfs/storage
store_path0=/data/fastdfs/storage
tracker_server=192.168.0.205:22122
url_have_group_name = true #是true 不是ture
vi client.conf
tracker_server=192.168.0.205:22122
base_path=/data/fastdfs/client
6,安装nginx和fastdfs-nginx-module模块
wget http://nginx.org/download/nginx-1.8.0.tar.gz
http://sourceforge.net/projects/fastdfs/files/FastDFS%20Nginx%20Module%20Source%20Code/fastdfs-nginx-module_v1.16.tar.gz/download
tar zxf nginx-1.8.0.tar.gz
ulimit -SHn 102400
useradd -s /sbin/nologin nginx
#修改模块中对应的路径,要不然模块不能正确安装加载
cd fastdfs-nginx-module/src
vi config #更改如下, 去掉local,并指定lib64(64系统)
CORE_INCS="$CORE_INCS /usr/include/fastdfs /usr/include/fastcommon/"
CORE_LIBS="$CORE_LIBS -L/usr/lib64 -lfastcommon -lfdfsclient"
cd /tools/nginx-1.8.0
./configure
--prefix=/usr/local/nginx \
--sbin-path=/usr/sbin/nginx \
--conf-path=/etc/nginx/nginx.conf \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--pid-path=/var/run/nginx/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--user=nginx \
--group=nginx \
--with-http_ssl_module \
--with-http_flv_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--with-pcre \
--with-http_realip_module \
--http-client-body-temp-path=/var/tmp/nginx/client/ \
--http-proxy-temp-path=/var/tmp/nginx/proxy/ \
--http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ \
--http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \
--http-scgi-temp-path=/var/tmp/nginx/scgi \
--add-module=/tools/fastdfs-nginx-module/src/ \
make
make install
7, 配置nginx
server {
listen 80;
server_name 192.168.0.205;
location /group1/M00 {
root /data/fastdfs/storage/data/;
ngx_fastdfs_module;
}
}
8,启动nginx和fastdfs
[root@test-1 vhost]# /etc/init.d/fdfs_storaged start
Starting FastDFS storage server:
[root@test-1 vhost]# /etc/init.d/fdfs_trackerd start
Starting FastDFS tracker server:
[root@test-1 vhost]#
[root@test-1 vhost]# /etc/init.d/nginx -t
ngx_http_fastdfs_set pid=8985
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@test-1 vhost]# /etc/init.d/nginx -s reload
ngx_http_fastdfs_set pid=8986
root@test-1 vhost]# netstat -npl |grep -E "nginx|fdfs"
tcp 0 0 0.0.0.0:22122 0.0.0.0:* LISTEN 8972/fdfs_trackerd
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 7561/nginx
tcp 0 0 0.0.0.0:23000 0.0.0.0:* LISTEN 8679/fdfs_storaged
也可以以下命令来监控服务器的状态
[root@tracker ~]# fdfs_monitor /etc/fdfs/client.conf
9,测试
[root@test-1 ~]# fdfs_test /etc/fdfs/client.conf upload test.html
This is FastDFS client test program v5.05
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.
[2015-06-14 02:46:06] DEBUG - base_path=/tmp, 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.0.205, port=23000
server 2. group_name=, ip_addr=192.168.0.206, port=23000
group_name=group1, ip_addr=192.168.0.206, port=23000
storage_upload_by_filename
group_name=group1, remote_filename=M00/00/00/wKgAzlV8em6Af8qBAAAADxtaRO466.html
source ip address: 192.168.0.206
file timestamp=2015-06-14 02:46:06
file size=15
file crc32=458900718
example file url: http://192.168.0.206/group1/M00/00/00/wKgAzlV8em6Af8qBAAAADxtaRO466.html
storage_upload_slave_by_filename
group_name=group1, remote_filename=M00/00/00/wKgAzlV8em6Af8qBAAAADxtaRO466_big.html
source ip address: 192.168.0.206
file timestamp=2015-06-14 02:46:06
file size=15
file crc32=458900718
example file url: http://192.168.0.206/group1/M00/00/00/wKgAzlV8em6Af8qBAAAADxtaRO466_big.html
Nginx配置缩略图与Fastdfs模块整合
nginx增加图片裁剪模块
--with-http_image_filter_module
vim /etc/nginx/conf.d/fastdfd.conf
location ~ group1/M00/(.+)_([0-9]+)x([0-9]+)\.(jpg|gif|png) {
root /home/fastdata/data;
ngx_fastdfs_module;
set $w $2;
set $h $3;
if ($w != "0") {
rewrite group1/M00(.+)_(\d+)x(\d+)\.(jpg|gif|png)$ group1/M00$1.$4 break;
}
image_filter resize $w $h;
image_filter_buffer 2M;
}
location ~ group1/M00/(.+)\.?(.+){
root /home/fastdata/data;
ngx_fastdfs_module;
}
原图访问
http://img3.jiupaicn.com/group1/M00/00/00/rBASWFeEibOAGRHwAADl6SrNlvQ206.jpg
缩略图访问
http://img3.jiupaicn.com/group1/M00/00/00/rBASWFeEibOAGRHwAADl6SrNlvQ206_500x150.jpg