confd是一个轻量级的配置管理工具,源码地址:https://github.com/kelseyhightower/confd,它可以将配置信息存储在etcd、consul、dynamodb、redis以及zookeeper等。confd定期会从这些存储节点pull最新的配置,然后重新加载服务,完成配置文件的更新。
一、confd 安装配置
- 安装
mkdir -p /root/go/src/github.com/kelseyhightower
git clone https://github.com/kelseyhightower/confd.git /root/go/src/github.com/kelseyhightower/confd
cd /root/go/src/github.com/kelseyhightower/confd
make
mv bin/confd /usr/local/bin
-
confd 语法配置
- 配置文件
# cat conf.d/www.imlcs.com.toml
[template]
src = "www.imlcs.com.tmpl"
dest = "/etc/nginx/conf.d/www.imlcs.com.conf"
keys = [
"/nginx/www",
"/domain/www",
]
check_cmd = "nginx -t"
reload_cmd = "nginx -s reload"
- 参数说明:
src:模板文件名
dest:生成目标文件的文件名及位置
keys:监控的keys,是一个数组,用到哪个key加入里面就行
check_cmd:检查命令,可以不用加
relaod_cmd:重新加载进程的命令
- 模板文件
# cat templates/www.imlcs.com.tmpl
upstream {{"/domain/www"}} { # 获取单个 key 的值
{{range getvs "/nginx/www/*"}} # 循环获取当前目录下的所有 key 的值
server {{.}};
{{end}}
}
server {
listen 80;
server_name {{getv "/domain/www"}};
proxy_pass http://www;
}
二、测试
- confd 启动命令
confd --watch -backend etcd -node http://127.0.0.1:2379
- 向 etcd 中写入数据
etcdctl set /nginx/www/node1 192.168.1.11:80
etcdctl set /nginx/www/node2 192.168.1.12:80
etcdctl set /nginx/www/node3 192.168.1.13:80
etcdctl set /domain/www www.imlcs.com
- 生成文件的内容
# cat /etc/nginx/conf.d/www.imlcs.com.conf
upstream /domain/www {
server 192.168.1.11:80;
server 192.168.1.12:80;
server 192.168.1.13:80;
}
server {
listen 80;
server_name www.imlcs.com;
proxy_pass http://www;
}
- supvervisor 管理 confd 进程
# cat /etc/supervisord.d/confd.conf
[program:confd]
command=/usr/local/bin/confd --watch -backend etcd -node http://127.0.0.1:2379
autostart=true
startsecs=5
autorestart=true
startretries=3
user=root
redirect_stderr = true
stdout_logfile_maxbytes=20MB
stdout_logfile_backups=20
stopasgroup=true
killasgroup=true
exitcodes=0,2
stopsignal=KILL