Confd+etcd实现高可用自动发现

Confd是一个轻量级的配置管理工具。通过查询Etcd,结合配置模板引擎,保持本地配置最新,同时具备定期探测机制,配置变更自动reload。其后端支持的数据类型有:etcd、consul、vault、environment variables、redis、zookeeper、dynamodb、stackengine、rancher。不过一般使用Confd和etcd的配合使用比较多。其常用架构如下:

etcd-confd-nginx

一、简单配置

1、配置etcd数据

具体步骤这里略过,这里只配置两条数据

  1. etcdctl set /myapp/database/url www.361way.com
  2. etcdctl set /myapp/database/user rob

2、confd安装

confd比较简单就一个文件,拿过来就可以执行,可以从github上下载:https://github.com/kelseyhightower/confd/releases ,并将其放到/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]
  3. src = "myconfig.conf.tmpl"
  4. dest = "/tmp/myconfig.conf"
  5. keys = [
  6. "/myapp/database/url",
  7. "/myapp/database/user",
  8. ]

4、创建模板文件

  1. # vim /etc/confd/templates/myconfig.conf.tmpl
  2. [myconfig]
  3. database_url = {{getv "/myapp/database/url"}}
  4. database_user = {{getv "/myapp/database/user"}}

5、执行生成配置文件

  1. confd -onetime -backend etcd -node http://127.0.0.1:2379 只一次
  2. confd -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]
  3. database_url = www.361way.com
  4. database_user = rob

二、etcd+confd+nginx配置

1、创建数据

  1. etcdctl set /myapp/subdomain myapp
  2. etcdctl set /myapp/upstream/app2 "10.0.1.100:80"
  3. etcdctl set /myapp/upstream/app1 "10.0.1.101:80"
  4. etcdctl set /yourapp/subdomain yourapp
  5. etcdctl set /yourapp/upstream/app2 "10.0.1.102:80"
  6. etcdctl set /yourapp/upstream/app1 "10.0.1.103:80"

2、创建配置文件

  1. # cat /etc/confd/conf.d/myapp-nginx.toml
  2. [template]
  3. prefix = "/myapp"
  4. src = "nginx.tmpl"
  5. dest = "/tmp/myapp.conf"
  6. owner = "nginx"
  7. mode = "0644"
  8. keys = [
  9. "/subdomain",
  10. "/upstream",
  11. ]
  12. check_cmd = "/usr/sbin/nginx -t -c {{.src}}"
  13. reload_cmd = "/usr/sbin/service nginx reload"
  14. # cat /etc/confd/conf.d/yourapp-nginx.toml
  15. [template]
  16. prefix = "/yourapp"
  17. src = "nginx.tmpl"
  18. dest = "/tmp/yourapp.conf"
  19. owner = "nginx"
  20. mode = "0644"
  21. keys = [
  22. "/subdomain",
  23. "/upstream",
  24. ]
  25. check_cmd = "/usr/sbin/nginx -t -c {{.src}}"
  26. reload_cmd = "/usr/sbin/service nginx reload"

这里创建了两个配置文件。接下来创建一个模板文件,两个配置文件会根据该模板文件生成配置:

  1. # cat /etc/confd/templates/nginx.tmpl
  2. upstream {{getv "/subdomain"}} {
  3. {{range getvs "/upstream/*"}}
  4. server {{.}};
  5. {{end}}
  6. }
  7. server {
  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
  2. 2017-05-08T19:06:07+08:00 etcd1 confd[10949]: ERROR "nginx: [emerg] "upstream" directive is not allowed here in /tmp/.myapp.conf835093196:1 nginx: configuration file /tmp/.myapp.conf835093196 test failed "
  3. 2017-05-08T19:06:07+08:00 etcd1 confd[10949]: ERROR Config check failed: exit status 1
  4. 2017-05-08T19:06:07+08:00 etcd1 confd[10949]: INFO Target config /tmp/yourapp.conf out of sync
  5. 2017-05-08T19:06:07+08:00 etcd1 confd[10949]: ERROR "nginx: [emerg] "upstream" directive is not allowed here in /tmp/.yourapp.conf196880350:1 nginx: configuration file /tmp/.yourapp.conf196880350 test failed "
  6. 2017-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
  2. upstream myapp {
  3. server 10.0.1.100:80;
  4. server 10.0.1.101:80;
  5. }
  6. server {
  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 。

  1)清空etcd :etcdctl rm --recursive  registry  (registry为自己set的名称)

       2)备份与恢复

         2.1)对于 API 2 备份与恢复方法官方 v2 admin guide

etcd的数据默认会存放在我们的命令工作目录中,我们发现数据所在的目录,会被分为两个文件夹中:

  • snap: 存放快照数据,etcd防止WAL文件过多而设置的快照,存储etcd数据状态。

  • wal: 存放预写式日志,最大的作用是记录了整个数据变化的全部历程。在etcd中,所有数据的修改在提交前,都要先写入到WAL中。

# etcdctl backup --data-dir /home/etcd/ --backup-dir /home/etcd_backup

# etcd -data-dir=/home/etcd_backup/  -force-new-cluster

恢复时会覆盖 snapshot 的元数据(member ID 和 cluster ID),所以需要启动一个新的集群。

    2.2)对于 API 3 备份与恢复方法官方 v3 admin guide

在使用 API 3 时需要使用环境变量 ETCDCTL_API 明确指定。

在命令行设置:

# export ETCDCTL_API=3

备份数据:

# etcdctl --endpoints localhost:2379 snapshot save snapshot.db

恢复:

# etcdctl snapshot restore snapshot.db --name m3 --data-dir=/home/etcd_data

恢复后的文件需要修改权限为 etcd:etcd
--name:重新指定一个数据目录,可以不指定,默认为 default.etcd
--data-dir:指定数据目录
建议使用时不指定 name 但指定 data-dir,并将 data-dir 对应于 etcd 服务中配置的 data-dir

etcd 集群都是至少 3 台机器,官方也说明了集群容错为 (N-1)/2,所以备份数据一般都是用不到,但是鉴上次 gitlab 出现的问题,对于备份数据也要非常重视。

原文地址:https://www.cnblogs.com/RENQIWEI1995/p/9675638.html