命令;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
注意
- value 中包含转义字符时;可用双引号或单引号;引用输入内容避免转义
- value 中包含 - 时;避免被翻译为 flag;可以在 key 或 value 前加上 --
事例 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
获取键 get
命令;etcdctl get [options] <key> [range_end] [flags]
options
- --from-key 按照字节序;筛选大于或等于的 key
- --prefix 筛选以该前缀的 key
- --keys-only 只输出 key
- --print-value-only 只输出 value
- --limit <max_num> 限制输出条数;当为 0 时不作限制
- --sort-by <;create;|;key;|;modify;|;value;|;version;> 按照创建时间、键、修改时间、值或版本号排序
- --order <;ascend;|;descend;> 按照升序或降序排列
- --rev <version> 指定 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
删除键 del
命令;etcdctl del [options] <key> [range_end] [flags]
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
观察键 watch
命令;etcdctl watch [options] [key | prefix] [range_end] [--] [exec-command arg1 arg2 ...] [flags]
options
- --prefix
- --prev-kv
- --interactive 开启交互式
- --rev <version> 指定 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
压缩版本 compact
命令;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
租约 lease
键可以绑定一个租约;当租约过期或被废弃后;该键也会失效
命令
- etcdctl lease grant <ttl> [flags] 创建租约
- etcdctl lease list [flags] 返回所有未过时或未被废弃的租约
- etcdctl lease revoke <leaseID> [flags] 废弃租约
- etcdctl lease keep-alive [--once] <leaseID> [flags] 周期性续约;--once 只续约一次
- etcdctl lease timetolive <leaseID> [--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) ...
上一篇:常见路由器与交换机的管理
下一篇:分享一下如何在印象笔记中添加图片
相关文章
网友评论
快盘下载暂未开通留言功能。
-
推荐文章
最新文章