1 cluster
# 查看版本
$ ./bin/etcdctl version
etcdctl version: 3.5.0
API version: 3.5
# 通过endpoints参数指定连接的远端etcd节点
$ ./bin/etcdctl --endpoints=http://127.0.0.1:2379 version
etcdctl version: 3.5.0
API version: 3.5
# 查看集群成员
$ ./bin/etcdctl member list
8e9e05c52164694d, started, etcd-cnlab0, http://localhost:2380, http://localhost:2379, false
# 查看集群状态
$ ./bin/etcdctl endpoint status --cluster -w table
+-----------------------+------------------+---------+---------+-----------+------------+-----------+------------+--------------------+--------+
| ENDPOINT | ID | VERSION | DB SIZE | IS LEADER | IS LEARNER | RAFT TERM | RAFT INDEX | RAFT APPLIED INDEX | ERRORS |
+-----------------------+------------------+---------+---------+-----------+------------+-----------+------------+--------------------+--------+
| http://localhost:2379 | 8e9e05c52164694d | 3.5.0 | 20 kB | true | false | 2 | 4 | 4 | |
+-----------------------+------------------+---------+---------+-----------+------------+-----------+------------+--------------------+--------+
# 查看集群健康情况
$ ./bin/etcdctl endpoint health --cluster -w table
+-----------------------+--------+------------+-------+
| ENDPOINT | HEALTH | TOOK | ERROR |
+-----------------------+--------+------------+-------+
| http://localhost:2379 | true | 2.059608ms | |
+-----------------------+--------+------------+-------+
2 put
$ ./bin/etcdctl put /foo1 "hello world"
$ ./bin/etcdctl put /foo1 100
$ ./bin/etcdctl put /foo3 huhu
3 get
$ ./bin/etcdctl get /foo1
/foo1
hello world
# 只显示值,不显示key
$ ./bin/etcdctl get /foo1 --print-value-only
hello world
# 范围查询,左闭右开 [/foo1, /foo3)
$ ./bin/etcdctl get /foo1 /foo3 --print-value-only
hello world
100
# 范围查询,大于或等于指定key值
$ ./bin/etcdctl get --from-key /foo2 --print-value-only
100
huhu
# 前缀查询
$ ./bin/etcdctl get --prefix /foo --print-value-only
hello world
100
huhu
# 限制返回的结果数量
$ ./bin/etcdctl get --prefix --limit=2 /foo --print-value-only
hello world
100
# 查询key的详细信息。etcd维护了一个全局递增的revision值,每次修改某个key的值就会增加1. 从下面的信息可以看出,key /foo1的最新revision值是7,创建时的revision值是2,共有4个版本(表示写入了4次)
$ ./bin/etcdctl get /foo1 -w=json
{"header":{"cluster_id":14841639068965178418,"member_id":10276657743932975437,"revision":7,"raft_term":3},"kvs":[{"key":"L2ZvbzE=","create_revision":2,"mod_revision":7,"version":4,"value":"Z29vZCBpZGVh"}],"count":1}
# 查看指定revison版本的值
$ ./bin/etcdctl get /foo1 --rev=2 --print-value-only
hello world
4 del
删除命令与get基本相似
$ ./bin/etcdctl del /foo1
1
# 删除范围
$ ./bin/etcdctl del /foo1 /foo3
# 删除范围,大于或等于指定key值
$ ./bin/etcdctl del --from-key /foo1
# 根据前缀删除
$ ./bin/etcdctl del --prefix /foo1
# 返回删除前的值
$ ./bin/etcdctl del /foo1 --prev-kv
1
/foo1
hell
5 lease
# 创建租约,生命周期为120秒,租约id为694d7c20bfb1c716
$ $ ./bin/etcdctl lease grant 120
lease 694d7c20bfb1c716 granted with TTL(30s)
# 绑定租约
$ ./bin/etcdctl put /foo5 "hello world" --lease=694d82b0f0dfb037
OK
# 查看租约剩余的TTL
$ ./bin/etcdctl lease timetolive 694d82b0f0dfb037
lease 694d82b0f0dfb037 granted with TTL(120s), remaining(42s)
# 查看租约剩余的TTL,同时查看绑定的key
$ ./bin/etcdctl lease timetolive --keys 694d82b0f0dfb037
lease 694d82b0f0dfb037 granted with TTL(120s), remaining(32s), attached keys([/foo5])
# 持续续租,直到CTRL + C退出续租
$ ./bin/etcdctl lease keep-alive 694d82b0f0dfb037
lease 694d82b0f0dfb037 keepalived with TTL(120)
lease 694d82b0f0dfb037 keepalived with TTL(120)
# 撤销租约
$ ./bin/etcdctl revoke 694d7c20bfb1c716
# 租约到期后,绑定的key被删除
$ ./bin/etcdctl get /foo5
6 watch
在一个窗口监听key /foo10
$ ./bin/etcdctl watch /foo10
在另一个窗口修改key /foo10
的值
-bash-4.1$ ./bin/etcdctl put /foo10 30
OK
-bash-4.1$ ./bin/etcdctl put /foo10 "hello world"
OK
-bash-4.1$ ./bin/etcdctl del /foo10
1
-bash-4.1$ ./bin/etcdctl lease grant 30
lease 694d82b0f0dfb033 granted with TTL(30s)
-bash-4.1$ ./bin/etcdctl put /foo10 123 --lease=694d82b0f0dfb033
OK
可以观察到监听窗口打印以下key值变化的过程
$ ./bin/etcdctl watch /foo10
PUT
/foo10
30
PUT
/foo10
hello world
DELETE
/foo10
PUT
/foo10
123
# 由租约到期删除key触发
DELETE
/foo10
# 监听范围
$ ./bin/etcdctl watch /foo1 /foo10
# 监听前缀
$ ./bin/etcdctl watch --prefix /foo
网友评论