当前位置: 首页 > 工具软件 > consul-retry > 使用案例 >

consul-template 使用

萧越泽
2023-12-01

consul-template

用途:与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-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

# 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

More

如果只需要consul-template 执行一次,可通过-once 参数来设置。

Eg:

consul-template -template="consul-template.tpl:consul-template.file" -once

参考文档

https://learn.hashicorp.com/consul/developer-configuration/consul-template

https://github.com/hashicorp/consul-template

 类似资料: