etcd 入门实战(1)-简介及安装

‌etcd 是一个分布式的、可靠的键值存储系统,专门用于保存分布式系统中最关键的数据。‌本文主要包括 etcd 简介和安装;文中所使用到的软件版本:etcd 3.5.18、Centos 7.9.2009。

1、简介

etcd 是一个强一致性的分布式键值存储系统,提供了一种可靠的方式来存储分布式系统的数据。它能够优雅地在网络分区期间进行 leader 选举,并且可以容忍机器故障,甚至在 leader 节点中也是如此。

1.1、etcd 功能

1.2.1、监控

监控(Watch) 是指 etcd 客户端可以持续监听某个键值对或键值对范围的变化(包括增、删、改)。一旦发生变化,etcd 会将变化的内容推送给监控客户端,而无需客户端主动去查询。

1.2.2、租约

在 etcd 中,租约(Lease) 是一种机制,用于为键值对(KV)赋予一个生命周期。租约的引入是为了支持 TTL(Time-to-Live),即键值对在一定时间内有效,超时后会自动被删除。租约在分布式系统中非常有用,特别是在协调一致性和保持数据更新时。
租约的关键概念:
创建租约:当你为某个键值对分配租约时,这个键值对会与租约绑定。
续约:租约可以被续约。续约意味着你可以通过延长租约的生存时间来防止它过期。每次续约都会重置租约的到期时间。
租约过期:如果租约过期了,所有与该租约绑定的键值对将会被删除。
租约ID:每个租约都会有一个唯一的ID,用于标识该租约。通过该ID,用户可以管理租约的生命周期,包括续约、删除等操作。

1.2.3、基于租约的锁

etcd 提供了一个可靠、高效且强一致性的方式来实现分布式锁。基于租约的锁使用了 etcd 的租约(lease) 功能。在这种机制下,客户端可以为锁设置一个超时时间(即租约的生存时间),如果在指定时间内客户端没有续期租约,etcd 会自动删除该锁,从而避免死锁的发生。

1.2.4、选举

etcd 选举是基于 Raft 协议 实现的分布式 leader 选举机制。它用于保证在分布式系统中始终有一个唯一的 leader 节点来协调其他节点的工作。这是分布式一致性算法中的一个核心概念,确保系统中的所有节点对某个关键决策(如配置、资源分配等)达成一致,并且可以容忍部分节点故障而不影响系统的正常运行。

1.2、与 zookeeper 的区别

etcd 和 Zookeeper 都是分布式系统中常用的协调服务,它们都有存储配置数据、服务发现、提供高可用性和一致性等功能;虽然它们有很多相似之处,但也有一些区别。

特性 etcd Zookeeper
一致性协议 Raft ZAB
数据模型 键值存储(KV) 树状结构(Znodes)
性能 通常更快,特别是在写操作方面 相对较慢,特别是在高并发写操作时
API 简单,支持 HTTP/gRPC,现代化 相对复杂,主要支持 Java
用途 配置管理、服务发现、集群状态存储 分布式协调、分布式锁、选举、队列等
集群扩展性 通常支持 7 个节点以内的集群 支持较大的集群
适用场景 Kubernetes、云原生架构等 大数据系统、分布式应用等

2、安装

2.1、单机安装

2.1.1、下载并解压 etcd

Github(https://github.com/etcd-io/etcd/releases) 下载 etcd 的二进制包,然后解压:

tar zxvf etcd-v3.5.18-linux-amd64.tar.gz

2.1.2、启动

./etcd \
--listen-client-urls=http://10.49.196.33:2379,http://127.0.0.1:2379 \
--advertise-client-urls=http://10.49.196.33:2379

2.2、集群安装

假设在 10.49.196.30、10.49.196.31、10.49.196.32 三台机器上安装 etcd,在各机器上下载并解压 etcd 二进制包。

2.2.1、启动

10.49.196.30 上启动 etcd

./etcd --name infra0 \
  --initial-advertise-peer-urls http://10.49.196.30:2380 \
  --listen-peer-urls http://10.49.196.30:2380 \
  --listen-client-urls http://10.49.196.30:2379,http://127.0.0.1:2379 \
  --advertise-client-urls http://10.49.196.30:2379 \
  --initial-cluster-token etcd-cluster-1 \
  --initial-cluster infra0=http://10.49.196.30:2380,infra1=http://10.49.196.31:2380,infra2=http://10.49.196.32:2380 \
  --initial-cluster-state new

10.49.196.31 上启动 etcd

./etcd --name infra1 \
  --initial-advertise-peer-urls http://10.49.196.31:2380 \
  --listen-peer-urls http://10.49.196.31:2380 \
  --listen-client-urls http://10.49.196.31:2379,http://127.0.0.1:2379 \
  --advertise-client-urls http://10.49.196.31:2379 \
  --initial-cluster-token etcd-cluster-1 \
  --initial-cluster infra0=http://10.49.196.30:2380,infra1=http://10.49.196.31:2380,infra2=http://10.49.196.32:2380 \
  --initial-cluster-state new

10.49.196.32 上启动 etcd 启动

./etcd --name infra2 \
  --initial-advertise-peer-urls http://10.49.196.32:2380 \
  --listen-peer-urls http://10.49.196.32:2380 \
  --listen-client-urls http://10.49.196.32:2379,http://127.0.0.1:2379 \
  --advertise-client-urls http://10.49.196.32:2379 \
  --initial-cluster-token etcd-cluster-1 \
  --initial-cluster infra0=http://10.49.196.30:2380,infra1=http://10.49.196.31:2380,infra2=http://10.49.196.32:2380 \
  --initial-cluster-state new

以 –initial-cluster 开头的参数将在后续的 etcd 启动中被忽略;可以在初始引导过程完成后,放心地删除这些参数。

2.2.2、删除成员

可以从集群中删除成员,先使用命令(./etcdctl member list)查看成员列表:

3fcb05114d00bff, started, infra1, http://10.49.196.31:2380, http://10.49.196.31:2379, false
39d5fdc963168cff, started, infra0, http://10.49.196.30:2380, http://10.49.196.30:2379, false
714552ae5b7875d1, started, infra2, http://10.49.196.32:2380, http://10.49.196.32:2379, false

这里删除 infra2 这个节点:

./etcdctl member remove 714552ae5b7875d1

再次查看成员列表:

3fcb05114d00bff, started, infra1, http://10.49.196.31:2380, http://10.49.196.31:2379, false
39d5fdc963168cff, started, infra0, http://10.49.196.30:2380, http://10.49.196.30:2379, false

2.2.3、添加成员

可以向集群中添加新的成员,这里添加成员 infra3(http://10.49.196.33:2380)。

A、通过 HTTP 成员 API、gRPC 成员 API 或 etcdctl member add 命令将新成员添加到集群中

./etcdctl member add infra3 --peer-urls=http://10.49.196.33:2380

B、使用新的集群配置启动新成员

./etcd --name infra3 \
  --initial-advertise-peer-urls http://10.49.196.33:2380 \
  --listen-peer-urls http://10.49.196.33:2380 \
  --listen-client-urls http://10.49.196.33:2379,http://127.0.0.1:2379 \
  --advertise-client-urls http://10.49.196.33:2379 \
  --initial-cluster-token etcd-cluster-1 \
  --initial-cluster infra0=http://10.49.196.30:2380,infra1=http://10.49.196.31:2380,infra3=http://10.49.196.33:2380 \
  --initial-cluster-state existing

再次查看成员列表:

3fcb05114d00bff, started, infra1, http://10.49.196.31:2380, http://10.49.196.31:2379, false
39b5a133e03275b4, started, infra3, http://10.49.196.33:2380, http://10.49.196.33:2379, false
39d5fdc963168cff, started, infra0, http://10.49.196.30:2380, http://10.49.196.30:2379, false

2.3、配置说明

2.3.1、命令行参数

etcd 启动时,可以根据需要调整参数的默认值;通过 “./etcd -h” 查看所有的参数。

A、Member

参数 说明 默认值
name 成员名称 default
data-dir 数据目录 ${name}.etcd
wal-dir wal 目录  
snapshot-count 触发快照写入磁盘的已提交事务数量 100000
heartbeat-interval 心跳间隔(ms) 100
election-timeout 选举超时时间(ms) 1000
listen-peer-urls 节点间通信监听地址 http://localhost:2380
listen-client-urls 客户端请求监听地址,包括 http 和 grpc 请求 http://localhost:2379
listen-client-http-urls 客户端请求 http 监听地址,改参数将会从 listen-client-urls 中删除 http 服务  
max-snapshots 最大快照文件数 5
max-wals 最大 wal 文件时 5

B、Clustering

参数 说明 默认值
initial-advertise-peer-urls 对外发布的节点间通信地址 http://localhost:2380
initial-cluster 初始化集群的配置信息 default=http://localhost:2380
initial-cluster-state 初始化集群的状态(‘new’ or ‘existing’) new
initial-cluster-token 初始化集群的令牌  
advertise-client-urls 对外发布的客户端请求地址 http://localhost:2379

2.3.2、环境变量

每个参数都有一个对应的环境变量,环境变量的名称与参数相同,但前面会加上 ETCD_ 前缀,并且采用全大写和蛇形命名法。例如,–some-flag 对应的环境变量为 ETCD_SOME_FLAG。

例如,可以使用以下脚本启动 etcd:

export ETCD_LISTEN_CLIENT_URLS="http://10.49.196.33:2379,http://127.0.0.1:2379"
export ETCD_ADVERTISE_CLIENT_URLS="http://10.49.196.33:2379"

./etcd

2.3.3、配置文件

也可以把参数写到配置文件中,启动时指定配置文件。

A、新建 etcd.conf.yml 配置文件

listen-client-urls: http://10.49.196.33:2379
advertise-client-urls: http://10.49.196.33:2379

B、通过配置文件启动 etcd

./etcd --config-file=./etcd.conf.yml

更多配置可查看官方提供的样例文件:https://github.com/etcd-io/etcd/blob/main/etcd.conf.yml.sample。

2.3.4、配置优先级

命令行参数优先于环境变量。
如果提供了配置文件,则所有命令行参数和环境变量将被忽略。

 

参考:
https://etcd.io/docs/v3.5/op-guide/clustering/。