How to Setup CockroachDB Cluster on Ubuntu 16.04

By Hitesh Jethva, Alibaba Cloud Tech Share Author. Tech Share is Alibaba Cloud’s incentive program to encourage the sharing of technical knowledge and best practices within the cloud community.

CockroachDB is a free and open source distributed SQL database that is designed to store copies of data in multiple locations in order to deliver requested data when needed. CockroachDB is a cloud-native SQL database for building global, scalable cloud services that survive disasters. It is built to automatically replicate, rebalance, and recover with minimal configuration and operational overhead. You can install CockroachDB on your local computer, a single server, a corporate development cluster, or a private or public cloud.

In this tutorial, we will be learning how to setup a CockroachDB cluster on an Alibaba Cloud Elastic Compute Service (ECS) instance with Ubuntu 16.04.

Prerequisites

  • Three fresh Alibaba Cloud ECS instance with Ubuntu 16.04 server installed.
  • A static IP address 192.168.0.103 is configured on the first instance, 192.168.0.104 is configured on the second instance and 192.168.0.105 is configured on the third instance.
  • Minimum 2GB RAM per instance.
  • A Root password is setup on each instance.

Launch Alibaba Cloud ECS Instance

First, log in to your https://ecs.console.aliyun.com">Alibaba Cloud ECS Console. Create a new ECS instance, choosing Ubuntu 16.04 as the operating system with at least 2GB RAM. Connect to your ECS instance and log in as the root user.

Once you are logged into your Ubuntu 16.04 instance, run the following command to update your base system with the latest available packages.

Setting up Hostname

Before starting, you will need to configure hosts file and hostname on each server, so each server can communicate with each other using the hostname

On Node1, run the following command:

On Node2, run the following command:

On Node3, run the following command:

Next, open /etc/hosts file on each Node and add the following lines:

Add the following lines:

Save and close the file when you are finished.

Install CockroachDB

First, you will need to download the latest version of the CockroachDB binary. On Node1, run the following command to download the binary:

Once the download is completed, extract the downloaded file using the following command:

Next, copy the binary from extracted directory to the /usr/local/bin so it is accessible from the command line:

Next, check the version of the CockroachDB using the following command:

You should see the following output:

Note: Run all of the above command on Node2 and Node3 to install CockroachDB.

Starting Cluster on Node1

CockroachDB is now installed, it’s time to start your Cluster.

Run the following command on Node1 to start Cluster:

You should see the following output:

Your CockroachDB cluster is now live. You can access its Dashboard by visiting the URL http://192.168.0.103:8080 on your web browser. You should see the following image:

Add Other Nodes to Cluster

Your Cluster is now live. Next, you will need to add other two Nodes to this Cluster.

On the Node2, run the following command to add Node2 on Cluster:

On the Node3, run the following command to add Node3 on Cluster:

You can now access the Cluster dashboard from any Node by visiting the URL http://192.168.0.104 on your web browser.

Now, all nodes are connected through the Cluster.

Test CockroachDB Replication

All of the Nodes are now live, it’s time to test whether replication is working or not between all the Nodes.

First, generate example data on Node1:

The above command will create a sample database called startrek.

Now, launch SQL client on Node1 and list the databases in your Cluster.

Output:

Next, list out the databaseusing the following command:

You should see the startrek database in the following output:

Now, go to the Node2 and launch SQL client to list the databases on Node2:

You should see that startrek database is replicated from Node1 to Node2:

Startrek database is now distributed among all the Nodes.

Test CockroachDB Failover

Here, we will remove Node2 from the Cluster and show that all of the cluster’s data is still available.

First, go to the Node2 and launch SQL client:

Next, count the number of rows in the quotes table of the startrek database:

Output:

You should see that the table has 200 rows.

Now, exit from the SQL client by pressing CTRL+C.

Next, remove the Node2 from the Cluster by running the following command on Node2:

Now, go to the Node3 and launch SQL client:

Now, run the following command to count the number of rows in the quotes table of the startrek database:

Output:

You should see that the Cluster still have 200 rows of data. That means CockroachDB has successfully tolerated a system failure and maintained the integrity of your data.

Congratulations! You have successfully installed and configured a highly available CockroachDB cluster in Ubuntu 16.04 server. You can now easily scale your cluster at any time if your requirements grow.

Related Alibaba Cloud Products

Data Transport is a PB-level, point-to-point, offline data migration service. You can use secure equipment to upload large amounts of data to Alibaba Cloud. Data Transport can help you resolve common problems associated with data migration to Alibaba Cloud. Common issues include expensive dedicated lines, lengthy transmission times, and potential security risks. Data Transport allows you to upload large amounts of data securely and efficiently all while reducing costs.

Compared to traditional techniques of data transmission, either over the Internet or using a high-speed leased line, this solution improves efficiency by up to 20 times and reduces costs by up to 60%.

Data Transport allows a large volume of data to be migrated offline in a single batch, significantly reducing logistics and network costs.

Each device supports data migration capabilities of 100 TB or 480 TB. By using multiple devices concurrently, Data Transport can quickly scale to migrate data at the PB level.

E-HPC is a HPCaaS cloud platform providing an all-in-one high-performance public computing service to meet increasing demand for HPC capability. Based on existing infrastructure, this product provides users with an all-in-one high-performance computing service cloud platform.

Alibaba Cloud E-HPC supports:

  • Infrastructure-as-a-Service (IaaS) with high-performance CPU and heterogeneous computing GPU instances.
  • Platform-as-a-Service (PaaS) with a high-performance computing software stack.
  • Software-as-a-Service (SaaS) with application template customization.

E-HPC is applicable to organizations in education and scientific research fields with the demand for large-scale computing capabilities. E-HPC supports applications such as HPC, AI, and large-scale data analysis.

E-HPC allows you to create ECS/EGS computing clusters and cluster managers and deploy high-performance computing environments and application programs with one-click. This allows users to quickly develop applications with superior computing capability and ease their computing burden.

Compared with traditional supercomputing centers and self-built HPC clusters, E-HPC provides an accessible, elastic, secure, interconnectable high-performance computing service on the public cloud.

Reference:

https://www.alibabacloud.com/blog/how-to-setup-cockroachdb-cluster-on-ubuntu-16-04_593807?spm=a2c41.11767211.0.0

Follow me to keep abreast with the latest technology news, industry insights, and developer trends.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store