通过 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系统为例:

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

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

Consul-Template常用命令

$ consul-template -h

Usage: consul-template [options]

  Watches a series of templates on the file system, writing new changes when
  Consul is updated. It runs until an interrupt is received unless the -once
  flag is specified.

Options:

  -config=
      Sets the path to a configuration file or folder on disk. This can be
      specified multiple times to load multiple files or folders. If multiple
      values are given, they are merged left-to-right, and CLI arguments take
      the top-most precedence.

  -consul-addr=
Sets the address of the Consul instance -consul-auth= Set the basic authentication username and password for communicating with Consul. -consul-retry Use retry logic when communication with Consul fails -consul-retry-attempts= The number of attempts to use when retrying failed communications -consul-retry-backoff= The base amount to use for the backoff duration. This number will be increased exponentially for each retry attempt. -consul-ssl Use SSL when connecting to Consul -consul-ssl-ca-cert= Validate server certificate against this CA certificate file list -consul-ssl-ca-path= Sets the path to the CA to use for TLS verification -consul-ssl-cert= SSL client certificate to send to server -consul-ssl-key= SSL/TLS private key for use in client authentication key exchange -consul-ssl-server-name= Sets the name of the server to use when validating TLS. -consul-ssl-verify Verify certificates when connecting via SSL -consul-token= Sets the Consul API token -consul-transport-dial-keep-alive= Sets the amount of time to use for keep-alives -consul-transport-dial-timeout= Sets the amount of time to wait to establish a connection -consul-transport-disable-keep-alives Disables keep-alives (this will impact performance) -consul-transport-max-idle-conns-per-host= Sets the maximum number of idle connections to permit per host -consul-transport-tls-handshake-timeout= Sets the handshake timeout -dedup Enable de-duplication mode - reduces load on Consul when many instances of Consul Template are rendering a common template -dry Print generated templates to stdout instead of rendering -exec= Enable exec mode to run as a supervisor-like process - the given command will receive all signals provided to the parent process and will receive a signal when templates change -exec-kill-signal= Signal to send when gracefully killing the process -exec-kill-timeout= Amount of time to wait before force-killing the child -exec-reload-signal= Signal to send when a reload takes place -exec-splay= Amount of time to wait before sending signals -kill-signal= Signal to listen to gracefully terminate the process -log-level= Set the logging level - values are "debug", "info", "warn", and "err" -max-stale= Set the maximum staleness and allow stale queries to Consul which will distribute work among all servers instead of just the leader -once Do not run the process as a daemon -pid-file= Path on disk to write the PID of the process -reload-signal= Signal to listen to reload configuration -retry= The amount of time to wait if Consul returns an error when communicating with the API -syslog Send the output to syslog instead of standard error and standard out. The syslog facility defaults to LOCAL0 and can be changed using a configuration file -syslog-facility= Set the facility where syslog should log - if this attribute is supplied, the -syslog flag must also be supplied -template=