前言
在《構建Consul集羣》章節中介紹瞭如何實現consul集羣的構建,通過對consul的進一步瞭解,其並沒有提供的對cluster直接操作的client-api,故需要針對Consul集羣構建一個統一入口,但這個並不需要我們過多的擔心,Consul的小夥伴Consul-Template正是爲此而生,通過Nginx+ConsulTemplate能夠非常方便的實現,本章將來介紹如何配置應用並驗證。
本章概要
1、準備工作;
2、Nginx配置;
3、編寫ctmpl模板;
4、啓動服務;
5、高可用集羣驗證;
準備工作
1、環境:
- Client節點:WIN10(192.168.6.78);
- Server節點:Linux(192.168.3.89);
3、安裝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節點,可以自定義一些元數據即可;
- 具體的語法和參數可以參考如下兩個地址:
- 語法:https://github.com/hashicorp/consul-template;
- 參數:https://github.com/hashicorp/consul-template/blob/master/dependency/catalog_nodes.go;https://github.com/hashicorp/consul-template/blob/master/dependency/catalog_node.go;https://github.com/hashicorp/consul-template/blob/master/dependency/health_service.go
- 以上具體有哪些參數可用也可以通過/v1/catalog/nodes相關的端口查看;
啓動服務
1、首先來看下已經準備好哪些文件:
2、啓動Nginx,其安裝在/usr/local/nginx-1.13.8目錄下:
/usr/local/nginx-1.13.8/sbin/nginx
3、啓動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
4、啓動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
5、編寫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的重啓操作;
高可用集羣驗證
1、此時至/usr/local/nginx-1.13.8/conf/conf.d/即可看到根據模板生成的conf配置文件:
可以看到兩個節點均被加入了負載服務列表;
3、停止Win下的Client節點,再次查看生成的conf配置文件:
可以看到其僅僅有一個存活狀態的Server節點存在,更新成功。
5、KEY/VALUE部分的驗證已經在之前的章節驗證,不再說明;
總結
本章節主要介紹瞭如何通過Nginx+ConsulTemplate實現高可用Consul的配置和驗證,其仍然存在一定的不足,需要重新啓動Nginx,重啓頻率低時還是可以接受的,如果很頻繁勢必造成一定的影響。