Cockroachdb is a PostgreSQL compatible SQL database. It’s architecture is somewhat like Google Spanner, but you need to install it on your own infrastructure. This article will give a brief overview of how this stuff is deployed on Google Cloud platform, and is aimed at novices who just want to try it out – like me. Using the Cloud platform console is always an adventure, so I’m writing this as I’m doing it for future reference.
bn.sh
docker network create -d bridge cockroachbridge
docker run -d \ --name=c1 \ --hostname=c1 \ --net=cockroachbridge \ -p 26257:26257 \ -p 8080:8080 \ -v "${PWD}/cockroach-data/c1:/cockroach/cockroach-data" \ cockroachdb/cockroach:v1.1.3 start --insecure docker run -d \ --name=c2 \ --hostname=c2 \ --net=cockroachbridge \ -v "${PWD}/cockroach-data/c2:/cockroach/cockroach-data" \ cockroachdb/cockroach:v1.1.3 start --insecure --join=c1 docker run -d \ --name=c3 \ --hostname=c3 \ --net=cockroachbridge \ -v "${PWD}/cockroach-data/c3:/cockroach/cockroach-data" \ cockroachdb/cockroach:v1.1.3 start --insecure --join=c1
docker container stop c1 c2 c3 docker container rm c1 c2 c3 docker network rm cockroachbridge
docker exec -i c1 ./cockroach sql --insecure < test.sql
test.sql
DROP DATABASE IF EXISTS airports CASCADE; CREATE DATABASE airports; CREATE TABLE airports.locations ( name STRING NOT NULL, latitude_deg FLOAT NOT NULL, longitude_deg FLOAT NOT NULL, elevation_ft INT NOT NULL, iso_country STRING(2) NOT NULL, municipality STRING NOT NULL, scheduled_service BOOL, iata_code STRING(3) PRIMARY KEY NOT NULL); INSERT INTO airports.locations VALUES ('Port Moresby Jacksons International Airport',-10.443380356,147.2200012,146,'PG','Port Moresby',TRUE,'POM'), ('Edmonton International Airport',53.30970001,-113.5800018,2373,'CA','Edmonton',TRUE,'YEG'), ('Halifax / Stanfield International Airport',44.88079834,-63.50859833,477,'CA','Halifax',TRUE,'YHZ'); SELECT name,iata_code,elevation_ft FROM airports.locations ORDER BY elevation_ft DESC;
and, it works!
bruce@cockroach ~ $ docker exec -i c1 ./cockroach sql --insecure < test.sql DROP DATABASE CREATE DATABASE CREATE TABLE INSERT 3 name iata_code elevation_ft Edmonton International Airport YEG 2373 Halifax / Stanfield International Airport YHZ 477 Port Moresby Jacksons International Airport POM 146 # 3 rows
Over in Firewall rules under network of the cloud console,
Set up this rule (here it allows connecting from any IP address 0.0.0.0/0 – but you may want to restrict that)
You’ll see the 3 nodes
And of course the database
new.sh
## create instance sh ci.sh ## create firewall rule sh fw.sh ## copy files to new instance gcloud compute scp ~/cockroach/docker/*.sh cockroach:~/ --zone us-central1-c gcloud compute scp ~/cockroach/docker/*.sql cockroach:~/ --zone us-central1-c
fw.sh
gcloud compute --project=effex-console firewall-rules create cockroach-admin --direction=INGRESS --priority=1000 --network=default --action=ALLOW --rules=tcp:8080 --source-ranges=0.0.0.0/0 --target-tags=cockroach-admin
ci.sh
gcloud beta compute --project "effex-console" instances create "cockroach" --zone "us-central1-c" --machine-type "f1-micro" --subnet "default" --metadata "gce-container-declaration=spec:\u000a containers:\u000a - name: cockroach\u000a image: 'cockroachdb/cockroach:v1.1.3'\u000a stdin: false\u000a tty: false\u000a restartPolicy: Always\u000a" --maintenance-policy "MIGRATE" --service-account "945638777525-compute@developer.gserviceaccount.com" --scopes "https://www.googleapis.com/auth/devstorage.read_only","https://www.googleapis.com/auth/logging.write","https://www.googleapis.com/auth/monitoring.write","https://www.googleapis.com/auth/servicecontrol","https://www.googleapis.com/auth/service.management.readonly","https://www.googleapis.com/auth/trace.append" --min-cpu-platform "Automatic" --tags "cockroach-admin" --image "cos-stable-63-10032-71-0" --image-project "cos-cloud" --boot-disk-size "10" --boot-disk-type "pd-standard" --boot-disk-device-name "cockroach"
whole.sh
## create network sh bn.sh ## start cockroach sh st.sh ## test db sh ex.sh
which should give something like this
33609bc8682be4c662ed7c0977ad49ee7d208bee20e82e818d238c31398c01e8c8 0c190eee04122ddbf6509912628c549dc76ad167f0ba5a375d9a01464a7119 f5e5d8516f027d55ee98725bc186360df3a6ae2134a39e3b08f7ad31c8e7dd09 ce0b225c5f68df47eb37dab5d34d5ed90d48b93fa5d9633cd4c8695190300d25 DROP DATABASE CREATE DATABASE CREATE TABLE INSERT 3 name iata_code elevation_ft Edmonton International Airport YEG 2373 Halifax / Stanfield International Airport YHZ 477 Port Moresby Jacksons International Airport POM 146 # 3 rows
For more like this, see React, redux, redis, material-UI and firebase
Why not join our forum, follow the blog or follow me on Twitter to ensure you get updates when they are available.
kubectl get deployment NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE kube-system-nginx-ingress-controller 1 1 1 1 1d kube-system-nginx-ingress-default-backend 1 1 1 1 1d play-around 1 1 1 1 1h playback 2 2 2 2 23h
bruce mcpherson is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License. Based on a work at http://www.mcpher.com. Permissions beyond the scope of this license may be available at code use guidelines