zoukankan      html  css  js  c++  java
  • Etcd+Confd实现配置文件动态更新

    简介

    如上图是一个很简单的架构,生产环境中经常会进行灰度发布,需要下掉一部分的节点。如果靠人工操作很容易错误,这里通过Etcd和Confd来实现nginx upstream的动态更新。

    类似的,自动化部署时服务的环境变量等也可存入etcd(配置中心website页面),coredns等配置文件内容均可存入etcd,由confd动态刷新。

    etcd: 分布式KV存储系统,一般用于共享配置和服务注册与发现。

    confd:管理本地应用配置文件,使用etcd或consul存储的数据渲染模板,还支持redis、zookeeper等, 通过watch定期监测对应的etcd中目录变化,获取最新的Value,然后渲染模板,更新配置文件。

    安装

    • 安装etcd
    yum -y install etcdsystemctl start etcd
    
    • 安装confd
    wget https://github.com/kelseyhightower/confd/releases/download/v0.16.0/confd-0.16.0-linux-arm64mkdir -p /etc/etcd/{conf.d,templates}mv confd-0.16.0-linux-arm64 /usr/bin/confd
    
    • conf.d 目录存放.toml配置文件
    • templates 目录存放.tmpl配置模版文件

    配置

    创建nginx配置和模版

    配置文件cat conf.d/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/nginx -s reload"
    ]
    

    模版文件cat templates/test.conf.tmpl

    upstream www_{{getv "/nginx/www/server/server_name"}} {   
    {{range getvs "/nginx/www/upstream/*"}}       
    server {{.}};  
    {{end}}
    }
    server {   
    server_name       
    {{getv "/nginx/www/server/server_name"}};  
    location / {       
    proxy_pass        http://www_{{getv "/nginx/www/server/server_name"}};        proxy_set_header Host $host;  
    proxy_set_header X-Real-IP $remote_addr;  
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;   
    proxy_set_header X-Forwarded-Proto https;     
    proxy_redirect    off;  
    }
    }
    

    配置etcd

    etcdctl set /nginx/https/www/server/server_name test.com
    etcdctl set /nginx/https/www/upstream/server1 192.168.1.110
    etcdctl set /nginx/https/www/upstream/server2 192.168.1.111
    

    启动confd监听

    confd -watch -backend etcd -node http://127.0.0.1:2379
    

    查看生产的nginx配置文件

     cat /tmp/test.confupstream www_test.com
     {        
     server 192.168.1.110;        
     server 192.168.1.111;}
     server {   
     server_name         test.com;    location / {        
     proxy_pass        http://www_test.com;        
     proxy_set_header Host $host;        
     proxy_set_header X-Real-IP $remote_addr;      
     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;      
     proxy_set_header X-Forwarded-Proto https;        
     proxy_redirect    off;    }
     }
    

    配置文件生成完成

  • 相关阅读:
    事务之三:编程式事务、声明式事务(XML配置事务、注解实现事务)
    file的getPath getAbsolutePath和getCanonicalPath的不同
    处理 JSON null 和空数组及对象
    Eclipse快捷键大全(转载)
    Annotation之三:自定义注解示例,利用反射进行解析
    Annotation之二:@Inherited注解继承情况
    innodb事务日志详解
    事务之二:spring事务(事务管理方式,事务5隔离级别,7个事务传播行为,spring事务回滚条件)
    Java 数组的三种创建方法,数组拷贝方法
    Eclipse 远程调试
  • 原文地址:https://www.cnblogs.com/lianhaifeng/p/13520094.html
Copyright © 2011-2022 走看看