$ etcdctl get --prefix foo # access the most recent versions of keysfoo
bar_new
foo1
bar1_new
$ etcdctl get --prefix --rev=4 foo # access the versions of keys at revision 4foo
bar_new
foo1
bar1
$ etcdctl get --prefix --rev=3 foo # access the versions of keys at revision 3foo
bar
foo1
bar1
$ etcdctl get --prefix --rev=2 foo # access the versions of keys at revision 2foo
bar
$ etcdctl get --prefix --rev=1 foo # access the versions of keys at revision 1
读取大于等于指定键字节值的键
应用程序可能需要读取字节值大于或等于指定键的键。
假设一个 etcd 集群中已存在以下键:
a=123b=456z=789
以下是读取键值大于或等于键 b 字节值的命令:
$ etcdctl get --from-key b
b
456z
789
删除键
应用程序可以从 etcd 集群中删除一个键或一组键。
假设一个 etcd 集群中已存在以下键:
foo= bar
foo1= bar1
foo3= bar3
zoo= val
zoo1= val1
zoo2= val2
a=123b=456z=789
以下是删除键 foo 的命令:
$ etcdctl del foo
1# one key is deleted
以下是删除键范围从 foo 到 foo9 的命令:
$ etcdctl del foo foo9
2# two keys are deleted
以下是删除键 zoo 的命令,删除后将返回被删除的键值对:
$ etcdctl del --prev-kv zoo
1# one key is deletedzoo # deleted keyval # the value of the deleted key
以下是用于删除前缀为 zoo 的键的命令:
$ etcdctl del --prefix zoo
2# two keys are deleted
以下是删除键值大于或等于键 b 字节值的命令:
$ etcdctl del --from-key b
2# two keys are deleted
监听键变化
应用程序可对键或键范围进行监听,以监控任何更新。
以下是监听键 foo 的命令:
$ etcdctl watch foo
# in another terminal: etcdctl put foo barPUT
foo
bar
以下是监听键 foo 的十六进制格式的命令:
$ etcdctl watch foo --hex
# in another terminal: etcdctl put foo barPUT
\x66\x6f\x6f # Key\x62\x61\x72# Value
以下是监听从 foo 到 foo9 范围键的命令:
$ etcdctl watch foo foo9
# in another terminal: etcdctl put foo barPUT
foo
bar
# in another terminal: etcdctl put foo1 bar1PUT
foo1
bar1
以下是监听键前缀为 foo 的键的命令:
$ etcdctl watch --prefix foo
# in another terminal: etcdctl put foo barPUT
foo
bar
# in another terminal: etcdctl put fooz1 barz1PUT
fooz1
barz1
以下是监听多个键 foo 和 zoo 的命令:
$ etcdctl watch -i
$ watch foo
$ watch zoo
# in another terminal: etcdctl put foo barPUT
foo
bar
# in another terminal: etcdctl put zoo valPUT
zoo
val
$ etcdctl put foo bar # revision = 2OK
$ etcdctl put foo1 bar1 # revision = 3OK
$ etcdctl put foo bar_new # revision = 4OK
$ etcdctl put foo1 bar1_new # revision = 5OK
以下是监听历史变更的示例:
# watch for changes on key `foo` since revision 2$ etcdctl watch --rev=2 foo
PUT
foo
bar
PUT
foo
bar_new
# watch for changes on key `foo` since revision 3$ etcdctl watch --rev=3 foo
PUT
foo
bar_new
以下是一个仅从最后一次历史变更开始监听的示例:
# watch for changes on key `foo` and return last revision value along with modified value$ etcdctl watch --prev-kv foo
# in another terminal: etcdctl put foo bar_latestPUT
foo # keybar_new # last value of foo key before modificationfoo # keybar_latest # value of foo key after modification
$ etcdctl watch -i
$ watch a
$ progress
progress notify: 1# in another terminal: etcdctl put x 0# in another terminal: etcdctl put y 1$ progress
progress notify: 3
$ etcdctl compact 5compacted revision 5# any revisions before the compacted one are not accessible$ etcdctl get --rev=4 foo
Error: rpc error: code=11desc= etcdserver: mvcc: required revision has been compacted
# grant a lease with 60 second TTL$ etcdctl lease grant 60lease 32695410dcc0ca06 granted with TTL(60s)# attach key foo to lease 32695410dcc0ca06$ etcdctl put --lease=32695410dcc0ca06 foo bar
OK
撤销租约
应用程序通过租约 ID 撤销租约。撤销租约将删除其所有关联的键。
假设已完成以下操作序列:
$ etcdctl lease grant 60lease 32695410dcc0ca06 granted with TTL(60s)$ etcdctl put --lease=32695410dcc0ca06 foo bar
OK
以下是撤销相同租约的命令:
$ etcdctl lease revoke 32695410dcc0ca06
lease 32695410dcc0ca06 revoked
$ etcdctl get foo
# empty response since foo is deleted due to lease revocation
保持租约有效
应用程序可通过刷新租约的 TTL 来维持租约有效,防止其过期。
假设已完成以下操作序列:
$ etcdctl lease grant 60lease 32695410dcc0ca06 granted with TTL(60s)
以下是保持相同租约持续有效的命令:
$ etcdctl lease keep-alive 32695410dcc0ca06
lease 32695410dcc0ca06 keepalived with TTL(60)lease 32695410dcc0ca06 keepalived with TTL(60)lease 32695410dcc0ca06 keepalived with TTL(60)...
# grant a lease with 500 second TTL$ etcdctl lease grant 500lease 694d5765fc71500b granted with TTL(500s)# attach key zoo1 to lease 694d5765fc71500b$ etcdctl put zoo1 val1 --lease=694d5765fc71500b
OK
# attach key zoo2 to lease 694d5765fc71500b$ etcdctl put zoo2 val2 --lease=694d5765fc71500b
OK
$ etcdctl lease timetolive --keys 694d5765fc71500b
lease 694d5765fc71500b granted with TTL(500s), remaining(132s), attached keys([zoo2 zoo1])# if the lease has expired or does not exist it will give the below response:Error: etcdserver: requested lease not found