首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

高可用高性能分布式文件系统FastDFS进阶keepalived+nginx对多tracker进行高可用热备

在上一篇 分布式文件系统FastDFS如何做到高可用 中已经介绍了FastDFS的原理和怎么搭建一个简单的高可用的分布式文件系统及怎么访问。

高可用是实现了,但由于我们只设置了一个group,如果现在有5台服务器那将会出现5台只有一个group,每台服务器内的文件内容都相同(互备份)如下图,会造成资源浪费。

因此下面就5台服务器进行优化改造,进一步添加keepalived+nginx多tracker 架构,做到真正的高可用和高性能。

FastDFS集群服务器分布

其中keepalived+nginx用作30和31 tracker服务器作热备。用虚拟VIP 27作统一入口。

我们将按上面的架构分服务器:

虚拟VIP用192.168.80.27

keepalived+nginx 热备、负载均衡 : 28、29

tracker:

30和31服务器

storage:

group1:32、33

group2:34、35

虽然每个服务器上都有Nginx,但这里还是先强调一下这里Nginx的用途:

首先所有的Nginx都是用于上传后的文件访问用的,跟上传无关。

28、29上面的就是常见的端口负载,这里是对两个tracker访问文件端口8000(自己设置)负载。

tracker 上安装的 nginx 主要为了提供 http 访问的反向代理、负载均衡以及缓存服务。

每一台storage服务器主机上部署Nginx及FastDFS扩展模块,由Nginx模块对storage存储的文件提供http下载服务, 仅当当前storage节点找不到文件时会向源storage主机发起redirect或proxy动作。

注:图中的tracker可能为多个tracker组成的集群;且当前FastDFS的Nginx扩展模块支持单机多个group的情况

tracker跟踪节点30、31

安装配置步骤前文一致,配置tracker.conf 和client.conf,主要注意防火墙 开启端口22122

记得手动创建两个文件夹:

/data/fastdfs/tracker

/data/fastdfs/client

两个节点内容一样。

storage存储节点32到35

记得手动创建两个文件夹:/data/fastdfs/storage

安装步骤与上文一致,配置:

/etc/fdfs/storage.conf

group_name=group1 # 组名(第一组为group1,第二组为group2,依次类推...)base_path=/data/fastdfs/storage # 数据和日志文件存储根目录store_path0=/data/fastdfs/storage # 第一个存储目录,第二个存储目录起名为:store_path1=xxx,其它存储目录名依次类推...tracker_server=192.168.80.30:22122# tracker服务器IP和端口tracker_server=192.168.80.31:22122# tracker服务器IP和端口

注意:

32和33 group_name=group1

34和35 group_name=group2

其他配置一样默认,注意防火墙 开启端口23000。

开启两个tracker,并加入开机启动:

/usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf

chkconfig fdfs_trakcerd on

开启4个storage,并加入开机启动:

/usr/bin/fdfs_storaged /etc/fdfs/storage.conf

chkconfig fdfs_storaged on

查看集群状态,会显示整体内容:

/usr/bin/fdfs_monitor /etc/fdfs/storage.conf

测试服务

/usr/bin/fdfs_test /etc/fdfs/client.conf upload /etc/fdfs/anti-steal.jpg

以上就是FastDFS的安装与配置。下面就与Nginx模块结合作介绍。

将nginx和fastdfs-nginx-module上传到 storage存储节点32到35

fastdfs-nginx-module的作用已经在上篇介绍过,在此主要是配置。

解压插件压缩包

unzip fastdfs-nginx-module.zip

nginx编译安装

cd nginx

对nginx重新config

./configure \

--prefix=/opt/nginx \

--pid-path=/opt/nginx/nginx.pid \

--lock-path=/opt/nginx.lock \

--error-log-path=/opt/nginx/log/error.log \

--http-log-path=/opt/nginx/log/access.log \

--with-http_gzip_static_module \

--http-client-body-temp-path=/opt/nginx/client \

--http-proxy-temp-path=/opt/nginx/proxy \

--http-fastcgi-temp-path=/opt/nginx/fastcgi \

--http-uwsgi-temp-path=/opt/nginx/uwsgi \

--http-scgi-temp-path=/opt/nginx/scgi \

--add-module=/opt/fastdfs-nginx-module/src

再进行 make && make install

将mod_fastdfs.conf 拷贝到fdfs下进行配置

cd fastdfs-nginx-module/src

cp mod_fastdfs.conf /etc/fdfs/ #将mod_fastdfs.conf 拷贝到fdfs下进行配置

vim /etc/fdfs/mod_fastdfs.conf

base_path=/data/fastdfs/storagetracker_server=192.168.80.30:22122

tracker_server=192.168.80.31:22122

storage_server_port=23000

group_name=group1 # 第一组storage的组名

url_have_group_name=true

store_path0=/data/fastdfs/storagegroup_count=2[group1]group_name=group1storage_server_port=23000

store_path_count=1

store_path0=/data/fastdfs/storage[group2]group_name=group2storage_server_port=23000

store_path_count=1

store_path0=/data/fastdfs/storage

注意:

32和33 group_name=group1

34和35 group_name=group2

配置nginx

vim /opt/nginx/conf/nginx.conf

在nginx的配置文件中添加一个Server:server {

listen8888;server_name localhost; location~/group([0-9])/M00/{ngx_fastdfs_module; }}

8888的请求且有group的都转给 ngx_fastdfs_module 插件处理,防火墙什么的就不说了。

启动Nginx,并加和自启动

shell>/opt/nginx/sbin/nginx

浏览器访问测试时上传的文件,应该都能正常访问。

http://192.168.80.32:8888/group1/M00/00/00/CgNXGVpddOKANpzbAABdrZgsqUU369_big.jpg

http://192.168.80.34:8888/group2/M00/00/00/CgNXGVpddOKANpzbAABdrZgsqUU369_big.jpg

跟踪节点安装Nginx和ngx_cache_purge模块

说明:每个节点执行相同的操作

tracker节点:30,31

在 tracker 上安装的 nginx 主要为了提供 http 访问的反向代理、负载均衡以及缓存服务。

同样都上传nginx和ngx_cache_purge包

解压文件到/opt文件夹下

tar -zxvf ngx_cache_purge-2.3.tar.gz

然后对nginx进行编译安装

cd nginx ./configure --prefix=/opt/nginx --sbin-path=/usr/bin/nginx --add-module=/opt/ngx_cache_purge-2.3make&&makeinstall

配置Nginx,设置tracker负载均衡以及缓存

vi /opt/nginx/conf/nginx.conf

添加以下内容

#设置缓存 server_names_hash_bucket_size128; client_header_buffer_size 32k; large_client_header_buffers432k; 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_timeout90; proxy_send_timeout90; proxy_read_timeout90; proxy_buffer_size 16k; proxy_buffers464k; proxy_busy_buffers_size 128k; proxy_temp_file_write_size 128k; #设置缓存存储路径、存储方式、分配内存大小、磁盘最大空间、缓存期限proxy_cache_path/data/fastdfs/cache/nginx/proxy_cache keys_zone=http-cache:100m;#设置 group1 的服务器upstream fdfs_group1 #设置 group2 的服务器upstream fdfs_group2 server {

listen8000;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_valid20030412h; 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_valid20030412h; proxy_cache_key $uri$is_args$args;

proxy_pass http://fdfs_group2;

expires 30d; } #设置清除缓存的访问权限 location~/purge(/.*) { allow127.0.0.1; allow 192.168.80.0/24; deny all; proxy_cache_purge http-cache $1$is_args$args; } #error_page404/404.html; # redirect server error pages to the static page/50x.html # error_page500502503504/50x.html; location= /50x.html { root html; } }

上面配置的是当请求8000端口时根据group1还是group2进行选择到对应的服务器响应。

要注意的已标色。还要提前创建缓存目录:/data/fastdfs/cache/nginx/proxy_cache

启动Nginx

/opt/nginx/sbin/nginx设置开机启动:

vi/etc/rc.local## 加入以下配置

/opt/nginx/sbin/nginx

chmod+x /etc/rc.local #centos7

文件访问测试

前面直接通过访问Storage节点中的Nginx访问文件:

http://192.168.80.32:8888/group1/M00/00/00/CgNXGVpddOKANpzbAABdrZgsqUU369_big.jpg

http://192.168.80.34:8888/group2/M00/00/00/CgNXGVpddOKANpzbAABdrZgsqUU369_big.jpg

现在可以通过Tracker中的Nginx来进行访问:

(1)、通过 Tracker1 中的 Nginx 来访问

http://192.168.80.30:8000/group1/M00/00/00/CgNXGVpddOKANpzbAABdrZgsqUU369_big.jpg

http://192.168.80.30:8000/group2/M00/00/00/CgNXGVpddOKANpzbAABdrZgsqUU369_big.jpg

(2)、通过 Tracker2 中的 Nginx 来访问

http://192.168.80.31:8000/group1/M00/00/00/CgNXGVpddOKANpzbAABdrZgsqUU369_big.jpg

http://192.168.80.31:8000/group2/M00/00/00/CgNXGVpddOKANpzbAABdrZgsqUU369_big.jpg

每一个Tracker中的Nginx都单独对后端的Storage组做了负载均衡,但整套FastDFS集群,如果想对外提供统一的文件访问地址,还需要对两个Tracker中的Nginx进行HA集群

配置Tracker服务器高可用、反向代理与负载均衡

其实上面可以通过30或31的8000端口进行文件访问了,下面统一端口,对30和31再进行一次负载均衡。

使用Keepalived + Nginx组成的高可用负载均衡集群,做两个Tracker节点中Nginx的负载均衡。

这里Nginx也可以用Haproxy代替。关于Keepalived和 Haproxy的安装和介绍,在 备胎的养成记KeepAlived实现热备负载 和 入坑系列之HAProxy负载均衡 中作过详细描述。

这里使用Nginx,将Keepalived 和 Nginx 安装到 28和29上,VIP为27。

vi/opt/nginx/conf/nginx.confhttp server {

listen80;

server_name localhost;location/error_page500502503504/50x.html;location= /50x.html ## FastDFS Proxylocation/dfs } }

启动Nginx

/opt/nginx/sbin/nginx

设置开机启动:vi /etc/rc.local## 加入以下配置/opt/nginx/sbin/nginxchmod +x /etc/rc.local #centos7通过VIP访问文件

http://192.168.80.27:8000/dfs/group1/M00/00/00/CgNXGVpddOKANpzbAABdrZgsqUU369_big.jpg

http://192.168.80.27:8000/dfs/group2/M00/00/00/CgNXGVpddOKANpzbAABdrZgsqUU369_big.jpg

以上就是整个配置过程,看起来是非常的繁杂,所以一定要先弄清整个的部署架构,然后每个插件是用来做什么的都要清楚,后面如果要扩展的话就有个清晰的思路。

环境搭建只是开始,真正的使用、优化才是重点,所以耐心的看吧。

今日荐文

  • 发表于:
  • 原文链接http://kuaibao.qq.com/s/20180118G03V2O00?refer=cp_1026
  • 腾讯「腾讯云开发者社区」是腾讯内容开放平台帐号(企鹅号)传播渠道之一,根据《腾讯内容开放平台服务协议》转载发布内容。
  • 如有侵权,请联系 cloudcommunity@tencent.com 删除。

扫码

添加站长 进交流群

领取专属 10元无门槛券

私享最新 技术干货

扫码加入开发者社群
领券