Confd是一个轻量级的配置管理工具。
经过查询后端存储,结合配置模板引擎,保持本地配置最新,同时具有按期探测机制,配置变动自动reload。
对应的后端存储能够是etcd,redis、zookeeper等等
咱们以etcd为后端来演示confd的使用,用最简单粗暴的方式教你们学会动态生成配置php
etcd -listen-client-urls="http://0.0.0.0:2379" --advertise-client-urls="http://0.0.0.0:2379" &
etcd搭建若是不会能够查看前面一篇文章《从零开始搭建etcd分布式存储系统+web管理界面》html
# 下载 wget https://github.com/kelseyhightower/confd/releases/download/v0.16.0/confd-0.16.0-linux-amd64 # 安装 mv confd-0.16.0-linux-amd64 /usr/local/bin/confd chmod +x /usr/local/bin/confd #检查是否安装成功 root@ubuntu:/home/chenqionghe/test/confd# confd --version confd 0.16.0 (Git SHA: 7217b0ca, Go Version: go1.10.2)
咱们先建立node
mkdir -p /etc/confd/{conf.d,templates}
confd的配置文件,主要包含配置的生成逻辑,例如模板源,后端存储对应的keys,命令执行等。
templates:配置模板Template,即基于不一样组件的配置,修改成go语言的模板文件。linux
[template] # 模板文件路径 src = "chenqionghe.tmpl" # 生成最终文件路径 dest = "/home/chenqionghe/test/confd/gym-data.txt" keys = [ "/chenqionghe/deap_squat", "/chenqionghe/bench_press", "/chenqionghe/dead_lift", ] # 生成文件后执行的命令 reload_cmd = "echo 'light weight baby' >> /home/chenqionghe/test/confd/reaload.txt"
[陈琼和] 深蹲 = {{getv "/chenqionghe/deap_squat"}} 卧槽 = {{getv "/chenqionghe/bench_press"}} 硬拉 = {{getv "/chenqionghe/dead_lift"}}
endpoints=http://127.0.0.1:2379 etcdctl --endpoints=$endpoints set /chenqionghe/deap_squat '130kg' etcdctl --endpoints=$endpoints set /chenqionghe/bench_press '100kg' etcdctl --endpoints=$endpoints set /chenqionghe/dead_lift '160kg'
confd支持以daemon或者onetime两种模式运行
onetime模式:只会生成一次配置,以后key不管变化不会再生成nginx
confd -onetime -backend etcd -node http://127.0.0.1:2379
confd -watch -backend etcd -node http://127.0.0.1:2379 &
咱们以daemon模式运行,而后改变key的值,观察文件变化,
能够看到reload.txt文件在持续的追加light weight baby
git
gym-data.txt跟随3个key的变化不断的更新
github
confd使用的模板就是go语言的template,对go语言熟悉的同窗应该会以为很是简单
confd已经集成了不少模板函数,参考连接web
咱们大概知道了confd的原理
1.读取配置文件 -> 2.使用模板生成指定文件 -> 3.运行重载命令(可选)redis
因此基本使用配置和reload命令的地方均可以使用confd,好比下边的需求ubuntu
只有想不到,没有作不到,之前想过本身写一个nginx的动态生成upstream,没想到已经有人写出来了,真的是厉害,小伙伴们赶忙high起来吧