ETCD 简介及基本用法
2024-08-30 13:57:24
1. 简介
ETCD 是一个高可用的分布式键值数据库,可用于服务发现。ETCD 采用 raft 一致性算法,基于 Go 语言实现。
特点
简单:安装配置使用简单,提供 HTTP API
安全:支持 SSL 证书
可靠:采用 raft 算法,实现分布式系统数据的可用性和一致性
2. 用法
通过 etcdctl 可以使用 ETCD。假设使用 v3 版本,且集群设置如下:
export ETCDCTL_API=3
HOST_1=10.240.0.17
HOST_2=10.240.0.18
HOST_3=10.240.0.19
ENDPOINTS=$HOST_1:2379,$HOST_2:2379,$HOST_3:2379
etcdctl --endpoints=$ENDPOINTS member list
版本设置
ETCD v2 和 v3 两个版本不兼容,且 v2 已经停止支持。如果一个系统上同时按照了两个版本,需要通过环境变量来切换:
export ETCDCTL_API=3
读写数据
put 命令写数据:
etcdctl --endpoints=$ENDPOINTS put foo "Hello World!"
get 命令读数据:
etcdctl --endpoints=$ENDPOINTS get foo
etcdctl --endpoints=$ENDPOINTS --write-out="json" get foo
通过 --write-out="json"
选项读数据时,会读包括元数据在内的所有数据,并以 JSON 格式输出。
通过 prefix 选项读数据
etcdctl --endpoints=$ENDPOINTS put web1 value1
etcdctl --endpoints=$ENDPOINTS put web2 value2
etcdctl --endpoints=$ENDPOINTS put web3 value3
etcdctl --endpoints=$ENDPOINTS get web --prefix
删除数据
etcdctl --endpoints=$ENDPOINTS put key myvalue
etcdctl --endpoints=$ENDPOINTS del key
etcdctl --endpoints=$ENDPOINTS put k1 value1
etcdctl --endpoints=$ENDPOINTS put k2 value2
etcdctl --endpoints=$ENDPOINTS del k --prefix
事务性写入 txn
通过 txn
可以将多个请求放入一个事务:
etcdctl --endpoints=$ENDPOINTS put user1 bad
etcdctl --endpoints=$ENDPOINTS txn --interactive
compares:
value("user1") = "bad"
success requests (get, put, delete):
del user1
failure requests (get, put, delete):
put user1 good
监控数据变化 watch
通过 watch
选项,可以在将来发生改变时获得通知:
etcdctl --endpoints=$ENDPOINTS watch stock1
etcdctl --endpoints=$ENDPOINTS put stock1 1000
etcdctl --endpoints=$ENDPOINTS watch stock --prefix
etcdctl --endpoints=$ENDPOINTS put stock1 10
etcdctl --endpoints=$ENDPOINTS put stock2 20
安全写入 lease
lease
选项可以通过 TTL 写入数据:
etcdctl --endpoints=$ENDPOINTS lease grant 300
# lease 2be7547fbc6a5afa granted with TTL(300s)
etcdctl --endpoints=$ENDPOINTS put sample value --lease=2be7547fbc6a5afa
etcdctl --endpoints=$ENDPOINTS get sample
etcdctl --endpoints=$ENDPOINTS lease keep-alive 2be7547fbc6a5afa
etcdctl --endpoints=$ENDPOINTS lease revoke 2be7547fbc6a5afa
# or after 300 seconds
etcdctl --endpoints=$ENDPOINTS get sample
分布式锁 lock
etcdctl --endpoints=$ENDPOINTS lock mutex1
# another client with the same name blocks
etcdctl --endpoints=$ENDPOINTS lock mutex1
集群状态
为每台机器指定初始群集配置:
etcdctl --write-out=table --endpoints=$ENDPOINTS endpoint status
+------------------+------------------+---------+---------+-----------+-----------+------------+
| ENDPOINT | ID | VERSION | DB SIZE | IS LEADER | RAFT TERM | RAFT INDEX |
+------------------+------------------+---------+---------+-----------+-----------+------------+
| 10.240.0.17:2379 | 4917a7ab173fabe7 | 3.0.0 | 45 kB | true | 4 | 16726 |
| 10.240.0.18:2379 | 59796ba9cd1bcd72 | 3.0.0 | 45 kB | false | 4 | 16726 |
| 10.240.0.19:2379 | 94df724b66343e6c | 3.0.0 | 45 kB | false | 4 | 16726 |
+------------------+------------------+---------+---------+-----------+-----------+------------+
etcdctl --endpoints=$ENDPOINTS endpoint health
10.240.0.17:2379 is healthy: successfully committed proposal: took = 3.345431ms
10.240.0.19:2379 is healthy: successfully committed proposal: took = 3.767967ms
10.240.0.18:2379 is healthy: successfully committed proposal: took = 4.025451ms
Member
添加,删除,更新成员。
最新文章
- Java NIO2:缓冲区
- hibernate总记录数查询和分页查询
- Linux 基础入门 第二周9.21~9.27
- cygwin的rebaseall失败
- Enable test automation in Testlink
- PictureBox控件鼠标进入的手形改变和提示
- Linqer工具
- system进程启动普通用户进程调研
- 牛人眼中如何精通spring?
- ZYB's Game(博弈)
- OCX控件在IE中无法侦测到键盘消息( MFC ActiveX Control in IE Doesn't Detect Keystrokes)
- java里程碑之泛型--擦除和转换
- Matlab和C语言混合编程,包含目录的设定
- Google Chrome Plus——绿色便携多功能谷歌浏览器
- elementui+vue修改elementUi默认样式不生效
- 20164310Exp2后门原理与实践
- Java通过遍历sessionId获取服务器所有会话session
- java多线程知识点
- C - Roll-call in Woop Woop High
- POW的重力之美