ETCD learning
Download etcd
#下载
wget https://github.com/etcd-io/etcd/releases/download/v3.3.18/etcd-v3.3.18-linux-amd64.tar.gz
#解压
tar zxvf etcd-v3.3.18-linux-amd64.tar.gz
cd etcd-v3.3.18-linux-amd64.tar.gz
#复制到用户目录
cp etcd etcdctl /usr/local/bin
#赋予执行权限
chmod +x etcd
chmod +x etcdctl
Start etcd
I am as an exercise, will etcd exposed to the public network can be directly accessed via ip, generally not recommended
./etcd --listen-client-urls 'http://0.0.0.0:2379' --advertise-client-urls 'http://0.0.0.0:2379'
# 后台启动
#nohup etcd --listen-client-urls 'http://0.0.0.0:2379' --advertise-client-urls 'http://0.0.0.0:2379' &
V3 set environment variables (default V2)
export ETCDCTL_API=3
#或者永久设置 vim /etc/profile 在最后添加export ETCDCTL_API=3 然后source /etc/profile
Operation etcd ( reference documentation )
Key-value command
PUT [options] <key> <value>
PUT provided kv value pairs, k, if exists, is covered
options Options
- lease - lease ID (16 hex lease ID) associated with the key
- prev-kv - a key former before returning modified
- ignore-value - the current value is updated using its key
- ignore-lease - use their current lease renewal key
Export
OK
Examples
# 设置
etcdctl put foo bar
# ok
#读取
etcdctl get foo
# foo
# bar
# 设置新kv并读取前一次的kv
etcdctl put foo bar1 --prev-kv
# OK
# foo
# bar
# 读取
etcdctl get foo
# foo
# bar1
# 给key添加租约(需要提前申请租约)
etcdctl put foo bar --lease=1234abcd
# 报错 etcdserver: requested lease not found。需要先申请一个租约
# 申请一个500秒的租约
etcdctl lease grant 500
# 给key添加租约
#lease 694d6ed6e8fed50f granted with TTL(500s)
etcdctl put foo bar --lease=694d6ed6e8fed50f
# OK
# 使用其当前租约更新key(为了使用现有租约)
etcdctl put foo bar4 --ignore-lease
# ok
etcdctl get foo
# foo
# bar4
# 使用当前值更新kv(为了解除租约,保持值不变)
etcdctl put foo --ignore-value
# OK
Remark
If you do not
If the value is the value includes "-", will be parsed as a flag, this time need to input "-" Resolution
etcdctl put foo -bar4
# Error: unknown shorthand flag: 'b' in -bar4
etcdctl put foo -- -bar4
etcdctl put -- foo -bar5
# OK
etcdctl put foo "bar 1 2 3"
# ok
etcdctl get foo
# foo
# bar 1 2 3
GET [options] <key> [range_end]
If range_end specified, the GET would get the key or key range [key, range_end) left and right to open and close
hex - hexadecimal coded output kv
limit - limit the maximum number of result sets
prefix - get the same prefix key
order - ascending or descending order to sort the results
sort-by - according to creation time, Key, modification time, values, goals version sort
rev - specified version
print-value-only - only the printing value when used with write-out = simple
consistency - linear identity or sequence identity
from-key - look back from the specified Key (comparison of the bytes)
keys-only - read only key
Output
<key>
<value>
<next_key>
<next_value>
...
Examples
First, set some Key
etcdctl put foo bar
# OK
etcdctl put foo1 bar1
# OK
etcdctl put foo2 bar2
# OK
etcdctl put foo3 bar3
# OK
Reading the key value foo
etcdctl get foo
# foo
# bar
Foo read key value, and performs 16-ary encoding
etcdctl get foo --hex
# \x66\x6f\x6f
# \x62\x61\x72
Reading the key value prefix foo
etcdctl get foo --prefix
# foo
# bar
# foo1
# bar1
# foo2
# foo2
# foo3
# bar3
Reading the key value foo prefix and reverse (the default is the ASCEND)
etcdctl get foo --prefix --order="DESCEND"
# foo3
# bar3
# foo2
# bar2
# foo1
# bar1
# foo
# bar
Read the value of all the key
etcdctl get --from-key ''
# foo
# bar
# foo1
# bar1
# foo2
# foo2
# foo3
# bar3
Reads the values of all key according creation time
etcdctl get foo --from-key '' --sort-by=CREATE
# foo
# bar
# foo1
# bar1
# foo2
# foo2
# foo3
# bar3
Reads the specified version of kv
etcdctl get foo --rev=0
# foo
# bar
Read only the specified version of the v
etcdctl get foo --print-value-only
# foo
Read only the specified version of k
etcdctl get foo --keys-only
# foo
#
Reading the key value is greater than the byte ordering of all key equal foo1
etcdctl get --from-key foo1
# foo1
# bar1
# foo2
# bar2
# foo3
# bar3
Reading the key value equals the byte ordering foo1 and less than all of the key foo3
etcdctl get foo1 foo3
# foo1
# bar1
# foo2
# bar2
Before reading the key value equals foo1 byte ordering all the key 2
etcdctl get --from-key foo1 --limit 2
# foo1
# bar1
# foo2
# bar2
Remark
If any key or value contains unprintable characters or control characters, since the wrap results in a simple format and output may not be clear. To solve this problem, set the --hex
hexadecimal encoding of all strings.
DEL [options] <key> [range_end]
If range_end specified, then delete the specified key or key range [key, range_end) left and right to open and close
Options
prefix - delete the specified prefix key
prev-kv - kv after deleting the return value
from-key - backward from the specified Key Delete (comparison of the bytes)
Delete the specified key
etcdctl put foo bar
# OK
etcdctl del foo
# 1
etcdctl get foo
Delete the specified key and returns the value of kv
etcdctl del foo1 --prev-kv
# 1
# foo1
# bar1
Began to be removed from the specified key
etcdctl del --from-key foo
# 2
Remove to foo prefix key
etcdctl del foo --prefix
# 2
TNX [options] (affairs)
TXN etcd read requests from the plurality of standard input and send them as a single atomic transaction applications.
Optional
hex - hex encoded string output
interactive - based on interactive prompts for transaction program
Input Format
<Txn> ::= <CMP>* "\n" <THEN> "\n" <ELSE> "\n"
<CMP> ::= (<CMPCREATE>|<CMPMOD>|<CMPVAL>|<CMPVER>|<CMPLEASE>) "\n"
<CMPOP> ::= "<" | "=" | ">"
<CMPCREATE> := ("c"|"create")"("<KEY>")" <CMPOP> <REVISION>
<CMPMOD> ::= ("m"|"mod")"("<KEY>")" <CMPOP> <REVISION>
<CMPVAL> ::= ("val"|"value")"("<KEY>")" <CMPOP> <VALUE>
<CMPVER> ::= ("ver"|"version")"("<KEY>")" <CMPOP> <VERSION>
<CMPLEASE> ::= "lease("<KEY>")" <CMPOP> <LEASE>
<THEN> ::= <OP>*
<ELSE> ::= <OP>*
<OP> ::= ((see put, get, del etcdctl command syntax)) "\n"
<KEY> ::= (%q formatted string)
<VALUE> ::= (%q formatted string)
<REVISION> ::= "\""[0-9]+"\""
<VERSION> ::= "\""[0-9]+"\""
<LEASE> ::= "\""[0-9]+\""
Export
SUCCESS
If etcd successfully processed transaction,
FAILURE
the transaction fails
Examples
Interactive mode if it is determined necessary to press twice enter, the next step
etcdctl put key1 1
etcdctl txn -i
# compares:
mod("key1") > "0"
# success requests (get, put, delete):
put key1 "overwrote-key1"
# failure requests (get, put, delete):
put key1 "created-key1"
put key2 "some extra key"
# SUCCESS
# OK
etcdctl get key --prefix
key1
overwrote-key1
key2
some extra key
# OK
Non-interactive mode
./etcdctl txn <<<'mod("key1") > "0"
put key1 "overwrote-key1"
put key1 "created-key1"
put key2 "some extra key"
'
# FAILURE
# OK
# OK
Remark
When using multi-line TXN command value, line breaks must be expressed is n. Newline character will lead to resolution failure.
COMPACTION [options] <revision> (压缩)
COMPACTION to discard all etcd event history before the scheduled revision. Since etcd multi-version concurrency control model, so it will leave all critical updates event history. When you no longer need to revise the history of the event, all the replaced key can be compressed to reclaim storage space etcd back-end database.
physical - compression to wait to physically remove all old revisions to true
Export
Print compressed version
Examples
etcdctl compaction 1
# compacted revision 1
WATCH [options] [key or prefix] [range_end] [--] [exec-command arg1 arg2 ...] (监听)
Optional
hex - Hex Trace
interactive - open interactive session monitor
prefix - monitor contains prefix key
prev-kv - kv kv before getting a change
rev - listening version
Input Format
Interactive mode only accept input.
watch [options] <key or prefix>\n
Export
<event>
[
<old_key>
<old_value>
]
<key>
<value>
...
Examples
Non-interactive mode
Enter etcdctl watch foo command to start listening, listening key when there is a change, print out the event key value
# bash1
etcdctl watch foo
# bash2
etcdctl put foo bar
# ok
# bash1
# PUT
# foo
# bar
LEASE <subcommand> (lease)
LEASE provides lease management set of commands
LEASE GRANT <ttl>
LEASE GRANT server selected time to live (in seconds) to create a new TTL value is greater than or equal to the lease request.
Export
Prints a message with the granted lease ID.
Example
etcdctl lease grant 60
# lease 694d6ed6e8fed554 granted with TTL(60s)
LEASE REVOKE <leaseID>
Rental withdrawal would destroy a given lease, and remove all additional key.
Output
Print the lease is revoked news
Examples
etcdctl lease revoke 694d6ed6e8fed558
# lease 694d6ed6e8fed558 revoked
LEASE TIMETOLIVE <leaseID> [options]
LEASE TIMETOLIVE retrieves a lease information of a given lease ID.
Options
- keys - keys get in on this lease
Export
Print lease information
Examples
# 申请一个租约
etcdctl lease grant 500
# lease 694d6ed6e8fed55b granted with TTL(500s)
# 将租约附加到kv上
etcdctl put foo1 bar --lease=694d6ed6e8fed55b
# OK
# 将租约附加到kv上
etcdctl put foo2 bar --lease=694d6ed6e8fed55b
# OK
# 查看租约的信息
etcdctl lease timetolive 694d6ed6e8fed55b
# lease 694d6ed6e8fed55b granted with TTL(500s), remaining(386s)
# 查看租约的信息和签约的keys
etcdctl lease timetolive 2d8257079fa1bc0c --keys
# lease 694d6ed6e8fed55b granted with TTL(500s), remaining(352s), attached keys([foo1 foo2])
# 查看租约的信息,以json输出
etcdctl lease timetolive 2d8257079fa1bc0c --write-out=json
# {"cluster_id":14841639068965178418,"member_id":10276657743932975437,"revision":31,"raft_term":2,"id":7587842816500225371,"ttl":321,"granted-ttl":500,"keys":null}
# 查看租约的信息和签约的keys,以json输出
etcdctl lease timetolive 2d8257079fa1bc0c --write-out=json --keys
# {"cluster_id":14841639068965178418,"member_id":10276657743932975437,"revision":31,"raft_term":2,"id":7587842816500225371,"ttl":289,"granted-ttl":500,"keys":["Zm9vMQ==","Zm9vMg=="]}
# 租约过期
etcdctl lease timetolive 694d6ed6e8fed55b
# lease 694d6ed6e8fed55b already expired
LEASE LIST
LEASE LIST lists all valid lease
Export
Valid lease list
Examples
etcdctl lease grant 500
# lease 694d6ed6e8fed560 granted with TTL(500s)
etcdctl lease list
# found 1 leases
# 694d6ed6e8fed560
LEASE KEEP-ALIVE <leaseID>
LEASE KEEP-ALIVE refreshed periodically so that the lease does not expire
Output
Print remains active for each sent a message, or printing a message indicating that the lease has expired.
Example
# 起一个10秒生存期的租约
etcdctl lease grant 10
# lease 694d6ed6e8fed562 keepalived with TTL(10)
# lease 694d6ed6e8fed562 keepalived with TTL(10)
# lease 694d6ed6e8fed562 keepalived with TTL(10)
# lease 694d6ed6e8fed562 keepalived with TTL(10)
# lease 694d6ed6e8fed562 keepalived with TTL(10)
...