confd:本地配置文件的管理工具confd

作者: 李佶澳   转载请保留:原文地址   发布时间:2017/09/21 16:00:08

说明

confd从etcd或者consul等获取数据更新,更新本地的配置文件后,重新加载目标应用。

confd支持一下backends:

etcd
consul
vault
environment variables
redis
zookeeper
dynamodb
rancher
ssm (AWS Simple Systems Manager Parameter Store)

使用

在backends中写入配置项的值,以etcd为例:

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"

在/etc/confd/conf.d/中创建应用模版

[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/templates中创建配置模版

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             $host;
        proxy_set_header  X-Real-IP        $remote_addr;
        proxy_set_header  X-Forwarded-For  $proxy_add_x_forwarded_for;
   }
}

启动confd之后,confd就会监听etcd中的key,根据应用模版中的内容,从配置模版生成配置文件

confd -onetime -backend etcd -node http://127.0.0.1:2379
confd -backend etcd -node http://127.0.0.1:2379

配置文件生成后,confd执行应用模版中指定命令进行配置文件的重新加载。

如果使用-onetime,则只生成一次,不持续监听、动态更新。

参考

  1. confd
  2. confd quick start

欢迎加微信,最好备注姓名和方向

QQ交流群

区块链实践互助QQ群:576555864

Kubernetes实践互助QQ群:947371129

Prometheus实践互助QQ群:952461804

API网关Kong实践互助QQ群:952503851

Ansible实践互助QQ群:955105412

Copyright @2011-2018 All rights reserved. 转载请添加原文连接,合作请加微信lijiaocn或者发送邮件: lijiaocn@foxmail.com,备注网站合作 友情链接: lijiaocn github.com