cockroachdb

by joaquinito2051

Helm chartv2.1.8App 19.1.0

CockroachDB is a scalable, survivable, strongly-consistent SQL database.

About this chart

CockroachDB Helm Chart

Documentation

Below is a brief overview of operating the CockroachDB Helm Chart and some specific implementation details. For additional information, please see https://www.cockroachlabs.com/docs/v19.1/orchestrate-cockroachdb-with-kubernetes-insecure.html

Prerequisites Details

StatefulSet Details

  • http://kubernetes.io/docs/concepts/abstractions/controllers/statefulsets/

StatefulSet Caveats

  • http://kubernetes.io/docs/concepts/abstractions/controllers/statefulsets/#limitations

Chart Details

This chart will do the following:

  • Set up a dynamically scalable CockroachDB cluster using a Kubernetes StatefulSet

Installing the Chart

To install the chart with the release name my-release:

helm install --name my-release <helm-repo>/cockroachdb

helm install --name my-release stable/cockroachdb Note that for a production cluster, you are very likely to want to modify the Storage and StorageClass parameters. This chart defaults to 100 GiB of disk space per pod, but you may want more or less depending on your use case, and the default persistent volume StorageClass in your environment may not be what you want for a database (e.g. on GCE and Azure the default is not SSD).

If you are running in secure mode (with configuration parameter Secure.Enabled set to true), then you will have to manually approve the cluster's security certificates as the pods are created. You can see the pending certificate-signing requests by running kubectl get csr, and approve them by running kubectl certificate approve <csr-name>. You'll have to approve one certificate for each node (e.g. default.node.eerie-horse-cockroachdb-0 and one client certificate for the job that initializes the cluster (e.g. default.node.root).

Confirm that three pods are running successfully and init has completed:

kubectl get pods
NAME                                READY     STATUS      RESTARTS   AGE
my-release-cockroachdb-0            1/1       Running     0          1m
my-release-cockroachdb-1            1/1       Running     0          1m
my-release-cockroachdb-2            1/1       Running     0          1m
my-release-cockroachdb-init-k6jcr   0/1       Completed   0          1m

Confirm that persistent volumes are created and claimed for each pod:

kubectl get persistentvolumes
NAME                                       CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS    CLAIM                                      STORAGECLASS   REASON    AGE
pvc-64878ebf-f3f0-11e8-ab5b-42010a8e0035   100Gi      RWO            Delete           Bound     default/datadir-my-release-cockroachdb-0   standard                 51s
pvc-64945b4f-f3f0-11e8-ab5b-42010a8e0035   100Gi      RWO            Delete           Bound     default/datadir-my-release-cockroachdb-1   standard                 51s
pvc-649d920d-f3f0-11e8-ab5b-42010a8e0035   100Gi      RWO            Delete           Bound     default/datadir-my-release-cockroachdb-2   standard                 51s

Upgrading

From 2.0.0 on

Launch a temporary interactive pod and start the built-in SQL client:

kubectl run cockroachdb -it \
--image=cockroachdb/cockroach \
--rm \
--restart=Never \
-- sql \
--insecure \
--host=my-release-cockroachdb-public

Set the cluster.preserve_downgrade_option cluster setting where $current_version = the version of CRDB currently running, e.g. 2.1: > SET CLUSTER SETTING cluster.preserve_downgrade_option = '$current_version';

Exit the shell and delete the temp pod: > \q

Kick off the upgrade process by changing to the new Docker image, where $new_version is the version being upgraded to:

kubectl delete job my-release-cockroachdb-init
helm upgrade \
my-release \
stable/cockroachdb \
--set ImageTag=$new_version \
--reuse-values

Monitor the cluster's pods until all have been successfully restarted:

kubectl get pods
NAME                                READY     STATUS              RESTARTS   AGE
my-release-cockroachdb-0            1/1       Running             0          2m
my-release-cockroachdb-1            1/1       Running             0          3m
my-release-cockroachdb-2            1/1       Running             0          3m
my-release-cockroachdb-3            0/1       ContainerCreating   0          25s
my-release-cockroachdb-init-nwjkh   0/1       ContainerCreating   0          6s
kubectl get pods \
-o jsonpath='{range .items[*]}{.metadata.name}{"\t"}{.spec.containers[0].image}{"\n"}'
my-release-cockroachdb-0    cockroachdb/cockroach:v19.1.1
my-release-cockroachdb-1    cockroachdb/cockroach:v19.1.1
my-release-cockroachdb-2    cockroachdb/cockroach:v19.1.1
my-release-cockroachdb-3    cockroachdb/cockroach:v19.1.1

Resume normal operations. Once you are comfortable that the stability and performance of the cluster is what you'd expect post upgrade, finalize it by running the following:

kubectl run cockroachdb -it \
--image=cockroachdb/cockroach \
--rm \
--restart=Never \
-- sql \
--insecure \
--host=my-release-cockroachdb-public
> RESET CLUSTER SETTING cluster.preserve_downgrade_option;
\q

To 2.0.0

Due to having no explicit selector set for the StatefulSet before version 2.0.0 of this chart, upgrading from any version that uses a version of kubernetes that locks the selector labels to any other version is impossible without deleting the StatefulSet. Luckily there is a way to do it without actually deleting all the resources managed by the StatefulSet. Use the workaround below to upgrade from charts versions previous to 2.0.0. The following example assumes that the release name is crdb:

$ kubectl delete statefulset crdb-cockroachdb --cascade=false

Verify that no pod is deleted and then upgrade as normal. A new StatefulSet will be created taking over the management of the existing pods upgrading them if needed.

For more information about the upgrading bug see https://github.com/helm/charts/issues/7680.

Configuration

The following table lists the configurable parameters of the CockroachDB chart and their default values.

ParameterDescriptionDefault
NameChart namecockroachdb
ImageContainer image namecockroachdb/cockroach
ImageTagContainer image tagv19.1.0
ImagePullPolicyContainer pull policyAlways
Replicask8s statefulset replicas3
MaxUnavailablek8s PodDisruptionBudget parameter1
Componentk8s selector keycockroachdb
ExternalGrpcPortCockroachDB primary serving port26257
ExternalGrpcNameCockroachDB primary serving port namegrpc
InternalGrpcPortCockroachDB inter-cockroachdb port26257
InternalGrpcNameCockroachDB inter-cockroachdb port namegrpc
InternalHttpPortCockroachDB HTTP port8080
ExternalHttpPortCockroachDB HTTP port on service8080
HttpNameName given to the http service porthttp
ResourcesResource requests and limits{}
StoragePersistent volume size100Gi
StorageClassPersistent volume classnull
CacheSizeSize of CockroachDB's in-memory cache25%
MaxSQLMemoryMax memory to use processing SQL queries25%
ClusterDomainCluster's default DNS domaincluster.local
NetworkPolicy.EnabledEnable NetworkPolicyfalse
NetworkPolicy.AllowExternalDon't require client label for connectionstrue
Service.TypePublic service typeClusterIP
Service.AnnotationsAnnotations to apply to the service{}
PodManagementPolicyOrderedReady or Parallel pod creation/deletion orderParallel
UpdateStrategy.typeallows setting of RollingUpdate strategyRollingUpdate
NodeSelectorNode labels for pod assignment{}
TolerationsList of node taints to tolerate{}
Secure.EnabledWhether to run securely using TLS certificatesfalse
Secure.RequestCertsImageImage to use for requesting TLS certificatescockroachdb/cockroach-k8s-request-cert
Secure.RequestCertsImageTagImage tag to use for requesting TLS certificates0.4
Secure.ServiceAccount.CreateWhether to create a new RBAC service accounttrue
Secure.ServiceAccount.NameName of RBAC service account to use""
JoinExistingList of already-existing cockroach instances[]
LocalityLocality attribute for this deployment""
ExtraArgsAdditional command-line arguments[]
ExtraSecretMountsAdditional secrets to mount at cluster members[]
ExtraEnvArgsAllows to set extra ENV args[]
ExtraAnnotationsAllows to set extra Annotations[]
ExtraInitAnnotationsAllows to set extra Annotations to init pod[]

Specify each parameter using the --set key=value[,key=value] argument to helm install.

Alternatively, a YAML file that specifies the values for the parameters can be provided while installing the chart. For example, Specify each parameter using the --set key=value[,key=value] argument to helm install.

helm install --name my-release -f values.yaml <helm-repo>/cockroachdb

helm install --name my-release -f values.yaml stable/cockroachdb

Tip: You can use the default values.yaml

Deep dive

Connecting to the CockroachDB cluster

Once you've created the cluster, you can start talking to it by connecting to its "public" service. CockroachDB is PostgreSQL wire protocol compatible so there's a wide variety of supported clients. For the sake of example, we'll open up a SQL shell using CockroachDB's built-in shell and play around with it a bit, like this (likely needing to replace "my-release-cockroachdb-public" with the name of the "-public" service that was created with your installed chart):

$ kubectl run -it --rm cockroach-client \
    --image=cockroachdb/cockroach \
    --restart=Never \
    --command -- ./cockroach sql --insecure --host my-release-cockroachdb-public
Waiting for pod default/cockroach-client to be running, status is Pending,
pod ready: false
If you don't see a command prompt, try pressing enter.
root@my-release-cockroachdb-public:26257> SHOW DATABASES;
+--------------------+
|      Database      |
+--------------------+
| information_schema |
| pg_catalog         |
| system             |
+--------------------+
(3 rows)
root@my-release-cockroachdb-public:26257> CREATE DATABASE bank;
CREATE DATABASE
root@my-release-cockroachdb-public:26257> CREATE TABLE bank.accounts (id INT
PRIMARY KEY,

Deploy cockroachdb with OpenTenant

Each tenant gets its own release of this chart, with its own namespace, secrets and domain. The builder below already has cockroachdb on a Hetzner server. Change the host, add apps from the marketplace, then copy the generated files. Providers and prices are samples, and nothing here is sent anywhere.

  1. Pick a serverConnect Hetzner, OVHcloud or AWS. The builder starts on Hetzner, the cheapest sample rate.
  2. Place the chartcockroachdb is pinned at v2.1.8 in opentenant.yaml. Drag more apps from the marketplace onto a rack.
  3. ApplySave opentenant.yaml and main.tf from the console, then run the commands below.
# opentenant.yaml, main.tf and dns.tf come from the builder below
opentenant validate opentenant.yaml
terraform init && terraform apply