失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > 使用etcd+confd管理nginx配置

使用etcd+confd管理nginx配置

时间:2018-12-05 10:26:40

相关推荐

使用etcd+confd管理nginx配置

1、前言

最近在项目中用nginx做反向代理,需要动态生成nginx的配置。大概流程是用户在页面上新增域名、http或https协议以及端口信息,后台会根据域名自动生成一个nginx的server配置,在nginx.conf配置文件中使用include将所有的server配置加载进来。遇到一个问题就是如何动态生成nginx的配置,以及配置更新。在此之前也接触过配置生成及更新,当时配置的格式都是基于xml的,使用protobuf与xml结合,先采用protobuf定义好配置的数据结构,然后将protobuf转换为xml配置文件。配置更新通过判断配置文件的md5是否发生变化进行。而如今接触的配置是nginx的配置,是一套自定义的配置。看了一眼nginx的配置源码,用一个void**** conf 来表示,顿时吓死宝宝了,膜拜作者编程技能。nginx配置代码内存组织是相当的牛逼,通用性非常强,有兴趣可以认真研究一下。我要自动生成的配置是如下所示:

upstreamwww_test{server196.75.121.112:443;(动态生成)}server{listen443ssl;(动态生成);(动态生成)ssl_protocolsTLSv1TLSv1.1TLSv1.2;;ssl_certificate/home/build/openresty/nginx/cert/dealssl/.crt;(动态生成)location/{proxy_passhttps://www_test;(动态生成)proxy_set_headerHost$host;proxy_set_headerX-Real-IP$remote_addr;proxy_set_headerX-Forwarded-For$proxy_add_x_forwarded_for;proxy_set_headerX-Forwarded-Protohttps;proxy_redirectoff;}}

2、实现框架

看完nginx的配置不难发现,nginx是一个典型的key value类型的,而且与文件系统的非常类似,一个目录下面可以包含其他配置,目录下还可以有目录,嵌套多层。如今key value类型的数据库非常多,redis、leveldb等,最近新秀etcd也是key-value分布式数据库,提供类似文件系统操作,使用raft协议保持数据一致性,非常适合云计算分布式部署场景,将confd与etcd搭配,非常适合nginx这样的配置格式。

使用etcd新建与nginx配置对应的目录如下:

为了配合webui展示配置和后台生成nginx配置,总体的配置流程图如下所示:

3、生成配置

WEBUI通过API将配置写入mysql和etcd,confd注册监控etcd的key为/nginx/,只要发生变化就通知confd根据模板生成配置。confd默认的配置路径为/etc/confd/,创建conf.d和template两个目录,分别存放配置资源和配置模板。

nginx的配置资源如下所示: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/servicenginxreload"

nginx的配置模板如下所示:test.conf.tmpl

upstreamwww_{{getv"/nginx/https/www/server/server_name"}}{{{rangegetvs"/nginx/https/www/upstream/*"}}server``.``;`end`}server{server_name{{getv"/nginx/https/www/server/server_name"}}:443;sslonssl_certificate{{getv"/nginx/https/www/server/ssl_certificate"}};ssl_certificate_key{{getv"/nginx/https/www/server/ssl_certificate_key"}};location/{proxy_passhttp://www_{{getv"/nginx/https/www/server/server_name"}};proxy_set_headerHost$host;proxy_set_headerX-Real-IP$remote_addr;proxy_set_headerX-Forwarded-For$proxy_add_x_forwarded_for;proxy_set_headerX-Forwarded-Protohttps;proxy_redirectoff;}}

开启confd,执行./confd -watch -backend etcd -node http://127.0.0.1:2379

使用ectdctl添加配置如下:

./etcdctlset/nginx/https/www/server/./etcdctlset/nginx/https/www/server/ssl_certificate/home/waf/build/openresty/nginx/cert/client/client.crt./etcdctlset/nginx/https/www/server/ssl_certificate_key/home/waf/build/openresty/nginx/cert/client/client.key;/etcdctlset/nginx/https/www/upstream/server1192.168.1.2:443./etcdctlset/nginx/https/www/upstream/server2192.168.4.2:443

confd的执行结果如下所示:

生成位置文件如下所示:

{server192.168.1.2:443;server192.168.4.2:443;}server{:443;sslonssl_certificate/home/waf/build/openresty/nginx/cert/client/client.crt;ssl_certificate_key/home/waf/build/openresty/nginx/cert/client/client.key;location/{proxy_pass;proxy_set_headerHost$host;proxy_set_headerX-Real-IP$remote_addr;proxy_set_headerX-Forwarded-For$proxy_add_x_forwarded_for;proxy_set_headerX-Forwarded-Protohttps;proxy_redirectoff;}}

与模板生成的保持一致。

4、参考资料

cond的github上文档介绍

/kelseyhightower/confd/blob/master/docs/template-resources.md

etcd与confd实现配置管理

//01/25/confd%E5%92%8Cetcd%E5%AE%9E%E7%8E%B0%E9%85%8D%E7%BD%AE%E7%AE%A1%E7%90%86%E5%8F%8A%E8%87%AA%E5%8A%A8%E5%8F%91%E7%8E%B0/

/articles/eeiAve

etcd的参考:

/coreos/etcd

/articles/11035

/cn/articles/coreos-analyse-etcd/

本文转自 蔡小赵 51CTO博客,原文链接:/zhaopeiyan/1972873

如果觉得《使用etcd+confd管理nginx配置》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。