Skip to main content
This page shows you how to manually deploy a secure multi-node CockroachDB cluster on Google Cloud Platform’s Compute Engine (GCE), using Google’s TCP Proxy Load Balancing service to distribute client traffic. If you are only testing CockroachDB, or you are not concerned with protecting network communication with TLS encryption, you can use an insecure cluster instead. Select Insecure above for instructions.
To try CockroachDB Cloud instead of running CockroachDB yourself, refer to the Cloud Quickstart.

Before you begin

Requirements

  • You must have locally. This is necessary for generating and managing your deployment’s certificates.
  • 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 communication
    • 8080 to expose your DB Console
  • Carefully review the , including supported hardware and software, and the 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, you can use either of these approaches:
    • Configure the disks or SSDs as a single RAID volume, then pass the RAID volume to the --store flag when starting the cockroach process on the node.
    • Provide a separate --store flag for each disk when starting the cockroach process on the node. For more details about stores, see . If you start a node with multiple --store flags, 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 --locality on 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.
  • 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

  • Decide how you want to access your DB Console:
    Access LevelDescription
    Partially openSet a firewall rule to allow only specific IP addresses to communicate on port 8080.
    Completely openSet a firewall rule to allow all IP addresses to communicate on port 8080.
    Completely closedSet 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 DB Console.

Step 1. Configure your network

CockroachDB requires TCP communication on two ports:
  • 26257 for inter-node communication (i.e., working as a cluster)
  • 8080 for exposing your DB Console
To expose your DB Console and allow traffic from the TCP proxy load balancer and health checker to your instances, create firewall rules for your project. When creating firewall rules, we recommend using Google Cloud Platform’s tag feature to apply the rule only to instances with the same tag.

DB Console

FieldRecommended Value
Namecockroachadmin
Source filterIP ranges
Source IP rangesYour local network’s IP ranges
Allowed protocols…tcp:8080
Target tagscockroachdb

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.
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.
For more details, see and .

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. ntpd should keep offsets in the single-digit milliseconds, so that software is featured here, but other methods of clock synchronization are suitable as well.
  1. SSH to the first machine.
  2. Disable timesyncd, which tends to be active by default on some Linux distributions:
    Verify that timesyncd is off:
    Look for Network time on: no or NTP enabled: no in the output.
  3. Install the ntp package:
  4. Stop the NTP daemon:
  5. Sync the machine’s clock with Google’s NTP service:
    To make this change permanent, in the /etc/ntp.conf file, remove or comment out any lines starting with server or pool and add the following lines:
    Restart the NTP daemon:
We recommend Google’s NTP service because it handles “smearing” the leap second. If you use a different NTP service that doesn’t smear the leap second, be sure to configure client-side smearing in the same way on each machine. See the for details.
  1. Verify that the machine is using a Google NTP server:
    The active NTP server will be marked with an asterisk.
  2. Repeat these steps for each machine where a CockroachDB node will run.
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 as pool.ntp.org, will handle the leap second. Therefore, you should: 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.
  • Configure each AWS instance to use the internal Amazon Time Sync Service.
    • Per the above instructions, ensure that etc/chrony.conf on the instance contains the line server 169.254.169.123 prefer iburst minpoll 4 maxpoll 4 and that other server or pool lines are commented out.
    • To verify that Amazon Time Sync Service is being used, run chronyc sources -v and check for a line containing * 169.254.169.123. The * denotes the preferred time server.
  • 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.
ntpd should keep offsets in the single-digit milliseconds, so that software is featured here. However, to run ntpd properly on Azure VMs, it’s necessary to first unbind the Time Synchronization device used by the Hyper-V technology running Azure VMs; this device aims to synchronize time between the VM and its host operating system but has been known to cause problems.
  1. SSH to the first machine.
  2. Find the ID of the Hyper-V Time Synchronization device:
  3. Unbind the device, using the Device_ID from the previous command’s output:
  4. Install the ntp package:
  5. Stop the NTP daemon:
  6. Sync the machine’s clock with Google’s NTP service:
    To make this change permanent, in the /etc/ntp.conf file, remove or comment out any lines starting with server or pool and add the following lines:
    Restart the NTP daemon:
    We recommend Google’s NTP service because it handles “smearing” the leap second. If you use a different NTP service that doesn’t smear the leap second, be sure to configure client-side smearing in the same way on each machine. See the for details.
  7. Verify that the machine is using a Google NTP server:
    The active NTP server will be marked with an asterisk.
  8. Repeat these steps for each machine where a CockroachDB node will run.

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.
GCE offers fully-managed TCP Proxy Load Balancing. This service lets you use a single IP address for all users around the world, automatically routing traffic to the instances that are closest to the user.
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).
To use GCE’s TCP Proxy Load Balancing service:
  1. 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 tcp26257 as the Port name and 26257 as the Port number.
  2. Add the relevant instances to each instance group.
  3. Configure TCP Proxy Load Balancing.
    • During backend configuration, create a health check, setting the Protocol to HTTPS, the Port to 8080, 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 note the IP address and the port you select. You’ll use this address and port for all client connections.
  4. 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/22 and 35.191.0.0/16 and set Target tags to cockroachdb (not to the value specified in the linked instructions).

Step 5. Generate certificates

You can use cockroach cert commands or to generate security certificates. This section features the cockroach cert commands. Locally, you’ll need to :
  • A certificate authority (CA) key pair (ca.crt and ca.key).
  • A node key pair for each node, issued to its IP addresses and any common names the machine uses, as well as to the IP addresses and common names for machines running load balancers.
  • A client key pair for the root user. You’ll use this to run a sample workload against the cluster as well as some cockroach client commands from your local machine.
  1. on your local machine, if you haven’t already.
  2. Create two directories:
    • certs: You’ll generate your CA certificate and all node and client certificates and keys in this directory and then upload some of the files to your nodes.
    • my-safe-directory: You’ll generate your CA key in this directory and then reference the key when generating node and client certificates. After that, you’ll keep the key safe and secret; you will not upload it to your nodes.
  3. Create the CA certificate and key:
  4. Create the certificate and key for the first node, issued to all common names you might use to refer to the node as well as to the load balancer instances:
  5. Upload the CA certificate and node certificate and key to the first node:
gcloud compute ssh associates your public SSH key with the GCP project and is only needed when connecting to the first node. See the GCP docs for more details.
  1. Delete the local copy of the node certificate and key:
This is necessary because the certificates and keys for additional nodes will also be named node.crt and node.key. As an alternative to deleting these files, you can run the next cockroach cert create-node commands with the --overwrite flag.
  1. Create the certificate and key for the second node, issued to all common names you might use to refer to the node as well as to the load balancer instances:
  2. Upload the CA certificate and node certificate and key to the second node:
  3. Repeat steps 6 - 8 for each additional node.
  4. Create a client certificate and key for the root user:
  5. Upload the CA certificate and client certificate and key to the machine where you will run a sample workload:
    In later steps, you’ll also use the root user’s certificate to run client commands from your local machine. If you might also want to run cockroach client commands directly on a node (e.g., for local debugging), you’ll need to copy the root user’s certificate and key to that node as well.
On accessing the DB Console in a later step, your browser will consider the CockroachDB-created certificate invalid and you’ll need to click through a warning message to get to the UI. You can avoid this issue by .

Step 6. Start nodes

By default, inter-node communication uses the internal IP addresses of your GCE instances.
You can start the nodes manually or automate the process using systemd. 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.
  1. Visit and download the full binary of CockroachDB to the node.
  2. On the node, follow the instructions to .
  3. Run the command:
    This command primes the node to start, using the following flags:
    FlagDescription
    --certs-dirSpecifies the directory where you placed the ca.crt file and the node.crt and node.key files for the node.
    --advertise-addrSpecifies the IP address/hostname and port to tell other nodes to use. The port number can be omitted, in which case it defaults to 26257.

    This value must route to an IP address the node is listening on (with --listen-addr unspecified, the node listens on all IP addresses).

    In some networking scenarios, you may need to use --advertise-addr and/or --listen-addr differently. For more details, see .
    --joinIdentifies the address of 3-5 of the initial nodes of the cluster. These addresses should match the addresses that the target nodes are advertising.
    --cache
    --max-sql-memory
    Increases the node’s cache size to 25% of available system memory to improve read performance. The capacity for in-memory SQL processing defaults to 25% of system memory but can be raised, if necessary, to increase the number of simultaneous client connections allowed by the node as well as the node’s capacity for in-memory processing of rows when using ORDER BY, GROUP BY, DISTINCT, joins, and window functions. For more details, see .
    --backgroundStarts the node in the background so you gain control of the terminal to issue more commands.
    When deploying across multiple datacenters, or when there is otherwise high latency between nodes, it is recommended to set --locality as well. 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=<node1 address:8080. To set these options manually, see .
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.
  1. SSH to the machine where you want the node to run. Ensure you are logged in as the root user.
  2. .
  3. Create the Cockroach directory:
  4. Create a Unix user named cockroach:
  5. Move the certs directory to the cockroach directory.
  6. Change the ownership of the cockroach directory to the user cockroach:
  7. 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:
Description=Cockroach Database cluster node Requires=network.target [Service] Type=notify WorkingDirectory=/var/lib/cockroach ExecStart=/usr/local/bin/cockroach start —certs-dir=certs —advertise-addr=<node1 address —join=<node1 address,<node2 address,<node3 address —cache=.25 —max-sql-memory=.25 TimeoutStopSec=300 Restart=always RestartSec=10 StandardOutput=syslog StandardError=syslog SyslogIdentifier=cockroach User=cockroach [Install] WantedBy=default.target
  1. Configure systemd to start CockroachDB automatically after a reboot:
  2. 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 securecockroachdb

Step 7. Initialize the cluster

On your local machine, run the command to complete the node startup process and have them join together as a cluster:
After running this command, each node prints helpful details to the , such as the CockroachDB version, the URL for the DB Console, and the SQL URL for clients.

Step 8. 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:
  1. On your local machine, launch the built-in SQL client, with the --host flag set to the address of the load balancer:
  2. Create a securenodetest database:
  3. View the cluster’s databases, which will include securenodetest:
  4. Use \q to exit the SQL shell.

Step 9. 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.
For comprehensive guidance on benchmarking CockroachDB with TPC-C, refer to .
  1. SSH to the machine where you want to run the sample TPC-C workload. This should be a machine that is not running a CockroachDB node, and it should already have a certs directory containing ca.crt, client.root.crt, and client.root.key files.
  2. .
  3. Use the command to load the initial schema and data, pointing it at the IP address of the load balancer:
  4. Use the cockroach workload command 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:
For more tpcc options, use cockroach workload run tpcc --help. For details about other workloads built into the cockroach binary, use cockroach workload --help.
  1. To monitor the load generator’s progress, open the by pointing a browser to the address in the admin field 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 10. 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 11. 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:
  1. SSH to the machine where you want the node to run.
  2. .
  3. Run the command, passing the new node’s address as the --advertise-addr flag and pointing --join to the three existing nodes (also include --locality if you set it earlier).
  4. Update your load balancer to recognize the new node.
For each additional node you want to add to the cluster, complete the following steps:
  1. SSH to the machine where you want the node to run. Ensure you are logged in as the root user.
  2. .
  3. Create the Cockroach directory:
  4. Create a Unix user named cockroach:
  5. Move the certs directory to the cockroach directory.
  6. Change the ownership of the cockroach directory to the user cockroach:
  7. Download the sample configuration template:
    Alternatively, you can create the file yourself and copy the script into it:
Description=Cockroach Database cluster node Requires=network.target [Service] Type=notify WorkingDirectory=/var/lib/cockroach ExecStart=/usr/local/bin/cockroach start —certs-dir=certs —advertise-addr=<node1 address —join=<node1 address,<node2 address,<node3 address —cache=.25 —max-sql-memory=.25 TimeoutStopSec=300 Restart=always RestartSec=10 StandardOutput=syslog StandardError=syslog SyslogIdentifier=cockroach User=cockroach [Install] WantedBy=default.target