etcdctl 常用操作

使用 v3 版本,export ETCDCTL_API=3

常用操作

etcdctl 支持命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[root@test-173 test]#  etcdctl -h
NAME:
etcdctl - A simple command line client for etcd3.

USAGE:
etcdctl [flags]

VERSION:
3.4.13

API VERSION:
3.4

...

数据库操作

数据库操作基本围绕着对键值和目录的 CRUD 操作及其对应的生命周期管理。 etcd 这些操作符合 REST 风格的一套 API 操作

键操作

键操作包括最常用的增删改查操作,包括 PUTGETDELETE 等命令。

PUT 设置或者更新某个键的值

1
2
3
4
5
6
[root@test-173 test]# etcdctl put /test/foo1 "Hello world"
OK
[root@test-173 test]# etcdctl put /test/foo2 "Hello world2"
OK
[root@test-173 test]# etcdctl put /test/foo3 "Hello world3"
OK

GET 获取指定键的值

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[root@test-173 test]#  etcdctl get /test/foo1
/test/foo1
Hello world

[root@test-173 test]# etcdctl get /test/foo2 --hex # 返回 十六进制格式
\x2f\x74\x65\x73\x74\x2f\x66\x6f\x6f\x32
\x48\x65\x6c\x6c\x6f\x20\x77\x6f\x72\x6c\x64\x32

[root@test-173 test]# etcdctl get /test/foo3 --hex --print-value-only # 返回 十六进制格式,只获取值
\x48\x65\x6c\x6c\x6f\x20\x77\x6f\x72\x6c\x64\x33

[root@test-173 test]# etcdctl get /test/foo1 /test/foo3 --print-value-only # 获取 [/test/foo1, /test/foo3)
Hello world
Hello world2

[root@test-173 test]# etcdctl get --prefix --limit=2 /test/foo --print-value-only # 获取 前缀为 /test/foo 的两个值
Hello world
Hello world2

DELETE 删除一个键或者特定范围的键

1
2
3
4
5
6
7
8
9
10
11
12
[root@test-173 test]# etcdctl del /test/foo1  /test/foo2  # 删除 [/test/foo1, /test/foo2)
1 # 删除了一个健
[root@test-173 test]# etcdctl get /test/foo1
[root@test-173 test]# etcdctl get /test/foo2
/test/foo2
Hello world2

root@test-173 test]# etcdctl del --prev-kv /test/foo2 # 删除键并返回值
1
/test/foo2
Hello world2
[root@test-173 test]# etcdctl get /test/foo2

watch 键值对的改动

watch 监测一个或多个键值的变化,一旦键值发生更新,就会输出最新的值并退出

1
[root@test-173 ~]# etcdctl watch foo1

新开窗口

1
2
3
4
5
6
[root@test-173 ~]# etcdctl put foo1 Hello watch
OK
[root@test-173 ~]# etcdctl put foo bar
OK
[root@test-173 ~]# etcdctl put foo1 bar1
OK

查看 watch 窗口

1
2
3
4
5
6
7
[root@test-173 ~]# etcdctl watch foo1
PUT
foo1
Hello
PUT
foo1
bar1

lease(租约)

一旦租约的 TTL 到期,租约就会过期并且所有附带的键都将被删除

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
[root@test-173 test]# etcdctl lease grant 100  # 授予租约,TTL 为 100 秒
lease 75fd78a5bc8db873 granted with TTL(100s)

[root@test-173 test]# etcdctl put --lease=75fd78a5bc8db873 foo1 bar # 附加键 foo1 到租约 75fd78a5bc8db873
OK
[root@test-173 test]# etcdctl get foo1
foo1
bar
[root@test-173 test]# etcdctl get foo1 # 两分钟后,发现 已没有值

[root@test-173 test]# etcdctl put --lease=75fd78a5bc8db873 foo1 bar # 租约已失效
{"level":"warn","ts":"2021-04-06T15:14:17.315+0800","caller":"clientv3/retry_interceptor.go:62","msg":"retrying of unary invoker failed","target":"endpoint://client-71d3b0ce-8010-4407-ad17-71c5f1970309/127.0.0.1:2379","attempt":0,"error":"rpc error: code = NotFound desc = etcdserver: requested lease not found"}


[root@test-173 test]# etcdctl lease grant 1000
lease 75fd78a5bc8dbb9b granted with TTL(1000s)
[root@test-173 test]# etcdctl put --lease=75fd78a5bc8dbb9b foo1 bar
OK
[root@test-173 test]# etcdctl get foo1
foo1
bar
[root@test-173 test]# etcdctl lease revoke 75fd78a5bc8dbb9b # 撤销租约
lease 75fd78a5bc8dbb9b revoked
[root@test-173 test]# etcdctl get foo1 # 撤销租约将删除所有附带的 key


# 刷新租约
[root@test-173 test]# etcdctl lease keep-alive 75fd78a5bc8dbe37 # 一直刷新
lease 75fd78a5bc8dbe37 keepalived with TTL(100)
lease 75fd78a5bc8dbe37 keepalived with TTL(100)
lease 75fd78a5bc8dbe37 keepalived with TTL(100)
lease 75fd78a5bc8dbe37 keepalived with TTL(100)

# 新开一个端口
root@test-173 ~]# etcdctl put --lease=75fd78a5bc8dbe37 foo1 bar
OK
[root@test-173 ~]# etcdctl lease timetolive 75fd78a5bc8dbe37 # 获取有关租赁信息
lease 75fd78a5bc8dbe37 granted with TTL(100s), remaining(68s)

[root@test-173 ~]# etcdctl lease timetolive --keys 75fd78a5bc8dbe37 # 获取哪些 key 绑定了租赁信息:
lease 75fd78a5bc8dbe37 granted with TTL(100s), remaining(91s), attached keys([foo1])