一、ETCD安装
下载地址:https://github.com/etcd-io/etcd/releases
按照机器配置选择release版本进行下载。每个release下列举了不同机器环境的bash脚本,以下是以macOS环境下的3.4.14版本为例进行说明:
ETCD_VER=v3.4.14 //版本号
# choose either URL //链接地址变量,DOWNLOAD_URL为真正的下载地址
GOOGLE_URL=https://storage.googleapis.com/etcd
GITHUB_URL=https://github.com/etcd-io/etcd/releases/download
DOWNLOAD_URL=${GOOGLE_URL}
// 删除已有的压缩包及解压文件夹
rm -f /tmp/etcd-${ETCD_VER}-darwin-amd64.zip
rm -rf /tmp/etcd-download-test && mkdir -p /tmp/etcd-download-test
// 下载压缩包
curl -L ${DOWNLOAD_URL}/${ETCD_VER}/etcd-${ETCD_VER}-darwin-amd64.zip -o /tmp/etcd-${ETCD_VER}-darwin-amd64.zip
// 压缩包解压后删除
unzip /tmp/etcd-${ETCD_VER}-darwin-amd64.zip -d /tmp && rm -f /tmp/etcd-${ETCD_VER}-darwin-amd64.zip
// 重命名文件夹
mv /tmp/etcd-${ETCD_VER}-darwin-amd64/* /tmp/etcd-download-test && rm -rf mv /tmp/etcd-${ETCD_VER}-darwin-amd64
// 检测是否生效
/tmp/etcd-download-test/etcd --version
/tmp/etcd-download-test/etcdctl version
上述步骤不是固定的,基本下载压缩包,解压即可使用。另:如果网络条件不好,直接点击github上的下载链接,可能会被禁止,可改用wget对链接进行下载。
etcdctl -get /xxx --prefix
可查看/xxx
路径下的所有key
etcdctl -h
查看使用命令,由于版本之间的差异,设置key
命令可能为set
(v2),也可能为put
(v3),注意区分
etcd
可视化工具etcd-viewer
测试的情况是暂时只能支持v2版本的HTTP api,而v3的gRPC api还不支持。暂时通过etcdctl
进行使用
二、confd安装
下载地址:https://github.com/kelseyhightower/confd/releases
与ETCD相似,直接点击链接可能会被禁止,使用wget。
下载完成的文件是一个二进制文件(不带任何后缀),重命名为confd
(非必须),使用命令chmod +x confd
赋予执行权限。使用命令./confd --help
查看是否生效
三、两者相互配合
使用onetime
生成目标配置文件,官方示例:
// etcd 设置key值,v3版本为put命令
etcdctl set /myapp/database/url db.example.com
etcdctl set /myapp/database/user rob
// 建立模版源文件
// /etc/confd/conf.d/myconfig.toml
[template]
src = "myconfig.conf.tmpl"
dest = "/tmp/myconfig.conf"
keys = [
"/myapp/database/url",
"/myapp/database/user",
]
// /etc/confd/templates/myconfig.conf.tmpl
[myconfig]
database_url = {{getv "/myapp/database/url"}}
database_user = {{getv "/myapp/database/user"}}
// 单一执行
confd -onetime -backend etcd -node http://127.0.0.1:2379
执行完毕后可查看/tmp/myconfig.conf
文件是否配置生效
上述文件夹/etc/confd
可通过./confd -confdir
命令自定义,从而不需要一定在etc
中进行文件配置
单一执行命令-onetime
可换为-watch
命令,检测key更新,并且及时更新到目标配置文件
注意:如果etcd版本为3时,
-backend
参数后面带etcdv3
,否则confd将不能识别请求返回。(https://github.com/kelseyhightower/confd/issues/826)
参考文章地址:
https://coolex.info/blog/483.html
https://www.jianshu.com/p/f68028682192