This page shows you how to manually deploy an insecure multi-node CockroachDB cluster on Amazon's AWS EC2 platform, using AWS's managed load balancing service to distribute client traffic.
Requirements
You must have SSH access to each machine. This is necessary for distributing and starting CockroachDB binaries.
Your network configuration must allow TCP communication on the following ports:
26257
for intra-cluster and client-cluster communication8080
to expose your Admin UI
Recommendations
If you plan to use CockroachDB in production, carefully review the Production Checklist.
Consider using a secure cluster instead. Using an insecure cluster comes with risks:
- Your cluster is open to any client that can access any node's IP addresses.
- Any user, even
root
, can log in without providing a password. - Any user, connecting as
root
, can read or write any data in your cluster. - There is no network encryption or authentication, and thus no confidentiality.
Decide how you want to access your Admin UI:
Access Level Description Partially open Set a firewall rule to allow only specific IP addresses to communicate on port 8080
.Completely open Set a firewall rule to allow all IP addresses to communicate on port 8080
.Completely closed Set a firewall rule to disallow all communication on port 8080
. In this case, a machine with SSH access to a node could use an SSH tunnel to access the Admin UI.All instances running CockroachDB should be members of the same Security Group.
Step 1. Configure your network
CockroachDB requires TCP communication on two ports:
26257
for inter-node communication (i.e., working as a cluster), for applications to connect to the load balancer, and for routing from the load balancer to nodes8080
for exposing your Admin UI
You can create these rules using Security Groups' Inbound Rules.
Inter-node and load balancer-node communication
Field | Recommended Value |
---|---|
Type | Custom TCP Rule |
Protocol | TCP |
Port Range | 26257 |
Source | The name of your security group (e.g., sg-07ab277a) |
Admin UI
Field | Recommended Value |
---|---|
Type | Custom TCP Rule |
Protocol | TCP |
Port Range | 8080 |
Source | Your network's IP ranges |
Application data
Field | Recommended Value |
---|---|
Type | Custom TCP Rules |
Protocol | TCP |
Port Range | 26257 |
Source | Your application's IP ranges |
Step 2. Create instances
Create an instance for each node you plan to have in your cluster.
Run at least 3 nodes to ensure survivability.
Use
m
(general purpose),c
(compute-optimized), ori
(storage-optimized) instances, with SSD-backed EBS volumes or Instance Store volumes. For example, Cockroach Labs has usedm3.large
instances (2 vCPUs and 7.5 GiB of RAM per instance) for internal testing.Do not use "burstable"
t2
instances, which limit the load on a single core.
For more details, see Hardware Recommendations and Cluster Topology.
Step 3. Synchronize clocks
CockroachDB requires moderate levels of clock synchronization to preserve data consistency. For this reason, when a node detects that its clock is out of sync with at least half of the other nodes in the cluster by 80% of the maximum offset allowed (500ms by default), it spontaneously shuts down. This avoids the risk of consistency anomalies, but it's best to prevent clocks from drifting too far in the first place by running clock synchronization software on each node.
Amazon provides the Amazon Time Sync Service, which uses a fleet of satellite-connected and atomic reference clocks in each AWS Region to deliver accurate current time readings. The service also smears the leap second.
- If you plan to run your entire cluster on AWS, configure each AWS instance to use the internal Amazon Time Sync Service.
- However, if you plan to run a hybrid cluster across AWS and other cloud providers or environments, configure all machines to use Google's external NTP service, which is comparably accurate and also handles "smearing" the leap second.
Step 4. Set up load balancing
Each CockroachDB node is an equally suitable SQL gateway to your cluster, but to ensure client performance and reliability, it's important to use load balancing:
Performance: Load balancers spread client traffic across nodes. This prevents any one node from being overwhelmed by requests and improves overall cluster performance (queries per second).
Reliability: Load balancers decouple client health from the health of a single CockroachDB node. In cases where a node fails, the load balancer redirects client traffic to available nodes.
AWS offers fully-managed load balancing to distribute traffic between instances.
- Add AWS load balancing. Be sure to:
- Set forwarding rules to route TCP traffic from the load balancer's port 26257 to port 26257 on the node Droplets.
- Configure health checks to use HTTP port 8080 and path
/health
.
- Note the provisioned IP Address for the load balancer. You'll use this later to test load balancing and to connect your application to the cluster.
Step 5. Start nodes
You can start the nodes manually or automate the process using systemd.
For each initial node of your cluster, complete the following steps:
SSH to the machine where you want the node to run.
Download the CockroachDB archive for Linux, and extract the binary:
$ curl https://binaries.cockroachdb.com/cockroach-v1.1.9.linux-amd64.tgz \ | tar -xz
Copy the binary into the
PATH
:$ cp -i cockroach-v1.1.9.linux-amd64/cockroach /usr/local/bin/
If you get a permissions error, prefix the command with
sudo
.Run the
cockroach start
command:$ cockroach start --insecure \ --host=<node1 address> \ --locality=<key-value pairs> \ --cache=.25 \ --max-sql-memory=.25 \ --join=<node1 address>:26257,<node2 address>:26257,<node3 address>:26257 \ --background
This command primes the node to start, using the following flags:
Flag Description --insecure
Indicates that the cluster is insecure, with no network encryption or authentication. --host
Specifies the hostname or IP address to listen on for intra-cluster and client communication, as well as to identify the node in the Admin UI. If it is a hostname, it must be resolvable from all nodes, and if it is an IP address, it must be routable from all nodes.
If you want the node to listen on multiple interfaces, leave--host
out.
If you want the node to communicate with other nodes on an internal address (e.g., within a private network) while listening on all interfaces, leave--host
out and set the--advertise-host
flag to the internal address.--locality
Key-value pairs that describe the location of the node, e.g., country, region, datacenter, rack, etc. It is recommended to set --locality
when deploying across multiple datacenters or when there is otherwise high latency between nodes. It is also required to use certain enterprise features. For more details, see Locality.--cache
--max-sql-memory
Increases the node's cache and temporary SQL memory size to 25% of available system memory to improve read performance and increase capacity for in-memory SQL processing (see Recommended Production Settings for more details). --join
Identifies the address and port of 3-5 of the initial nodes of the cluster. --background
Starts the node in the background so you gain control of the terminal to issue more commands. For other flags not explicitly set, the command uses default values. For example, the node stores data in
--store=cockroach-data
, binds internal and client communication to--port=26257
, and binds Admin UI HTTP requests to--http-port=8080
. To set these options manually, see Start a Node.Repeat these steps for each additional node that you want in your cluster.
For each initial node of your cluster, complete the following steps:
SSH to the machine where you want the node to run. Ensure you are logged in as the
root
user.Download the CockroachDB archive for Linux, and extract the binary:
$ curl https://binaries.cockroachdb.com/cockroach-v1.1.9.linux-amd64.tgz \ | tar -xz
Copy the binary into the
PATH
:$ cp -i cockroach-v1.1.9.linux-amd64/cockroach /usr/local/bin/
If you get a permissions error, prefix the command with
sudo
.Create the Cockroach directory:
$ mkdir /var/lib/cockroach
Create a Unix user named
cockroach
:$ useradd cockroach
Change the ownership of
Cockroach
directory to the usercockroach
:$ chown cockroach /var/lib/cockroach
Download the sample configuration template:
$ wget -qO- https://raw.githubusercontent.com/cockroachdb/docs/master/_includes/v1.1/prod-deployment/insecurecockroachdb.service
Alternatively, you can create the file yourself and copy the script into it:
[Unit] Description=Cockroach Database cluster node Requires=network.target [Service] Type=notify WorkingDirectory=/var/lib/cockroach ExecStart=/usr/local/bin/cockroach start --insecure --join=<node1 address>:26257,<node2 address>:26257,<node3 address>:26257 --cache=.25 --max-sql-memory=.25 TimeoutStopSec=60 Restart=always RestartSec=10 StandardOutput=syslog StandardError=syslog SyslogIdentifier=cockroach User=cockroach [Install] WantedBy=default.target
Save the file in the
/etc/systemd/system/
directoryCustomize the sample configuration template for your deployment:
Specify values for the following flags in the sample configuration template:
Flag Description --join
Identifies the address and port of 3-5 of the initial nodes of the cluster. --host
Specifies the hostname or IP address to listen on for intra-cluster and client communication, as well as to identify the node in the Admin UI. If it is a hostname, it must be resolvable from all nodes, and if it is an IP address, it must be routable from all nodes.
If you want the node to listen on multiple interfaces, leave--host
empty.
If you want the node to communicate with other nodes on an internal address (e.g., within a private network) while listening on all interfaces, leave--host
empty and set the--advertise-host
flag to the internal address.Start the CockroachDB cluster:
$ systemctl start insecurecockroachdb
Repeat these steps for each additional node that you want in your cluster.
systemd
handles node restarts in case of node failure. To stop a node without systemd
restarting it, run systemctl stop insecurecockroachdb
Step 6. Initialize the cluster
On your local machine, complete the node startup process and have them join together as a cluster:
Install CockroachDB on your local machine, if you haven't already.
Run the
cockroach init
command, with the--host
flag set to the address of any node:$ cockroach init --insecure --host=<address of any node>
Each node then prints helpful details to the standard output, such as the CockroachDB version, the URL for the admin UI, and the SQL URL for clients.
Step 7. Test the cluster
CockroachDB replicates and distributes data for you behind-the-scenes and uses a Gossip protocol to enable each node to locate data across the cluster.
To test this, use the built-in SQL client locally as follows:
On your local machine, launch the built-in SQL client, with the
--host
flag set to the address of any node:$ cockroach sql --insecure --host=<address of any node>
Create an
insecurenodetest
database:> CREATE DATABASE insecurenodetest;
Use
\q
orctrl-d
to exit the SQL shell.Launch the built-in SQL client, with the
--host
flag set to the address of a different node:$ cockroach sql --insecure --host=<address of different node>
View the cluster's databases, which will include
insecurenodetest
:> SHOW DATABASES;
+--------------------+ | Database | +--------------------+ | crdb_internal | | information_schema | | insecurenodetest | | pg_catalog | | system | +--------------------+ (5 rows)
Use
\q
orctrl-d
to exit the SQL shell.
Step 8. Set up monitoring and alerting
Despite CockroachDB's various built-in safeguards against failure, it is critical to actively monitor the overall health and performance of a cluster running in production and to create alerting rules that promptly send notifications when there are events that require investigation or intervention.
For details about available monitoring options and the most important events and metrics to alert on, see Monitoring and Alerting.
Step 9. Scale the cluster
You can start the nodes manually or automate the process using systemd.
For each additional node you want to add to the cluster, complete the following steps:
SSH to the machine where you want the node to run.
Download the CockroachDB archive for Linux, and extract the binary:
$ curl https://binaries.cockroachdb.com/cockroach-v1.1.9.linux-amd64.tgz \ | tar -xz
Copy the binary into the
PATH
:$ cp -i cockroach-v1.1.9.linux-amd64/cockroach /usr/local/bin/
If you get a permissions error, prefix the command with
sudo
.Run the
cockroach start
command just like you did for the initial nodes:$ cockroach start --insecure \ --host=<node4 address> \ --locality=<key-value pairs> \ --cache=.25 \ --max-sql-memory=.25 \ --join=<node1 address>:26257,<node2 address>:26257,<node3 address>:26257 \ --background
Update your load balancer to recognize the new node.
For each additional node you want to add to the cluster, complete the following steps:
SSH to the machine where you want the node to run. Ensure you are logged in as the
root
user.Download the CockroachDB archive for Linux, and extract the binary:
$ curl https://binaries.cockroachdb.com/cockroach-v1.1.9.linux-amd64.tgz \ | tar -xz
Copy the binary into the
PATH
:$ cp -i cockroach-v1.1.9.linux-amd64/cockroach /usr/local/bin/
If you get a permissions error, prefix the command with
sudo
.Create the Cockroach directory:
$ mkdir /var/lib/cockroach
Create a Unix user named
cockroach
:$ useradd cockroach
Change the ownership of
Cockroach
directory to the usercockroach
:$ chown cockroach /var/lib/cockroach
Download the sample configuration template:
$ wget -qO- https://raw.githubusercontent.com/cockroachdb/docs/master/_includes/v1.1/prod-deployment/insecurecockroachdb.service
Alternatively, you can create the file yourself and copy the script into it:
[Unit] Description=Cockroach Database cluster node Requires=network.target [Service] Type=notify WorkingDirectory=/var/lib/cockroach ExecStart=/usr/local/bin/cockroach start --insecure --join=<node1 address>:26257,<node2 address>:26257,<node3 address>:26257 --cache=.25 --max-sql-memory=.25 TimeoutStopSec=60 Restart=always RestartSec=10 StandardOutput=syslog StandardError=syslog SyslogIdentifier=cockroach User=cockroach [Install] WantedBy=default.target
Save the file in the
/etc/systemd/system/
directoryCustomize the sample configuration template for your deployment:
Specify values for the following flags in the sample configuration template:
Flag Description --host
Specifies the hostname or IP address to listen on for intra-cluster and client communication, as well as to identify the node in the Admin UI. If it is a hostname, it must be resolvable from all nodes, and if it is an IP address, it must be routable from all nodes.
If you want the node to listen on multiple interfaces, leave--host
empty.
If you want the node to communicate with other nodes on an internal address (e.g., within a private network) while listening on all interfaces, leave--host
empty and set the--advertise-host
flag to the internal address.--join
Identifies the address and port of 3-5 of the initial nodes of the cluster. Repeat these steps for each additional node that you want in your cluster.
Step 10. Use the cluster
Now that your deployment is working, you can:
- Implement your data model.
- Create users and grant them privileges.
- Connect your application. Be sure to connect your application to the AWS load balancer, not to a CockroachDB node.