命令:etcdctl put [options]
options
--lease
设置 key 所使用的 lease ID--prev-kv
返回该 key 修改之前的值--ignore-lease
使用当前的 lease 更新 key--ignore-value
使用当前的 value 更新 key事例 1
$ etcdctl put k1 a
OK
$ etcdctl put k1 b --prev-kv
OK
k1
a
$ etcdctl put k1 --ignore-value
OK
$ etcdctl get k1
k1
b
$ etcdctl lease grant 1000
lease 694d8403baac463d granted with TTL(1000s)
$ etcdctl put k1 c --lease 694d8403baac463d
OK
$ etcdctl put k1 d --ignore-lease
OK
事例 2
$ echo hello | etcdctl put k1
$ etcdctl get k1
k1
hello
注意
-
时,避免被翻译为 flag,可以在 key 或 value 前加上 --
事例 3
$ etcdctl put k1 hello\nworld
OK
$ etcdctl get k1
k1
hellonworld
$ etcdctl put k1 -test
Error: unknown shorthand flag: 't' in -test
事例 4
$ etcdctl put k1 "hello\nworld"
OK
$ etcdctl get k1
k1
hello\nworld
$ etcdctl put -- k1 -testA
OK
$ etcdctl get k1
k1
-testA
$ etcdctl put k1 -- -testB
OK
$ etcdctl get k1
k1
-testB
命令:etcdctl get [options]
options
--from-key
按照字节序,筛选大于或等于的 key--prefix
筛选以该前缀的 key--keys-only
只输出 key--print-value-only
只输出 value--limit
限制输出条数,当为 0 时不作限制--sort-by <"create"|"key"|"modify"|"value"|"version">
按照创建时间、键、修改时间、值或版本号排序--order <"ascend"|"descend">
按照升序或降序排列--rev
指定 key 的版本。etcd 对 key 的写操作,会更新全局修订版本,通过指定版本可以读取旧的数据--count-only
--consistency <"l"|"s">
一致性,默认是 Linearizable,可以设置为 Serializable事例 1
$ etcdctl get k1
k1
a
$ etcdctl get k1 k4
k1
a
k2
b
k3
c
$ etcdctl get k3 --from-key
k3
c
k4
d
$ etcdctl get k --prefix
k1
a
k2
b
k3
c
k4
d
$ etcdctl get k1 --keys-only
k1
$etcdctl get k1 --print-value-only
a
事例 2
$ etcdctl get k --prefix --sort-by modify --order descend
k2
b
k4
d
k1
a
k3
c
$ etcdctl get k --prefix --sort-by modify --order descend --limit 2
k2
b
k4
d
事例 3
# 假设版本信息如下
# f1 = a, rev = 1
# f2 = b, rev = 2
# f1 = c, rev = 3
# f2 = d, rev = 4
$ etcdctl get f --prefix --rev=1
f1
a
$ etcdctl get f --prefix --rev=2
f1
a
f2
b
$ etcdctl get f --prefix --rev=3
f1
c
f2
b
$ etcdctl get f --prefix --rev=4
f1
c
f2
d
命令:etcdctl del [options]
options
--from-key
按照字节序,筛选大于或等于的 key--prefix
筛选所有以该前缀 的 key--prev-kv
返回该 key 修改之前的值--range
该选项避免删除延迟,默认延迟 2s,事例 1
# 假设已有 k1 = a, k2 = b, k3 = c, k4 = d, k5 = e
$ etcdctl del k1
1
$ etcdctl del k5 --prev-kv
1
k2
b
$ etcdctl del k2 k5
Warning: Keys between "k2" and "k5" will be deleted. Please interrupt the command within next 2 seconds to cancel. You can provide `--range` flag to avoid the delay.
3
事例 2
# 假设已有 k1 = a, k2 = b, k3 = c, k4 = d, k5 = e
$ etcdctl del k2 k5 --range
3
$ etcdctl del k --prefix
2
事例 3
$ etcdctl del k2 --from-key
4
命令:etcdctl watch [options] [key | prefix] [range_end] [--] [exec-command arg1 arg2 ...] [flags]
options
--prefix
--prev-kv
--interactive
开启交互式--rev
指定 key 的版本,输出当前版本开始的所有状态变更--progress-notify
事例 1
$ etcdctl watch k1 k9
# 另一个终端:etcdctl put k1 abcdef
PUT
k1
abcdef
# 另一个终端:etcdctl del k1
DELETE
k1
$ etcdctl watch --prefix k --prev-kv
# 另一个终端:etcdctl put k1 abcdef
PUT
k1
abcdef
# 另一个终端:etcdctl del k1
DELETE
k1
a
k1
事例 2
$ etcdctl watch k1 k9
watch a
# 另一个终端:etcdctl put a 1
PUT
a
1
watch b
# 另一个终端:etcdctl put b 2
PUT
b
2
注意:状态发生变更后,执行 [--] [exec-command arg1 arg2 ...]
指定的命令
事例 3
$ etcdctl watch a -- echo hello a
# 另一个终端:etcdctl put a 1
PUT
a
1
hello a
事例 4
# 假设版本信息如下
# f1 = a, rev = 1
# f2 = b, rev = 2
# f1 = c, rev = 3
# f2 = d, rev = 4
$ etcdctl watch f1 --rev 2
PUT
f1
c
命令:etcdctl compaction [options]
丢弃给定 revision 之前的所有 etcd 事件历史。由于 etcd 使用多版本并发控制模型,它会以事件历史的形式保存所有的 key 更新记录。当给定 revision 之前的事件历史不再需要时候,此时可以对 key 进行压缩以释放 etcd 后端数据存储空间
options:--physical
等待物理移除所有旧的版本
事例
$ etcdctl compact 5
compacted revision 110
$ etcdctl get k1 --rev 3
{"level":"warn","ts":"2022-10-23T22:20:28.174+0800","logger":"etcd-client","caller":"v3/retry_interceptor.go:64","msg":"retrying of unary invoker failed","target":"etcd-endpoints://0x140000d81e0/127.0.0.1:2379","method":"/etcdserverpb.KV/Range","attempt":0,"error":"rpc error: code = OutOfRange desc = etcdserver: mvcc: required revision has been compacted"}
Error: etcdserver: mvcc: required revision has been compacted
键可以绑定一个租约,当租约过期或被废弃后,该键也会失效
命令
etcdctl lease grant [flags]
创建租约etcdctl lease list [flags]
返回所有未过时或未被废弃的租约etcdctl lease revoke [flags]
废弃租约etcdctl lease keep-alive [--once] [flags]
周期性续约,--once
只续约一次etcdctl lease timetolive [--keys] [flags]
获取租约信息,--keys
附带关联的键信息事例 1
$ etcdctl lease grant 1000
lease 694d84055233d604 granted with TTL(1000s)
$ etcdctl lease list
found 1 leases
694d84055233d604
$ etcdctl lease timetolive 694d84055233d604
lease 694d84055233d604 granted with TTL(1000s), remaining(948s)
$ etcdctl lease timetolive --keys
lease 694d84055233d604 granted with TTL(1000s), remaining(922s), attached keys([k1])
$ etcdctl put k1 a --lease 694d84055233d604
$ etcdctl lease revoke 694d84055233d604
lease 694d84055233d604 revoked
事例 2
$ etcdctl lease grant 10
lease 694d84055233d609 granted with TTL(10s)
$ etcdctl lease keep-alive --once
lease 694d84055233d609 keepalived with TTL(10)
$ etcdctl lease keep-alive
lease 694d84055233d609 keepalived with TTL(10)
lease 694d84055233d609 keepalived with TTL(10)
lease 694d84055233d609 keepalived with TTL(10)
lease 694d84055233d609 keepalived with TTL(10)
lease 694d84055233d609 keepalived with TTL(10)
lease 694d84055233d609 keepalived with TTL(10)
...