etcd Cluster
Distributed reliable key-value store for critical data coordination.
Overview
etcd is a distributed, strongly consistent key-value store originally developed by CoreOS and now maintained by the Cloud Native Computing Foundation. Built on the Raft consensus algorithm, etcd serves as the backbone for critical distributed system coordination, most notably as the primary datastore for Kubernetes cluster state. Its design prioritizes consistency and reliability over performance, making it ideal for storing configuration data, metadata, and coordination information that distributed systems depend on. This three-node etcd cluster configuration implements a production-grade distributed setup that can tolerate one node failure while maintaining cluster availability and data consistency. The cluster uses the Raft consensus protocol to elect leaders, replicate data, and ensure that all nodes maintain identical state, providing the strong consistency guarantees that critical infrastructure components require. Each node participates in both client serving (port 2379) and peer communication (port 2380) to maintain cluster membership and data synchronization. Organizations running Kubernetes clusters, microservices architectures requiring distributed coordination, or any system needing reliable configuration management will benefit from this etcd cluster setup. The three-node configuration strikes the optimal balance between fault tolerance and resource efficiency, providing high availability while minimizing the overhead associated with larger cluster sizes that offer diminishing returns in most scenarios.
Key Features
- Raft consensus algorithm ensuring strong consistency across all cluster members
- Multi-version concurrency control (MVCC) for efficient read operations and historical queries
- Watch API for real-time notification of key changes and cluster state updates
- Lease-based TTL system for automatic key expiration and resource cleanup
- ACID transactions supporting conditional operations and atomic multi-key updates
- Built-in snapshot and restore capabilities for backup and disaster recovery
- gRPC-based API with both HTTP/JSON and binary protocol support
- Cluster membership management with automatic leader election and failover
Common Use Cases
- 1Kubernetes cluster datastore for storing cluster state, configuration, and secrets
- 2Microservices service discovery and configuration management across distributed environments
- 3Distributed system leader election for coordinating singleton processes
- 4Feature flag and configuration distribution with real-time updates across services
- 5Distributed locking and coordination for preventing race conditions in multi-instance applications
- 6Container orchestration metadata storage for custom scheduling and deployment systems
- 7CI/CD pipeline coordination and state management across distributed build agents
Prerequisites
- Minimum 1.5GB RAM total (512MB per etcd node) for stable cluster operation
- Understanding of distributed systems concepts including consensus algorithms and CAP theorem
- Network connectivity allowing communication on ports 2379 (client) and 2380 (peer)
- Basic familiarity with etcdctl command-line tool for cluster management and debugging
- Knowledge of Raft consensus behavior including leader election and split-brain scenarios
- SSD storage recommended for optimal write performance and reduced commit latency
For development & testing. Review security settings, change default credentials, and test thoroughly before production use. See Terms
docker-compose.yml
docker-compose.yml
1services: 2 etcd1: 3 image: quay.io/coreos/etcd:latest4 container_name: etcd15 command: 6 - etcd7 - --name=etcd18 - --initial-advertise-peer-urls=http://etcd1:23809 - --listen-peer-urls=http://0.0.0.0:238010 - --listen-client-urls=http://0.0.0.0:237911 - --advertise-client-urls=http://etcd1:237912 - --initial-cluster=etcd1=http://etcd1:2380,etcd2=http://etcd2:2380,etcd3=http://etcd3:238013 - --initial-cluster-state=new14 volumes: 15 - etcd1_data:/etcd-data16 ports: 17 - "2379:2379"18 networks: 19 - etcd-network2021 etcd2: 22 image: quay.io/coreos/etcd:latest23 container_name: etcd224 command: 25 - etcd26 - --name=etcd227 - --initial-advertise-peer-urls=http://etcd2:238028 - --listen-peer-urls=http://0.0.0.0:238029 - --listen-client-urls=http://0.0.0.0:237930 - --advertise-client-urls=http://etcd2:237931 - --initial-cluster=etcd1=http://etcd1:2380,etcd2=http://etcd2:2380,etcd3=http://etcd3:238032 - --initial-cluster-state=new33 volumes: 34 - etcd2_data:/etcd-data35 networks: 36 - etcd-network3738 etcd3: 39 image: quay.io/coreos/etcd:latest40 container_name: etcd341 command: 42 - etcd43 - --name=etcd344 - --initial-advertise-peer-urls=http://etcd3:238045 - --listen-peer-urls=http://0.0.0.0:238046 - --listen-client-urls=http://0.0.0.0:237947 - --advertise-client-urls=http://etcd3:237948 - --initial-cluster=etcd1=http://etcd1:2380,etcd2=http://etcd2:2380,etcd3=http://etcd3:238049 - --initial-cluster-state=new50 volumes: 51 - etcd3_data:/etcd-data52 networks: 53 - etcd-network5455volumes: 56 etcd1_data: 57 etcd2_data: 58 etcd3_data: 5960networks: 61 etcd-network: 62 driver: bridge.env Template
.env
1# etcd cluster configurationUsage Notes
- 1Docs: https://etcd.io/docs/
- 2Client API on port 2379 | Peer port 2380
- 3Use etcdctl: ETCDCTL_API=3 etcdctl --endpoints=localhost:2379 put key value
- 4Raft consensus protocol - requires 3+ nodes for HA
- 5Used by Kubernetes for cluster state storage
- 6Monitor: etcdctl endpoint health --cluster
Individual Services(3 services)
Copy individual services to mix and match with your existing compose files.
etcd1
etcd1:
image: quay.io/coreos/etcd:latest
container_name: etcd1
command:
- etcd
- "--name=etcd1"
- "--initial-advertise-peer-urls=http://etcd1:2380"
- "--listen-peer-urls=http://0.0.0.0:2380"
- "--listen-client-urls=http://0.0.0.0:2379"
- "--advertise-client-urls=http://etcd1:2379"
- "--initial-cluster=etcd1=http://etcd1:2380,etcd2=http://etcd2:2380,etcd3=http://etcd3:2380"
- "--initial-cluster-state=new"
volumes:
- etcd1_data:/etcd-data
ports:
- "2379:2379"
networks:
- etcd-network
etcd2
etcd2:
image: quay.io/coreos/etcd:latest
container_name: etcd2
command:
- etcd
- "--name=etcd2"
- "--initial-advertise-peer-urls=http://etcd2:2380"
- "--listen-peer-urls=http://0.0.0.0:2380"
- "--listen-client-urls=http://0.0.0.0:2379"
- "--advertise-client-urls=http://etcd2:2379"
- "--initial-cluster=etcd1=http://etcd1:2380,etcd2=http://etcd2:2380,etcd3=http://etcd3:2380"
- "--initial-cluster-state=new"
volumes:
- etcd2_data:/etcd-data
networks:
- etcd-network
etcd3
etcd3:
image: quay.io/coreos/etcd:latest
container_name: etcd3
command:
- etcd
- "--name=etcd3"
- "--initial-advertise-peer-urls=http://etcd3:2380"
- "--listen-peer-urls=http://0.0.0.0:2380"
- "--listen-client-urls=http://0.0.0.0:2379"
- "--advertise-client-urls=http://etcd3:2379"
- "--initial-cluster=etcd1=http://etcd1:2380,etcd2=http://etcd2:2380,etcd3=http://etcd3:2380"
- "--initial-cluster-state=new"
volumes:
- etcd3_data:/etcd-data
networks:
- etcd-network
Quick Start
terminal
1# 1. Create the compose file2cat > docker-compose.yml << 'EOF'3services:4 etcd1:5 image: quay.io/coreos/etcd:latest6 container_name: etcd17 command:8 - etcd9 - --name=etcd110 - --initial-advertise-peer-urls=http://etcd1:238011 - --listen-peer-urls=http://0.0.0.0:238012 - --listen-client-urls=http://0.0.0.0:237913 - --advertise-client-urls=http://etcd1:237914 - --initial-cluster=etcd1=http://etcd1:2380,etcd2=http://etcd2:2380,etcd3=http://etcd3:238015 - --initial-cluster-state=new16 volumes:17 - etcd1_data:/etcd-data18 ports:19 - "2379:2379"20 networks:21 - etcd-network2223 etcd2:24 image: quay.io/coreos/etcd:latest25 container_name: etcd226 command:27 - etcd28 - --name=etcd229 - --initial-advertise-peer-urls=http://etcd2:238030 - --listen-peer-urls=http://0.0.0.0:238031 - --listen-client-urls=http://0.0.0.0:237932 - --advertise-client-urls=http://etcd2:237933 - --initial-cluster=etcd1=http://etcd1:2380,etcd2=http://etcd2:2380,etcd3=http://etcd3:238034 - --initial-cluster-state=new35 volumes:36 - etcd2_data:/etcd-data37 networks:38 - etcd-network3940 etcd3:41 image: quay.io/coreos/etcd:latest42 container_name: etcd343 command:44 - etcd45 - --name=etcd346 - --initial-advertise-peer-urls=http://etcd3:238047 - --listen-peer-urls=http://0.0.0.0:238048 - --listen-client-urls=http://0.0.0.0:237949 - --advertise-client-urls=http://etcd3:237950 - --initial-cluster=etcd1=http://etcd1:2380,etcd2=http://etcd2:2380,etcd3=http://etcd3:238051 - --initial-cluster-state=new52 volumes:53 - etcd3_data:/etcd-data54 networks:55 - etcd-network5657volumes:58 etcd1_data:59 etcd2_data:60 etcd3_data:6162networks:63 etcd-network:64 driver: bridge65EOF6667# 2. Create the .env file68cat > .env << 'EOF'69# etcd cluster configuration70EOF7172# 3. Start the services73docker compose up -d7475# 4. View logs76docker compose logs -fOne-Liner
Run this command to download and set up the recipe in one step:
terminal
1curl -fsSL https://docker.recipes/api/recipes/etcd/run | bashTroubleshooting
- etcd cluster unhealthy after startup: Verify all nodes can reach each other on port 2380 and check Docker network connectivity
- Leader election failures or frequent re-elections: Increase heartbeat and election timeouts, check for network latency between nodes
- Database space exceeded errors: Run 'etcdctl defrag' on each node and consider enabling auto-compaction
- Split-brain scenarios with multiple leaders: Ensure odd number of nodes and verify --initial-cluster configuration matches across all members
- High memory usage and slow queries: Enable periodic compaction and check for excessive watch subscriptions
- Authentication failures after cluster restart: Verify RBAC settings and ensure auth tokens are properly configured across all nodes
Community Notes
Loading...
Loading notes...
Download Recipe Kit
Get all files in a ready-to-deploy package
Includes docker-compose.yml, .env template, README, and license
Ad Space
Shortcuts: C CopyF FavoriteD Download