Install cockroachdb in kubernetes

We will be using operator to install secure cockroachdb in kubernetes. If you want to install it using helm or configs then please refer to official guide.

  1. Create a new namespace and name it cockroach.
kubectl create ns cockroach
  1. Download crd and apply it
wget https://raw.githubusercontent.com/cockroachdb/cockroach-operator/master/config/crd/bases/crdb.cockroachlabs.com_crdbclusters.yaml -O 1.crdb.cockroachlabs.com_crdbclusters.yaml

kubectl apply -f 1.crdb.cockroachlabs.com_crdbclusters.yaml -n cockroach
  1. Download the operator
wget https://raw.githubusercontent.com/cockroachdb/cockroach-operator/master/manifests/operator.yaml -O 2.operator.yaml
  1. Edit the file, change default namespace to cockroach and apply it
kubectl apply -f 2.operator.yaml
  1. Download official example.yaml file
https://raw.githubusercontent.com/cockroachdb/cockroach-operator/master/manifests/operator.yaml -O 3.example.yaml

Modify it to look like following. Set the resources requests, memory and storage according to your need.

apiVersion: crdb.cockroachlabs.com/v1alpha1
kind: CrdbCluster
metadata:
  name: cockroachdb
spec:
  dataStore:
    pvc:
      spec:
        accessModes:
          - ReadWriteOnce
        resources:
          requests:
            cpu: "4"
            memory: "32Gi"
            storage: "500Gi"            
          limits:
            cpu: "4"
            memory: "32Gi"     
        volumeMode: Filesystem
  tlsEnabled: true
  # cockroachDBVersion: v20.2.5
  image:
    name: cockroachdb/cockroach:v20.2.5
  nodes: 3

Apply it

kubectl apply -f 3.example.yaml -n cockroach

After few minutes pods will appear in cockroach namespace.

If you want to use NodePort then you can change the service type to NodePort.

kubectl get svc -n cockroach

NAME                 TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)              AGE
cockroachdb          ClusterIP   None           <none>        26257/TCP,8080/TCP   39m
cockroachdb-public   ClusterIP   10.43.40.174   <none>        26257/TCP,8080/TCP   39m

Edit the cockroachdb-public service

kubectl edit svc cockroachdb-public -n cockroach

Change type: ClusterIP to type: NodePort

kubectl get svc -n cockroach

NAME                 TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)                          AGE
cockroachdb          ClusterIP   None           <none>        26257/TCP,8080/TCP               40m
cockroachdb-public   NodePort    10.43.40.174   <none>        26257:32698/TCP,8080:32255/TCP   40m

Then browse https://NODE-IP-ADDRESS:32255/

Login using built in sql

kubectl exec -it cockroachdb-2 -n cockroach  -- ./cockroach sql --certs-dir cockroach-certs

#
# Welcome to the CockroachDB SQL shell.
# All statements must be terminated by a semicolon.
# To exit, type: \q.
#
# Server version: CockroachDB CCL v20.2.5 (x86_64-unknown-linux-gnu, built 2021/02/16 12:52:58, go1.13.14) (same version as client)
# Cluster ID: 5d49b5b8-b172-41b8-b8b6-ec53342a578b
#
# Enter \? for a brief introduction.
#
[email protected]:26257/defaultdb>

Create new user;

CREATE USER prakash WITH PASSWORD 'password';
GRANT admin TO prakash;

Leave a Reply

Your email address will not be published. Required fields are marked *