入门ETCD——配合confd实现配置管理

confd简介

什么是confd

Confd是一个轻量级的配置管理工具。通过查询后端存储,结合配置模板引擎,保持本地配置最新,同时具备定期探测机制,配置变更自动reload。对应的后端存储可以是etcd,redis、zookeeper等。

confd用途

随着上线的服务越来越多,配置文件和配置项越来越复杂,管理和变更维护配置文件逐渐成为一件麻烦的事情。在这时候,就需要一套集中化的配置文件管理系统。一方面实现配置文件的统一管理,版本回溯,另一方面提供配置文件的批量自动下发,已经动态加载重启服务。而confd+etcd就是为解决上述问题提出的一种解决思路。
image.png

confd工作原理

image.png

使用confd

  • confd配置文件默认在/etc/confd中,可以通过参数-confdir指定。目录中包含两个子目录,分别是:conf.d templates。
  • confd会先读取conf.d目录中的配置文件(toml格式),然后根据文件指定的模板路径去渲染模板,再执行<reload_cmd>。

接下来以NGINX配置为例,通过监听etcd存储变化,动态的修改NGINX主页。

安装confd

[root@tiaoban ~]# wget https://github.com/kelseyhightower/confd/releases/download/v0.16.0/confd-0.16.0-linux-amd64
[root@tiaoban ~]# mv confd-0.16.0-linux-amd64 /usr/local/bin/confd
[root@tiaoban ~]# chmod +x /usr/local/bin/confd
[root@tiaoban ~]# confd -version
confd 0.16.0 (Git SHA: 7217b0ca, Go Version: go1.10.2)

创建nginx资源文件

[root@tiaoban ~]# dnf -y install nginx 
[root@tiaoban ~]# systemctl start nginx
# 准备两个html文件目录
[root@tiaoban nginx]# pwd
/usr/share/nginx
[root@tiaoban nginx]# tree .
.
├── html
│   ├── 404.html
│   ├── 50x.html
│   ├── index.html
│   ├── nginx-logo.png
│   └── poweredby.png
├── modules
│   ├── mod-http-image-filter.conf
│   ├── mod-http-perl.conf
│   ├── mod-http-xslt-filter.conf
│   ├── mod-mail.conf
│   └── mod-stream.conf
├── v1
│   └── index.html
└── v2
    └── index.html

4 directories, 12 files
[root@tiaoban nginx]# cat v1/index.html 
<!DOCTYPE html>
<html>
	<head>
		<title>nginx-v1</title>
		</head>
	<body>
		<h1>hello nginx v1</h1>
	</body>
</html>
[root@tiaoban nginx]# cat v2/index.html 
<!DOCTYPE html>
<html>
  <head>
  	<title>nginx-v2</title>
	</head>
	<body>
		<h1>hello nginx v2</h1>
	</body>
</html>

创建配置目录

[root@tiaoban ~]# mkdir -p /etc/confd/{conf.d,templates}

confd目录主要使用两个核心的目录

  • conf.d:主要包含配置的生成逻辑,例如模板源,后端存储对应的keys,命令执行等。
  • templates:配置模板Template,即基于不同组件的配置,修改为符合 Golang text templates的模板文件。

创建模板文件

Template定义了单一应用配置的模板,默认存储在/etc/confd/templates目录下,模板文件符合Go的text/template格式。
模板文件常用函数有base,get,gets,lsdir,json等。具体可参考https://github.com/kelseyhightower/confd/blob/master/docs/templates.md
此处的逻辑是使用root_dir变量,动态更新nginx的root文件目录,如果root_dir这个key不存在,则默认值为/usr/share/nginx/html。

[root@tiaoban ~]# cd /etc/confd/templates
[root@tiaoban templates]# cat nginx.tmpl
server {
        listen       80;
        server_name  ~^.*$;
        location / {
            root  {{getv "/root_dir" "/usr/share/nginx/html"}};
            index  index.html index.htm;
        }
    }

创建配置文件

模板源配置文件是TOML格式的文件,主要包含配置的生成逻辑,例如模板源,后端存储对应的keys,命令执行等。默认目录在/etc/confd/conf.d。

  • 必要参数

dest:目标文件(字符串类型)
keys : 键数组(字符串数组)
src :配置模板的相对路径(字符串)

  • 可选参数

gid:应该拥有该文件的gid。默认为有效的gid(整数)
mode:文件的权限模式(字符串)
uid:应该拥有该文件的uid。默认为有效的uid(整数)
reload_cmd:重新加载配置的命令(字符串)
check_cmd:检查配置的命令(字符串)
prefix:键前缀的字符串(字符串)

[root@tiaoban confd]# cat /etc/confd/conf.d/myapp-nginx.toml 
[template]
prefix = "/nginx" # 指定字符串前缀,便于区分不同confd项目
src = "nginx.tmpl" #配置模板相对路径
dest = "/etc/nginx/conf.d/myapp.conf" # 目标路径
mode = "0644" # 文件权限
keys = [
  "/root_dir"
] # 键数组,与模板使用的键对应
check_cmd = "/usr/sbin/nginx -t" # 配置检查命令
reload_cmd = "/usr/sbin/nginx -s reload" # 重新加载配置命令 

初始化etcd中的数据

先在etcd中创建一个名为/nginx/root_dir的键,值为/usr/share/nginx/v1。需要注意的是如果在配置文件中指定前缀,那么在创建键时键名为前缀+keys。

[root@tiaoban ~]# etcdctl put /nginx/root_dir '/usr/share/nginx/v1'
OK

启动confd的服务

confd支持以daemon或者onetime两种模式运行

  • onetime模式:只会生成一次配置,之后key无论变化不会再生成,一般很少使用这种模式。
[root@tiaoban ~]# confd -onetime -backend etcd -node http://127.0.0.1:2379
  • daemon模式:confd会监听后端存储的配置变化,根据配置模板动态生成目标配置文件。
[root@tiaoban ~]# confd -watch -backend etcdv3 -node http://192.168.10.100:2379
2023-03-22T22:04:55+08:00 tiaoban confd[5312]: INFO Backend set to etcdv3
2023-03-22T22:04:55+08:00 tiaoban confd[5312]: INFO Starting confd
2023-03-22T22:04:55+08:00 tiaoban confd[5312]: INFO Backend source(s) set to http://192.168.10.100:2379
2023-03-22T22:04:55+08:00 tiaoban confd[5312]: INFO Target config /etc/nginx/conf.d/myapp.conf out of sync
2023-03-22T22:04:55+08:00 tiaoban confd[5312]: INFO Target config /etc/nginx/conf.d/myapp.conf has been updated

由日志可知,已经成功根据模板文件和etcd的key生成了配置文件。

访问验证

查看生成的nginx配置文件

[root@tiaoban ~]# cat /etc/nginx/conf.d/myapp.conf 
server {
        listen       80;
        server_name  ~^.*$;
        location / {
            root  /usr/share/nginx/v1;
            index  index.html index.htm;
        }
    }

访问nginx服务,查看页面

[root@tiaoban ~]# curl 127.0.0.1/
<!DOCTYPE html>
<html>
  <head>
    <title>nginx-v1</title>
  </head>
  <body>
    <h1>hello nginx v1</h1>
  </body>
</html>

ok,v1版本的nginx服务访问正常,接下来我们更新/nginx/root_dir,模拟版本更新操作

[root@tiaoban ~]# etcdctl put /nginx/root_dir '/usr/share/nginx/v2'
OK

观察confd日志,有检测到了etcd键值变化,并触发了自动更新操作

[root@tiaoban confd]# confd -watch -backend etcdv3 -node http://192.168.10.100:2379
2023-03-22T22:06:06+08:00 tiaoban confd[5344]: INFO Backend set to etcdv3
2023-03-22T22:06:06+08:00 tiaoban confd[5344]: INFO Starting confd
2023-03-22T22:06:06+08:00 tiaoban confd[5344]: INFO Backend source(s) set to http://192.168.10.100:2379
2023-03-22T22:06:06+08:00 tiaoban confd[5344]: INFO Target config /etc/nginx/conf.d/myapp.conf out of sync
2023-03-22T22:06:06+08:00 tiaoban confd[5344]: INFO Target config /etc/nginx/conf.d/myapp.conf has been updated
2023-03-22T22:09:25+08:00 tiaoban confd[5344]: INFO /etc/nginx/conf.d/myapp.conf has md5sum 86f04524c0f5e58d81f86649b6feef79 should be 47da5e695a2c5ad1e5561bec683a920c
2023-03-22T22:09:25+08:00 tiaoban confd[5344]: INFO Target config /etc/nginx/conf.d/myapp.conf out of sync
2023-03-22T22:09:25+08:00 tiaoban confd[5344]: INFO Target config /etc/nginx/conf.d/myapp.conf has been updated

查看新生成的nginx配置文件,并访问验证

[root@tiaoban ~]# cat /etc/nginx/conf.d/myapp.conf 
server {
        listen       80;
        server_name  ~^.*$;
        location / {
            root  /usr/share/nginx/v2;
            index  index.html index.htm;
        }
    }
[root@tiaoban ~]# curl 127.0.0.1/
<!DOCTYPE html>
<html>
<head>
<title>nginx-v2</title>
</head>
<body>
<h1>hello nginx v2</h1>
</body>
</html>

由此可见,confd实时监听/nginx/root_dir这个key的值变化,当键的值更新时,自动渲染模板文件,生成了新的nginx配置,并自动执行了nginx -t和nginx -s reload操作。

查看更多

微信公众号

微信公众号同步更新,欢迎关注微信公众号第一时间获取最近文章。在这里插入图片描述

博客网站

崔亮的博客-专注devops自动化运维,传播优秀it运维技术文章。更多原创运维开发相关文章,欢迎访问https://www.cuiliangblog.cn

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值