Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

How to Setup a CockroachDB Cluster on Ubuntu 16.04

DZone's Guide to

How to Setup a CockroachDB Cluster on Ubuntu 16.04

Take a look at how you can create a CockroachDB cluster among three nodes on Ubuntu with Alibaba Cloud.

· Open Source Zone ·
Free Resource

Introduction

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 going to explain how to setup CockroachDB cluster on Ubuntu 16.04.

Requirements

  • Three fresh Alibaba Cloud instances 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 set up on each instance.

Launch Alibaba Cloud ECS Instance

First, login to your 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.

apt-get update -y

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:

hostnamectl set-hostname Node1

On Node2, run the following command:

hostnamectl set-hostname Node2

On Node3, run the following command:

hostnamectl set-hostname Node3

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

nano /etc/hosts

Add the following lines:

192.168.0.103 Node1
192.168.0.104 Node2
192.168.0.105 Node3

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:

wget https://binaries.cockroachdb.com/cockroach-latest.linux-amd64.tgz


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

tar -xvzf cockroach-latest.linux-amd64.tgz


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

cp cockroach-latest.linux-amd64/cockroach /usr/local/bin/


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

cockroach version


You should see the following output:

Build Tag:    v2.0.2
Build Time:   2018/05/21 14:55:20
Distribution: CCL
Platform:     linux amd64 (x86_64-unknown-linux-gnu)
Go Version:   go1.10
C Compiler:   gcc 6.3.0
Build SHA-1:  fa2b2bfd802ebe8150030a14b0a36c25fef90b91
Build Type:   release


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

Starting a Cluster on Node1

CockroachDB is now installed, so it's time to start your Cluster.
Run the following command on Node1 to start Cluster:

cockroach start --insecure --background --advertise-host=192.168.0.103


You should see the following output:

*
* WARNING: RUNNING IN INSECURE MODE!
* 
* - Your cluster is open for any client that can access <all your 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 nor authentication, and thus no confidentiality.
* 
* Check out how to secure your cluster: https://www.cockroachlabs.com/docs/v2.0/secure-a-cluster.html
*
CockroachDB node starting at 2018-06-02 16:07:35.672730869 +0000 UTC (took 3.7s)
build: CCL v2.0.2 @ 2018/05/21 14:55:20 (go1.10)
admin: http://Node1:8080
sql: postgresql://root@192.168.0.103:26257?sslmode=disable
logs: /root/cockroach-data/logs
temp dir: /root/cockroach-data/cockroach-temp482291951
external I/O path: /root/cockroach-data/extern
store[0]: path=/root/cockroach-data
status: initialized new cluster
clusterID: e16c6cb0-fc8e-47d7-85c3-30b4b671302e
nodeID: 1

 --insecure : It specifies that node runs without SSL.

 --background : It specifies that the node runs in background.
 --advertise-host : It specifies the IP address of Node1.

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:

Image title

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:

cockroach start --insecure --background --advertise-host=192.168.0.104 --join=192.168.0.103:26257


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

cockroach start --insecure --background --advertise-host=192.168.0.105 --join=192.168.0.103:26257


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


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:

cockroach gen example-data startrek | cockroach sql --insecure


The above command will create a sample database called startrek.

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

cockroach sql --insecure


Output:

# Welcome to the cockroach SQL interface.
# All statements must be terminated by a semicolon.
# To exit: CTRL + D.
#
# Server version: CockroachDB CCL v2.0.2 (x86_64-unknown-linux-gnu, built 2018/05/21 14:55:20, go1.10) (same version as client)
# Cluster ID: e16c6cb0-fc8e-47d7-85c3-30b4b671302e
#
# Enter \? for a brief introduction.
#
warning: no current database set. Use SET database = <dbname> to change, CREATE DATABASE to make a new database.


Next, list out the database using the following command:

root@:26257/> SHOW DATABASES;


You should see the startrek database in the following output:

+----------+
| Database |
+----------+
| startrek |
| system |
+----------+
(2 rows)

Time: 181.107653ms


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

cockroach sql --insecure
root@:26257/> SHOW DATABASES;


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

+----------+
| Database |
+----------+
| startrek |
| system   |
+----------+
(2 rows)

Time: 181.107653ms


The 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:

cockroach sql --insecure


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

root@:26257/> SELECT COUNT(*) FROM startrek.quotes;


Output:

+-------+
| count |
+-------+
| 200 |
+-------+
(1 row)

Time: 1.598875038s


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:

cockroach quit --insecure


Now, go to the Node3 and launch SQL client:

cockroach sql --insecure


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

root@:26257/> SELECT COUNT(*) FROM startrek.quotes;


Output:

+-------+
| count |
+-------+
| 200 |
+-------+
(1 row)

Time: 1.598875038s


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.

Written 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. Join us, write and get rewarded.


Topics:
cockroachdb ,alibaba cloud ,tutorial ,ubuntu 16.04 ,open source

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}