关于confd是什么?
他是一个可以在etcd和consul的基础上实现配置管理的工具。 etcd和consul在功能上是有些重叠的,所以咱们就拿etcd来测试吧。
再简单来描述下conf,他是可以从etcd里面获取kv数据,然后通过咱们提前定制的模板,渲染配置文件。。。。 然后可以check_md 和 reload_md
文章本来在github看到的,自己又重新加了点料。
https://github.com/kelseyhightower/confd/blob/master/docs/quick-start-guide.md
首先增加keys关键字,我这边是测试,所以用的时etcdctl客户端,做实际应用还是推荐用python的etcd客户端。
etcdctl set /myapp/database/url db.example.com etcdctl set /myapp/database/user rob
Create the confdir
创建配置目录及模板的目录
sudo mkdir -p /etc/confd/{conf.d,templates}
Create a template resource config
文件的后缀要有.toml ,这个也算个规范吧 。 toml本身也是个配置的数据类型,有点像configparse和yaml似的。
[template] src = "myconfig.conf.tmpl" dest = "/tmp/myconfig.conf" keys = [ "/myapp/database/url", "/myapp/database/user", ]
Create the source template
创建动态的模板,里面需要有可以获取key的动态逻辑。
/etc/confd/templates/myconfig.conf.tmpl
[myconfig] database_url = {{getv "/myapp/database/url"}} database_user = {{getv "/myapp/database/user"}}
Process the template
调用confd命令来获取,backend是指明客户端,下面用的时etcd
confd -onetime -backend etcd -node 127.0.0.1:4001
然后执行后的结果是:
2014-07-08T20:38:36-07:00 confd[16252]: WARNING Skipping confd config file. 2014-07-08T20:38:36-07:00 confd[16252]: INFO Target config /tmp/myconfig.conf out of sync 2014-07-08T20:38:36-07:00 confd[16252]: INFO Target config /tmp/myconfig.conf has been updated
那么下载出来的配置文件是这个样子
cat /tmp/myconfig.conf
# This a comment [myconfig] database_url = db.example.com database_user = rob
Advanced Example
etcdctl set /myapp/subdomain myapp etcdctl set /myapp/upstream/app2 "10.0.1.100:80" etcdctl set /myapp/upstream/app1 "10.0.1.101:80" etcdctl set /yourapp/subdomain yourapp etcdctl set /yourapp/upstream/app2 "10.0.1.102:80" etcdctl set /yourapp/upstream/app1 "10.0.1.103:80"
Create template resources
/etc/confd/conf.d/myapp-nginx.toml
[template] prefix = "/myapp" src = "nginx.tmpl" dest = "/tmp/myapp.conf" owner = "nginx" mode = "0644" keys = [ "/subdomain", "/upstream", ] check_cmd = "/usr/sbin/nginx -t -c {{.src}}" reload_cmd = "/usr/sbin/service nginx reload"
/etc/confd/conf.d/yourapp-nginx.toml
[template] prefix = "/yourapp" src = "nginx.tmpl" dest = "/tmp/yourapp.conf" owner = "nginx" mode = "0644" keys = [ "/subdomain", "/upstream", ] check_cmd = "/usr/sbin/nginx -t -c {{.src}}" reload_cmd = "/usr/sbin/service nginx reload"
Create the source template
/etc/confd/templates/nginx.tmpl
upstream {{getv "/subdomain"}} {
{{range getvs "/upstream/*"}}
server {{.}};
{{end}}
}
server {
server_name {{getv "/subdomain"}}.example.com;
location / {
proxy_pass http://{{getv "/subdomain"}};
proxy_redirect off;
proxy_set_header Host remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
confd etcd 是在一个老外的博客看到的,本来也想把地址给贴上来,但是找不到了。
confd也是个有趣的应用,他其实就是简化了自己关于管理配置和模板开发,但是问题来了, 我是个pythoner程序员,对于python的第三方的模板 jinja2 mako 是相当的熟悉,但是如果学习confd的话,我还需要学习confd自己特定的语法。 这有些不效率了了。
这里再贴下 confd对于模板渲染的语法,貌似没有for这个函数,然后还缺少自定义函数的功能,这个让我很是蛋疼。
exists
如果这个key在etcd定义了的话?
{{if exists "/key"}} value: {{getv "/key"}} {{end}}
get
如果这个key没有,他不会返回一个error
{{with get "/key"}} key: {{.Key}} value: {{.Value}} {{end}}
gets
Returns all KVPair, []KVPair, where key matches its argument. Returns an error if key is not found.
{{range gets "/*"}} key: {{.Key}} value: {{.Value}} {{end}}
getv
Returns the value as a string where key matches its argument. Returns an error if key is not found.
value: {{getv "/key"}}
getvs
Returns all values, []string, where key matches its argument. Returns an error if key is not found.
{{range getvs "/*"}} value: {{.}} {{end}}
datetime
Alias for time.Now
# Generated by confd {{datetime}}
Outputs:
# Generated by confd 2015-01-23 13:34:56.093250283 -0800 PST
# Generated by confd {datetime.Format("Jan 2, 2006 at 3:04pm (MST)")}
Outputs:
# Generated by confd Jan 23, 2015 at 1:34pm (EST)
See the time package for more usage: http://golang.org/pkg/time/
split
可以对于一条数据进行自定义切分,splite
{{ ParseError: KaTeX parse error: Expected 'EOF', got '}' at position 41: …/service") ":" }̲}
host: {{i…url 0}}
port: {{index $url 1}}
json
Returns an map[string]interface{} of the json value.
貌似 confd 现在是支持 for 循环的功能吧
不知道呀….
最后那个json没有例子么?你参考的网址是多少?
实在是不记得了….
https://github.com/kelseyhightower/confd/blob/master/docs/quick-start-guide.md
博主,我用python连接etcd的时候直接报错Connection to etcd failed due to MaxRetryError,这是为啥
大哥,能否在写点etcd的文章
恩,下次写个etcd集群模式