CockroachDB is a highly scalable and reliable distributed SQL database that is designed to handle large-scale cloud-based applications. In this tutorial, we will walk you through the process of setting up a CockroachDB cluster on Ubuntu 22.04 server. By the end of this guide, you will have a fully functional CockroachDB cluster that can handle your database needs.
Prerequisites
Before we begin, make sure you have the following prerequisites in place:
- Three servers running Ubuntu 22.04
- Root password configured on each server
For the purpose of this tutorial, we will assume the following server setup:
Hostname |
IP Address |
---|---|
node1 |
192.168.10.10 |
node2 |
192.168.10.11 |
node3 |
192.168.10.12 |
Step 1: Update Your System
Before we start installing CockroachDB, it is always a good practice to update and upgrade all nodes to the latest version. Run the following commands on each node to update the system:
apt update -y apt upgrade -y
Once the update and upgrade process is complete, restart the servers to apply the changes.
Step 2: Setup Time Synchronization
Time synchronization is crucial for the proper functioning of a CockroachDB cluster. We will use chrony
to synchronize the time between all nodes. Follow the steps below to set up time synchronization:
- Install
chrony
on all nodes by running the following command:
apt install chrony -y
- After the installation is complete, edit the
chrony
configuration file by running the following command:
nano /etc/chrony/chrony.conf
- Locate the default pool lines in the configuration file and replace them with the following lines:
pool 0.id.pool.ntp.org iburst maxsources 4 pool 1.id.pool.ntp.org iburst maxsources 1 pool 2.id.pool.ntp.org iburst maxsources 1 pool 3.id.pool.ntp.org iburst maxsources 2
- Save and close the file. Then, restart the
chrony
service and enable it to start at system reboot with the following commands:
systemctl restart chrony systemctl enable chrony
With time synchronization set up, we can proceed to the next step.
Step 3: Install CockroachDB on All Nodes
In this step, we will install CockroachDB on all nodes. Follow the instructions below to install CockroachDB:
- Download the latest version of CockroachDB from their official website using the following command:
wget https://binaries.cockroachdb.com/cockroach-latest.linux-amd64.tgz
- Once the download is complete, extract the downloaded file with the following command:
tar -xvzf cockroach-latest.linux-amd64.tgz
- Copy the CockroachDB binary to the
/usr/local/bin
directory using the following command:
cp cockroach-*/cockroach /usr/local/bin/
- Verify the installation by checking the CockroachDB version:
cockroach version
You should see the version information displayed on the screen.
With CockroachDB installed on all nodes, we can now proceed to the next step.
Step 4: Create Certificates
CockroachDB requires server and client certificates for secure communication. In this step, we will create the necessary certificates. Follow the instructions below to create the certificates:
- Create a directory to store the certificate files on all nodes by running the following command:
mkdir ~/certs
- Create a CA certificate on
node1
using the following command:
cockroach cert create-ca --certs-dir=certs --ca-key=certs/ca.key
This command will generate ca.key
and ca.crt
files inside the ~/certs
directory.
- Copy the generated CA certificate to both
node2
andnode3
using the following commands:
scp ~/certs/ca.crt ~/certs/ca.key [email protected]:~/certs/ scp ~/certs/ca.crt ~/certs/ca.key [email protected]:~/certs/
- Generate a client certificate on all nodes using the following command:
cockroach cert create-client root --certs-dir=certs --ca-key=certs/ca.key
- Generate server certificates on each node using the following commands:
cockroach cert create-node localhost $(hostname) 192.168.10.10 --certs-dir=certs --ca-key=certs/ca.key cockroach cert create-node localhost $(hostname) 192.168.10.11 --certs-dir=certs --ca-key=certs/ca.key cockroach cert create-node localhost $(hostname) 192.168.10.12 --certs-dir=certs --ca-key=certs/ca.key
These commands will generate the necessary server certificates ( node.key
and node.crt
) inside the ~/certs
directory.
With the certificates created, we can move on to the next step.
Step 5: Start CockroachDB Cluster
Now that we have the certificates in place, we can start the CockroachDB cluster. Follow the steps below to start the cluster:
- On
node1
, run the following command to start the CockroachDB cluster:
cockroach start --background --certs-dir=certs --advertise-host=192.168.10.10 --join=192.168.10.10,192.168.10.11,192.168.10.12
- Initialize the cluster by running the following command on
node1
:
cockroach init --certs-dir=certs --host=192.168.10.10
By default, the CockroachDB cluster listens on port 26257. You can check the status of the cluster by running the following command:
cockroach node status --certs-dir=certs --host=192.168.10.10
You should see the status of the cluster displayed on the screen.
With the CockroachDB cluster up and running on node1
, we can proceed to the next step.
Step 6: Add Remaining Nodes to the Cluster
Now that the first node is running, we can add the remaining nodes to the CockroachDB cluster. Follow the instructions below to add the nodes:
- On
node2
, run the following command to add it to the CockroachDB cluster:
cockroach start --background --certs-dir=certs --advertise-host=192.168.10.11 --listen-addr=192.168.10.11 --join=192.168.10.10:26257
- On
node3
, run the following command to add it to the CockroachDB cluster:
cockroach start --background --certs-dir=certs --advertise-host=192.168.10.12 --listen-addr=192.168.10.12 --join=192.168.10.10:26257
- Go back to
node1
and check the status of the CockroachDB cluster by running the following command:
cockroach node status --certs-dir=certs --host=192.168.10.10
You should see that all nodes are added to the cluster.
With all nodes added to the cluster, we can move on to the next step.
Step 7: Access CockroachDB Dashboard
CockroachDB provides a web-based dashboard that allows you to monitor and manage the cluster. Follow the instructions below to access the CockroachDB dashboard:
- Log into the CockroachDB SQL shell on
node1
using the following command:
cockroach sql --certs-dir=certs --host=192.168.10.10
- Create an admin user and set a password by running the following commands:
CREATE USER user1 WITH PASSWORD 'securepassword';
- Exit the SQL shell by typing
q
orexit;
. - Open your web browser and enter the URL
https://node1-ip-address:8080
. - Enter your admin username and password, then click on the “LOG IN” button.
You should now be able to access the CockroachDB dashboard and monitor your cluster.
Step 8: Verify Database Replication
In this step, we will verify whether the database is replicated across all nodes in the CockroachDB cluster. Follow the instructions below to verify the replication:
- Log into the CockroachDB SQL shell on
node1
using the following command:
cockroach sql --certs-dir=certs --host=192.168.10.10
- Create two databases named
wpdb1
andwpdb2
using the following commands:
CREATE DATABASE wpdb1; CREATE DATABASE wpdb2;
- Verify the databases by running the following command:
SHOW DATABASES;
You should see the wpdb1
and wpdb2
databases listed.
- Log into the CockroachDB SQL shell on
node2
andnode3
to check if the databases are replicated:
cockroach sql --certs-dir=certs --host=192.168.10.11 cockroach sql --certs-dir=certs --host=192.168.10.12
On each node, run the SHOW DATABASES;
command to verify the replication.
If the databases are replicated across all nodes, the setup is successful.
Conclusion
Congratulations! You have successfully set up a CockroachDB cluster on Ubuntu 22.04. You now have a reliable and scalable distributed SQL database that can handle your cloud-based applications. With CockroachDB, you can ensure data consistency and availability even in the face of hardware or network failures.
To further enhance your CockroachDB cluster, consider using Shape.host’s Linux SSD VPS hosting services. Their high-performance virtual private servers are optimized for database workloads and can provide you with the reliability and scalability you need.
If you have any questions or need further assistance, feel free to reach out. Happy clustering!