consul-template + nginx实现动态负载均衡、故障转移

consul-template + nginx实现动态负载均衡、故障转移

前置条件

确保nginx服务器已安装consul客户端

参考《Consul client端安装》

一、Consul-Template安装

wget https://releases.hashicorp.com/consul-template/0.24.1/consul-template_0.24.1_linux_amd64.tgz
tar -xvf consul-template_0.24.1_linux_amd64.tgz

mv consul-template /usr/local/bin/

mkdir -p /data/consul/template
mkdir -p /data/logs/consul-template/

二、Consul-Template使用帮助

查看帮助

执行consul-template -h即可看到consul-temple的使用参数

  -auth=<user[:pass]>      设置基本的认证用户名和密码
  -consul=<address>        设置Consul实例的地址
  -max-stale=<duration>    查询过期的最大频率,默认是1s
  -dedup                   启用重复数据删除,当许多consul template实例渲染一个模板的时候可以降低consul的负载
  -ssl                     使用https连接Consul使用SSL
  -ssl-verify              通过SSL连接的时候检查证书
  -ssl-cert                SSL客户端证书发送给服务器
  -ssl-key                 客户端认证时使用的SSL/TLS私钥
  -ssl-ca-cert             验证服务器的CA证书列表
  -token=<token>           设置Consul API的token
  -syslog                  把标准输出和标准错误重定向到syslog,syslog的默认级别是local0。
  -syslog-facility=<f>     设置syslog级别,默认是local0,必须和-syslog配合使用
  -template=<template>     增加一个需要监控的模板,格式是:'templatePath:outputPath(:command)',多个模板则可以设置多次
  -wait=<duration>         当呈现一个新的模板到系统和触发一个命令的时候,等待的最大最小时间。如果最大值被忽略,默认是最小值的4倍。
  -retry=<duration>        当在和consul api交互的返回值是error的时候,等待的时间,默认是5s。
  -config=<path>           配置文件或者配置目录的路径
  -pid-file=<path>         PID文件的路径
  -log-level=<level>       设置日志级别,可以是"debug","info", "warn" (default), and "err"
  -dry                     Dump生成的模板到标准输出,不会生成到磁盘
  -once                    运行consul-template一次后退出,不以守护进程运行
  -reap                    子进程自动收割

三、Consul-Template使用实例

1、创建模板文件

cd /data/consul/template

vi nginx.conf.ctmpl

{{range services}} {{$name := .Name}} {{$service := service .Name}}
upstream {{$name}} {
  zone upstream-{{$name}} 64k;
  {{range $service}}server {{.Address}}:{{.Port}} max_fails=2 fail_timeout=30 weight=1;
  {{else}}server 127.0.0.1:65535; # force a 502{{end}}
} {{end}}
2、通过consul-template生成nginx配置文件

调用模板文件生成查询结果

consul-template -consul-addr 127.0.0.1:8500 -template="/data/consul/template/nginx.conf.ctmpl:/data/nginx/vhost/upstream.conf" -once

查询生成的配置文件

cat upstream.conf

upstream admin-api {
  zone upstream-admin-api 64k;
  server 172.16.10.2:9090 max_fails=2 fail_timeout=30 weight=1;

}   
upstream app-api {
  zone upstream-app-api 64k;
  server 172.16.10.251:8085 max_fails=2 fail_timeout=30 weight=1;

} 

如果想生成Nginx配置文件后自动加载配置,可以这样:

consul-template -consul-addr 127.0.0.1:8500 -template="/data/consul/template/nginx.conf.ctmpl:/data/nginx/vhost/upstream.conf:nginx -s reload" -once

指定日志输出到相应目录,并设置后台启动

nohup consul-template -consul-addr 127.0.0.1:8500 -template="/data/consul/template/nginx.conf.ctmpl:/data/nginx/vhost/upstream.conf:/usr/local/openresty/nginx/sbin/nginx -s reload" >> /data/logs/consul-template.log 2>&1 &

四、将Consul-Template添加到服务

1、编写启动脚本

mkdir -p /opt/server/consul-template && cd /opt/server/consul-template

vim start.sh

#!/bin/bash
nohup consul-template \
-consul-addr 127.0.0.1:8500 \
-template="/data/consul/template/nginx.conf.ctmpl:/data/nginx/vhost/upstream.conf:/usr/local/openresty/nginx/sbin/nginx -s reload" \
>> /data/logs/consul-template/consul-template.log 2>&1
2、赋予可执行权限
chmod +x start.sh
4、将consul-template注册为Systemd服务

cd /usr/lib/systemd/system/

vim consul-template.service

[Unit]
Description=consul-template
After=network.target
    
[Service]
ExecStart=/opt/server/consul-template/start.sh
KillSignal=SIGTERM
    
[Install]
WantedBy=multi-user.target
5、 启动consul-template
systemctl start consul-template
6、查询服务状态
systemctl status consul-template
7、查询日志
tailf /data/logs/consul-template/consul-template.log
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值