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

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动态变更配置文件重启服务,实现远程配置,多机同步配置。

你可能感兴趣的:(linux,go)