ETCD部署
ETCD 的部署方式主要取决于你的使用场景。对于个人开发或功能验证,最简单的就是本地单节点部署;而在生产环境中,则必须部署多节点集群来保证高可用。
我把几种主流的部署方式和适用场景整理了一下,你可以根据需求来选:
| 部署方式 | 适用场景 | 复杂度 | 关键步骤 / 工具 |
|---|---|---|---|
| 本地快速体验 | 开发测试、功能验证、学习研究 | ⭐ | 直接运行etcd二进制文件 |
| 单节点服务 | 小型项目、个人服务、对高可用无要求的内部环境 | ⭐⭐ | 预编译二进制包 + systemd 管理 |
| 容器化部署 | 微服务架构、需要环境隔离、集成到 K8s 环境 | ⭐⭐⭐ | Docker / Kubernetes StatefulSet |
| 多节点集群 | 生产环境、核心服务、对高可用和数据一致性有严格要求 | ⭐⭐⭐⭐⭐ | 至少3台服务器 + 手动配置集群参数 |
ETCD包
https://github.com/etcd
下载最新版本的 ETCD 二进制文件,解压并运行。适用于开发或测试环境。
wget https://github.com/etcd-io/etcd/releases/download/v3.5.0/etcd-v3.5.0-linux-amd64.tar.gz tar -xvf etcd-v3.5.0-linux-amd64.tar.gz cd etcd-v3.5.0-linux-amd64 ./etcd默认端口2379、2380 1. 监听client请求的ip & port listen-client-urls: http://127.0.0.1:2379 2. 该节点在集群内通信的ip & port listen-peer-urls: http://127.0.0.1:2380 即使是单节点部署,这里也需要配置集群内的通信ip & port部署
单点部署
推荐方式是从 GitHub 下载预编译二进制包来安装。不推荐用apt等包管理器,因为版本可能过旧。安装后建议创建systemd服务文件来管理,能实现开机自启和自动重启
# This is the configuration file for the etcd server. # Human-readable name for this member. name: 'etcd-single-node' # Path to the data directory.>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使用 systemd 管理
创建 systemd 服务文件,实现 ETCD 开机自启和进程管理。
# /etc/systemd/system/etcd.service [Unit] Description=etcd key-value store Documentation=https://github.com/etcd-io/etcd [Service] ExecStart=/usr/local/bin/etcd Restart=always User=etcd Type=notify [Install] WantedBy=multi-user.target安全配置
启用 TLS 加密通信,提高 ETCD 集群安全性。
./etcd --name secure-node \ --cert-file=/path/to/server.crt \ --key-file=/path/to/server.key \ --trusted-ca-file=/path/to/ca.crt \ --peer-cert-file=/path/to/peer.crt \ --peer-key-file=/path/to/peer.key \ --peer-trusted-ca-file=/path/to/ca.crt监控与维护
定期备份 ETCD 数据,确保数据安全。
# Prometheus 配置示例 scrape_configs: - job_name: 'etcd' static_configs: - targets: ['10.0.0.1:2379']启动
nohup ./etcd --config-conf > etcd.log 2>&1 &性能调优建议
硬件配置
- SSD存储设备确保IO性能
- 每个节点至少2核CPU和8GB内存
- 生产环境建议万兆网络
参数优化
--heartbeat-interval:适当调低心跳间隔(默认100ms)--election-timeout:选举超时时间设置为心跳间隔的5倍--snapshot-count:触发快照的事务数(默认100000)
验证
bin/etcdctl --endpoints=http://$ip:$port member list bin/etcdctl --endpoints=http://$ip:$port endpoint status -w=table添加用户
#添加root bin/etcdctl --endpoints=http://127.0.0.1:2379 user add root #开启鉴权 bin/etcdctl --endpoints=http://127.0.0.1:2379 auth enable #添加普通用户 bin/etcdctl --endpoints=http://127.0.0.1:2379 name_test:password #添加角色 bin/etcdctl --endpoints=http://127.0.0.1:2379 --user=root:123456 role add normal #角色授权 bin/etcdctl --endpoints=http://127.0.0.1:2379 role grant-permission --prefix=true normal readwrite /v1/api #用户绑定角色 bin/etcdctl --endpoints http://127.0.0.1:2379 --user=root:123456 user grant-role name_test normal