Deploy CockroachDB Cluster

Pramod Shehan
3 min readFeb 6, 2023

Background

CockroachDB is distributed SQL database. Main motivation of CockroachDB is to facilitate high scalability, consistency, resiliency and transactional(ACID guarantee). All data access in CockroachDB is transactional, which guarantees data integrity.

CockroachDb always uses serializable isolation(every transaction behaves as if it had exclusive use of the entire cluster as it performed the transaction). Event many legacy SQL databases don’t offer serializable isolation.

CockroachDB = NO SQL + Relational Database

CockroachDB was designed to be highly compatible with PostgreSQL. We can use a wide variety of existing PostgreSQL client drivers to talk to CockroachDB.

It supports Geo-replication also. It means ability to control where your data resides in a globally distributed cluster.

CockroachDB isn’t tied to a specific cloud provider. We can run cockroachDB on any bare metal server or any cloud provider.

ACID Principle

Atomicity- If any part of a transaction fails, the entire transaction is abort and the database is left unchanged.

Consistency- Data must be in a consistent state before and after the transaction.

Isolation- Any read or write will not be impacted by other reads or writes of separate transactions.

Durability- Once a transaction is committed, it will remain so event in the event of power loss, crashes or failure.

Deploy using Docker

  1. Create a bridge network
docker network create -d bridge roachnet

2. Create docker volumes

docker volume create roach1
docker volume create roach2
docker volume create roach3

3. Start the node 1

docker run -d --name=roach1 --hostname=roach1 --net=roachnet -p 26257:26257 
-p 8080:8080 -v "roach1:/cockroach/cockroach-data"
cockroachdb/cockroach:v22.2.3 start --insecure --join=roach1,roach2,roach3

4. Start the node 2

docker run -d --name=roach2 --hostname=roach2 --net=roachnet -v 
"roach2:/cockroach/cockroach-data" cockroachdb/cockroach:v22.2.3
start --insecure --join=roach1,roach2,roach3

5. Start the node 3

docker run -d --name=roach3 --hostname=roach3 --net=roachnet -v 
"roach3:/cockroach/cockroach-data" cockroachdb/cockroach:v22.2.3
start --insecure --join=roach1,roach2,roach3

6. Connect to the SQL shell

docker exec -it roach1 ./cockroach sql --insecure

7. CockroachDB dashboard

http://localhost:8081

Deploy in Kubernetes(minikube)

  1. Start a local Kubernetes cluster
minikube start

2. Use the Kubernetes custom resource definition (CRD) for the Operator.

kubectl apply -f https://raw.githubusercontent.com/cockroachdb/cockroach-operator/v2.10.0/install/crds.yaml

3. Deploy cockroachDB operator

Operator is used to manage CockroachDB instances on the cluster.

kubectl apply -f https://raw.githubusercontent.com/cockroachdb/cockroach-operator/v2.10.0/install/operator.yaml
kubectl config set-context --current --namespace=cockroach-operator-system

4. Deploy the cluster

This custom resource specifies CPU and memory resources. If you need to change it, download and change the CPU and memory.

kubectl apply -f https://raw.githubusercontent.com/cockroachdb/cockroach-operator/v2.10.0/examples/example.yaml

5. Deploy cockroachDB SQL client.

kubectl create -f https://raw.githubusercontent.com/cockroachdb/cockroach-operator/v2.10.0/examples/client-secure-operator.yaml
kubectl exec -it cockroachdb-client-secure -- ./cockroach sql 
--certs-dir=/cockroach/cockroach-certs --host=cockroachdb-public

6. CockroachDB dashboard

kubectl port-forward service/cockroachdb-public 8080

http://localhost:8080/#/metrics/overview/cluster

Resources

https://www.cockroachlabs.com/docs/stable/start-a-local-cluster-in-docker-mac.html

https://www.cockroachlabs.com/docs/stable/architecture/overview.html

--

--