1、课程名称:如何借助 FastDFS搭建分布式集群

2、具体内容
在之前已经建立过了一个伪分布式集群,很明显,之前的集群只是实现了一个简单的文件的上传管理操作,但是这些文件并没有形成真正图片服务器的需求(往往需要单独申请一个新的域名做文件服务器)。www.taobao.com、tbimg.com,或者是与之类似的域名。
由于 FastDFS 本身不再默认集成 WEB 服务器了,所以现在需要去结合 Nginx 搭建好 WEB 服务器。在整个的处理过程之中,需要使用到一个 nginx 与 fastdfs 整合模块才可以实现文件的显示。同时考虑到现实的环境,本次将搭建两组服务器;group1、group2。

本次要使用的主机列表如下:
No. 主机名称 IP 地址 描述
1 fastdfs-tracker-server 192.168.122.198 Tracker、Nginx(Tracker)
2 fastdfs-storage-group1-01 192.168.122.199 Storage、Nginx(FastDFS-Module)
3 fastdfs-storage-group1-02 192.168.122.200 Storage、Nginx(FastDFS-Module)
4 fastdfs-storage-group2-01 192.168.122.201 Storage、Nginx(FastDFS-Module)
5 fastdfs-storage-group2-02 192.168.122.202 Storage、Nginx(FastDFS-Module)
现在已经准备出了两台主机,配置是相同的:
提供好了 libfastcommon 编译;
提供好了所需要使用到的软连接;
已经编译好了 fastdfs 软件。
2.1、配置 storage
首先先配置一个 storage 节点主机,而后在这台主机上配置好相应的 nginx 服务;
1、 【fastdfs-storage-group1-01】进行 storage 配置:
通过模版文件拷贝出配置文件:cp /etc/fdfs/storage.conf.sample /etc/fdfs/storage.conf;
建立相应的工作目录:mkdir -p /usr/data/fdfs/storage;
; 编辑配置文件:vim /etc/fdfs/storage.conf;
group_name=group1
base_path=/usr/data/fdfs/storage
tracker_server=192.168.122.198:22122
2、 【fastdfs-storage-group1-01】将本次使用的 Ningx 的开发包上传到系统之中,需要如下的几个开发包:nginx-1.11.3.tar.gz、echo-nginx-module-0.59.tar.gz、 fastdfs-nginx-module_v1.16.tar.gz 、 ngx_cache_purge-2.3.tar.gz 、 nginx-upstream-fair-a18b409.tar.gz 、pcre-8.36.tar.gz、zlib-1.2.8.tar.gz。
3、 【fastdfs-storage-group1-01】将所有的源代码的开发包进行解压缩:
tar xzvf /srv/ftp/echo-nginx-module-0.59.tar.gz -C /usr/local/src/
tar xzvf /srv/ftp/fastdfs-nginx-module_v1.16.tar.gz -C /usr/local/src/
tar xzvf /srv/ftp/nginx-1.11.3.tar.gz -C /usr/local/src/
tar xzvf /srv/ftp/nginx-upstream-fair-a18b409.tar.gz -C /usr/local/src/
tar xzvf /srv/ftp/ngx_cache_purge-2.3.tar.gz -C /usr/local/src/
tar xzvf /srv/ftp/pcre-8.36.tar.gz -C /usr/local/src/
tar xvf /srv/ftp/zlib-1.2.8.tar.gz -C /usr/local/src/
4、 【fastdfs-storage-group1-01】编译 nginx:
· 需要建立好 nginx 编译之后保存的数据目录:
mkdir -p /usr/local/nginx/{logs,conf,fastcgi_temp,sbin,client_bo_temp,proxy_temp,uwsgi_temp,scgi_temp}
· 进行 nginx 的编译处理:cd /usr/local/src/nginx-1.11.3/;
./configure --prefix=/usr/local/nginx/
--sbin-path=/usr/local/nginx/sbin/
--with-http_ssl_module
--conf-path=/usr/local/nginx/conf/nginx.conf
--pid-path=/usr/local/nginx/logs/nginx.pid
--error-log-path=/usr/local/nginx/logs/error.log
--http-log-path=/usr/local/nginx/logs/access.log
--http-fastcgi-temp-path=/usr/local/nginx/fastcgi_temp
--http-client-bo-temp-path=/usr/local/nginx/client_bo_temp
--http-proxy-temp-path=/usr/local/nginx/proxy_temp
--http-uwsgi-temp-path=/usr/local/nginx/uwsgi_temp
--http-scgi-temp-path=/usr/local/nginx/scgi_temp
--add-module=/usr/local/src/echo-nginx-module-0.59
--add-module=/usr/local/src/gnosek-nginx-upstream-fair-a18b409
--add-module=/usr/local/src/ngx_cache_purge-2.3
--add-module=/usr/local/src/fastdfs-nginx-module/src
--with-zlib=/usr/local/src/zlib-1.2.8
--with-pcre=/usr/local/src/pcre-8.36
· 编译并安装:make ; make install;
5、 【fastdfs-storage-group1-01】将 fastdfs-nginx-model 模块中的配置文件拷贝到"/etc/fdfs"目录下:
cp /usr/local/src/fastdfs-nginx-module/src/mod_fastdfs.conf /etc/fdfs/
6、 【fastdfs-storage-group1-01】修改"mod_fastdfs.conf"文件:vim /etc/fdfs/mod_fastdfs.conf;
base_path=/usr/data/fdfs/storage
tracker_server=192.168.122.198:22122
group_name=group1
store_path0=/usr/data/fdfs/storage
url_have_group_name = true
"url_have_group_name"表示现在的访问路径上是否需要追加有组名称。
7、 【fastdfs-storage-group1-01】拷贝配置文件:
· 拷贝 http.conf 配置文件:cp /usr/local/src/FastDFS/conf/http.conf /etc/fdfs/;
· 拷贝 mime.types 配置文件:cp /usr/local/src/FastDFS/conf/mime.types /etc/fdfs/;
8、 【fastdfs-storage-group1-01】修改 nginx 的配置项:vim /usr/local/nginx/conf/nginx.conf
listen 9999;
location ~/group[1-3]/M00 {
root /usr/data/fdfs/storage ;
ngx_fastdfs_module ;
}
9、 【fastdfs-storage-group1-01】做一个软连接:
ln -s /usr/data/fdfs/storage/data/ /usr/data/fdfs/storage/M00
10、 【fastdfs-storage-group1-01】检测当前的 nginx 配置是否正确;
/usr/local/nginx/sbin/nginx -t
现在配置的 storage 里面并没有具体的图片信息,所以此时还不敢保证操作一切都是正确的。
2.2、配置多台 storage
1、 将已经配置好的 storage 主机关机,而后将其复制为多份主机;
2、 storage 主机一共有两组,每组有两台服务器,那么要修改各自主机的名称:vim /etc/hostname;
3、 【fastdfs-storage-group2-*】修改组的名称:vim /etc/fdfs/storage.conf
· group_name=group2
4、 【fastdfs-storage-group2-*】修改组名称:vim /etc/fdfs/mod_fastdfs.conf;
group_name=group2
那么这个时候两组的 storage 存储就配置完成了,那么在进行存储的时候将根据 group 的信息进行对应的保存。
2.3、配置 tracker 主机
每一台 storage 主机上的 nginx 只是作为负载均衡使用的,真正起作用的还是在于 tracker 主机上。
1、 【 fastdfs-tracker-server】 nginx 有关的开发包上传到 tracker 之中: nginx-1.11.3.tar.gz、 echo-nginx-module-0.59.tar.gz、
ngx_cache_purge-2.3.tar.gz、nginx-upstream-fair-a18b409.tar.gz、pcre-8.36.tar.gz、zlib-1.2.8.tar.gz。
2、 【fastdfs-tracker-server】将所有的源代码的开发包进行解压缩:
tar xzvf /srv/ftp/echo-nginx-module-0.59.tar.gz -C /usr/local/src/
tar xzvf /srv/ftp/nginx-1.11.3.tar.gz -C /usr/local/src/
tar xzvf /srv/ftp/nginx-upstream-fair-a18b409.tar.gz -C /usr/local/src/
tar xzvf /srv/ftp/ngx_cache_purge-2.3.tar.gz -C /usr/local/src/
tar xzvf /srv/ftp/pcre-8.36.tar.gz -C /usr/local/src/
tar xvf /srv/ftp/zlib-1.2.8.tar.gz -C /usr/local/src/
3、 【fastdfs-tracker-server】进行 nginx 的编译处理:
· 建立相应的目录:
mkdir -p /usr/local/nginx/{logs,conf,fastcgi_temp,sbin,client_bo_temp,proxy_temp,uwsgi_temp,scgi_temp}
· 进入到 nginx 源代码目录之中:cd /usr/local/src/nginx-1.11.3/;
./configure --prefix=/usr/local/nginx/
--sbin-path=/usr/local/nginx/sbin/
--with-http_ssl_module
--conf-path=/usr/local/nginx/conf/nginx.conf
--pid-path=/usr/local/nginx/logs/nginx.pid
--error-log-path=/usr/local/nginx/logs/error.log
--http-log-path=/usr/local/nginx/logs/access.log
--http-fastcgi-temp-path=/usr/local/nginx/fastcgi_temp
--http-client-bo-temp-path=/usr/local/nginx/client_bo_temp
--http-proxy-temp-path=/usr/local/nginx/proxy_temp
--http-uwsgi-temp-path=/usr/local/nginx/uwsgi_temp
--http-scgi-temp-path=/usr/local/nginx/scgi_temp
--add-module=/usr/local/src/echo-nginx-module-0.59
--add-module=/usr/local/src/gnosek-nginx-upstream-fair-a18b409
--add-module=/usr/local/src/ngx_cache_purge-2.3
--with-zlib=/usr/local/src/zlib-1.2.8
--with-pcre=/usr/local/src/pcre-8.36
编译与安装:make ; make install;
4、 【fastdfs-tracker-server】配置 tracker:
· 建立 tracker 工作目录:mkdir -p /usr/data/fdfs/tracker;
· 通过模版拷贝配置文件:cp /etc/fdfs/tracker.conf.sample /etc/fdfs/tracker.conf;
· 编辑配置文件:vim /etc/fdfs/tracker.conf;
base_path=/usr/data/fdfs/tracker
5、 【fastdfs-tracker-server】配置 nginx:
tracker 所在的主机作为整体访问的主机,所以为了访问迅速需要配置缓存交 :
mkdir -p /usr/data/nginx/{cache,tmp}
修改 nginx 的配置文件项:vim /usr/local/nginx/conf/nginx.conf;
#user nobo;
worker_processes 1;
error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 65536;
use epoll;
}
http {
include mime.types;
default_type application/octet-stream;
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_bo_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 /usr/data/nginx/cache levels=1:2 keys_zone=http-cache:500m max_size=10g inactive=30d;
proxy_temp_path /usr/data/nginx/tmp;
upstream fdfs_group1 {
server 192.168.122.199:9999 weight=1 max_fails=2 fail_timeout=30s;
server 192.168.122.200:9999 weight=1 max_fails=2 fail_timeout=30s;
}
upstream fdfs_group2 {
server 192.168.122.201:9999 weight=1 max_fails=2 fail_timeout=30s;
server 192.168.122.202:9999 weight=1 max_fails=2 fail_timeout=30s;
}
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $bo_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
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.122.0/24;
deny all;
proxy_cache_purge http-cache $1$is_args$args;
}
location / {
root html;
index index.html index.htm;
}
#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;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
##location ~ .php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ .php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /.ht {
# deny all;
#}
}# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
##server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
那么此时 tracker 就应该正常了。
2.4、启动测试
1、 【fastdfs-tracker-server】启动相应的 tracker 进程:
/usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf
2、 【fastdfs-storage-*】启动 storage 进程:
/usr/bin/fdfs_storaged /etc/fdfs/storage.conf
3、 【fastdfs-client】修改 client.conf 文件,主要是设置 tracker 的地址:vim /etc/fdfs/client.conf;
tracker_server=192.168.122.198:22122
4、 【fastdfs-client】检测当前的服务状态:
/usr/bin/fdfs_monitor /etc/fdfs/client.conf
现在可以发现已经有四台主机准备操作了。
5、 【fastdfs-storage-*】启动 nginx 服务:
/usr/local/nginx/sbin/nginx
6、 【fastdfs-tracker-server】启动 nginx 服务:
/usr/local/nginx/sbin/nginx
7、 【fastdfs-client】上传一个新的图片:
/usr/bin/fdfs_test /etc/fdfs/client.conf upload /srv/ftp/pic-a.jpg
给出的访问地址:http://192.168.122.198/group1/M00/00/00/wKh6x1g7qMmABcBrAASM4cOp7lk901.jpg
8、浏览器的访问地址:http://192.168.122.198/group1/M00/00/00/wKh6x1g7qMmABcBrAASM4cOp7lk901.jpg;
清除缓存:http://192.168.122.198/purge/group1/M00/00/00/wKh6x1g7qMmABcBrAASM4cOp7lk901.jpg;
9、 设置自动切换,现在所进行的数据存储实际上默认情况下基本上都会找到 group2,因为这个是在配置文件里面定义好的;
打开配置文件:vim /etc/fdfs/tracker.conf;
store_lookup=2
如果设置为 2 则表示会根据存储的空间自动进行分配存储。
随后需要重新启动一下 tracker 服务,只能够先杀死进程,而后再进行重新启动即可。