在consul-template沒出現以前,你們構建服務發現系統,大多采用的是zookeeper、etcd+confd這樣相似的系統,以前寫過一篇consul+confd的文,講的是如何動態生成配置文件的,現在consul官方推出了本身的模板系統,就是consul-template,這樣的話動態的配置系統能夠分化爲etcd+confd和consul+consul-template兩大陣營。consul是一個和etcd相似但又強於etcd的系統,關於etcd和consul能夠翻閱之前的文章,consul-template的定位就和confd差很少同樣了,confd的後端能夠是etcd或者consul,相信consul搭配consul-template能發揮更大的效果。consul-template提供了一個便捷的方式從consul中獲取存儲的值,consul-template守護進程會查詢consul實例,來更新系統上指定的任何模板,當更新完成後,模板能夠選擇運行一些任意的命令。node
consul template的使用場景:consul template能夠查詢consul中的服務目錄、key、key-values等。這種強大的抽象功能和查詢語言模板可使consul template特別適合動態的建立配置文件。例如:建立apache/nginx proxy balancers、haproxy backends、varnish servers、application configurations。nginx
consul template的特性:git
quiescence:consul template內製靜止平衡功能,能夠智能的發現consul實例中的更改信息。這個功能能夠防止頻繁的更新模板而引發系統的波動。 dry mode:不肯定當前架構的狀態?擔憂模板的變化會破壞子系統?無須擔憂,由於consul template還有-dry模式。在dry模式,consul template會將結果呈如今STDOUT,因此操做員能夠檢查輸出是否正常,以決定更換模板是否安全 CLI and Config:若是你喜歡在命令行上指定一切,consul template均可以hold住。隨着內置HCL的支持,consul template接收一個配置文件,命令行參數,或者二者的混合。經過這種方式你能夠繼續使用你如今已有的配置管理工具和consul template來配合。 verbose debugging:即便每件事你都作的近乎完美,可是有時候仍是會有失敗發生。consul template能夠提供更詳細的debug日誌信息。
下載consul-templategithub
繼承了consul的風格,consul-template下載下來解壓後就是一個二進制文件,沒有其餘多餘的內容,downloadgolang
查看幫助web
執行consul-template -h便可看到consul-temple的使用參數redis
-auth=<user[:pass]> 設置基本的認證用戶名和密碼 -consul=<address> 設置Consul實例的地址 -max-stale=<duration> 查詢過時的最大頻率,默認是1s -dedup 啓用重複數據刪除,當許多consul template實例渲染一個模板的時候能夠下降consul的負載 -ssl 使用https鏈接Consul使用SSL -ssl-verify 經過SSL鏈接的時候檢查證書 -ssl-cert SSL客戶端證書發送給服務器 -ssl-key 客戶端認證時使用的SSL/TLS私鑰 -ssl-ca-cert 驗證服務器的CA證書列表 -token=<token> 設置Consul API的token -syslog 把標準輸出和標準錯誤重定向到syslog,syslog的默認級別是local0。 -syslog-facility=<f> 設置syslog級別,默認是local0,必須和-syslog配合使用 -template=<template> 增長一個須要監控的模板,格式是:'templatePath:outputPath(:command)',多個模板則能夠設置屢次 -wait=<duration> 當呈現一個新的模板到系統和觸發一個命令的時候,等待的最大最小時間。若是最大值被忽略,默認是最小值的4倍。 -retry=<duration> 當在和consul api交互的返回值是error的時候,等待的時間,默認是5s。 -config=<path> 配置文件或者配置目錄的路徑 -pid-file=<path> PID文件的路徑 -log-level=<level> 設置日誌級別,能夠是"debug","info", "warn" (default), and "err" -dry Dump生成的模板到標準輸出,不會生成到磁盤 -once 運行consul-template一次後退出,不以守護進程運行 -reap 子進程自動收割
下面看一些例子:apache
consul實例:demo.consul.io後端
模板:/tmp/template.ctmplapi
模板輸出路徑:/tmp/result
1 運行consul-temple做爲一個服務
consul-template \ -consul demo.consul.io \ -template "/tmp/template.ctmpl:/tmp/result"
2 查詢本地consl實例,生成模板後重啓nginx,若是consul不可用,若是api故障則每30s嘗試檢測一次值,consul-template運行一次後退出
consul-template \ -consul 127.0.0.1:8500 \ -template "/tmp/template.ctmpl:/var/www/nginx.conf:service nginx restart" \ -retry 30s \ -once
3 查詢一個實例,渲染多個模板,而後重啓相關服務
consul-template \ -consul my.consul.internal:6124 \ -template "/tmp/nginx.ctmpl:/var/nginx/nginx.conf:service nginx restart" \ -template "/tmp/redis.ctmpl:/var/redis/redis.conf:service redis restart" \ -template "/tmp/haproxy.ctmpl:/var/haproxy/haproxy.conf"
4 查詢一個實例,dump模板到標準輸出,參數中的-template則會被忽略
consul-template \ -consul my.consul.internal:6124 \ -template "/tmp/template.ctmpl:/tmp/result:service nginx restart" -dry
以上參數除了在命令行使用,也能夠直接配置在文件中,下面看看Consul-Template的配置文件,簡稱HCL(HashiCorp Configuration Language),它是和JSON兼容的,下面看個例子:
consul = "127.0.0.1:8500" token = "abcd1234" retry = "10s" max_stale = "10m" log_level = "warn" pid_file = "/path/to/pid" wait = "5s:10s" vault { address = "https://vault.service.consul:8200" token = "abcd1234" renew = true ssl { // ... } } auth { enabled = true username = "test" password = "test" } ssl { enabled = true verify = false cert = "/path/to/client/cert" key = "/path/to/client/key" ca_cert = "/path/to/ca" } syslog { enabled = true facility = "LOCAL5" } deduplicate { enabled = true prefix = "consul-template/dedup/" } template { source = "/path/on/disk/to/template.ctmpl" destination = "/path/on/disk/where/template/will/render.txt" command = "restart service foo" command_timeout = "60s" perms = 0600 backup = true left_delimiter = "{{" right_delimiter = "}}" wait = "2s:6s" }
以上並非全部的fields都須要,好比Vault你可能就不須要,因此你就不須要指定Vault配置。以上就是配置文件。
下面看看配置模板到底怎麼寫,模板文件的語法和Go template的格式同樣,confd也是遵循Go template的。
先看看API 功能語法:
datacenters:在consul目錄中查詢全部的datacenters,{{datacenters}} file:讀取並輸出本地磁盤上的文件,若是沒法讀取,則報錯,{{file "/path/to/local/file"}} key:查詢consul中該key的值,若是沒法轉換成一個類字符串的值,則會報錯,{{key "service/redis/maxconns@east-aws"}} east-aws指定的是數據中心,{{key "service/redis/maxconns"}} key_or_default:查詢consul中該key的值,若是key不存在,則使用指定的值代替,{{key_or_default "service/redis/maxconns@east-aws" "5"}} ls:在consul中查詢給定前綴的key的頂級域值,{{range ls "service/redis@east-aws"}} {{.Key}} {{.Value}}{{end}} node:查詢consul目錄中的單個node,若是不指定node,則是當前agent的,{{node "node1"}} nodes:查詢consul目錄中的全部nodes,你也能夠指定datacenter,{{nodes "@east-aws"}} service:查詢consul中匹配的service組,{{service "release.web@east-aws"}}或者{{service "web"}},也能夠返回一組HealthService服務{{range service "web@datacenter"}} server {{.Name}} {{.Address}}:{{.Port}}{{end}},默認值返回健康的服務,若是你想返回全部服務,則{{service "web" "any"}} services:查詢consul目錄中的全部services,{{services}},也能夠指定datacenter:{{services "@east-aws"}} tree:查詢consul中給定前綴的全部K/V值,{{range tree "service/redis@east-aws"}} {{.Key}} {{.Value}}{{end}}
再看看輔助函數語法:
byKey、byTag、contains、env、explode、in、loop、trimSpace、join、parseBool、parseFloat、parseInt、parseJSON、parseUint、regexMatch、regexReplaceAll、replaceAll、split、timestamp、toJSON等函數可使用,具體用法看官文