confd 模板自动生成配置,安装使用介绍

本贴最后更新于 2438 天前,其中的信息可能已经渤澥桑田

confd 配置生成工具,安装方便 linux 环境直接下载二进制包就可以使用。可以结合
(etcd/consul/vault/environment variables/redis/zookeeper/dynamodb/rancher/ssm)本文介绍与 etcd 的结合使用方法

1.下载 confd

mkdir -p /usr/local/confd/
wget  https://github.com/kelseyhightower/confd/releases/download/v0.14.0/confd-0.14.0-linux-amd64

创建模板文件夹(templates) 和 配置文件夹(conf.d) confd 默认使用的是这两个目录文件名

mkdir -p /usr/local/confd/{conf.d,templates}

2.etcd 创建存储

/usr/local/etcd//etcdctl put /nginx/user  www
/usr/local/etcd//etcdctl put /nginx/group  www
/usr/local/etcd//etcdctl put /nginx/worker_processes auto
/usr/local/etcd//etcdctl put /nginx/error_log   /home/wwwlogs/nginx_error.log
/usr/local/etcd//etcdctl put /nginx/pid_path   /usr/local/nginx/logs/nginx.pid
/usr/local/etcd//etcdctl put /nginx/log_level  crit

3.创建模板配置

vi  /usr/local/confd/conf.d/nginx.toml

[template]
src = "nginx.conf.tmpl"   ## templates中模板文件名
dest = "/usr/local/nginx/conf/nginx.conf"  ## 配置文件生成路径
keys = [
        "/nginx/group",
        "/nginx/user",
        "/nginx/worker_processes",
        "/nginx/error_log",
        "/nginx/log_level",
        "/nginx/pid_path"
]
check_cmd = "/usr/sbin/nginx -t -c {{.src}}" ##nginx检测配置文件
reload_cmd = "/usr/sbin/service nginx reload"  ##nginx服务重启

4.创建 nginx 配置模板(只更改部分参数用于演示)

vi  /usr/local/confd/templates/nginx.conf.tmpl 

user  {{getv "/nginx/group"}} {{getv "/nginx/user"}};
worker_processes {{getv "/nginx/worker_processes"}};
error_log  {{getv "/nginx/error_log"}}  {{getv "/nginx/log_level"}};
pid        {{getv "/nginx/pid_path"}};
worker_rlimit_nofile 51200;
events
    {
        use epoll;
        worker_connections 51200;
        multi_accept on;
    }
http
    {
        include       mime.types;
        default_type  application/octet-stream;
        server_names_hash_bucket_size 128;
        client_header_buffer_size 32k;
        large_client_header_buffers 4 32k;
        client_max_body_size 50m;
        sendfile   on;
        tcp_nopush on;
        keepalive_timeout 60;
        tcp_nodelay on;
        fastcgi_connect_timeout 300;
        fastcgi_send_timeout 300;
        fastcgi_read_timeout 300;
        fastcgi_buffer_size 64k;
        fastcgi_buffers 4 64k;
        fastcgi_busy_buffers_size 128k;
        fastcgi_temp_file_write_size 256k;
        access_log off;
server
    {
        listen 80 default_server;
        server_name _;
        index index.html index.htm index.php;
        root  /home/wwwroot/default;
        include enable-php.conf;
        location /nginx_status
        {
            stub_status on;
            access_log   off;
        }

        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
        {
            expires      30d;
        }

        location ~ .*\.(js|css)?$
        {
            expires      12h;
        }

        location ~ /.well-known {
            allow all;
        }

        location ~ /\.
        {
            deny all;
        }

        access_log  /home/wwwlogs/access.log;
    }
}

5. 测试

命令返回我们看到nginx的配置文件已经更新,nginx配置文件reload加载成功

```shell
./confd  -confdir /usr/local/confd/ -onetime -backend etcdv3 -node http://127.0.0.1:2379 -log-level debug
2017-11-10T11:05:26+08:00 localhost.localdomain ./confd[52470]: INFO Backend set to etcdv3
2017-11-10T11:05:26+08:00 localhost.localdomain ./confd[52470]: INFO Starting confd
2017-11-10T11:05:26+08:00 localhost.localdomain ./confd[52470]: INFO Backend source(s) set to http://127.0.0.1:2379
2017-11-10T11:05:26+08:00 localhost.localdomain ./confd[52470]: DEBUG Loading template resources from confdir /usr/local/confd/
2017-11-10T11:05:26+08:00 localhost.localdomain ./confd[52470]: DEBUG Found template: /usr/local/confd/conf.d/nginx.toml
2017-11-10T11:05:26+08:00 localhost.localdomain ./confd[52470]: DEBUG Loading template resource from /usr/local/confd/conf.d/nginx.toml
2017-11-10T11:05:26+08:00 localhost.localdomain ./confd[52470]: DEBUG Retrieving keys from store
2017-11-10T11:05:26+08:00 localhost.localdomain ./confd[52470]: DEBUG Key prefix set to /
2017-11-10T11:05:26+08:00 localhost.localdomain ./confd[52470]: DEBUG Got the following map from store: map[/nginx/user:www /nginx/worker_processes:auto /nginx/error_log:/home/wwwlogs/nginx_error.log /nginx/log_level:crit /nginx/pid_path:/usr/local/nginx/logs/nginx.pid /nginx/group:www]
2017-11-10T11:05:26+08:00 localhost.localdomain ./confd[52470]: DEBUG Using source template /usr/local/confd/templates/nginx.conf.tmpl
2017-11-10T11:05:26+08:00 localhost.localdomain ./confd[52470]: DEBUG Compiling source template /usr/local/confd/templates/nginx.conf.tmpl
2017-11-10T11:05:26+08:00 localhost.localdomain ./confd[52470]: DEBUG Comparing candidate config to /usr/local/nginx/conf/nginx.conf
2017-11-10T11:05:26+08:00 localhost.localdomain ./confd[52470]: INFO Target config /usr/local/nginx/conf/nginx.conf out of sync
2017-11-10T11:05:26+08:00 localhost.localdomain ./confd[52470]: DEBUG Running /usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/.nginx.conf050458651
2017-11-10T11:05:26+08:00 localhost.localdomain ./confd[52470]: DEBUG "nginx: the configuration file /usr/local/nginx/conf/.nginx.conf050458651 syntax is ok\nnginx: configuration file /usr/local/nginx/conf/.nginx.conf050458651 test is successful\n"
2017-11-10T11:05:26+08:00 localhost.localdomain ./confd[52470]: DEBUG Overwriting target config /usr/local/nginx/conf/nginx.conf
2017-11-10T11:05:26+08:00 localhost.localdomain ./confd[52470]: DEBUG Running /usr/sbin/service nginx reload
2017-11-10T11:05:26+08:00 localhost.localdomain ./confd[52470]: DEBUG "Reload service nginx...  done\n"
2017-11-10T11:05:26+08:00 localhost.localdomain ./confd[52470]: INFO Target config /usr/local/nginx/conf/nginx.conf has been updated

6.常用参数

confd 默认配置路径/etc/confd/ -confdir 参数更改路径为 /usr/local/confd/
-onetime 运行一次
-backend 后端数据服务节点
-log-level 置顶日志级别
-interval 轮询周期 默认 10 分钟

我们可以通过运维平台在线改动服务配置,利用 confd 动态变更配置文件重启服务,实现远程配置,多机同步配置。

  • confd
    1 引用
  • golang

    Go 语言是 Google 推出的一种全新的编程语言,可以在不损失应用程序性能的情况下降低代码的复杂性。谷歌首席软件工程师罗布派克(Rob Pike)说:我们之所以开发 Go,是因为过去 10 多年间软件开发的难度令人沮丧。Go 是谷歌 2009 发布的第二款编程语言。

    497 引用 • 1388 回帖 • 278 关注

相关帖子

欢迎来到这里!

我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。

注册 关于
请输入回帖内容 ...