etcd常用命令

etcd下载地址:https://github.com/etcd-io/etcd/releases

1、只查看 Key 列表
作用:只列出 key,不显示 value。
使用场景:👉 快速浏览 ETCD 中有哪些 key,适合做全局排查。

[yunwei@localhost etcd-v3.6.2-linux-amd64]$ ./etcdctl get --prefix --keys-only ""
/registry/apiextensions.k8s.io/customresourcedefinitions/applications.argoproj.io

/registry/apiextensions.k8s.io/customresourcedefinitions/applicationsets.argoproj.io

/registry/apiextensions.k8s.io/customresourcedefinitions/appprojects.argoproj.io

2、只查看 Value
作用:只显示 value,不输出 key。
使用场景:👉 用于配置内容的导出与备份。

[yunwei@localhost etcd-v3.6.2-linux-amd64]$ ./etcdctl  get /registry/configmaps/argocd/argocd-cm --print-value-only

3、列出指定前缀的所有键值
作用:模糊查询,获取所有以 /config/ 开头的键值。
使用场景:👉 当忘记具体 key 时,通过前缀快速查找。

[yunwei@localhost etcd-v3.6.2-linux-amd64]$ ./etcdctl get /config/ --prefix

4、批量删除指定前缀的键
作用:一次性删除某类配置。
使用场景:👉 清空某个业务模块配置,避免残留数据影响新版本

etcdctl del /config/ --prefix

5、从文件写入配置
使用场景:👉 将修改后的配置批量写入 ETCD,常用于 配置恢复、环境初始化。

etcdctl put /config/xxx < /tmp/xxxx.txt

6、监听 Key 的变化
作用:实时监听某个 key 是否有变动。
使用场景:👉 调试配置动态更新机制,验证程序是否能正确响应。

etcdctl watch /config/xxxx

7、快照备份与恢复
使用场景:👉 适用于全量数据保护,灾备恢复必备。

备份:
etcdctl snapshot save snapshot.db
恢复:
etcdctl snapshot restore snapshot.db --data-dir /var/lib/etcd-restored

参考文档:
https://www.cnblogs.com/wuyongyin/p/18782617
https://www.51cto.com/article/823119.html

posted @ 2025-08-21 11:23  wh459086748  阅读(43)  评论(0)    收藏  举报