$ 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を16進数形式で監視するコマンドは、次のとおりです。
$ 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
# 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