阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

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

153次阅读
没有评论

共计 2039 个字符,预计需要花费 6 分钟才能阅读完成。

关于 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 = “https://www.linuxidc.com/Linux/2016-12/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.

本文永久更新链接地址 :http://www.linuxidc.com/Linux/2016-12/137934.htm

正文完
星哥说事-微信公众号
post-qrcode
 
星锅
版权声明:本站原创文章,由 星锅 2022-01-21发表,共计2039字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中