The
--insecure flag used in this tutorial is intended for non-production testing only. To run CockroachDB in production, use a secure cluster instead.Before you begin
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:
26257for intra-cluster and client-cluster communication8080to expose your DB Console
- Carefully review the and recommended .
- Do not run multiple node processes on the same VM or machine. This defeats CockroachDB’s replication and causes the system to be a single point of failure. Instead, start each node on a separate VM or machine.
-
To start a node with multiple disks or SSDs, provide a separate
--storeflag for each disk when starting thecockroachprocess on the node. For more details about stores, see . If you start a node with multiple--storeflags, it is not possible to scale back down to only using a single store on the node. Instead, you must decommission the node and start a new node with the updated--store. -
When starting each node, use the flag to describe the node’s location, for example,
--locality=region=west,zone=us-west-1. The key-value pairs should be ordered from most to least inclusive, and the keys and order of key-value pairs must be the same on all nodes. -
When deploying in a single availability zone:
- To be able to tolerate the failure of any 1 node, use at least 3 nodes with the . In this case, if 1 node fails, each range retains 2 of its 3 replicas, a majority.
- To be able to tolerate 2 simultaneous node failures, use at least 5 nodes and to 5. The replication factor for is 5 by default, so no adjustments are needed for internal data. In this case, if 2 nodes fail at the same time, each range retains 3 of its 5 replicas, a majority.
-
When deploying across multiple availability zones:
- To be able to tolerate the failure of 1 entire AZ in a region, use at least 3 AZs per region and set
--localityon each node to spread data evenly across regions and AZs. In this case, if 1 AZ goes offline, the 2 remaining AZs retain a majority of replicas. - To ensure that ranges are split evenly across nodes, use the same number of nodes in each AZ. This is to avoid overloading any nodes with excessive resource consumption.
- To be able to tolerate the failure of 1 entire AZ in a region, use at least 3 AZs per region and set
-
When deploying across multiple regions:
- To be able to tolerate the failure of 1 entire region, use at least 3 regions.
- This article covers the use of Linux instances with GCE. You may wish to review the instructions for connecting to Windows instances.
Recommendations
- Consider using a 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 DB Console:
Step 1. Configure your network
CockroachDB requires TCP communication on two ports:26257for inter-node communication (i.e., working as a cluster)8080for exposing your DB Console
DB Console
Application data
Applications will not connect directly to your CockroachDB nodes. Instead, they’ll connect to GCE’s TCP Proxy Load Balancing service, which automatically routes traffic to the instances that are closest to the user. Because this service is implemented at the edge of the Google Cloud, you’ll need to create a firewall rule to allow traffic from the load balancer and health checker to your instances. This is covered in Step 4.Step 2. Create instances
Create an instance for each node you plan to have in your cluster. If you plan to run a sample workload against the cluster, create a separate instance for that workload.- Run at least 3 nodes to .
- Use general-purpose
t2d-standard,n2-standard, orn2d-standardVMs, or use custom VMs. For example, Cockroach Labs has usedt2d-standard-8,n2-standard-8, andn2d-standard-8for performance benchmarking.
Do not use
f1 or g1 shared-core machines, which limit the load on CPU resources.- If you used a tag for your firewall rules, when you create the instance, click Management, security, disks, networking, sole tenancy. Under the Networking tab, in the Network tags field, enter cockroachdb.
Step 3. Synchronize clocks
CockroachDB requires moderate levels of 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. Compute Engine instances are preconfigured to use NTP, which should keep offsets in the single-digit milliseconds. However, Google can’t predict how external NTP services, such aspool.ntp.org, will handle the leap second. Therefore, you should:
- Configure each GCE instance to use Google’s internal NTP service.
- If you plan to run a hybrid cluster across GCE and other cloud providers or environments, note that all of the nodes must be synced to the same time source, or to different sources that implement leap second smearing in the same way. See the for details.
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.
When using TCP Proxy Load Balancing, you cannot use firewall rules to control access to the load balancer. If you need such control, consider using Network TCP Load Balancing instead, but note that it cannot be used across regions. You might also consider using the HAProxy load balancer (see the tutorial for guidance).
- For each zone in which you’re running an instance, create a distinct instance group.
- To ensure that the load balancer knows where to direct traffic, specify a port name mapping, with
tcp26257as the Port name and26257as the Port number.
- To ensure that the load balancer knows where to direct traffic, specify a port name mapping, with
- Add the relevant instances to each instance group.
- Configure Proxy Load Balancing.
- During backend configuration, create a health check, setting the Protocol to
HTTP, the Port to8080, and the Request path to path/health?ready=1. This ensures that load balancers do not direct traffic to nodes that are live but not ready to receive requests.- If you want to maintain long-lived SQL connections that may be idle for more than tens of seconds, increase the backend timeout setting accordingly.
- During frontend configuration, reserve a static IP address and choose a port. Note this address/port combination, as you’ll use it for all of you client connections.
- During backend configuration, create a health check, setting the Protocol to
- Create a firewall rule to allow traffic from the load balancer and health checker to your instances. This is necessary because TCP Proxy Load Balancing is implemented at the edge of the Google Cloud.
- Be sure to set Source IP ranges to
130.211.0.0/22and35.191.0.0/16and set Target tags tocockroachdb(not to the value specified in the linked instructions).
- Be sure to set Source IP ranges to
Step 5. Start nodes
By default, inter-node communication uses the internal IP addresses of your GCE instances.
After completing these steps, nodes will not yet be live. They will complete the startup process and join together to form a cluster as soon as the cluster is initialized in the next step.
- Visit and download the full binary of CockroachDB to the node.
- On the node, follow the instructions to .
-
Run the command:
This command primes the node to start, using the following flags:
When deploying across multiple datacenters, or when there is otherwise high latency between nodes, it is recommended to set
--locality as well. It is also required to use certain enterprise features. For more details, see .
For other flags not explicitly set, the command uses default values. For example, the node stores data in --store=cockroach-data and binds DB Console HTTP requests to --http-addr=localhost:8080. To set these options manually, see .
4. Repeat these steps for each additional node that you want in your cluster.
For each initial node of your cluster, complete the following steps:
After completing these steps, nodes will not yet be live. They will complete the startup process and join together to form a cluster as soon as the cluster is initialized in the next step.
-
SSH to the machine where you want the node to run. Ensure you are logged in as the
rootuser. - .
-
Create the Cockroach directory:
-
Create a Unix user named
cockroach: -
Change the ownership of the
cockroachdirectory to the usercockroach: -
Download the sample configuration template and save the file in the
/etc/systemd/system/directory:Alternatively, you can create the file yourself and copy the script into it:
Previously, the sample configuration file set
TimeoutStopSec to 60 seconds. This recommendation has been lengthened to 300 seconds, to give the cockroach process more time to stop gracefully.- In the sample configuration template, specify values for the following flags:
When deploying across multiple datacenters, or when there is otherwise high latency between nodes, it is recommended to set
--locality as well. It is also required to use certain enterprise features. For more details, see .
For other flags not explicitly set, the command uses default values. For example, the node stores data in --store=cockroach-data and binds DB Console HTTP requests to --http-port=8080. To set these options manually, see .
8. Start the CockroachDB cluster:
-
Configure
systemdto start CockroachDB automatically after a reboot: - 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 insecurecockroachdbStep 6. Initialize the cluster
On your local machine, complete the node startup process and have them join together as a cluster:- on your local machine, if you haven’t already.
-
Run the command, with the
--hostflag set to the address of any node:Each node then prints helpful details to the , such as the CockroachDB version, the URL for the DB Console, and the SQL URL for clients.
Step 7. Test the cluster
CockroachDB replicates and distributes data behind-the-scenes and uses a Gossip protocol to enable each node to locate data across the cluster. Once a cluster is live, any node can be used as a SQL gateway. When using a load balancer, you should issue commands directly to the load balancer, which then routes traffic to the nodes. Use the locally as follows:-
On your local machine, launch the built-in SQL client, with the
--hostflag set to the address of the load balancer: -
Create an
insecurenodetestdatabase: -
View the cluster’s databases, which will include
insecurenodetest: -
Use
\qto exit the SQL shell.
Step 8. Run a sample workload
CockroachDB comes with a number of for simulating client traffic. This step features CockroachDB’s version of the TPC-C workload.Be sure that you have configured your network to allow traffic from the application to the load balancer. In this case, you will run the sample workload on one of your machines. The traffic source should therefore be the internal (private) IP address of that machine.
- SSH to the machine where you want the run the sample TPC-C workload. This should be a machine that is not running a CockroachDB node.
- .
-
Use the command to load the initial schema and data, pointing it at the IP address of the load balancer:
-
Use the
cockroach workloadcommand to run the workload for 10 minutes:You’ll see per-operation statistics print to standard output every second:After the specified duration (10 minutes in this case), the workload will stop and you’ll see totals printed to standard output:
-
To monitor the load generator’s progress, open the by pointing a browser to the address in the
adminfield in the standard output of any node on startup. Since the load generator is pointed at the load balancer, the connections will be evenly distributed across nodes. To verify this, click Metrics on the left, select the SQL dashboard, and then check the SQL Connections graph. You can use the Graph menu to filter the graph for specific nodes.
Step 9. Monitor the cluster
Despite CockroachDB’s various , 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 .Step 10. 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.
- .
-
Run the command, passing the new node’s address as the
--advertise-addrflag and pointing--jointo the three existing nodes (also include--localityif you set it earlier). - Update your load balancer to recognize the new node.
-
SSH to the machine where you want the node to run. Ensure you are logged in as the
rootuser. - .
-
Create the Cockroach directory:
-
Create a Unix user named
cockroach: -
Change the ownership of the
cockroachdirectory to the usercockroach: -
Download the sample configuration template:
Alternatively, you can create the file yourself and copy the script into it:Previously, the sample configuration file set
TimeoutStopSecto 60 seconds. This recommendation has been lengthened to 300 seconds, to give thecockroachprocess more time to stop gracefully. Save the file in the/etc/systemd/system/directory - Customize the sample configuration template for your deployment: Specify values for the following flags in the sample configuration template:
- Repeat these steps for each additional node that you want in your cluster.
Step 11. Use the cluster
Now that your deployment is working, you can:- .
- and .
- . Be sure to connect your application to the GCE load balancer, not to a CockroachDB node.

