天天看點

consul-template詳解

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-template -config ./tmpl.json -once
192.168.1.153      

繼續閱讀