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

Tags: manual 

目录

说明

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

推荐阅读

Copyright @2011-2019 All rights reserved. 转载请添加原文连接,合作请加微信lijiaocn或者发送邮件: [email protected],备注网站合作

友情链接:  系统软件  程序语言  运营经验  水库文集  网络课程  微信网文  发现知识星球