哎禹供 发表于 2025-6-8 12:11:36

etcd 入门实战(2)-etcdctl 使用

本文主要介绍 etcd 命令行客户端 etcdctl 的使用,文中所使用到的软件版本:etcd 3.5.18、Centos 7.9.2009。
1、查看用法

可以使用 etcdctl -h 或 etcdctl --help 查看 etcdctl 的用法。
2、全局参数

etcdctl 各子命令都可以使用的参数:
参数说明默认值--debug启用客户端 debug 日志false--endpointsetcd 服务器端点127.0.0.1:2379-w, --write-out输出格式(fields, json, protobuf, simple, table)simple3、键值操作

A、设置键值
etcdctl put <key> <value>如:
shell> ./etcdctl put abc hello
OKB、查询键
etcdctl get <key> 如:
shell> ./etcdctl get abc
abc
helloC、删除键
etcdctl del <key> 如:
shell> ./etcdctl del abc
14、成员操作

A、列出集群中的成员
etcdctl member list 如:
shell> ./etcdctl member list
3fcb05114d00bff, started, infra1, http://10.49.196.31:2380, http://10.49.196.31:2379, false
39d5fdc963168cff, started, infra0, http://10.49.196.30:2380, http://10.49.196.30:2379, false
6336e054665be748, started, infra2, http://10.49.196.32:2380, http://10.49.196.32:2379, falseB、删除节点
etcdctl member remove <memberID> 如:
shell> ./etcdctl member remove 6336e054665be748
Member 6336e054665be748 removed from cluster 7d1fcb88f08d30f1C、添加节点
etcdctl member add <memberName> 如:
shell> ./etcdctl member add infra3 --peer-urls=http://10.49.196.33:2380
Member6de4e36ac34b1ee added to cluster 7d1fcb88f08d30f1

ETCD_NAME="infra3"
ETCD_INITIAL_CLUSTER="infra1=http://10.49.196.31:2380,infra3=http://10.49.196.33:2380,infra0=http://10.49.196.30:2380"
ETCD_INITIAL_ADVERTISE_PEER_URLS="http://10.49.196.33:2380"
ETCD_INITIAL_CLUSTER_STATE="existing"5、端点操作

A、检查端点的健康状态
etcdctl endpoint health 如:
shell> ./etcdctl endpoint health --endpoints=http://10.49.196.30:2379,http://10.49.196.31:2379,http://10.49.196.32:2379 -w=table
+--------------------------+--------+------------+-------+
|         ENDPOINT         | HEALTH |    TOOK    | ERROR |
+--------------------------+--------+------------+-------+
| http://10.49.196.31:2379 |   true | 4.782328ms |       |
| http://10.49.196.32:2379 |   true | 3.244365ms |       |
| http://10.49.196.30:2379 |   true | 3.246379ms |       |
+--------------------------+--------+------------+-------+B、查看端点的状态
etcdctl endpoint status 如:
shell> ./etcdctl endpoint status --endpoints=http://10.49.196.30:2379,http://10.49.196.31:2379,http://10.49.196.32:2379 -w=table
+--------------------------+------------------+---------+---------+-----------+------------+-----------+------------+--------------------+--------+
|         ENDPOINT         |      ID      | VERSION | DB SIZE | IS LEADER | IS LEARNER | RAFT TERM | RAFT INDEX | RAFT APPLIED INDEX | ERRORS |
+--------------------------+------------------+---------+---------+-----------+------------+-----------+------------+--------------------+--------+
| http://10.49.196.30:2379 | 39d5fdc963168cff |3.5.18 |   20 kB |   false |      false |         8 |         79 |               79 |      |
| http://10.49.196.31:2379 |3fcb05114d00bff |3.5.18 |   25 kB |      true |      false |         8 |         79 |               79 |      |
| http://10.49.196.32:2379 | 666f49a44fa8c350 |3.5.18 |   20 kB |   false |      false |         8 |         79 |               79 |      |
+--------------------------+------------------+---------+---------+-----------+------------+-----------+------------+--------------------+--------+6、监控键的变化

shell> ./etcdctl watch abc
PUT
abc
123

#另一终端
shell> ./etcdctl put abc 1237、租约操作

etcdctl lease <subcommand> 如:
shell> ./etcdctl lease grant 100
lease 694d95c642d69c1b granted with TTL(100s)
shell>./etcdctl put sample value --lease=694d95c642d69c1b
OK
shell> ./etcdctl get sample
sample
value
shell> ./etcdctl get sample #等待 100s 或在另一终端执行 ./etcdctl lease revoke 694d95c642d69c1b
shell> 8、锁操作

#两个终端同时执行
shell> ./etcdctl lock mutex19、选举操作

etcdctl elect <election-name> 如:
shell> ./etcdctl elect my-elect p1

#另一终端执行
shell> ./etcdctl elect my-elect p2 
参考:
https://github.com/etcd-io/etcd/tree/main/etcdctl。

来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
页: [1]
查看完整版本: etcd 入门实战(2)-etcdctl 使用