本文最后更新于 2024-06-25,文章内容可能已经过时。

介绍

官网:https://etcd.io/

官方简介:

etcd is a strongly consistent, distributed key-value store that provides a reliable way to store data that needs to be accessed by a distributed system or cluster of machines. It gracefully handles leader elections during network partitions and can tolerate machine failure, even in the leader node.

翻译:

Etcd是一种强一致性的分布式键值存储,它提供了一种可靠的方式来存储需要由分布式系统或机器集群访问的数据。它在网络分区期间优雅地处理leader选举,并且可以容忍机器故障,甚至在leader节点中也是如此。

基于go实现,更轻量 性能更好.

详细原理为什么优势等待后续文章

简单来说,特点是分布式键值存储,强一致性,优雅的选举机制,容灾,安全可靠,高可用,用于云原生场景。选举机制raft协议,参考下文:

https://www.cnblogs.com/huaweiyuncce/p/10130522.htmlhttps://cloud.tencent.com/developer/article/1683582

etcd服务启动后,会进入 follower 状态,follower检测到leader 心跳超时后会进入选举状态。candidate广播选举请求,收到多数节点同意则变为leader。

类似: zookeeper/redis

安装

通过docker安装

notmastr@notmastr-PC:/media/notmastr/新加卷/program/work$ docker pull quay.io/coreos/etcd:v3.5.0

v3.5.0: Pulling from coreos/etcd
1813d21adc01: Pull complete 
6e96907ab677: Pull complete 
444ed0ea8673: Pull complete 
0fd2df5633f0: Pull complete 
8cc22b9456bb: Pull complete 
7ac70aecd290: Pull complete 
4b376c64dfe4: Pull complete 
Digest: sha256:28759af54acd6924b2191dc1a1d096e2fa2e219717a21b9d8edf89717db3631b
Status: Downloaded newer image for quay.io/coreos/etcd:v3.5.0
quay.io/coreos/etcd:v3.5.0
notmastr@notmastr-PC:/media/notmastr/新加卷/program/work$ 
notmastr@notmastr-PC:/media/notmastr/新加卷/program/work$ docker run -d \
>   -p 2379:2379 \
>   -p 2380:2380 \
>   --name etcd \
>   quay.io/coreos/etcd:v3.5.0 \
>   /usr/local/bin/etcd \
>   -advertise-client-urls http://0.0.0.0:2379 \
>   -listen-client-urls http://0.0.0.0:2379 \
>   -initial-advertise-peer-urls http://0.0.0.0:2380 \
>   -listen-peer-urls http://0.0.0.0:2380 \
>   -initial-cluster default=http://0.0.0.0:2380
dc50e7310b709c2e734d15f84987da42e947a2cfdba62a96e566a922d13cccee
notmastr@notmastr-PC:/media/notmastr/新加卷/program/work$ docker exec etcd etcd --version
etcd Version: 3.5.0
Git SHA: 946a5a6f2
Go Version: go1.16.3
Go OS/Arch: linux/amd64

参考

https://www.cnblogs.com/qxlzzj/p/18101279

状态机:每个ETCD节点都维护了一个状态机,用于写入已提交的raft log日志数据(保证leader的状态机数据是最新的,wal日志则是用于持久化数据操作)

https://zhuanlan.zhihu.com/p/663130092

Go使用

详见仓库:
https://github.com/notmaster-C/go-study/blob/master/db/etcd.go