在实际生产环境中,为了确保服务的可靠性与数据的安全性,像 Etcd 这样的分布式键值存储系统通常不会以单节点形式运行。本文将手把手带你完成一个三节点的 Etcd 集群部署,从环境规划、节点启动到集群状态验证,涵盖完整的二进制部署流程。
环境规划
首先,我们需要为每个节点定义唯一的名称并分配 IP 地址。这里我们根据节点的 IP 地址尾数来命名:
| 角色 |
IP |
| etcd100 |
192.168.31.100 |
| etcd101 |
192.168.31.101 |
| etcd102 |
192.168.31.102 |
部署与启动
我们将按照顺序在三台服务器上分别启动 Etcd 服务,它们将共同组成一个高可用的集群。对于大规模的数据存储和配置管理场景,一个健壮的分布式存储系统是基石,你可以深入数据库/中间件/技术栈板块探索更多相关知识。
1. 启动 etcd100 节点
由于 Etcd 集群使用 Raft 共识算法,单个节点无法选举出 Leader,因此第一个启动的节点会处于等待状态,无法正常提供读写服务。
./etcd \
--name etcd100 \
--data-dir /tmp/etcd \
--listen-client-urls http://192.168.31.100:2379 \
--advertise-client-urls http://192.168.31.100:2379 \
--listen-peer-urls http://192.168.31.100:2380 \
--initial-advertise-peer-urls http://192.168.31.100:2380 \
--initial-cluster etcd100=http://192.168.31.100:2380,etcd101=http://192.168.31.101:2380,etcd102=http://192.168.31.102:2380 \
--initial-cluster-token my-etcd-token \
--initial-cluster-state new
2. 启动 etcd101 节点
当第二个节点启动并加入集群后,集群成员达到选举所需的最小数量,此时可以成功选举出 Leader,集群开始对外提供服务。
./etcd \
--name etcd101 \
--data-dir /tmp/etcd \
--listen-client-urls http://192.168.31.101:2379 \
--advertise-client-urls http://192.168.31.101:2379 \
--listen-peer-urls http://192.168.31.101:2380 \
--initial-advertise-peer-urls http://192.168.31.101:2380 \
--initial-cluster etcd100=http://192.168.31.100:2380,etcd101=http://192.168.31.101:2380,etcd102=http://192.168.31.102:2380 \
--initial-cluster-token my-etcd-token \
--initial-cluster-state new
3. 启动 etcd102 节点
最后,启动第三个节点以完成集群的构建,集群的容错能力得到进一步提升。
./etcd \
--name etcd102 \
--data-dir /tmp/etcd \
--listen-client-urls http://192.168.31.102:2379 \
--advertise-client-urls http://192.168.31.102:2379 \
--listen-peer-urls http://192.168.31.102:2380 \
--initial-advertise-peer-urls http://192.168.31.102:2380 \
--initial-cluster etcd100=http://192.168.31.100:2380,etcd101=http://192.168.31.101:2380,etcd102=http://192.168.31.102:2380 \
--initial-cluster-token my-etcd-token \
--initial-cluster-state new
关键启动参数详解
为了清晰理解配置,这里对几个核心参数进行解释:
--listen-client-urls:服务监听客户端请求的地址,可配置多个(通常包括本机 IP 和 127.0.0.1)。
--advertise-client-urls:告知客户端应该使用哪个地址来连接本节点。
--listen-peer-urls:监听集群内部节点间通信的地址。
--initial-advertise-peer-urls:告知集群其他成员,与本节点通信时使用的地址。
--initial-cluster:集群初始成员列表。这里的节点名称(如etcd100)必须与--name参数一一对应。
--initial-cluster-token:集群令牌,所有节点必须使用相同的令牌才能组成一个集群。
--initial-cluster-state:集群初始状态,新集群使用new,已有集群添加节点时用existing。
检查 Etcd 集群状态
集群启动后,我们需要使用 etcdctl 工具进行健康检查和状态验证,这是保障服务稳定性的关键运维步骤。
首先设置 API 版本:
export ETCDCTL_API=3
然后,你可以使用以下多种方式来验证集群的健康与状态:
# 方式1:检查单个节点的健康状态
etcdctl --endpoints=192.168.31.100:2379 endpoint health
# 方式2:同时检查所有节点的健康状态
etcdctl --endpoints=192.168.31.100:2379,192.168.31.101:2379,192.168.31.102:2379 endpoint health
# 方式3:查看集群成员列表
etcdctl --endpoints=192.168.31.100:2379 member list
执行 member list 命令后,你会看到类似下图的输出,列出了所有成员的信息:

# 方式4:写入并读取测试数据,验证数据同步
etcdctl --endpoints=192.168.31.100:2379 put test_key "hello world"
etcdctl --endpoints=192.168.31.101:2379 get test_key
# 方式5:以表格形式查看所有节点的详细状态,包括领导者信息
etcdctl --endpoints=http://192.168.31.100:2379,http://192.168.31.101:2379,http://192.168.31.102:2379 endpoint status --write-out=table
endpoint status 命令的表格化输出非常直观,能清晰展示每个节点的版本、数据大小、是否为领导者等关键信息:

至此,一个高可用的三节点 Etcd 集群已经部署并验证完成。理解其部署过程是深入掌握分布式系统的基础。如果你对构建稳定、可扩展的后端架构感兴趣,可以到云栈社区与更多开发者交流学习。