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

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

一、简单配置

1、配置etcd数据

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

etcdctl set /myapp/database/url www.qq.com
etcdctl set /myapp/database/user rob

2、confd安装

confd比较简单就一个文件,拿过来就可以执行,可以从github上下载:https://github.com/kelseyhightower/confd/releases ,并将其放到 /usr/local/bin目录下即可。不过使用前需要创建相应的配置目录:

[[email protected] bin]# mkdir -p /etc/confd/{conf.d,templates}

3、创建confd配置文件

vim  /etc/confd/conf.d/myconfig.toml
[template]
src = "myconfig.conf.tmpl"
dest = "/tmp/myconfig.conf"
keys = [
    "/myapp/database/url",
    "/myapp/database/user",
]

4、创建模板文件

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

5、执行生成配置文件

confd -onetime -backend etcd -node http://127.0.0.1:2379    #只一次
confd -interval=60 -backend etcd -node http://127.0.0.1:2379 &   #按时间轮询

6、验证文件生成

[[email protected] tmp]# cat /tmp/myconfig.conf [myconfig] database_url = www.qq.com database_user = rob

二、etcd+confd+nginx配置

1、创建数据

etcdctl set /myapp/subdomain myapp
etcdctl set /myapp/upstream/app2 "10.0.1.100:80"
etcdctl set /myapp/upstream/app1 "10.0.1.101:80"
etcdctl set /yourapp/subdomain yourapp
etcdctl set /yourapp/upstream/app2 "10.0.1.102:80"
etcdctl set /yourapp/upstream/app1 "10.0.1.103:80"

2、创建配置文件

cat /etc/confd/conf.d/myapp-nginx.toml
[template]
prefix = "/myapp"
src = "nginx.tmpl"
dest = "/tmp/myapp.conf"
owner = "nginx"
mode = "0644"
keys = [
  "/subdomain",
  "/upstream",
]
check_cmd = "/usr/sbin/nginx -t -c {{.src}}"
reload_cmd = "/usr/sbin/service nginx reload"
# cat /etc/confd/conf.d/yourapp-nginx.toml
[template]
prefix = "/yourapp"
src = "nginx.tmpl"
dest = "/tmp/yourapp.conf"
owner = "nginx"
mode = "0644"
keys = [
  "/subdomain",
  "/upstream",
]
check_cmd = "/usr/sbin/nginx -t -c {{.src}}"
reload_cmd = "/usr/sbin/service nginx reload"

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

# cat /etc/confd/templates/nginx.tmpl
upstream {{getv "/subdomain"}} {
{{range getvs "/upstream/*"}}
    server {{.}};
{{end}}
}
server {
    server_name  {{getv "/subdomain"}}.example.com;
    location / {
        proxy_pass        http://{{getv "/subdomain"}};
        proxy_redirect    off;
        proxy_set_header  Host             $host;
        proxy_set_header  X-Real-IP        $remote_addr;
        proxy_set_header  X-Forwarded-For  $proxy_add_x_forwarded_for;
   }
}

3、验证

# cat myapp.conf
upstream myapp {
    server 10.0.1.100:80;
    server 10.0.1.101:80;
}
server {
    server_name  myapp.example.com;
    location / {
        proxy_pass        http://myapp;
        proxy_redirect    off;
        proxy_set_header  Host             $host;
        proxy_set_header  X-Real-IP        $remote_addr;
        proxy_set_header  X-Forwarded-For  $proxy_add_x_forwarded_for;
   }
}