Etcd+confd实现动态修改nginx文件

上一篇已经安装并配置了ETCD集群。

一、介绍

confd is a lightweight configuration management tool focused on:
keeping local configuration files up-to-date using data stored in etcd, consul, dynamodb, redis, vault, zookeeper or env vars and processing template resources.
reloading applications to pick up new config file changes

二、安装

采用二进制安装,下载二进制文件

wget https://github.com/kelseyhightower/confd/releases/download/v0.13.0/confd-0.13.0-linux-amd64

添加执行权限,并移动到/usr/local/bin目录下

chmod +x confd-0.13.0-linux-amd64
mv confd-0.13.0-linux-amd64 /usr/local/bin/confd

三、配置

  • 在ETCD集群添加相应KEY
etcdctl set /dev/nginxdev zmz
etcdctl set /dev/upstream/app1 "10.10.149.229:80"
etcdctl set /dev/upstream/app2 "10.10.152.165:80"
  • Create conf、templates directory
mkdir /etc/confd/{conf.d,templates}
  • Create template resources
[template]
src = "nginx.tmpl"
dest = "/etc/nginx/conf.d/zmz.conf"
owner = "root"
mode = "0644"
keys = [
    "/dev/nginxdev",
    "/dev/upstream",
]
check_cmd = "nginx -t -c {{ .src }}"
reload_cmd = "systemctl reload nginx"
  • Create the source template
upstream {{getv "/dev/nginxdev"}} {
{{range getvs "/dev/upstream/*"}}
    server {{.}};
{{end}}
}
server {
    server_name  {{getv "/dev/nginxdev"}}.example.com;
    location / {
        proxy_pass        http://{{getv "/dev/nginxdev"}};
        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;
   }
}
  • Create confd boot files
backend = "etcd"
confdir = "/etc/confd"
log-level = "debug"
interval = 60
nodes = [
   "http://10.10.149.229:4001",
   "http://10.10.152.165:4001",
   "http://10.10.160.64:4001",
]
noop = false
#prefix = "/production"

四、启动confd 并验证

  • 启动


    Etcd+confd实现动态修改nginx文件_第1张图片
  • 查看配置文件


    Etcd+confd实现动态修改nginx文件_第2张图片

五、参考文档

  • https://github.com/kelseyhightower/confd/tree/master/docs

你可能感兴趣的:(Etcd+confd实现动态修改nginx文件)