在consul-template没出现以前,你们构建服务发现系统,大多采用的是zookeeper、etcd+confd这样相似的系统,以前写过一篇consul+confd的文,讲的是如何动态生成配置文件的,现在consul官方推出了本身的模板系统,就是consul-template,这样的话动态的配置系统能够分化为etcd+confd和consul+consul-template两大阵营。consul是一个和etcd相似但又强于etcd的系统,关于etcd和consul能够翻阅之前的文章,consul-template的定位就和confd差很少同样了,confd的后端能够是etcd或者consul,相信consul搭配consul-template能发挥更大的效果。consul-template提供了一个便捷的方式从consul中获取存储的值,consul-template守护进程会查询consul实例,来更新系统上指定的任何模板,当更新完成后,模板能够选择运行一些任意的命令。node
consul template的使用场景:consul template能够查询consul中的服务目录、key、key-values等。这种强大的抽象功能和查询语言模板可使consul template特别适合动态的建立配置文件。例如:建立apache/nginx proxy balancers、haproxy backends、varnish servers、application configurations。nginx
consul template的特性:git
quiescence:consul template内制静止平衡功能,能够智能的发现consul实例中的更改信息。这个功能能够防止频繁的更新模板而引发系统的波动。 dry mode:不肯定当前架构的状态?担忧模板的变化会破坏子系统?无须担忧,由于consul template还有-dry模式。在dry模式,consul template会将结果呈如今STDOUT,因此操做员能够检查输出是否正常,以决定更换模板是否安全 CLI and Config:若是你喜欢在命令行上指定一切,consul template均可以hold住。随着内置HCL的支持,consul template接收一个配置文件,命令行参数,或者二者的混合。经过这种方式你能够继续使用你如今已有的配置管理工具和consul template来配合。 verbose debugging:即便每件事你都作的近乎完美,可是有时候仍是会有失败发生。consul template能够提供更详细的debug日志信息。
下载consul-templategithub
继承了consul的风格,consul-template下载下来解压后就是一个二进制文件,没有其余多余的内容,downloadgolang
查看帮助web
执行consul-template -h便可看到consul-temple的使用参数redis
-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 子进程自动收割
下面看一些例子:apache
consul实例:demo.consul.io后端
模板:/tmp/template.ctmplapi
模板输出路径:/tmp/result
1 运行consul-temple做为一个服务
consul-template \ -consul demo.consul.io \ -template "/tmp/template.ctmpl:/tmp/result"
2 查询本地consl实例,生成模板后重启nginx,若是consul不可用,若是api故障则每30s尝试检测一次值,consul-template运行一次后退出
consul-template \ -consul 127.0.0.1:8500 \ -template "/tmp/template.ctmpl:/var/www/nginx.conf:service nginx restart" \ -retry 30s \ -once
3 查询一个实例,渲染多个模板,而后重启相关服务
consul-template \ -consul my.consul.internal:6124 \ -template "/tmp/nginx.ctmpl:/var/nginx/nginx.conf:service nginx restart" \ -template "/tmp/redis.ctmpl:/var/redis/redis.conf:service redis restart" \ -template "/tmp/haproxy.ctmpl:/var/haproxy/haproxy.conf"
4 查询一个实例,dump模板到标准输出,参数中的-template则会被忽略
consul-template \ -consul my.consul.internal:6124 \ -template "/tmp/template.ctmpl:/tmp/result:service nginx restart" -dry
以上参数除了在命令行使用,也能够直接配置在文件中,下面看看Consul-Template的配置文件,简称HCL(HashiCorp Configuration Language),它是和JSON兼容的,下面看个例子:
consul = "127.0.0.1:8500" token = "abcd1234" retry = "10s" max_stale = "10m" log_level = "warn" pid_file = "/path/to/pid" wait = "5s:10s" vault { address = "https://vault.service.consul:8200" token = "abcd1234" renew = true ssl { // ... } } auth { enabled = true username = "test" password = "test" } ssl { enabled = true verify = false cert = "/path/to/client/cert" key = "/path/to/client/key" ca_cert = "/path/to/ca" } syslog { enabled = true facility = "LOCAL5" } deduplicate { enabled = true prefix = "consul-template/dedup/" } template { source = "/path/on/disk/to/template.ctmpl" destination = "/path/on/disk/where/template/will/render.txt" command = "restart service foo" command_timeout = "60s" perms = 0600 backup = true left_delimiter = "{{" right_delimiter = "}}" wait = "2s:6s" }
以上并非全部的fields都须要,好比Vault你可能就不须要,因此你就不须要指定Vault配置。以上就是配置文件。
下面看看配置模板到底怎么写,模板文件的语法和Go template的格式同样,confd也是遵循Go template的。
先看看API 功能语法:
datacenters:在consul目录中查询全部的datacenters,{{datacenters}} file:读取并输出本地磁盘上的文件,若是没法读取,则报错,{{file "/path/to/local/file"}} key:查询consul中该key的值,若是没法转换成一个类字符串的值,则会报错,{{key "service/redis/maxconns@east-aws"}} east-aws指定的是数据中心,{{key "service/redis/maxconns"}} key_or_default:查询consul中该key的值,若是key不存在,则使用指定的值代替,{{key_or_default "service/redis/maxconns@east-aws" "5"}} ls:在consul中查询给定前缀的key的顶级域值,{{range ls "service/redis@east-aws"}} {{.Key}} {{.Value}}{{end}} node:查询consul目录中的单个node,若是不指定node,则是当前agent的,{{node "node1"}} nodes:查询consul目录中的全部nodes,你也能够指定datacenter,{{nodes "@east-aws"}} service:查询consul中匹配的service组,{{service "release.web@east-aws"}}或者{{service "web"}},也能够返回一组HealthService服务{{range service "web@datacenter"}} server {{.Name}} {{.Address}}:{{.Port}}{{end}},默认值返回健康的服务,若是你想返回全部服务,则{{service "web" "any"}} services:查询consul目录中的全部services,{{services}},也能够指定datacenter:{{services "@east-aws"}} tree:查询consul中给定前缀的全部K/V值,{{range tree "service/redis@east-aws"}} {{.Key}} {{.Value}}{{end}}
再看看辅助函数语法:
byKey、byTag、contains、env、explode、in、loop、trimSpace、join、parseBool、parseFloat、parseInt、parseJSON、parseUint、regexMatch、regexReplaceAll、replaceAll、split、timestamp、toJSON等函数可使用,具体用法看官文