Consul-Template

Consul-Template简介

Consul-Template是基于Consul的自动替换配置文件的应用。在Consul-Template没出现之前,大家构建服务发现系统大多采用的是Zookeeper、Etcd+Confd这样类似的系统。

Consul官方推出了自己的模板系统Consul-Template后,动态的配置系统可以分化为Etcd+Confd和Consul+Consul-Template两大阵营。Consul-Template的定位和Confd差不多,Confd的后端可以是Etcd或者Consul。

Consul-Template提供了一个便捷的方式从Consul中获取存储的值,Consul-Template守护进程会查询Consul实例来更新系统上指定的任何模板。当更新完成后,模板还可以选择运行一些任意的命令。

 
Consul-Template的使用场景

Consul-Template可以查询Consul中的服务目录、Key、Key-values等。这种强大的抽象功能和查询语言模板可以使Consul-Template特别适合动态的创建配置文件。例如:创建Apache/Nginx Proxy Balancers、Haproxy Backends、Varnish Servers、Application Configurations等。

Consul-Template特性

  • Quiescence:Consul-Template内置静止平衡功能,可以智能的发现Consul实例中的更改信息。这个功能可以防止频繁的更新模板而引起系统的波动。
  • Dry Mode:不确定当前架构的状态,担心模板的变化会破坏子系统?无须担心。因为Consul-Template还有Dry模式。在Dry模式,Consul-Template会将结果呈现在STDOUT,所以操作员可以检查输出是否正常,以决定更换模板是否安全。
  • CLI and Config:Consul-Template同时支持命令行和配置文件。
  • Verbose Debugging:即使每件事你都做的近乎完美,但是有时候还是会有失败发生。Consul-Template可以提供更详细的Debug日志信息。

项目地址:https://github.com/hashicorp/consul-template

Consul-Template安装

Consul-Template和Consul一样,也是用Golang实现。因此具有天然可移植性(支持 Linux、windows 和macOS)。安装包仅包含一个可执行文件。Consul-Template安装非常简单,只需要下载对应系统的软件包并解压后就可使用。

这里以Linux系统为例:

 
1 $ wget https://releases.hashicorp.com/consul-template/0.18.3/consul-template_0.18.3_linux_amd64.zip
2 $ unzip consul-template_0.18.3_linux_amd64.zip
3 $ mv consul-template /usr/local/bin/

 

其它系统版本可在这里下载:https://releases.hashicorp.com/consul-template/

Consul-Template常用命令

  1 $ consul-template -h
  2 
  3 Usage: consul-template [options]
  4 
  5   Watches a series of templates on the file system, writing new changes when
  6   Consul is updated. It runs until an interrupt is received unless the -once
  7   flag is specified.
  8 
  9 Options:
 10 
 11   -config=
 12       Sets the path to a configuration file or folder on disk. This can be
 13       specified multiple times to load multiple files or folders. If multiple
 14       values are given, they are merged left-to-right, and CLI arguments take
 15       the top-most precedence.
 16 
 17   -consul-addr=
18 Sets the address of the Consul instance 19 20 -consul-auth= 21 Set the basic authentication username and password for communicating 22 with Consul. 23 24 -consul-retry 25 Use retry logic when communication with Consul fails 26 27 -consul-retry-attempts= 28 The number of attempts to use when retrying failed communications 29 30 -consul-retry-backoff= 31 The base amount to use for the backoff duration. This number will be 32 increased exponentially for each retry attempt. 33 34 -consul-ssl 35 Use SSL when connecting to Consul 36 37 -consul-ssl-ca-cert= 38 Validate server certificate against this CA certificate file list 39 40 -consul-ssl-ca-path= 41 Sets the path to the CA to use for TLS verification 42 43 -consul-ssl-cert= 44 SSL client certificate to send to server 45 46 -consul-ssl-key= 47 SSL/TLS private key for use in client authentication key exchange 48 49 -consul-ssl-server-name= 50 Sets the name of the server to use when validating TLS. 51 52 -consul-ssl-verify 53 Verify certificates when connecting via SSL 54 55 -consul-token= 56 Sets the Consul API token 57 58 -consul-transport-dial-keep-alive= 59 Sets the amount of time to use for keep-alives 60 61 -consul-transport-dial-timeout= 62 Sets the amount of time to wait to establish a connection 63 64 -consul-transport-disable-keep-alives 65 Disables keep-alives (this will impact performance) 66 67 -consul-transport-max-idle-conns-per-host= 68 Sets the maximum number of idle connections to permit per host 69 70 -consul-transport-tls-handshake-timeout= 71 Sets the handshake timeout 72 73 -dedup 74 Enable de-duplication mode - reduces load on Consul when many instances of 75 Consul Template are rendering a common template 76 77 -dry 78 Print generated templates to stdout instead of rendering 79 80 -exec= 81 Enable exec mode to run as a supervisor-like process - the given command 82 will receive all signals provided to the parent process and will receive a 83 signal when templates change 84 85 -exec-kill-signal= 86 Signal to send when gracefully killing the process 87 88 -exec-kill-timeout= 89 Amount of time to wait before force-killing the child 90 91 -exec-reload-signal= 92 Signal to send when a reload takes place 93 94 -exec-splay= 95 Amount of time to wait before sending signals 96 97 -kill-signal= 98 Signal to listen to gracefully terminate the process 99 100 -log-level= 101 Set the logging level - values are "debug", "info", "warn", and "err" 102 103 -max-stale= 104 Set the maximum staleness and allow stale queries to Consul which will 105 distribute work among all servers instead of just the leader 106 107 -once 108 Do not run the process as a daemon 109 110 -pid-file= 111 Path on disk to write the PID of the process 112 113 -reload-signal= 114 Signal to listen to reload configuration 115 116 -retry= 117 The amount of time to wait if Consul returns an error when communicating 118 with the API 119 120 -syslog 121 Send the output to syslog instead of standard error and standard out. The 122 syslog facility defaults to LOCAL0 and can be changed using a 123 configuration file 124 125 -syslog-facility= 126 Set the facility where syslog should log - if this attribute is supplied, 127 the -syslog flag must also be supplied 128 129 -template=