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=<path> 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=<address> Sets the address of the Consul instance
-consul-auth=<username[:password]> Set the basic authentication username and password for communicating with Consul.
-consul-retry Use retry logic when communication with Consul fails
-consul-retry-attempts=<int> The number of attempts to use when retrying failed communications
-consul-retry-backoff=<duration> 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=<string> Validate server certificate against this CA certificate file list
-consul-ssl-ca-path=<string> Sets the path to the CA to use for TLS verification
-consul-ssl-cert=<string> SSL client certificate to send to server
-consul-ssl-key=<string> SSL/TLS private key for use in client authentication key exchange
-consul-ssl-server-name=<string> Sets the name of the server to use when validating TLS.
-consul-ssl-verify Verify certificates when connecting via SSL
-consul-token=<token> Sets the Consul API token
-consul-transport-dial-keep-alive=<duration> Sets the amount of time to use for keep-alives
-consul-transport-dial-timeout=<duration> 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=<int> Sets the maximum number of idle connections to permit per host
-consul-transport-tls-handshake-timeout=<duration> 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=<command> 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> Signal to send when gracefully killing the process
-exec-kill-timeout=<duration> Amount of time to wait before force-killing the child
-exec-reload-signal=<signal> Signal to send when a reload takes place
-exec-splay=<duration> Amount of time to wait before sending signals
-kill-signal=<signal> Signal to listen to gracefully terminate the process
-log-level=<level> Set the logging level - values are "debug", "info", "warn", and "err"
-max-stale=<duration> 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> Path on disk to write the PID of the process
-reload-signal=<signal> Signal to listen to reload configuration
-retry=<duration> 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=<facility> Set the facility where syslog should log - if this attribute is supplied, the -syslog flag must also be supplied
-template=<template> Adds a new template to watch on disk in the format 'in:out(:command)'
-vault-addr=<address> Sets the address of the Vault server
-vault-renew-token Periodically renew the provided Vault API token - this defaults to "true" and will renew the token at half of the lease duration
-vault-retry Use retry logic when communication with Vault fails
-vault-retry-attempts=<int> The number of attempts to use when retrying failed communications
-vault-retry-backoff=<duration> The base amount to use for the backoff duration. This number will be increased exponentially for each retry attempt.
-vault-ssl Specifies is communications with Vault should be done via SSL
-vault-ssl-ca-cert=<string> Sets the path to the CA certificate to use for TLS verification
-vault-ssl-ca-path=<string> Sets the path to the CA to use for TLS verification
-vault-ssl-cert=<string> Sets the path to the certificate to use for TLS verification
-vault-ssl-key=<string> Sets the path to the key to use for TLS verification
-vault-ssl-server-name=<string> Sets the name of the server to use when validating TLS.
-vault-ssl-verify Enable SSL verification for communications with Vault.
-vault-token=<token> Sets the Vault API token
-vault-transport-dial-keep-alive=<duration> Sets the amount of time to use for keep-alives
-vault-transport-dial-timeout=<duration> Sets the amount of time to wait to establish a connection
-vault-transport-disable-keep-alives Disables keep-alives (this will impact performance)
-vault-transport-max-idle-conns-per-host=<int> Sets the maximum number of idle connections to permit per host
-vault-transport-tls-handshake-timeout=<duration> Sets the handshake timeout
-vault-unwrap-token Unwrap the provided Vault API token (see Vault documentation for more information on this feature)
-wait=<duration> Sets the 'min(:max)' amount of time to wait before writing a template (and triggering a command)
#!/bin/sh set -e consul maint -enable -service web -reason "Consul Template updated" service nginx reload consul maint -disable -service web
另外如果你没有安装Consul agent,你可以直接调用API请求:
1 2 3 4 5
#!/bin/sh set -e curl -X PUT "http://$CONSUL_HTTP_ADDR/v1/agent/service/maintenance/web?enable=true&reason=Consul+Template+Updated" service nginx reload curl -X PUT "http://$CONSUL_HTTP_ADDR/v1/agent/service/maintenance/web?enable=false"
upstream consul { zone upstream-consul 64k; server 192.168.2.210:8300 max_fails=3 fail_timeout=60 weight=1; server 192.168.2.211:8300 max_fails=3 fail_timeout=60 weight=1; server 192.168.2.212:8300 max_fails=3 fail_timeout=60 weight=1;
} upstream hi-linux { zone upstream-hi-linux 64k; server 192.168.2.210:8080 max_fails=3 fail_timeout=60 weight=1;
} upstream web { zone upstream-web 64k; server 192.168.2.210:8080 max_fails=3 fail_timeout=60 weight=1;
}
server { listen 80 default_server;
location / { root /usr/share/nginx/html/; index index.html; }