用途:与consul集成,通过监听consul中的k-v
来更新配置模板,并执行命令。
# curl -O https://releases.hashicorp.com/consul-template/0.19.5/consul-template<_version_OS>.tgz
# tar -zxf consul-template<_version_OS>.tgz
将解压后的consul-template
添加到系统路径
consul {
address = "127.0.0.1:8500"
token = "xxxxxxxxxxxxxxxxxxxxxxxxx"
retry {
enabled = true
attempts = 12
backoff = "250ms"
max_backoff = "1m"
}
}
reload_signal = "SIGHUP"
kill_signal = "SIGTERM"
max_stale = "5s"
log_level = "ERR"
wait {
min = "5s"
max = "10s"
}
template {
source = "/tmp/consul-template/test.ctmpl"
destination = "/tmp/consul-template/test.txt"
create_dest_dirs = true
command = "cat /tmp/consul-template/test.txt"
command_timeout = "20s"
error_on_missing_key = false
perms = 0644
backup = true
left_delimiter = "{{"
right_delimiter = "}}"
wait {
min = "2s"
max = "10s"
}
}
test.ctmpl
模板文件内容:
# cat test.ctmpl
{{ key "walker/test/text" }}
# consul-template -config ./test.hcl
在consul
界面上添加walker/test/text
key, 并写入内容 hello consul-template!
终端显示:
# consul-template -config ./test.hcl
hello consul-template!
修改 key walker/test/text
, 写入hello consul-template! now i'm changed
终端显示:
# consul-template -config ./test.hcl
hello consul-template!
hello consul-template! now i'm changed
如果只需要consul-template
执行一次,可通过-once
参数来设置。
Eg:
consul-template -template="consul-template.tpl:consul-template.file" -once
https://learn.hashicorp.com/consul/developer-configuration/consul-template