1 cluster
# 查看版本
$ ./bin/etcdctl version
etcdctl version: 3.5.0
API version: 3.5
# 通過endpoints參數(shù)指定連接的遠(yuǎn)端etcd節(jié)點(diǎn)
$ ./bin/etcdctl --endpoints=http://127.0.0.1:2379 version
etcdctl version: 3.5.0
API version: 3.5
# 查看集群成員
$ ./bin/etcdctl member list
8e9e05c52164694d, started, etcd-cnlab0, http://localhost:2380, http://localhost:2379, false
# 查看集群狀態(tài)
$ ./bin/etcdctl endpoint status --cluster -w table
+-----------------------+------------------+---------+---------+-----------+------------+-----------+------------+--------------------+--------+
| ENDPOINT | ID | VERSION | DB SIZE | IS LEADER | IS LEARNER | RAFT TERM | RAFT INDEX | RAFT APPLIED INDEX | ERRORS |
+-----------------------+------------------+---------+---------+-----------+------------+-----------+------------+--------------------+--------+
| http://localhost:2379 | 8e9e05c52164694d | 3.5.0 | 20 kB | true | false | 2 | 4 | 4 | |
+-----------------------+------------------+---------+---------+-----------+------------+-----------+------------+--------------------+--------+
# 查看集群健康情況
$ ./bin/etcdctl endpoint health --cluster -w table
+-----------------------+--------+------------+-------+
| ENDPOINT | HEALTH | TOOK | ERROR |
+-----------------------+--------+------------+-------+
| http://localhost:2379 | true | 2.059608ms | |
+-----------------------+--------+------------+-------+
2 put
$ ./bin/etcdctl put /foo1 "hello world"
$ ./bin/etcdctl put /foo1 100
$ ./bin/etcdctl put /foo3 huhu
3 get
$ ./bin/etcdctl get /foo1
/foo1
hello world
# 只顯示值,不顯示key
$ ./bin/etcdctl get /foo1 --print-value-only
hello world
# 范圍查詢竿拆,左閉右開 [/foo1, /foo3)
$ ./bin/etcdctl get /foo1 /foo3 --print-value-only
hello world
100
# 范圍查詢宙拉,大于或等于指定key值
$ ./bin/etcdctl get --from-key /foo2 --print-value-only
100
huhu
# 前綴查詢
$ ./bin/etcdctl get --prefix /foo --print-value-only
hello world
100
huhu
# 限制返回的結(jié)果數(shù)量
$ ./bin/etcdctl get --prefix --limit=2 /foo --print-value-only
hello world
100
# 查詢key的詳細(xì)信息。etcd維護(hù)了一個(gè)全局遞增的revision值丙笋,每次修改某個(gè)key的值就會(huì)增加1. 從下面的信息可以看出谢澈,key /foo1的最新revision值是7,創(chuàng)建時(shí)的revision值是2御板,共有4個(gè)版本(表示寫入了4次)
$ ./bin/etcdctl get /foo1 -w=json
{"header":{"cluster_id":14841639068965178418,"member_id":10276657743932975437,"revision":7,"raft_term":3},"kvs":[{"key":"L2ZvbzE=","create_revision":2,"mod_revision":7,"version":4,"value":"Z29vZCBpZGVh"}],"count":1}
# 查看指定revison版本的值
$ ./bin/etcdctl get /foo1 --rev=2 --print-value-only
hello world
4 del
刪除命令與get基本相似
$ ./bin/etcdctl del /foo1
1
# 刪除范圍
$ ./bin/etcdctl del /foo1 /foo3
# 刪除范圍锥忿,大于或等于指定key值
$ ./bin/etcdctl del --from-key /foo1
# 根據(jù)前綴刪除
$ ./bin/etcdctl del --prefix /foo1
# 返回刪除前的值
$ ./bin/etcdctl del /foo1 --prev-kv
1
/foo1
hell
5 lease
# 創(chuàng)建租約,生命周期為120秒稳吮,租約id為694d7c20bfb1c716
$ $ ./bin/etcdctl lease grant 120
lease 694d7c20bfb1c716 granted with TTL(30s)
# 綁定租約
$ ./bin/etcdctl put /foo5 "hello world" --lease=694d82b0f0dfb037
OK
# 查看租約剩余的TTL
$ ./bin/etcdctl lease timetolive 694d82b0f0dfb037
lease 694d82b0f0dfb037 granted with TTL(120s), remaining(42s)
# 查看租約剩余的TTL缎谷,同時(shí)查看綁定的key
$ ./bin/etcdctl lease timetolive --keys 694d82b0f0dfb037
lease 694d82b0f0dfb037 granted with TTL(120s), remaining(32s), attached keys([/foo5])
# 持續(xù)續(xù)租,直到CTRL + C退出續(xù)租
$ ./bin/etcdctl lease keep-alive 694d82b0f0dfb037
lease 694d82b0f0dfb037 keepalived with TTL(120)
lease 694d82b0f0dfb037 keepalived with TTL(120)
# 撤銷租約
$ ./bin/etcdctl revoke 694d7c20bfb1c716
# 租約到期后灶似,綁定的key被刪除
$ ./bin/etcdctl get /foo5
6 watch
在一個(gè)窗口監(jiān)聽key /foo10
$ ./bin/etcdctl watch /foo10
在另一個(gè)窗口修改key /foo10
的值
-bash-4.1$ ./bin/etcdctl put /foo10 30
OK
-bash-4.1$ ./bin/etcdctl put /foo10 "hello world"
OK
-bash-4.1$ ./bin/etcdctl del /foo10
1
-bash-4.1$ ./bin/etcdctl lease grant 30
lease 694d82b0f0dfb033 granted with TTL(30s)
-bash-4.1$ ./bin/etcdctl put /foo10 123 --lease=694d82b0f0dfb033
OK
可以觀察到監(jiān)聽窗口打印以下key值變化的過程
$ ./bin/etcdctl watch /foo10
PUT
/foo10
30
PUT
/foo10
hello world
DELETE
/foo10
PUT
/foo10
123
# 由租約到期刪除key觸發(fā)
DELETE
/foo10
# 監(jiān)聽范圍
$ ./bin/etcdctl watch /foo1 /foo10
# 監(jiān)聽前綴
$ ./bin/etcdctl watch --prefix /foo