手把手教你搭建FastDFS集群(下)
手把手教你搭建FastDFS集群(下)
由于博客图片量大,篇幅太长,因此需要分上、中、下三篇博客来写,上篇和中篇我们已经一起学习完了,这篇博客我们一起学习下剩余部分。
一、配置反向代理
我们需要在两个跟踪器上安装nginx(也就是192.168.156.5和192.168.156.6)以提供反向代理服务,目的是使用统一的一个IP地址对外提供服务。为了避免一些不必要的错误,我们先把其它四台虚拟机的窗口关掉。
1.解压ngx_cache_purge-2.3.tar.gz,解压命令:tar -zxvf ngx_cache_purge-2.3.tar.gz -C /usr/local/fast/,如下图所示(另一台设备就不粘贴图片了)。
解压完之后我们在/usr/local/fast/目录下可以看到多了一个ngx_cache_purge-2.3文件夹。如下图所示。
2.下载需要的依赖库,在两台设备上依次执行下面四条命令。
-
yum install pcre
-
yum install pcre-devel
-
yum install zlib
-
yum install zlib-devel
接着,我们在下面的输入框中输入:tar -zxvf nginx-1.6.2.tar.gz -C /usr/local/并按回车,会在两个窗口同时执行解压操作。如下图所示。
接下来我们在下面的输入框中输入:cd /usr/local并按回车,两台设备都进入到/usr/local/nginx-1.6.2目录下。如下图所示。
接着,在下面的输入框中加入模块命令:./configure --add-module=/usr/local/fast/ngx_cache_purge-2.3,回车就会在两台设备上都执行添加cache模块并会检查环境。
接着在下面的输入框中输入命令:make && make install,回车就会在两台设备上都执行编译安装。如下图所示。
下面我们需要修改下两台设备/usr/local/nginx/conf/目录下的nginx.conf文件,大家可以直接把下面代码替换这个目录下的该文件,也可以直接到:http://download.csdn.net/detail/u012453843/9803673这个地址下载nginx.conf文件来替换。不过由于我们搭建环境的虚拟机IP可能不一样,因此,我们需要根据实际情况修改下IP等信息。(注意192.168.156.5和192.168.156.6这两台设备的/usr/local/nginx/conf/目录下的nginx.conf都要修改)
-
#user nobody;
-
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 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;
-
#tcp_nopush on;
-
-
#keepalive_timeout 0;
-
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:200m max_size=1g inactive=30d;
-
proxy_temp_path /fastdfs/cache/nginx/proxy_cache/tmp;
-
#group1的服务设置
-
upstream fdfs_group1 {
-
server 192.168.156.7:8888 weight=1 max_fails=2 fail_timeout=30s;
-
server 192.168.156.8:8888 weight=1 max_fails=2 fail_timeout=30s;
-
}
-
#group2的服务设置
-
upstream fdfs_group2 {
-
server 192.168.156.9:8888 weight=1 max_fails=2 fail_timeout=30s;
-
server 192.168.156.10: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;
-
#group1的负载均衡配置
-
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;
-
#对应group1的服务设置
-
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;
-
#对应group2的服务设置
-
proxy_pass http://fdfs_group2;
-
expires 30d;
-
}
-
-
location ~/purge(/.*) {
-
allow 127.0.0.1;
-
allow 192.168.156.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的访问端口是8000,而我们的防火墙是不允许访问该端口的,因此我们需要修改下防火墙,使其允许访问8000端口,这个操作我在上篇和中篇都介绍过了,这里就不啰嗦了。
下面我们便来启动这两台设备上的nginx。启动所使用的命令是/usr/local/nginx/sbin/nginx。启动完之后,可以使用ps -ef | grep nginx命令来查看nginx是否正常启动,如果看到root 4027 1 0 08:18 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx这条信息,说明正常启动了。
-
[root@itcast05 conf]# /usr/local/nginx/sbin/nginx
-
[root@itcast05 conf]# ps -ef | grep nginx
-
root 4027 1 0 08:18 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
-
nobody 4028 4027 0 08:18 ? 00:00:00 nginx: worker process
-
nobody 4029 4027 0 08:18 ? 00:00:00 nginx: cache manager process
-
nobody 4030 4027 0 08:18 ? 00:00:00 nginx: cache loader process
-
root 4032 1522 0 08:18 pts/0 00:00:00 grep nginx
-
[root@itcast05 conf]# /usr/bin/fdfs_upload_file /etc/fdfs/client.conf /usr/local/3.jpg
-
group1/M00/00/00/wKicCFjkOVGAMlQvAAHk-VzqZ6w757.jpg
-
[root@itcast05 conf]# /usr/bin/fdfs_upload_file /etc/fdfs/client.conf /usr/local/3.jpg
-
group2/M00/00/00/wKicCVjkOeaAVb0dAAHk-VzqZ6w123.jpg
-
[root@itcast05 conf]#
我们知道,nginx对外提供服务有可能碰到服务挂掉的时候,这时候高可用就显得异常重要了,因此现在我们搭建一个nginx和keepalived结合实现的nginx集群高可用的环境,大家可以参考http://blog.csdn.net/u012453843/article/details/69668663这篇博客进行学习。
我们现在要把keepalived实现的nginx集群高可用应用到我们的FastDFS集群当中,现在用于搭建nginx集群高可用的设备是192.168.156.11和192.168.156.12,我们只需要修改下这两台设备的nginx.conf文件,配置文件如下
-
#user nobody;
-
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 1024;
-
}
-
-
-
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 0;
-
keepalive_timeout 65;
-
-
#gzip on;
-
-
upstream fastdfs_tracker {
-
server 192.168.156.5:8000 weight=1 max_fails=2 fail_timeout=30s;
-
server 192.168.156.6:8000 weight=1 max_fails=2 fail_timeout=30s;
-
}
-
-
server {
-
listen 80;
-
server_name localhost;
-
-
#charset koi8-r;
-
-
#access_log logs/host.access.log main;
-
-
location /fastdfs {
-
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;
-
}
-
-
#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;
-
}
-
}
-
}
我们对配置文件做了两处修改,一处是添加了负载均衡upstream fastdfs_tracker,如下所示。我们是把192.168.156.5和192.168.156.6两台设备作为tracker,现在我们加了一层nginx来代理这两个tracker。
-
upstream fastdfs_tracker {
-
server 192.168.156.5:8000 weight=1 max_fails=2 fail_timeout=30s;
-
server 192.168.156.6:8000 weight=1 max_fails=2 fail_timeout=30s;
-
}
-
location /fastdfs {
-
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;
-
}
-
[root@nginx1 conf]# /usr/local/nginx/sbin/nginx -s reload
-
[root@nginx1 conf]#
-
[root@nginx2 conf]# /usr/local/nginx/sbin/nginx -s reload
-
[root@nginx2 conf]#
-
[root@itcast05 conf]# /usr/bin/fdfs_upload_file /etc/fdfs/client.conf /usr/local/3.jpg
-
group1/M00/00/00/wKicB1jqnPqARiT6AAHk-VzqZ6w956.jpg
-
[root@itcast05 conf]#
这样我们的FastDFS集群便搭建完了,搭建完后的集群图如下图所示。这个集群当中192.168.156.7、192.168.156.8、192.168.156.9、192.168.156.10这四台设备以8888端口对外提供服务,我们使用Tracker跟踪器管理这四台storage设备,两个Tracker的nginx对外提供的端口号是8000,也就是我们可以使用两台Tracker的任何一台设备的IP并且使用端口8000来访问存储在storage上的资源文件。其实我们完全可以在两台Tracker设备上搭建keepalived和nginx相结合的高可用环境并且对外提供虚拟IP192.168.156.110和端口80来访问资源文件。只不过这里为了展示多层nginx负载均衡所以才在192.168.156.11和192.168.156.12上专门搭建了keepalived和nginx相结合的高可用环境,由这两台设备对外提供虚拟IP服务,由于端口使用了默认的80,因此我们在使用虚拟IP192.168.156.110访问图片的时候才不用输入端口号的。
备注:启动集群步骤
1.启动6台设备(192.168.156.5、192.168.156.6、192.168.156.7、192.168.156.8、192.168.156.9、192.168.156.10)的nginx(其中192.168.156.11和192.168.156.12配置了keepalived开机自启动,顺带会启动nginx,因此这两台设备不用启动nginx)
2.启动tracker(192.168.156.5和192.168.156.6,启动命令:/etc/init.d/fdfs_trackerd start)
3.启动storage(192.168.156.7、192.168.156.8、192.168.156.9、192.168.156.10,启动命令:/etc/init.d/fdfs_storaged start)
这样FastDFS集群便都启动完了。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本