命令;etcdctl put [options] <key> <value> (<value> can also be given from stdin) [flags]
options
事例 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
注意
事例 3
$ etcdctl put k1 hello
world
OK
$ etcdctl get k1
k1
hellonworld
$ etcdctl put k1 -test
Error: unknown shorthand flag: ;t; in -test
事例 4
$ etcdctl put k1 ;hello
world;
OK
$ etcdctl get k1
k1
hello
world
$ etcdctl put -- k1 -testA
OK
$ etcdctl get k1
k1
-testA
$ etcdctl put k1 -- -testB
OK
$ etcdctl get k1
k1
-testB
命令;etcdctl get [options] <key> [range_end] [flags]
options
事例 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] <key> [range_end] [flags]
options
事例 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
事例 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> [flags]
丢弃给定 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
键可以绑定一个租约;当租约过期或被废弃后;该键也会失效
命令
事例 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)
...