转载请出自出处:http://www.cnblogs.com/hd3013779515/html
1.FastDFS简单介绍nginx
FastDFS是由淘宝的余庆先生所开发,是一个轻量级、高性能的开源分布式文件系统,用纯C语言开发,包括文件存储、文件同步、文件访问(上传、下载)、存取负载均衡、在线扩容、相同内容只存储一份等功能,适合有大容量存储需求的应用或系统。作分布式系统开发时,其中要解决的一个问题就是图片、音视频、文件共享的问题,分布式文件系统正好能够解决这个需求。同类的分布式文件系统有谷歌的GFS、HDFS(Hadoop)、TFS(淘宝)等。c++
源码开放下载地址:https://github.com/happyfish100
早期源码开放下载地址:https://sourceforge.net/projects/fastdfs/files/
官网论坛:http://bbs.chinaunix.net/forum-240-1.htmlgit
一、client询问tracker上传到的storage,不须要附加参数;
二、tracker返回一台可用的storage;
三、client直接和storage通信完成文件上传。github
一、client询问tracker下载文件的storage,参数为文件标识(组名和文件名);
二、tracker返回一台可用的storage;
三、client直接和storage通信完成文件下载。shell
FastDFS两个主要的角色:Tracker Server 和 Storage Server
Tracker Server:跟踪服务器,主要负责调度storage节点与client通讯,在访问上起负载均衡的做用,和记录storage节点的运行状态,是链接client和storage节点的枢纽。
Storage Server:存储服务器,保存文件和文件的meta data(元数据)
Group:文件组,也能够称为卷。同组内服务器上的文件是彻底相同的,作集群时每每一个组会有多台服务器,上传一个文件到同组内的一台机器上后,FastDFS会将该文件即时同步到同组内的其它全部机器上,起到备份的做用。
meta data:文件相关属性,键值对(Key Value Pair)方式,如:width=1024, height=768。和阿里云OSS的meta data类似。vim
2.FastDFS集群环境安装后端
2.1集群规划浏览器
跟踪服务器负载均衡节点1:192.168.137.160 dfs-nginx-proxy-1
跟踪服务器负载均衡节点2:192.168.137.161 dfs-nginx-proxy-2
跟踪服务器1:192.168.137.162 dfs-tracker-1
跟踪服务器2:192.168.137.163 dfs-tracker-2
存储服务器1:192.168.137.164 dfs-storage-group1-1
存储服务器2:192.168.137.165 dfs-storage-group1-2
存储服务器3:192.168.137.166 dfs-storage-group2-1
存储服务器4:192.168.137.167 dfs-storage-group2-2
HA虚拟IP:192.168.137.170
HA软件:Keepalived
操做系统:CentOS 6.8
用户:root
数据目录:/fastdfs缓存
2.2安装包
FastDFS_v5.05.tar.gz:FastDFS源码
libfastcommon-master.zip:(从 FastDFS 和 FastDHT 中提取出来的公共 C 函数库)
fastdfs-nginx-module_v1.16.tar.gz:storage节点http服务nginx模块
nginx-1.6.2.tar.gz:Nginx安装包
ngx_cache_purge-2.3.tar.gz:Nginx图片缓存清除模块
keepalived-1.2.18.tar.gz:高可用
能够从百度网盘下载。
连接:
http://pan.baidu.com/s/1jHIwtsq 密码:n757
外部统一访问192.168.137.170这个虚拟IP,来访问集群中各节点的文件。
在全部跟踪服务器和存储服务器节点上执行如下操做,即:162 ~ 167节点
shellyuminstallmakecmakegccgcc-c++>
shell> cd /home/test
shell> unzip libfastcommon-master.zip -d /usr/local/fast/
shell> cd /usr/local/fast/libfastcommon-master/
## 编译、安装
shell> ./make.sh
shell> ./make.sh install
FastDFS主程序设置的目录为/usr/local/lib/,因此须要建立/usr/lib64下的一些核心执行程序的软链接文件。
shell> mkdir /usr/local/lib/
shell> ln -s /usr/lib64/libfastcommon.so /usr/local/lib/libfastcommon.so
shell> ln -s /usr/lib64/libfastcommon.so /usr/lib/libfastcommon.so
shell> ln -s /usr/lib64/libfdfsclient.so /usr/local/lib/libfdfsclient.so
shell> ln -s /usr/lib64/libfdfsclient.so /usr/lib/libfdfsclient.so
shell> cd /home/test
shell> tar -zxvf FastDFS_v5.05.tar.gz -C /usr/local/fast
shell> cd /usr/local/fast/FastDFS/
## 编译、安装
shell> ./make.sh
shell> ./make.sh install
由于FastDFS服务脚本设置的bin目录为/usr/local/bin/下,可是实际安装在了/usr/bin/下面。因此须要修改配置文件的路径,也就是修改下面两个文件。
shell> vim /etc/init.d/fdfs_storaged
进行全局替换命令:%s+/usr/local/bin+/usr/bin
shell> vim /etc/init.d/fdfs_trackerd
进行全局替换命令:%s+/usr/local/bin+/usr/bin
说明:每一个节点执行相同的操做
cp.conf.sample.confshell>/etc/fdfs/tracker/etc/fdfs/tracker
shell> vim /etc/fdfs/tracker.conf
# 修改的内容以下:
disabled=false # 启用配置文件
port=22122 # tracker服务器端口(默认22122)
base_path=/fastdfs/tracker # 存储日志和数据的根目录
其它参数保留默认配置, 具体配置解释可参考官方文档说明:http://bbs.chinaunix.net/thread-1941456-1-1.html
shell> mkdir -p /fastdfs/tracker
shell> vi /etc/sysconfig/iptables
添加以下端口行:-A INPUT -m state --state NEW -m tcp -p tcp --dport 22122 -j ACCEPT
重启防火墙:shell> service iptables restart
/etc/initshell>.d/fdfs_trackerd start
初次启动,会在/fastdfs/tracker目录下生成logs、data两个目录:
检查FastDFS Tracker Server是否启动成功:ps -ef | grep fdfs_trackerd
/etc/initshell>.d/fdfs_trackerd stop
shellon> chkconfig fdfs_trackerd
group1: 192.168.137.164,192.168.137.165
group2: 192.168.137.166,192.168.137.167
说明:每一个节点执行相同的操做
cp.conf.sample.confshell>/etc/fdfs/storage/etc/fdfs/storage
shell> vi /etc/fdfs/storage.conf
# 修改的内容以下:
disabled=false # 启用配置文件
port=23000 # storage服务端口
group_name=group1 # 组名(第一组为group1,第二组为group2,依次类推...)
base_path=/fastdfs/storage # 数据和日志文件存储根目录
store_path0=/fastdfs/storage # 第一个存储目录,第二个存储目录起名为:store_path1=xxx,其它存储目录名依次类推...
store_path_count=1 # 存储路径个数,须要和store_path个数匹配
tracker_server=192.168.137.162:22122 # tracker服务器IP和端口
tracker_server=192.168.137.163:22122 # tracker服务器IP和端口
http.server_port=8888 # http访问文件的端口
其它参数保留默认配置, 具体配置解释可参考官方文档说明:http://bbs.chinaunix.net/thread-1941456-1-1.html
shell> mkdir -p /fastdfs/storage
shell> vi /etc/sysconfig/iptables
添加以下端口行:-A INPUT -m state --state NEW -m tcp -p tcp --dport 23000 -j ACCEPT
重启防火墙:shell> service iptables restart
/etc/initshell>.d/fdfs_storaged start
初次启动,会在/fastdfs/storage目录下生成logs、data两个目录。
检查FastDFS Tracker Server是否启动成功:ps -ef | grep fdfs_storaged
各节点启动后,使用tail -f /fastdfs/storage/logs/storaged.log
命令监听存储节点的日志,能够看到存储节点连接到跟踪服务器,并提示哪个为leader跟踪服务器,同时也能看到同一组中其它节点加入进来的日志信息。
全部存储节点都启动以后,能够在任一存储节点上使用以下命令查看集群的状态信息:
/usr/bin/fdfs_monitor /etc/fdfs/storageshell>.conf
/etc/initshell>.d/fdfs_storaged stop
shellon> chkconfig fdfs_storaged
cp.conf.sample.confshell>/etc/fdfs/client/etc/fdfs/client
.confshell> vi /etc/fdfs/client
base_path=/fastdfs/tracker
tracker_server=192.168.137.162:22122
tracker_server=192.168.137.163:22122
/usr/bin/fdfs_upload_file /etc/fdfs/clientshell>.conf /home/test/FastDFS_v5.05.tar.gz
返回如下ID号,说明文件上传成功:
group1/M00/00/00/wKiJpFkS0ByATie5AAVFOL7FJU4.tar.gz
group2/M00/00/00/wKiJplkS0CyAJyzTAAVFOL7FJU4.tar.gz
(从返回的ID号中也能够看出,同一个文件分别存储在两个组内group1和group2,但也有可能在同一组中,具体策略是由FastDFS根据服务器的存储状况来分配的)
说明:每一个节点执行相同的操做
FastDFS 经过 Tracker 服务器,将文件放在 Storage 服务器存储,可是同组存储服务器之间须要进入文件复制流程,有同步延迟的问题。假设 Tracker 服务器将文件上传到了 192.168.137.164,上传成功后文件 ID已经返回给客户端。此时 FastDFS 存储集群机制会将这个文件同步到同组存储 192.168.137.165,在文件尚未复制完成的状况下,客户端若是用这个文件 ID 在 192.168.137.165上取文件,就会出现文件没法访问的错误。而 fastdfs-nginx-module 能够重定向文件链接到源服务器(192.168.137.164)上取文件,避免客户端因为复制延迟致使的文件没法访问错误。
安装fastdfs-nginx-module_v1.16.tar.gz
shell> cd /home/test
shell> tar -zxvf fastdfs-nginx-module_v1.16.tar.gz -C /usr/local/fast/
shell/usr/local/fast/fastdfs-nginx-module/src/> cd
shell/usr/local/fast/fastdfs-nginx-module/src/config> vim
CORE_INCS="$CORE_INCS /usr/local/include/fastdfs /usr/local/include/fastcommon/"
改成
CORE_INCS="$CORE_INCS /usr/include/fastdfs /usr/include/fastcommon/"
## 安装nginx所需的依赖包
shell> yum install gcc gcc-c++ make automake autoconf libtool pcre pcre-devel zlib zlib-devel openssl openssl-devel
## 编译安装nginx(添加fastdfs-nginx-module模块)
shell> cd /home/test
shell> tar -zxvf nginx-1.6.2.tar.gz -C /usr/local/
shell1.6.2/> cd /usr/local/nginx-
shell--add-module=/usr/local/fast/fastdfs-nginx-module/src/> ./configure
shell> make && make install
shelllocal> cp /usr//fast/fastdfs-nginx-module/src/mod_fastdfs.conf /etc/fdfs/
shell> vi /etc/fdfs/mod_fastdfs.conf
connect_timeout=10
base_path=/tmp
tracker_server=192.168.137.162:22122
tracker_server=192.168.137.163:22122
storage_server_port=23000
group_name=group1 # 第一组storage的组名
url_have_group_name=true
store_path0=/fastdfs/storage
group_count=2
[group1]
group_name=group1
storage_server_port=23000
store_path_count=1
store_path0=/fastdfs/storage
[group2]
group_name=group2
storage_server_port=23000
store_path_count=1
store_path0=/fastdfs/storage
第二组的mod_fastdfs.confg配置与第一组的配置只有group_name不一样:
group_name=group2
shelllocal> cd /usr//fast/FastDFS/conf
shellhttp> cp.conf mime.types /etc/fdfs/
shelldata/ /fastdfs/storage/data/M00> ln -s /fastdfs/storage/
.confshell> vi /usr/local/nginx/conf/nginx
user nobody;
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 8888;
server_name localhost;
# FastDFS 文件访问配置(fastdfs-nginx-module模块)
location ~/group([0-9])/M00 {
ngx_fastdfs_module;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
注意:
A、8888 端口值要与/etc/fdfs/storage.conf 中的 http.server_port=8888 相对应,由于 http.server_port 默认为 8888,若是想改为 80,则要对应修改过来。
B、Storage 对应有多个 group 的状况下,访问路径带 group 名,如:http://xxxx/group1/M00/00/00/xxx, 对应的 Nginx 配置为:
group09location ~/([-])/M00 {
ngx_fastdfs_module;
}
C、以下载时如发现老报 404,将nginx.conf第一行user nobody;
修改成user root;
后从新启动。
shell> vi /etc/sysconfig/iptables
## 添加
--state NEW -m tcp -p tcp --dport 8888 -j ACCEPT-A INPUT -m state
## 重启防火墙
shell> service iptables restart
/usr/local/nginx/sbin/nginxshell>
# fastdfs-nginx-module进程IDngx_http_fastdfs_set pid=xxx
重启 Nginx 的命令为:/usr/local/nginx/sbin/nginx -s reload
设置Nginx开机启动:
shelllocal> vi /etc/rc.
# 加入
/usr/local/nginx/sbin/nginx
shelllocal> chmod +x /etc/rc.
http://192.168.137.164:8888/group1/M00/00/00/wKiJpFkS0ByATie5AAVFOL7FJU4.tar.gz
http://192.168.137.166:8888/group2/M00/00/00/wKiJplkS0CyAJyzTAAVFOL7FJU4.tar.gz
说明:每一个节点执行相同的操做
tracker节点:192.168.137.162,192.168.137.163
在 tracker 上安装的 nginx 主要为了提供 http 访问的反向代理、负载均衡以及缓存服务。
>-c++-devel-devel-develshellyum install gcc gccmake automake autoconf libtool pcre pcrezlib zlibopenssl openssl
shell> cd /home/test
shell> tar -zxvf nginx-1.6.2.tar.gz -C /usr/local/
shell> tar -zxvf ngx_cache_purge-2.3.tar.gz -C /usr/local/fast/
shell1.6.2> cd /usr/local/nginx-
shell--add-module=/usr/local/fast/ngx_cache_purge-2.3> ./configure
shell> make && make install
shell> vi /usr/local/nginx/conf/nginx.conf
user nobody;
worker_processes 1;
events {
worker_connections 1024;
use epoll;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
tcp_nopush on;
keepalive_timeout 65;
#gzip on;
#设置缓存
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 300m;
proxy_redirect off;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 16k;
proxy_buffers 4 64k;
proxy_busy_buffers_size 128k;
proxy_temp_file_write_size 128k;
#设置缓存存储路径、存储方式、分配内存大小、磁盘最大空间、缓存期限
proxy_cache_path /fastdfs/cache/nginx/proxy_cache levels=1:2 keys_zone=http-cache:500m max_size=10g inactive=30d;
proxy_temp_path /fastdfs/cache/nginx/proxy_cache/tmp;
#设置 group1 的服务器
upstream fdfs_group1 {
server 192.168.137.164:8888 weight=1 max_fails=2 fail_timeout=30s;
server 192.168.137.165:8888 weight=1 max_fails=2 fail_timeout=30s;
}
#设置 group2 的服务器
upstream fdfs_group2 {
server 192.168.137.166:8888 weight=1 max_fails=2 fail_timeout=30s;
server 192.168.137.167:8888 weight=1 max_fails=2 fail_timeout=30s;
}
server {
listen 8000;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
#设置 group 的负载均衡参数
location /group1/M00 {
proxy_next_upstream http_502 http_504 error timeout invalid_header;
proxy_cache http-cache;
proxy_cache_valid 200 304 12h;
proxy_cache_key $uri$is_args$args;
proxy_pass http://fdfs_group1;
expires 30d;
}
location /group2/M00 {
proxy_next_upstream http_502 http_504 error timeout invalid_header; proxy_cache http-cache;
proxy_cache_valid 200 304 12h;
proxy_cache_key $uri$is_args$args;
proxy_pass http://fdfs_group2;
expires 30d;
}
#设置清除缓存的访问权限
location ~/purge(/.*) {
allow 127.0.0.1;
allow 192.168.1.0/24;
deny all;
proxy_cache_purge http-cache $1$is_args$args;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
按以上 nginx 配置文件的要求,建立对应的缓存目录:
>-pcacheshellmkdir/fastdfs//nginx/proxy_cache
>-pcacheshellmkdir/fastdfs//nginx/proxy_cache/tmp
shell> vi /etc/sysconfig/iptables
## 添加以下配置
--state NEW -m tcp -p tcp --dport 8000 -j ACCEPT -A INPUT -m state
shell# 从新启动防火墙> service iptables restart
/usr/local/nginx/sbin/nginxshell>
设置开机启动:
shelllocal> vi /etc/rc.
## 加入如下配置
/usr/local/nginx/sbin/nginx
shelllocal> chmod +x /etc/rc.
前面直接经过访问Storage节点中的Nginx访问文件:
http://192.168.137.164:8888/group1/M00/00/00/wKiJpFkS0ByATie5AAVFOL7FJU4.tar.gz
http://192.168.137.166:8888/group2/M00/00/00/wKiJplkS0CyAJyzTAAVFOL7FJU4.tar.gz
如今能够经过Tracker中的Nginx来进行访问:
(1)、经过 Tracker1 中的 Nginx 来访问
http://192.168.137.162:8000/group1/M00/00/00/wKiJpFkS0ByATie5AAVFOL7FJU4.tar.gz
http://192.168.137.162:8000/group2/M00/00/00/wKiJplkS0CyAJyzTAAVFOL7FJU4.tar.gz
(2)、经过 Tracker2 中的 Nginx 来访问
http://192.168.137.163:8000/group1/M00/00/00/wKiJpFkS0ByATie5AAVFOL7FJU4.tar.gz
http://192.168.137.163:8000/group2/M00/00/00/wKiJplkS0CyAJyzTAAVFOL7FJU4.tar.gz
由上面的文件访问效果能够看到,每个Tracker中的Nginx都单独对后端的Storage组作了负载均衡,但整套FastDFS集群,若是想对外提供统一的文件访问地址,还须要对两个Tracker中的Nginx进行HA集群
使用Keepalived + Nginx组成的高可用负载均衡集群,作两个Tracker节点中Nginx的负载均衡。
分别在192.168.137.160和192.168.137.161两个节点安装Keepalived与Nginx。
keepalived安装与配置:请参考本人Nginx相关blog
Nginx的安装与配置:请参考本人Nginx相关blog
请参考本人Nginx相关blog
注意:将VIP的IP地址修改成192.168.137.170
2个节点的Nginx配置相同,以下所示:
shell> vi /usr/local/nginx/conf/nginx.conf
user root;
worker_processes 1;
events {
worker_connections 1024;
use epoll;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
## FastDFS Tracker Proxy
upstream fastdfs_tracker {
server 192.168.137.162:8000 weight=1 max_fails=2 fail_timeout=30s;
server 192.168.137.163:8000 weight=1 max_fails=2 fail_timeout=30s;
}
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
## FastDFS Proxy
location /dfs {
root html;
index index.html index.htm;
proxy_pass http://fastdfs_tracker/;
proxy_set_header Host $http_host;
proxy_set_header Cookie $http_cookie;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
client_max_body_size 300m;
}
}
}
/usr/local/nginx/sbin/nginxshell>-s reload
如今能够经过 Keepalived+Nginx 组成的高可用负载集群的 VIP(192.168.137.170)来访问 FastDFS 集群中的文件了:
http://192.168.137.170/dfs/group1/M00/00/00/wKiJpFkS0ByATie5AAVFOL7FJU4.tar.gz
http://192.168.137.170/dfs/group2/M00/00/00/wKiJplkS0CyAJyzTAAVFOL7FJU4.tar.gz
注意:千万不要使用 kill -9 命令强杀 FastDFS 进程,不然可能会致使 binlog 数据丢失。
参考:
http://blog.csdn.net/xyang81/article/details/52928230