consul-template介紹
consul-template是基於consul自動替換配置文件的應用。
github:https://github.com/hashicorp/consul-template
安裝
地址:https://releases.hashicorp.com/consul-template/
$ wget https://releases.hashicorp.com/consul-template/0.25.0/consul-template_0.25.0_linux_amd64.zip
$ unzip consul-template_0.25.0_linux_amd64.zip
$ mv consul-template /usr/local/bin/
$ consul-template -v
consul-template v0.25.0 (99efa642)
示例:
實驗前準備:
啓動一個consul集羣,詳情請點擊
$ consul agent -dev -ui -client 0.0.0.0
準備consul-template的配置文件tmpl.json,放在當前目錄:
$ vim tmpl.json
consul = "127.0.0.1:8500" //需要連接的consul
template {
source = "./print1.ctmpl" //文件模板
destination = "./print1.py" //需要生成的文件
command = "python ./print1.py" //執行生成的文件
}
文件模板內容
$ vim print1.ctmpl
#!/usr/bin/python
#coding:utf-8
#bottle
iplist = [ {{range service "hello"}} "{{.Address}}",{{end}} ]
port = 8080
for ip in iplist:
print ip
含義是:從consul拿到服務"hello"的ip,並打印出來
$ consul-template -config ./tmpl.json -once
192.168.1.153