接上文,配置好环境后,我们就来使用 etcdctl 学习基本用法
sh
复制代码
# export 只在当前的窗口的环境变量对有效 export ETCDCTL_API=3 HOST=192.168.159.128 ENDPOINTS=$HOST:12379,$HOST:22379,$HOST:32379 # 获取 etcd 集群列表 etcdctl --endpoints=$ENDPOINTS member list
0. hello world
sh
复制代码
# put 方法 etcdctl --endpoints=$ENDPOINTS put foo "Hello World!" # OK # get 方法 etcdctl --endpoints=$ENDPOINTS get foo # foo # Hello World! etcdctl --endpoints=$ENDPOINTS --write-out="json" get foo # {"header":{"cluster_id":11950442302842952918,"member_id":4958722187866700405,"revision":2,"raft_term":7},"kvs":[{"key":"Zm9v","create_revision":2,"mod_revision":2,"version":1,"value":"SGVsbG8gV29ybGQh"}],"count":1}
1. 前缀 key
sh
复制代码
etcdctl --endpoints=$ENDPOINTS put web1 value1 # OK etcdctl --endpoints=$ENDPOINTS put web2 value2 # OK etcdctl --endpoints=$ENDPOINTS put web3 value3 # OK etcdctl --endpoints=$ENDPOINTS get web --prefix # web1 # value1 # web2 # value2 # web3 # value3
2. 删除 key
sh
复制代码
etcdctl --endpoints=$ENDPOINTS put key myvalue # OK etcdctl --endpoints=$ENDPOINTS del key # 1 etcdctl --endpoints=$ENDPOINTS put k1 value1 # OK etcdctl --endpoints=$ENDPOINTS put k2 value2 # OK etcdctl --endpoints=$ENDPOINTS del k --prefix # 2
option:
--prefix[=false]:删除前缀匹配的键
--prev-kv[=false]:返回已删除的键值对
--from-key[=false]:使用字节比较删除大于或等于给定键的键
--range[=false]:立即删除键的范围
3.事务中的多次写入
txn
将多个请求打包到一个事务中
下面以 交互模式(interactive) 举例
sh
复制代码
etcdctl --endpoints=$ENDPOINTS put user1 bad # OK etcdctl --endpoints=$ENDPOINTS txn --interactive # compares: value("user1") = "bad" # success requests (get, put, del): del user1 # failure requests (get, put, del): put user1 good # SUCCESS # 1
compares
: 这部分定义了事务中的比较操作。在这里,它在 etcd 中检查键为"user1"
的值是否为"bad"
。success requests
: 这部分定义了在事务成功时要执行的操作。在这里,如果比较成功(即键 "user1" 的值为 "bad"),则会执行del user1
操作,删除键为 "user1" 的值。failure requests
: 这部分定义了在事务失败时要执行的操作。在这里,如果比较失败(即键 "user1" 的值不是 "bad"),则会执行put user1 good
操作,将键 "user1" 的值设置为 "good"。
最后一行输出 SUCCESS
表示事务最后执行了 success requests 还是 failure requests 下的命令。
总的来说,这个命令的目的是:如果键 "user1" 的值是 "bad",则删除键 "user1";否则,则将其值设置为 "good"。
4. 监听 key
watch 用法:etcdctl watch $KEY [$END_KEY]
在一个窗口里面输入下面命令,可以发现被阻塞了
sh
复制代码
etcdctl --endpoints=$ENDPOINTS watch stock1
新开一个窗口(别忘记配置这个窗口的环境变量),在执行下面命令
sh
复制代码
etcdctl --endpoints=$ENDPOINTS put stock1 1000 # OK
可以发现原先的窗口输出了
sh
复制代码
# PUT # stock1 # 1000
[$END_KEY]
可选参数-i, --interactive[=false]:互动模式
--prefix[=false]:如果设置了前缀,则监视前缀
--rev=0:修订开始观看
--prev-kv[=false]:在事件发生前获取前一个键值对
--progress-notify[=false]:从服务器获取定期监视进度通知
使用前缀功能监听
shell
复制代码
# 窗口 1 etcdctl --endpoints=$ENDPOINTS watch stock --prefix # 窗口 2 etcdctl --endpoints=$ENDPOINTS put stock1 10 etcdctl --endpoints=$ENDPOINTS put stock2 20
窗口 1 结果如下
shell
复制代码
# PUT # stock1 # 10 # PUT # stock2 # 20
5. 创建租约 Lease
创建租约
shell
复制代码
etcdctl --endpoints=$ENDPOINTS lease grant 300 # lease 7ace8e8408de070e granted with TTL(300s)
给键值对上租约
shell
复制代码
etcdctl --endpoints=$ENDPOINTS put sample value --lease=7ace8e8408de070e # OK etcdctl --endpoints=$ENDPOINTS get sample # sample # value
续约
shell
复制代码
etcdctl --endpoints=$ENDPOINTS lease keep-alive 7ace8e8408de070e # lease 7ace8e8408de070e keepalived with TTL(300)
撤销租约,和租约时间到达效果类似
shell
复制代码
etcdctl --endpoints=$ENDPOINTS lease revoke 7ace8e8408de070e # lease 7ace8e8408de070e revoked etcdctl --endpoints=$ENDPOINTS get sample
6. 创建 锁/分布式锁
官网上教程太简单了,参考了这个etcd:一款比Redis更骚的分布式锁的实现方式!用它 - 知乎 (zhihu.com)
使用 etcdctl 创建锁:
bash
复制代码
etcdctl --endpoints=$ENDPOINTS lock mutex1
这个命令将创建一个名为 mutex1
的锁。如果没有其他客户端持有相同名称的锁,那么该命令会成功获取锁并返回。否则,如果有其他客户端持有相同名称的锁,则该命令会阻塞,直到获取锁成功为止。
尝试获取锁:
再打开一个终端窗口,并执行相同的命令:
bash
复制代码
etcdctl --endpoints=$ENDPOINTS lock mutex1 # mutex1/1ee98e8408e28d0c # 上面是获取成功的结果,如果没成功,就会阻塞
这个命令会尝试获取名为 mutex1
的锁。如果之前的命令已经成功获取了锁,则这个命令会被阻塞,直到之前的锁被释放。
释放锁:
一旦一个客户端完成了对共享资源的访问,它应该释放锁,以允许其他客户端获取锁并访问共享资源。要释放锁,可以按下 Ctrl+C
终止当前的 etcdctl lock
命令。
7. 选举 Leader 节点
发起选举请求:
bash
复制代码
etcdctl --endpoints=$ENDPOINTS elect one p1
当多个选举者同时发起选举请求时,etcd 集群会根据选举算法来决定谁将成为领导者。
bash
复制代码
etcdctl --endpoints=$ENDPOINTS elect one p2
one
:指定选举的类型。在这个例子中,one
表示要进行的是一种单一领导者选举。在这种类型的选举中,只有一个节点可以成为领导者,其他节点将成为追随者。
p1
:指定选举者的名称。每个参与选举的节点都需要一个唯一的名称,以便在选举过程中被识别。在这个例子中,p1
是选举者的名称。
8. 检查集群状态
shell
复制代码
etcdctl --write-out=table --endpoints=$ENDPOINTS endpoint statusus +-----------------------+------------------+---------+---------+-----------+------------+-----------+------------+--------------------+--------+ | ENDPOINT | ID | VERSION | DB SIZE | IS LEADER | IS LEARNER | RAFT TERM | RAFT INDEX | RAFT APPLIED INDEX | ERRORS | +-----------------------+------------------+---------+---------+-----------+------------+-----------+------------+--------------------+--------+ | 192.168.159.128:12379 | ee83444820dfface | 3.3.13 | 25 kB | true | false | 7 | 75 | 0 | | | 192.168.159.128:22379 | 44d0eb8eb3a47a75 | 3.3.13 | 25 kB | false | false | 7 | 75 | 0 | | | 192.168.159.128:32379 | 25d45772f3301ee9 | 3.3.13 | 25 kB | false | false | 7 | 75 | 0 | | +-----------------------+------------------+---------+---------+-----------+------------+-----------+------------+--------------------+--------+
健康检查
sh
复制代码
etcdctl --endpoints=$ENDPOINTS endpoint health # 192.168.159.128:12379 is healthy: successfully committed proposal: took = 23.103296ms # 192.168.159.128:22379 is healthy: successfully committed proposal: took = 23.485907ms # 192.168.159.128:32379 is healthy: successfully committed proposal: took = 23.451049ms
9. 保存db文件
shell
复制代码
etcdctl --endpoints=$ENDPOINTS snapshot save my.db {"level":"info","ts":"2024-03-28T17:54:19.881726+0800","caller":"snapshot/v3_snapshot.go:65","msg":"created temporary db file","path":"my.db.part"} {"level":"info","ts":"2024-03-28T17:54:19.883132+0800","logger":"client","caller":"v3@v3.5.12/maintenance.go:212","msg":"opened snapshot stream; downloading"} {"level":"info","ts":"2024-03-28T17:54:19.8832+0800","caller":"snapshot/v3_snapshot.go:73","msg":"fetching snapshot","endpoint":"192.168.159.128:12379"} {"level":"info","ts":"2024-03-28T17:54:19.884331+0800","logger":"client","caller":"v3@v3.5.12/maintenance.go:220","msg":"completed snapshot read; closing"} {"level":"info","ts":"2024-03-28T17:54:19.885653+0800","caller":"snapshot/v3_snapshot.go:88","msg":"fetched snapshot","endpoint":"192.168.159.128:12379","size":"25 kB","took":"now"} {"level":"info","ts":"2024-03-28T17:54:19.885711+0800","caller":"snapshot/v3_snapshot.go:97","msg":"saved","path":"my.db"} Snapshot saved at my.db etcdutl --write-out=table snapshot status my.db +----------+----------+------------+------------+ | HASH | REVISION | TOTAL KEYS | TOTAL SIZE | +----------+----------+------------+------------+ | 8bc3a253 | 40 | 39 | 25 kB | +----------+----------+------------+------------+