浅谈--ETCD的基本概念及用法

1. 简介

ETCD 是一个高可用的分布式键值数据库,可用于服务发现。ETCD 采用 raft 一致性算法,基于 Go 语言实现。

raft是一个强一致的集群日志同步算法。

ETCD使用gRPC,网络性能高

部署的节点数量必须是2N+1个

特点

简单:安装配置使用简单,提供 HTTP API

安全:支持 SSL 证书

可靠:采用 raft 算法,实现分布式系统数据的可用性和一致性

2. 用法

etcd  这是个节点     etcdctl这是个客户端

通过 etcdctl 可以使用 ETCD。假设使用 v3 版本,且集群设置如下:

export ETCDCTL_API=3                         ##注意警告信息,默认是v2的Api版本,要设置为v3的Api版本

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 命令读数据:     get会将k/v都检索出来

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            ##取出指定前缀的Key

删除数据

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

监听一个Key的变化,监听之后,这个Key的添加修改删除都会被监听到。

通过 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 20Key的全部操作都会监听到
➜ ./etcdctl put /aaa/a 123
OK
➜ ./etcdctl del /aaa/a
1
➜ ./etcdctl watch /aaa/a
PUT
/aaa/a
123
DELETE
/aaa/a
 

安全写入 lease(租约)

lease机制和redis中的Key的过期机制是一样的,可以申请一个lease,设置一个时间限制,然后将这个租约设置到一个Key上,那么这个Key过期将会自动删除或者续约。

lease 选项可以通过 TTL 写入数据:

etcdctl --endpoints=$ENDPOINTS lease grant 300           ----申请一个lease

# lease 2be7547fbc6a5afa granted with TTL(300s)           -----lease的ID

etcdctl --endpoints=$ENDPOINTS put sample value --lease=2be7547fbc6a5afa             ----将申请的lease设置到sample这个Key上

etcdctl --endpoints=$ENDPOINTS get sample

etcdctl --endpoints=$ENDPOINTS lease keep-alive 2be7547fbc6a5afa             -------keep-alive进行续租

etcdctl --endpoints=$ENDPOINTS lease revoke 2be7547fbc6a5afa                -----撤销Lease

# or after 300 seconds

etcdctl --endpoints=$ENDPOINTS get sample                    -----lease到期自动删除Key(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

 

 

ETCD的使用:

转载地址:https://www.cnblogs.com/linkstar/p/11022329.html

 

posted @   派大没有星  阅读(435)  评论(0编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 写一个简单的SQL生成工具
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)
点击右上角即可分享
微信分享提示