What etcd Does
- Distributed KV — reliable storage with strong consistency
- Raft consensus — leader election and log replication
- Watches — subscribe to key changes in real time
- Leases — TTL-based key expiration
- Transactions — atomic multi-key compare-and-swap
- MVCC — multi-version concurrency control, historic reads
- gRPC API — binary protocol for performance
- Dynamic reconfiguration — add/remove members at runtime
Architecture
A cluster of 3 or 5 etcd members runs the Raft protocol. One is elected leader, others are followers. Writes go through the leader and are replicated. Reads can be linearizable (through leader) or serializable (local). Key revisions use MVCC.
Self-Hosting
# 3-node cluster
etcd --name infra1 --initial-advertise-peer-urls http://10.0.1.10:2380 \
--listen-peer-urls http://10.0.1.10:2380 \
--listen-client-urls http://10.0.1.10:2379,http://127.0.0.1:2379 \
--advertise-client-urls http://10.0.1.10:2379 \
--initial-cluster-token etcd-cluster-1 \
--initial-cluster infra1=http://10.0.1.10:2380,infra2=http://10.0.1.11:2380,infra3=http://10.0.1.12:2380 \
--initial-cluster-state newKey Features
- Strong consistency (Raft)
- Watches for change notifications
- Lease TTLs
- MVCC historical reads
- Transactions (compare-and-swap)
- Role-based access control
- TLS encryption
- Dynamic cluster reconfiguration
- gRPC client protocol
Comparison
| Store | Consistency | Use Case |
|---|---|---|
| etcd | Strong (Raft) | Config, K8s state |
| Consul | Strong (Raft) | Service discovery + KV |
| ZooKeeper | Strong (ZAB) | Coordination (legacy) |
| Redis | Eventual (cluster) | Cache, queues |
| TiKV | Strong (Raft) | Transactional KV store |
常见问题 FAQ
Q: 集群要几个节点? A: 奇数(3、5、7)以形成多数派。3 节点容忍 1 故障,5 容忍 2。更多节点写入延迟增加。
Q: etcd vs Consul? A: etcd 专注 KV + 强一致;Consul 功能更广(服务发现、健康检查、服务网格)。K8s 用 etcd 因为只需要可靠 KV。
Q: 数据上限? A: 默认 2GB,可调到 8GB。超过建议分 namespace 或用其他方案。etcd 不是通用数据库。
来源与致谢 Sources
- Docs: https://etcd.io/docs
- GitHub: https://github.com/etcd-io/etcd
- License: Apache 2.0