DEV Community

Cover image for Deploying CockroachDB on Kubernetes
Sanskriti Harmukh for Vultr

Posted on with Aashish Chaurasiya • Originally published at docs.vultr.com

Deploying CockroachDB on Kubernetes

CockroachDB is a distributed SQL database that replicates data across nodes for high availability. If a node fails, the cluster elects new nodes and rebalances automatically, and an automated repair feature detects and corrects data inconsistencies. This guide deploys CockroachDB on Kubernetes via the CockroachDB Operator, and covers access through both the SQL client and the graphical console.

Prerequisites: a Kubernetes cluster with at least 3 nodes, 8GB RAM and 4 vCPUs each, and a workstation with kubectl configured against the cluster.


Install the CockroachDB Operator

The operator automates scaling and maintenance of CockroachDB nodes.

$ kubectl apply -f https://raw.githubusercontent.com/cockroachdb/cockroach-operator/v2.12.0/install/crds.yaml
$ kubectl apply -f https://raw.githubusercontent.com/cockroachdb/cockroach-operator/v2.12.0/install/operator.yaml
Enter fullscreen mode Exit fullscreen mode

Check the operator tags page for the latest version before installing.

Wait ~2 minutes, then verify:

$ kubectl get pods -n cockroach-operator-system
Enter fullscreen mode Exit fullscreen mode

You should see cockroach-operator-manager-* running.


Create a CockroachDB Cluster

Production minimums: a PVC-backed volume of at least 60GB, 2 vCPUs, 8GB memory per node.

$ mkdir cockroachdb-cluster
$ cd cockroachdb-cluster
$ nano cluster.yaml
Enter fullscreen mode Exit fullscreen mode
apiVersion: crdb.cockroachlabs.com/v1alpha1
kind: CrdbCluster
metadata:
  name: cockroachdb
spec:
  dataStore:
    pvc:
      spec:
        accessModes:
          - ReadWriteOnce
        resources:
          requests:
            storage: "60Gi"
        volumeMode: Filesystem
  resources:
    requests:
      cpu: 2
      memory: 4Gi
    limits:
      cpu: 2
      memory: 8Gi
  tlsEnabled: true
  image:
    name: cockroachdb/cockroach:v23.1.11
  nodes: 3
  additionalLabels:
    crdb: cockroachdb-cluster
Enter fullscreen mode Exit fullscreen mode
$ kubectl apply -f cluster.yaml
Enter fullscreen mode Exit fullscreen mode

Wait ~3 minutes for the cluster and its backing volumes to provision, then verify:

$ kubectl get crdbcluster
$ kubectl get pods
Enter fullscreen mode Exit fullscreen mode

You should see cockroachdb-0, cockroachdb-1, cockroachdb-2 all Running.


Access the Cluster

CockroachDB is reachable internally via the cockroachdb-public service.

1. Check services:

$ kubectl get svc
Enter fullscreen mode Exit fullscreen mode

2. Deploy a secure SQL client pod:

$ kubectl create -f https://raw.githubusercontent.com/cockroachdb/cockroach-operator/v2.12.0/examples/client-secure-operator.yaml
$ kubectl get pods
Enter fullscreen mode Exit fullscreen mode

3. Open a SQL shell against the cluster:

$ kubectl exec -it cockroachdb-client-secure -- ./cockroach sql --certs-dir=/cockroach/cockroach-certs --host=cockroachdb-public
Enter fullscreen mode Exit fullscreen mode

4. Create a database and admin user:

> CREATE DATABASE exampledb;
> CREATE USER example_admin WITH PASSWORD 'strong-pass';
> GRANT admin TO example_admin;
> \q
Enter fullscreen mode Exit fullscreen mode

5. Expose the graphical console via a LoadBalancer:

$ kubectl expose service cockroachdb-public --type=LoadBalancer --name=cockroachdb-external
Enter fullscreen mode Exit fullscreen mode

Wait a few minutes for an external IP, then:

$ kubectl get svc
Enter fullscreen mode Exit fullscreen mode

6. Open the console at https://<external-ip>:8080 and log in with example_admin. From there you can inspect cluster status, nodes, and databases.


Next Steps

CockroachDB is running as a 3-node cluster with TLS enabled and both SQL and console access. From here:

  • Scale nodes in the cluster spec to add capacity
  • Set up automated backups to object storage via BACKUP scheduled jobs
  • Point application connection strings at cockroachdb-public for in-cluster access, or the LoadBalancer IP for external clients

For the full guide, visit the original article on Vultr Docs.

Top comments (0)