Etcd+Confd实现配置文件动态更新

简介

file

如上图是一个很简单的架构,生产环境中经常会进行灰度发布,需要下掉一部分的节点。如果靠人工操作很容易错误,这里通过Etcd和Confd来实现nginx upstream的动态更新。

类似的,自动化部署时服务的环境变量等也可存入etcd(配置中心website页面),coredns等配置文件内容均可存入etcd,由confd动态刷新。

etcd: 分布式KV存储系统,一般用于共享配置和服务注册与发现。

confd:管理本地应用配置文件,使用etcd或consul存储的数据渲染模板,还支持redis、zookeeper等, 通过watch定期监测对应的etcd中目录变化,获取最新的Value,然后渲染模板,更新配置文件。

安装

  • 安装etcd
    yum -y install etcdsystemctl start etcd
  • 安装confd
    wget https://github.com/kelseyhightower/confd/releases/download/v0.16.0/confd-0.16.0-linux-arm64mkdir -p /etc/etcd/{conf.d,templates}mv confd-0.16.0-linux-arm64 /usr/bin/confd
  • conf.d 目录存放.toml配置文件
  • templates 目录存放.tmpl配置模版文件

配置

创建nginx配置和模版

配置文件cat conf.d/test.conf.toml

[
template]src = "test.conf.tmpl"
dest = "/tmp/test.conf"
keys = [ "/nginx",]
check_cmd = "/usr/sbin/nginx -t -c {{.src}}"
reload_cmd = "/usr/sbin/nginx -s reload"
]

模版文件cat templates/test.conf.tmpl

upstream www_{{getv "/nginx/www/server/server_name"}} {   
{{range getvs "/nginx/www/upstream/*"}}       
server {{.}};  
{{end}}
}
server {   
server_name       
{{getv "/nginx/www/server/server_name"}};  
location / {       
proxy_pass        http://www_{{getv "/nginx/www/server/server_name"}};        proxy_set_header Host $host;  
proxy_set_header X-Real-IP $remote_addr;  
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;   
proxy_set_header X-Forwarded-Proto https;     
proxy_redirect    off;  
}
}

配置etcd

etcdctl set /nginx/https/www/server/server_name test.com
etcdctl set /nginx/https/www/upstream/server1 192.168.1.110
etcdctl set /nginx/https/www/upstream/server2 192.168.1.111

启动confd监听

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

查看生产的nginx配置文件

 cat /tmp/test.confupstream www_test.com
 {        
 server 192.168.1.110;        
 server 192.168.1.111;}
 server {   
 server_name         test.com;    location / {        
 proxy_pass        http://www_test.com;        
 proxy_set_header Host $host;        
 proxy_set_header X-Real-IP $remote_addr;      
 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;      
 proxy_set_header X-Forwarded-Proto https;        
 proxy_redirect    off;    }
 }

配置文件生成完成

本文由博客一文多发平台 OpenWrite 发布!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值