FastDFS存儲-單機配置

因爲網站使用nfs共享方式保存用戶上傳的圖片,附件等資料,而後經過apache下載的方式供用戶訪問,在網站架構初期,使用這 種簡單的方式實現了靜態資源的讀寫分離,但隨着網站數據量的增長,圖片服務器漸漸成爲整個網站的短板,緣次催生了使用fastfds的想法,故而先進行一 番簡單的測試!在開始以前仍是先來看看fastfds的介紹信息:

fastdfs是一個開源的,高性能的的分佈式文件系統,他主要的功能包括:文件存儲,同步和訪問,設計基於高可用和負載均衡,fastfd很是適用於基於文件服務的站點,例如圖片分享和視頻分享網站
fastfds有兩個角色:跟蹤服務和存儲服務,跟蹤服務控制,調度文件以負載均衡的方式訪問;存儲服務包括:文件存儲,文件同步,提供文件訪問接口,同時以key value的方式管理文件的元數據
跟蹤和存儲服務能夠由1臺或者多臺服務器組成,同時能夠動態的添加,刪除跟蹤和存儲服務而不會對在線的服務產生影響,在集羣中,tracker服務是對等的
存儲系統由一個或多個卷組成,卷與卷之間的文件是相互獨立的,全部卷的文件容量累加就是整個存儲系統中的文件容量。一個卷能夠由一臺或多臺存儲服務 器組成,一個卷下的存儲服務器中的文件都是相同的,卷中的多臺存儲服務器起到了冗餘備份和負載均衡的做用。在卷中增長服務器時,同步已有的文件由系統自動 完成,同步完成後,系統自動將新增服務器切換到線上提供服務。當存儲空間不足或即將耗盡時,能夠動態添加捲。只須要增長一臺或多臺服務器,並將它們配置爲 一個新的卷,這樣就擴大了存儲系統的容量。
下面幾張圖能夠清楚的說明fastfds的架構和文件上傳和下載流程等:html

下面將介紹下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://www.javashuo.com/tag/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.jpgnginx

相關文章
相關標籤/搜索