一、etcd狀態查看
1、版本號查看
[root@localhost etcd-v3.3.10-linux-amd64]# ./etcdctl --version etcdctl version: 3.3.10 API version: 2
2、查看集群成員信息
[root@localhost etcd-v3.3.10-linux-amd64]# ./etcdctl member list 4c14bc06668e9505: name=etcd3 peerURLs=http://192.168.159.130:2380 clientURLs=http://192.168.159.130:2379 isLeader=false 57bf4d2527966724: name=etcd2 peerURLs=http://192.168.159.129:2380 clientURLs=http://192.168.159.129:2379 isLeader=true a11e107c0081dbf8: name=etcd1 peerURLs=http://192.168.159.128:2380 clientURLs=http://192.168.159.128:2379 isLeader=false
3、查看集群狀態
[root@localhost etcd-v3.3.10-linux-amd64]# ./etcdctl cluster-health member 4c14bc06668e9505 is healthy: got healthy result from http://192.168.159.130:2379 member 57bf4d2527966724 is healthy: got healthy result from http://192.168.159.129:2379 member a11e107c0081dbf8 is healthy: got healthy result from http://192.168.159.128:2379 cluster is healthy
4、查看leader與自己狀態
# 查看自己是否是leader [root@localhost etcd-v3.3.10-linux-amd64]# curl http://127.0.0.1:2379/v2/stats/leader {"message":"not current leader"} # 查看自己的狀態 [root@localhost etcd-v3.3.10-linux-amd64]# curl http://127.0.0.1:2379/v2/stats/self {
"name":"etcd1",
"id":"a11e107c0081dbf8",
"state":"StateFollower",
"startTime":"2021-02-13T14:29:28.567323474+08:00",
"leaderInfo":{"leader":"57bf4d2527966724","uptime":"10m7.805197808s","startTime":"2021-02-13T14:29:35.818024876+08:00"},
"recvAppendRequestCnt":10,
"sendAppendRequestCnt":0
}
5、--endpoints
[root@localhost etcd-v3.3.10-linux-amd64]# ./etcdctl --endpoint=192.168.159.128:2379 --version etcdctl version: 3.3.10 API version: 2
二、讀、寫、刪除
etcd默認使用的是v2版本的API,如果需要使用v3版本的需要先導入環境變量:
[root@localhost etcd-v3.3.10-linux-amd64]# export ETCDCTL_API=3
1、寫、讀操作
# 寫入多個值 [root@localhost etcd-v3.3.10-linux-amd64]# ./etcdctl put foo bar OK [root@localhost etcd-v3.3.10-linux-amd64]# ./etcdctl put foo1 bar1 OK [root@localhost etcd-v3.3.10-linux-amd64]# ./etcdctl put foo2 bar2 OK # 讀值 [root@localhost etcd-v3.3.10-linux-amd64]# ./etcdctl get foo foo bar # 讀取范圍值[start,end)半開區間 [root@localhost etcd-v3.3.10-linux-amd64]# ./etcdctl get foo foo3 foo bar foo1 bar1 foo2 bar2 # 前綴讀值 [root@localhost etcd-v3.3.10-linux-amd64]# ./etcdctl get --prefix foo foo bar foo1 bar1 foo2 bar2 # 使用--limit限制讀出的數量 [root@localhost etcd-v3.3.10-linux-amd64]# ./etcdctl get --prefix --limit=2 foo foo bar foo1 bar1 # --from-key按照key讀取值讀取比foo1大的值 [root@localhost etcd-v3.3.10-linux-amd64]# ./etcdctl get --from-key foo1 foo1 bar1 foo2 bar2
2、刪除操作
可以刪除一個etcd集群中一個key獲取一個范圍的key:
# 刪除一個key [root@localhost etcd-v3.3.10-linux-amd64]# ./etcdctl del foo 1 # 刪除一個范圍的key [root@localhost etcd-v3.3.10-linux-amd64]# ./etcdctl del foo foo3 2 # 通過--prev-kv刪除時會返回對應的value [root@localhost etcd-v3.3.10-linux-amd64]# ./etcdctl del --prev-kv k1 1 k1 v1
當然del命令也支持前綴--prefix和按照鍵--from-key來刪除數據。
三、租約(lease)
etcd支持申請定時器,比如:可以申請一個TTL=10s的lease(租約),會返回一個lease ID標識定時器。你可以在put一個key的同時攜帶lease ID,那么就實現了一個自動過期的key。在etcd中,一個release可以關聯任意多的key,當lease過期后鎖關聯的key都將被自動刪除。
1、租約的使用
# 申請租約 [root@localhost etcd-v3.3.10-linux-amd64]# ./etcdctl lease grant 50 lease 5bf8779a1477771f granted with TTL(50s) # 關聯租約到key [root@localhost etcd-v3.3.10-linux-amd64]# ./etcdctl put k3 v3 --lease=5bf8779a1477771f OK # 查看 # [root@localhost etcd-v3.3.10-linux-amd64]# ./etcdctl get k3 k3 v3 # 租約到期后查看 [root@localhost etcd-v3.3.10-linux-amd64]# ./etcdctl get k3 [root@localhost etcd-v3.3.10-linux-amd64]#
2、續租
租約會有到期的時候,客戶端可以通過刷新TTL的方式為租約續期,使其不過期(前提是它還沒過期):
# 到期后會自動續租,可另開一個終端進行測試 [root@localhost etcd-v3.3.10-linux-amd64]# ./etcdctl lease keep-alive 5bf8779a14777725 lease 5bf8779a14777725 keepalived with TTL(50) lease 5bf8779a14777725 keepalived with TTL(50) lease 5bf8779a14777725 keepalived with TTL(50) ...
3、撤銷租約
租約也可以撤銷,租約一旦被撤銷將會刪除綁定在上面的key:
[root@localhost etcd-v3.3.10-linux-amd64]# ./etcdctl lease revoke 5bf8779a14777727 lease 5bf8779a14777727 revoked [root@localhost etcd-v3.3.10-linux-amd64]# ./etcdctl get k5 #綁定的key已經不存在了
4、獲取租約信息
# 返回租約TTL以及剩下的時間 [root@localhost etcd-v3.3.10-linux-amd64]# ./etcdctl lease timetolive 5bf8779a1477772f lease 5bf8779a1477772f granted with TTL(300s), remaining(280s) #返回所綁定的key [root@localhost etcd-v3.3.10-linux-amd64]# ./etcdctl lease timetolive --keys 5bf8779a1477772f lease 5bf8779a1477772f granted with TTL(300s), remaining(244s), attached keys([k5])
四、觀察者(watch)
1、鍵、值觀察
etcd具有觀察機制,一旦某個key發生變化,etcd可以感知其變化。對應的就是客戶端的watch命令:
# 觀察已有的key {foo:bar},此時處於觀察等待中 [root@localhost etcd-v3.3.10-linux-amd64]# ./etcdctl watch foo #另起一個終端來更新foo [root@localhost etcd-v3.3.10-linux-amd64]# ./etcdctl put foo bar1 OK # 此時被觀擦的key的終端 [root@localhost etcd-v3.3.10-linux-amd64]# ./etcdctl watch foo PUT foo bar1
也可以觀察一個范圍的key:
# 觀察范圍key [root@localhost etcd-v3.3.10-linux-amd64]# ./etcdctl watch foo foo3 #另開終端 更新key [root@localhost etcd-v3.3.10-linux-amd64]# ./etcdctl put foo2 bar1 OK #原終端 [root@localhost etcd-v3.3.10-linux-amd64]# ./etcdctl watch foo foo3 PUT foo2 bar1
另外,也支持前綴--prefix觀察,如:./etcdctl watch --prefix foo
2、版本號觀察
觀察某個key的所有變化,比如:
[root@localhost etcd-v3.3.10-linux-amd64]# ./etcdctl put k6 v6 #reversion=2 OK [root@localhost etcd-v3.3.10-linux-amd64]# ./etcdctl put k6 new #reversion=3 OK
可以通過版本號進行觀察k6的所有變化(包括歷史記錄):
[root@localhost etcd-v3.3.10-linux-amd64]# ./etcdctl watch --rev=2 k6 PUT k6 v6 PUT k6 new
注意:用戶版本號從2開始,1是etcd的保留版本號。
