Confd+etcd实现高可用自动发现
Confd是一个轻量级的配置管理工具。通过查询Etcd,结合配置模板引擎,保持本地配置最新,同时具备定期探测机制,配置变更自动reload。其后端支持的数据类型有:etcd、consul、vault、environment variables、redis、zookeeper、dynamodb、stackengine、rancher。不过一般使用Confd和etcd的配合使用比较多。其常用架构如下:
一、简单配置
1、配置etcd数据
具体步骤这里略过,这里只配置两条数据
1etcdctl set /myapp/database/url www.361way.com
2etcdctl set /myapp/database/user rob
2、confd安装
confd比较简单就一个文件,拿过来就可以执行,可以从github上下载: ,并将其放到/usr/local/bin目录下即可。不过使用前需要创建相应的配置目录:
1[root@etcd1 bin]# mkdir -p /etc/confd/{conf.d,templates}
3、创建confd配置文件
1# vim /etc/confd/conf.d/myconfig.toml
2[template]
3src = "myconfig.conf.tmpl"
4dest = "/tmp/myconfig.conf"
5keys = [
6 "/myapp/database/url",
7 "/myapp/database/user",
8]
4、创建模板文件
1# vim /etc/confd/templates/myconfig.conf.tmpl
2[myconfig]
3database_url = {{getv "/myapp/database/url"}}
4database_user = {{getv "/myapp/database/user"}}
5、执行生成配置文件
1confd -onetime -backend etcd -node http://127.0.0.1:2379 只一次
2confd -interval=60 -backend etcd -node http://127.0.0.1:2379 & 按时间轮询
使用onetime参数的,配置文件生成一次后,confd程序就退出了,下面的那句,会每隔60秒轮询一次。一旦后端etcd相应的值发生变化就会重新生成相应的配置文件。
6、验证文件生成
1[root@etcd1 tmp]# cat /tmp/myconfig.conf
2[myconfig]
3database_url = www.361way.com
4database_user = rob
二、etcd+confd+nginx配置
1、创建数据
1etcdctl set /myapp/subdomain myapp
2etcdctl set /myapp/upstream/app2 "10.0.1.100:80"
3etcdctl set /myapp/upstream/app1 "10.0.1.101:80"
4etcdctl set /yourapp/subdomain yourapp
5etcdctl set /yourapp/upstream/app2 "10.0.1.102:80"
6etcdctl set /yourapp/upstream/app1 "10.0.1.103:80"
2、创建配置文件
1# cat /etc/confd/conf.d/myapp-nginx.toml
2[template]
3prefix = "/myapp"
4src = "nginx.tmpl"
5dest = "/tmp/myapp.conf"
6owner = "nginx"
7mode = "0644"
8keys = [
9 "/subdomain",
10 "/upstream",
11]
12check_cmd = "/usr/sbin/nginx -t -c {{.src}}"
13reload_cmd = "/usr/sbin/service nginx reload"
14# cat /etc/confd/conf.d/yourapp-nginx.toml
15[template]
16prefix = "/yourapp"
17src = "nginx.tmpl"
18dest = "/tmp/yourapp.conf"
19owner = "nginx"
20mode = "0644"
21keys = [
22 "/subdomain",
23 "/upstream",
24]
25check_cmd = "/usr/sbin/nginx -t -c {{.src}}"
26reload_cmd = "/usr/sbin/service nginx reload"
这里创建了两个配置文件。接下来创建一个模板文件,两个配置文件会根据该模板文件生成配置:
1# cat /etc/confd/templates/nginx.tmpl
2upstream {{getv "/subdomain"}} {
3{{range getvs "/upstream/*"}}
4 server {{.}};
5{{end}}
6}
7server {
8 server_name {{getv "/subdomain"}}.example.com;
9 location / {
10 proxy_pass http://{{getv "/subdomain"}};
11 proxy_redirect off;
12 proxy_set_header Host $host;
13 proxy_set_header X-Real-IP $remote_addr;
14 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
15 }
16}
3、验证
在进行验证启用的时候,会发现有如下报错:
1[root@etcd1 conf.d]# 2017-05-08T19:06:07+08:00 etcd1 confd[10949]: INFO Target config /tmp/myapp.conf out of sync
22017-05-08T19:06:07+08:00 etcd1 confd[10949]: ERROR "nginx: [emerg] "upstream" directive is not allowed here in /tmp/.myapp.conf835093196:1\nnginx: configuration file /tmp/.myapp.conf835093196 test failed\n"
32017-05-08T19:06:07+08:00 etcd1 confd[10949]: ERROR Config check failed: exit status 1
42017-05-08T19:06:07+08:00 etcd1 confd[10949]: INFO Target config /tmp/yourapp.conf out of sync
52017-05-08T19:06:07+08:00 etcd1 confd[10949]: ERROR "nginx: [emerg] "upstream" directive is not allowed here in /tmp/.yourapp.conf196880350:1\nnginx: configuration file /tmp/.yourapp.conf196880350 test failed\n"
62017-05-08T19:06:07+08:00 etcd1 confd[10949]: ERROR Config check failed: exit status 1
原因很简单,注意配置文件中的check_cmd 命令,该命令会进行配置文件检测,检测不通过时,配置文件不会修改,且不会执行后面的reload_cmd命令。这里想不报错也很简单,将配置文件中nginx的配置指向正确的位置,而且让nginx可以正常检测,且检测结果没有错误。再次执行命令,并修改配置文件,会发现有如下信息:
1# cat myapp.conf
2upstream myapp {
3 server 10.0.1.100:80;
4 server 10.0.1.101:80;
5}
6server {
7 server_name myapp.example.com;
8 location / {
9 proxy_pass http://myapp;
10 proxy_redirect off;
11 proxy_set_header Host $host;
12 proxy_set_header X-Real-IP $remote_addr;
13 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
14 }
15}
三、其他
在模板文件中,会用到一些函数:map、base、exists、get、gets、getenv、datetime等 ,关于这些函数的使用,可以参看官方文档templates 。
etcd+confd 基本上可以和任何应用进行组合,如网上比较常见的etcd+confd+nginx 、etcd+confd+haproxy 、etcd+confd+k8s、etcd+confd+tomcat等等。和tomcat的整合,在官方上也有相应的示例,具体可以参看官方文档tomcat-sample 。
捐赠本站(Donate)
如您感觉文章有用,可扫码捐赠本站!(If the article useful, you can scan the QR code to donate))
- Author: shisekong
- Link: https://blog.361way.com/confd-etcd/5470.html
- License: This work is under a 知识共享署名-非商业性使用-禁止演绎 4.0 国际许可协议. Kindly fulfill the requirements of the aforementioned License when adapting or creating a derivative of this work.