欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

etcd 入门实战(2)-etcdctl 使用,1、查看用法可以使用

来源: javaer 分享于  点击 2952 次 点评:20

etcd 入门实战(2)-etcdctl 使用,1、查看用法可以使用


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

1、查看用法

可以使用 etcdctl -h 或 etcdctl --help 查看 etcdctl 的用法。

2、全局参数

etcdctl 各子命令都可以使用的参数:

参数 说明 默认值
--debug 启用客户端 debug 日志 false
--endpoints etcd 服务器端点 127.0.0.1:2379
-w, --write-out 输出格式(fields, json, protobuf, simple, table) simple

3、键值操作

A、设置键值

etcdctl put [options] <key> <value>

如:

shell> ./etcdctl put abc hello
OK

B、查询键

etcdctl get [options] <key> [range_end] [flags]

如:

shell> ./etcdctl get abc
abc
hello

C、删除键

etcdctl del [options] <key> [range_end] [flags]

如:

shell> ./etcdctl del abc
1

4、成员操作

A、列出集群中的成员

etcdctl member list [flags]

如:

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, false

B、删除节点

etcdctl member remove <memberID> [flags]

如:

shell> ./etcdctl member remove 6336e054665be748
Member 6336e054665be748 removed from cluster 7d1fcb88f08d30f1

C、添加节点

etcdctl member add <memberName> [options] [flags]

如:

shell> ./etcdctl member add infra3 --peer-urls=http://10.49.196.33:2380
Member  6de4e36ac34b1ee 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 [flags]

如:

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 [flags]

如:

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 123

7、租约操作

etcdctl lease <subcommand> [flags]

如:

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 mutex1

9、选举操作

etcdctl elect <election-name> [proposal] [flags]

如:

shell> ./etcdctl elect my-elect p1

#另一终端执行
shell> ./etcdctl elect my-elect p2

 

参考:
https://github.com/etcd-io/etcd/tree/main/etcdctl。

相关栏目:

用户点评