etcdctl客户端操作
作者:互联网
一、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的保留版本号。
标签:v3.3,10,amd64,etcdctl,etcd,linux,操作,localhost,客户端 来源: https://www.cnblogs.com/shenjianping/p/14400390.html