Confd 介绍

发布时间: 更新时间: 总字数:835 阅读时间:2m 作者: 分享 复制网址

CONFD是有一套集中化配置管理系统,一方面提供统一的配置管理,另一方面提供配置变更的自动下发,及时生效。

简介

使用

部署

wget https://github.com/kelseyhightower/confd/releases/download/v0.16.0/confd-0.16.0-linux-amd64

chmod +x confd-0.16.0-linux-amd64
mv confd-0.16.0-linux-amd64 /usr/local/bin/confd

confd --help

confdir

  -confdir string
    	confd conf directory (default "/etc/confd")
  • 创建配置文件夹
sudo mkdir -p /etc/confd/{conf.d,templates}
  • conf.d: Template Resources,包含:prefixsrcdestkeysownermodecheck_cmdreload_cmd
  • templates: Template,参照https://golang.org/pkg/text/template/语法规范
  • Template Resources

示例/etc/confd/conf.d/app-tengine.toml

[template]
prefix = "/"
src = "tengine.tmpl"
dest = "/etc/tengine/conf.d/app_upstreams.conf"
keys = [
  "/apps/upstreams",
]
owner = "root"
mode = "0644"
check_cmd = "tengine -t -c /etc/tengine/tengine.conf"
reload_cmd = "echo reloading tengine && tengine -s reload"

参数说明:

  • dest (string) - The target file.
  • keys (array of strings) - An array of keys.
  • src (string) - The relative path of a configuration template.
  • gid (int, optional) - The gid that should own the file. Defaults to the effective gid.
  • mode (string, optional) - The permission mode of the file.
  • uid (int, optional) - The uid that should own the file. Defaults to the effective uid.
  • reload_cmd (string, optional) - The command to reload config.
  • check_cmd (string, optional) - The command to check config. Use {{.src}} to reference the rendered * source template.
  • prefix (string, optional) - The string to prefix to keys.
  • Template

示例/etc/confd/templates/tengine.tmpl

#for prod env
{{- range gets "/apps/upstreams/*"}}
upstream {{base .Key}} {
  {{ range jsonArray (getv .Key)}} server {{.}} max_fails=1 fail_timeout=30s; {{end}}
}
{{end}}

语法参考:https://github.com/kelseyhightower/confd/blob/master/docs/templates.md

后端存储

  -backend string
    	backend to use (default "etcd")

confd的后端可以是consul、etcd、redis等

etcdctl put "/apps/upstreams/p.1" '["10.0.0.2:8081", "10.0.0.2:8082"]'
etcdctl put "/apps/upstreams/p.2" '["10.0.0.2:8081", "10.0.0.2:8082"]'
  • 查看数据
etcdctl get /apps/upstreams/p.1

启动confd

  • 参数
  -node value
    	list of backend nodes
  • 命令
# enable watch support
confd -watch -backend etcdv3 -node http://127.0.0.1:2379 & #或采用-interval 5
# run once and exit
confd -onetime -backend etcdv3 -node http://127.0.0.1:2379 &
  • 日志
2018-06-09T11:03:29+08:00 a1.xiexianbin.cn confd[15395]: INFO Backend set to etcdv3
2018-06-09T11:03:29+08:00 a1.xiexianbin.cn confd[15395]: INFO Starting confd
2018-06-09T11:03:29+08:00 a1.xiexianbin.cn confd[15395]: INFO Backend source(s) set to http://127.0.0.1:2379
2018-06-09T11:03:29+08:00 a1.xiexianbin.cn confd[15395]: INFO Target config /etc/tengine/conf.d/app_upstreams.conf out of sync
2018-06-09T11:03:29+08:00 a1.xiexianbin.cn confd[15395]: INFO Target config /etc/tengine/conf.d/app_upstreams.conf has been updated
...
  • 目标文件
#for prod env
upstream p.1 {
   server 10.0.0.2:8081 max_fails=1 fail_timeout=30s;  server 10.0.0.2:8082 max_fails=1 fail_timeout=30s;
}

upstream p.2 {
   server 10.0.0.2:8081 max_fails=1 fail_timeout=30s;  server 10.0.0.2:8082 max_fails=1 fail_timeout=30s;
}
  • 更新
etcdctl put "/apps/upstreams/p.3" '["10.0.0.2:8081", "10.0.0.2:8082"]'

配置文件自动更新

# etcdctl put "/apps/upstreams/p.3" '["10.0.0.2:8081", "10.0.0.2:8082"]'
2018-06-09T14:14:04+08:00 a1.xiexianbin.cn confd[25974]: INFO /etc/tengine/conf.d/app_upstreams.conf has md5sum ebaf0788995ab350200505ea5b31373a should be 4207db050f6cb76d5cb4c59444034af8
2018-06-09T14:14:04+08:00 a1.xiexianbin.cn confd[25974]: INFO Target config /etc/tengine/conf.d/app_upstreams.conf out of sync
OK
2018-06-09T14:14:04+08:00 a1.xiexianbin.cn confd[25974]: INFO Target config /etc/tengine/conf.d/app_upstreams.conf has been updated

# cat /etc/tengine/conf.d/app_upstreams.conf
#for prod env
upstream p.1 {
   server 10.0.0.2:8081 max_fails=1 fail_timeout=30s;  server 10.0.0.2:8082 max_fails=1 fail_timeout=30s;
}

upstream p.2 {
   server 10.0.0.2:8081 max_fails=1 fail_timeout=30s;  server 10.0.0.2:8082 max_fails=1 fail_timeout=30s;
}

upstream p.3 {
   server 10.0.0.2:8081 max_fails=1 fail_timeout=30s;  server 10.0.0.2:8082 max_fails=1 fail_timeout=30s;
}
Home Archives Categories Tags Statistics
本文总阅读量 次 本站总访问量 次 本站总访客数