confd和etcd实现配置管理及模板使用

关于confd是什么?

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客户端。 

etcd客户端。

Create the confdir

创建配置目录及模板的目录

Python

sudo mkdir -p /etc/confd/{conf.d,templates}

Create a template resource config

文件的后缀要有.toml  ,这个也算个规范吧 。  toml本身也是个配置的数据类型,有点像configparse和yaml似的。

Python

[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

Process the template

调用confd命令来获取,backend是指明客户端,下面用的时etcd

然后执行后的结果是:

那么下载出来的配置文件是这个样子

Advanced Example

Create template resources

/etc/confd/conf.d/myapp-nginx.toml

/etc/confd/conf.d/yourapp-nginx.toml

Create the source template

/etc/confd/templates/nginx.tmpl

confd etcd 是在一个老外的博客看到的,本来也想把地址给贴上来,但是找不到了。

confd也是个有趣的应用,他其实就是简化了自己关于管理配置和模板开发,但是问题来了, 我是个pythoner程序员,对于python的第三方的模板  jinja2 mako 是相当的熟悉,但是如果学习confd的话,我还需要学习confd自己特定的语法。 这有些不效率了了。

这里再贴下 confd对于模板渲染的语法,貌似没有for这个函数,然后还缺少自定义函数的功能,这个让我很是蛋疼。

exists

如果这个key在etcd定义了的话?

get

如果这个key没有,他不会返回一个error

Python

{{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.

getv

Returns the value as a string where key matches its argument. Returns an error if key is not found.

Python

value: {{getv "/key"}}

getvs

Returns all values, []string, where key matches its argument. Returns an error if key is not found.

Python

{{range getvs "/*"}}

    value: {{.}}

{{end}}

datetime

Alias for time.Now

Python

# Generated by confd {{datetime}}

Outputs:

Python

# Generated by confd {datetime.Format("Jan 2, 2006 at 3:04pm (MST)")}

Outputs:

Python

# Generated by confd Jan 23, 2015 at 1:34pm (EST)

See the time package for more usage: http://golang.org/pkg/time/

split

可以对于一条数据进行自定义切分,splite

json

Returns an map[string]interface{} of the json value.

猜你喜欢

转载自www.linuxidc.com/Linux/2016-12/137934.htm