CockroachDB Cluster
CockroachDB distributed SQL database cluster.
Overview
CockroachDB is a distributed SQL database designed to survive data center failures while maintaining ACID guarantees and horizontal scalability. Developed by Cockroach Labs and built on Google's Spanner paper, it combines the familiar SQL interface with automatic sharding, replication, and fault tolerance. The database uses a multi-version concurrency control system and implements the Raft consensus algorithm to ensure data consistency across nodes.
This deployment creates a three-node CockroachDB cluster with HAProxy as the load balancer and connection multiplexer. The HAProxy instance distributes SQL connections across all healthy CockroachDB nodes while providing a single endpoint for applications. Each CockroachDB node automatically replicates data using a default replication factor of 3, ensuring that the cluster can survive the loss of any single node without data loss or service interruption.
This configuration targets organizations requiring a PostgreSQL-compatible database with built-in horizontal scaling and high availability. Ideal for teams transitioning from traditional SQL databases who need geographic distribution, automatic failover, and the ability to scale beyond single-machine limitations without application rewrites.
Key Features
- Multi-active cluster with automatic leader election using Raft consensus
- PostgreSQL wire protocol compatibility for existing application integration
- Automatic range-based sharding with configurable replication zones
- Distributed ACID transactions with serializable snapshot isolation
- Built-in SQL optimizer with cost-based query planning
- HAProxy TCP load balancing with health checks for connection distribution
- Gossip protocol for cluster membership and metadata synchronization
- Time-travel queries and point-in-time recovery capabilities
Common Use Cases
- 1Multi-region financial applications requiring strong consistency guarantees
- 2E-commerce platforms needing automatic scaling during traffic spikes
- 3SaaS applications with tenant data isolation and geographic distribution
- 4Gaming backends requiring low-latency writes with global read replicas
- 5IoT data collection systems processing high-volume time-series data
- 6Microservices architectures needing distributed transaction coordination
- 7Disaster recovery setups with automated cross-datacenter replication
Prerequisites
- Minimum 2GB RAM per CockroachDB node (6GB total for cluster)
- Available ports 26257 (SQL) and 8080 (Admin UI) on Docker host
- HAProxy configuration file (haproxy.cfg) with CockroachDB backend definitions
- Understanding of distributed systems concepts and CAP theorem tradeoffs
- Familiarity with PostgreSQL syntax and connection parameters
- Network connectivity between nodes for gossip protocol communication
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 roach-1: 3 image: cockroachdb/cockroach:latest4 container_name: roach-15 restart: unless-stopped6 command: start --insecure --join=roach-1,roach-2,roach-37 volumes: 8 - roach1_data:/cockroach/cockroach-data910 roach-2: 11 image: cockroachdb/cockroach:latest12 container_name: roach-213 restart: unless-stopped14 command: start --insecure --join=roach-1,roach-2,roach-315 volumes: 16 - roach2_data:/cockroach/cockroach-data1718 roach-3: 19 image: cockroachdb/cockroach:latest20 container_name: roach-321 restart: unless-stopped22 command: start --insecure --join=roach-1,roach-2,roach-323 volumes: 24 - roach3_data:/cockroach/cockroach-data2526 roach-init: 27 image: cockroachdb/cockroach:latest28 container_name: roach-init29 command: init --insecure --host=roach-130 depends_on: 31 - roach-132 - roach-233 - roach-33435 haproxy: 36 image: haproxy:lts-alpine37 container_name: roach-haproxy38 restart: unless-stopped39 ports: 40 - "${SQL_PORT:-26257}:26257"41 - "${UI_PORT:-8080}:8080"42 volumes: 43 - ./haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg:ro44 depends_on: 45 - roach-146 - roach-247 - roach-34849volumes: 50 roach1_data: 51 roach2_data: 52 roach3_data: .env Template
.env
1# CockroachDB2SQL_PORT=262573UI_PORT=8080Usage Notes
- 1CockroachDB UI at http://localhost:8080
- 2SQL at localhost:26257
- 3PostgreSQL wire protocol
- 4Automatic replication
Individual Services(5 services)
Copy individual services to mix and match with your existing compose files.
roach-1
roach-1:
image: cockroachdb/cockroach:latest
container_name: roach-1
restart: unless-stopped
command: start --insecure --join=roach-1,roach-2,roach-3
volumes:
- roach1_data:/cockroach/cockroach-data
roach-2
roach-2:
image: cockroachdb/cockroach:latest
container_name: roach-2
restart: unless-stopped
command: start --insecure --join=roach-1,roach-2,roach-3
volumes:
- roach2_data:/cockroach/cockroach-data
roach-3
roach-3:
image: cockroachdb/cockroach:latest
container_name: roach-3
restart: unless-stopped
command: start --insecure --join=roach-1,roach-2,roach-3
volumes:
- roach3_data:/cockroach/cockroach-data
roach-init
roach-init:
image: cockroachdb/cockroach:latest
container_name: roach-init
command: init --insecure --host=roach-1
depends_on:
- roach-1
- roach-2
- roach-3
haproxy
haproxy:
image: haproxy:lts-alpine
container_name: roach-haproxy
restart: unless-stopped
ports:
- ${SQL_PORT:-26257}:26257
- ${UI_PORT:-8080}:8080
volumes:
- ./haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg:ro
depends_on:
- roach-1
- roach-2
- roach-3
Quick Start
terminal
1# 1. Create the compose file2cat > docker-compose.yml << 'EOF'3services:4 roach-1:5 image: cockroachdb/cockroach:latest6 container_name: roach-17 restart: unless-stopped8 command: start --insecure --join=roach-1,roach-2,roach-39 volumes:10 - roach1_data:/cockroach/cockroach-data1112 roach-2:13 image: cockroachdb/cockroach:latest14 container_name: roach-215 restart: unless-stopped16 command: start --insecure --join=roach-1,roach-2,roach-317 volumes:18 - roach2_data:/cockroach/cockroach-data1920 roach-3:21 image: cockroachdb/cockroach:latest22 container_name: roach-323 restart: unless-stopped24 command: start --insecure --join=roach-1,roach-2,roach-325 volumes:26 - roach3_data:/cockroach/cockroach-data2728 roach-init:29 image: cockroachdb/cockroach:latest30 container_name: roach-init31 command: init --insecure --host=roach-132 depends_on:33 - roach-134 - roach-235 - roach-33637 haproxy:38 image: haproxy:lts-alpine39 container_name: roach-haproxy40 restart: unless-stopped41 ports:42 - "${SQL_PORT:-26257}:26257"43 - "${UI_PORT:-8080}:8080"44 volumes:45 - ./haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg:ro46 depends_on:47 - roach-148 - roach-249 - roach-35051volumes:52 roach1_data:53 roach2_data:54 roach3_data:55EOF5657# 2. Create the .env file58cat > .env << 'EOF'59# CockroachDB60SQL_PORT=2625761UI_PORT=808062EOF6364# 3. Start the services65docker compose up -d6667# 4. View logs68docker 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/cockroachdb-cluster/run | bashTroubleshooting
- Clock synchronization failed: Ensure Docker host system time is synchronized via NTP
- Node won't join cluster: Verify --join parameter includes all node names exactly as defined
- Connection refused on port 26257: Check if roach-init container completed cluster initialization
- Range unavailable errors: Wait for cluster to achieve quorum, may take 2-3 minutes after startup
- HAProxy 503 errors: Verify CockroachDB nodes are healthy using admin UI health endpoints
- Persistent volume mounting issues: Ensure Docker daemon has sufficient permissions for volume creation
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