Consul-Template&Nginx实现Consul集群高可用

前言
在《构建Consul集群》章节中介绍了如何实现consul集群的构建,经过对consul的进一步了解,其并无提供的对cluster直接操做的client-api,故须要针对Consul集群构建一个统一入口,但这个并不须要咱们过多的担忧,Consul的小伙伴Consul-Template正是为此而生,经过Nginx+ConsulTemplate可以很是方便的实现,本章未来介绍如何配置应用并验证。

本章概要
一、准备工做;
二、Nginx配置;
三、编写ctmpl模板;
四、启动服务;
五、高可用集群验证;

准备工做
一、环境:
  • Client节点:WIN10(192.168.6.78);
  • Server节点:Linux(192.168.3.89);
二、下载consul-template:地址 https://releases.hashicorp.com/consul-template/
三、安装nginx1.13.8;
Note :其中nginx和consul-template均会部署在linux中.

Nginx配置
为了将咱们的个性化配置与默认配置分离,在 nginx.conf 中添加以下 include conf.d/*.conf; 配置,并调整其默认监听端口为8112,大体以下:

编写ctmpl模板
建立 consul.ctmpl 文件,内容以下:
upstream consul {     
    # Refer: http://nginx.org/en/docs/http/ngx_http_upstream_module.html#upstream    
	# ip_hash;   
    # least_conn;    
    # least_time;    
{{range nodes "@dc2~_agent"}}    
    server {{.Address }}:{{.Meta.httpport}} max_fails=3 fail_timeout=60 weight=1;
{{else}}server 127.0.0.1:65535; # force a 502{{end}}    
}
server {   
    listen 8111;    
    server_name localhost;
    location / {   
        client_max_body_size    0;    
        proxy_connect_timeout 300s;    
        proxy_send_timeout   900;    
        proxy_read_timeout   900;    
        proxy_buffer_size    32k;    
        proxy_buffers      4 32k;    
        proxy_busy_buffers_size 64k;    
        proxy_redirect     off;    
        proxy_hide_header  Vary;    
        proxy_set_header   Accept-Encoding '';    
        proxy_set_header   Host   $host;    
        proxy_set_header   Referer $http_referer;    
        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_headers_hash_max_size 51200;    
        proxy_headers_hash_bucket_size 6400;    
        proxy_pass          http://consul;    
    }    
}
Note:
  • 网上包括官网有一些案例能够参考,但其在获取节点信息时均采用的service方式,其仅仅能够获取到以server模式启动的节点,但实际在应用中,咱们须要链接的是非持久化的client节点,故调整为获取Nodes信息;
  • 获取每一个节点的端口信息采用{{.Meta.httpport}}方式。官方提供的{{.Port}}并不是是http-port属性值,故须要经过自定义元数据来实现;
  • 经过"@dc2~_agent"对Nodes进行了一些过滤,若是须要区分是否为server节点,能够自定义一些元数据便可;
  • 具体的语法和参数能够参考以下两个地址:
  1. 语法:https://github.com/hashicorp/consul-template;
  2. 参数:https://github.com/hashicorp/consul-template/blob/master/dependency/catalog_nodes.gohttps://github.com/hashicorp/consul-template/blob/master/dependency/catalog_node.gohttps://github.com/hashicorp/consul-template/blob/master/dependency/health_service.go
  • 以上具体有哪些参数可用也能够经过/v1/catalog/nodes相关的端口查看;

启动服务
一、首先来看下已经准备好哪些文件:

二、启动Nginx,其安装在/usr/local/nginx-1.13.8目录下:
/usr/local/nginx-1.13.8/sbin/nginx

三、启动Linux下Server(Master)节点:
./consul agent -server -bootstrap-expect 1 -ui -datacenter dc2 -disable-host-node-id -client 0.0.0.0 -bind 192.168.3.89 -data-dir ./data/ -http-port 8501 -node node2 -node-meta httpport:8501

四、启动WIN10下的Client节点:
D:\consul>consul agent -datacenter dc2 -client 0.0.0.0 -ui -data-dir ./data/ -http-port 8500 -bind 192.168.6.78 -join 192.168.3.89 -disable-host-node-id -node node1 -node-meta httpport:8500

五、编写consul-template-start.sh以下,其主要用来启动consul-template:
./consul-template -consul-addr 192.168.3.89:8501 -template ./consul.ctmpl:/usr/local/nginx-1.13.8/conf/conf.d/consul.conf:"/usr/local/nginx-1.13.8/sbin/nginx -s reload"
Note:
  • -consul-addr:表示其链接监听的数据来源,我的链接可靠稳定的server节点比较好;
  • -template:指定模板生成conf后放置自定义的Nginx配置目录;
  • 最后进行Nginx的重启操做;

六、此时咱们经过 http://192.168.3.89:8501/ui/#/dc2/nodes http://192.168.6.78:8500/ui/#/dc2/nodes 均可以看下以下

高可用集群验证
一、此时至/usr/local/nginx-1.13.8/conf/conf.d/便可看到根据模板生成的conf配置文件:
能够看到两个节点均被加入了负载服务列表;

二、经过浏览器访问 http://192.168.3.89:8111/ui/#/dc2/nodes ,可以正常获取到各类信息:


三、中止Win下的Client节点,再次查看生成的conf配置文件:

能够看到其仅仅有一个存活状态的Server节点存在,更新成功。

四、再次经过浏览器访问 http://192.168.3.89:8111/ui/#/dc2/nodes ,节点信息以下:

五、KEY/VALUE部分的验证已经在以前的章节验证,再也不说明;

总结
本章节主要介绍了如何经过Nginx+ConsulTemplate实现高可用Consul的配置和验证,其仍然存在必定的不足,须要从新启动Nginx,重启频率低时仍是能够接受的,若是很频繁势必形成必定的影响。