Experimental CockroachDB cluster setup in docker.
This is suitable for:
- Local testing and learning
- Bare-metal (offline) deployment
- Automatically create the cluster.
- Create certs from short-lived container, self-signed certificate for local testing
- Does not create certs unnecessarily
- Init cluster from short-lived container, safe to init even if cluster already exists
- Load balancing across cluster
- for http console
- for cockroachdb sql client
- Set up automation for using the cluster with multiple projects
(assume one project uses one database)
- Script for creating a database and its user
- Set up postgres for feature comparison with cockroachdb
- Example nodejs code to use this cluster
- pg and pg-migrate ?
- knex.js?
- https://sqorn.org/benchmarks.html ?
- Example python code to use this cluster
./python-psycopg3
- using psycopg3 directly
- using yoyo migrations through terminal
- docker exec into the container to run the
yoyo
cli
- docker exec into the container to run the
[ ] sqlalchemy core and alembic? (alembic is too coupled with sqlalchemy for my taste)./python-sql-alchemy-core
- yoyo migrations called from python code
- sqlalchemy-core for data manipulation
Tested on the following:
- Linux (EndeavourOS)
- docker >= 24.0.5
- docker compose >= 2.20.3
- Clone this repository
cd cockroach-db-cluster
: Enter the project root directory.- Unless otherwise stated, all commands in documentation are meant to be run from this directory.
cp dbcluster/.env.example dbcluster/.env
- Edit variables as necessary. Default values in
.env.example
should be sufficient to simply run the system. .env
file should contain deployment-specific values and should not be committed to the repository.
- Edit variables as necessary. Default values in
docker compose -f dbcluster/docker-compose.yml up
: Sets up a 3-instance cluster- Create the required certs
- Create a docker volume for each instance
- Start the cluster instances
- Init the cluster (after some healthcheck delays)
- Set a password for the
root
user - Create HAProxy container with bind-mounted configs
- Using the current configs, cockroachdb console is served by all 3 instances through https. Browser will complain about security but that's fine for a dev/test cluster.
- HAProxy has been set up to serve out the web console as well as sql clients. Only the web console port has been forwarded to the host network.
- Edit
./dbcluster/scripts/create-db-and-user.sh
, to set theDB_NAME
,DB_USER
, andDB_PSWD
. These must agree with whatever is set in the client apps. - If necessary,
chmod +x ./dbcluster/scripts/create-db-and-user.sh
. - Run
./dbcluster/scripts/create-db-and-user.sh
. - Some other program should be able to use this database, through this user, either using a cockroachdb client or a postgres client.
- Docker exec into cockroachdb through the loadbalancer (as the
root
user):docker compose -f ./dbcluster/docker-compose.yml \ run --no-deps --entrypoint "cockroach sql" \ cluster-init \ --host=cockroach-db.local \ --user=root \ --certs-dir=/certs
- Commands are similar to psql cli
AND mysql cli:
- List databases:
\l
orSHOW DATABASES;
- Connect to a database:
\c system
orUSE DATABASE system
- List tables:
\dt
orSHOW TABLES;
- All of the example client apps will use the database
test_app
(as mentioned in Create Database and User above).
- List databases:
Example code is provided for testing with the cluster, compared with postgres.
For feature/behaviour comparison, the client applications may connect to either postgres or cockroachdb.
docker compose -f ./dbcluster/docker-compose-postgres.yml up
Normal python code:
- Edit
./python-psycopg3/docker-compose.yml
, uncomment environment variables to choose either cockroachdb or postgres. docker compose -f ./python-psycopg3/docker-compose.yml up --build
- see some logs...
docker compose -f ./python-psycopg3/docker-compose.yml down
Using yoyo migrations:
- Set up an interactive terminal to call yoyo commands:
docker compose -f python-psycopg3/docker-compose.yml run python-psycopg3-client bash
- Yoyo is installed in poetry environment, so call it through poetry:
poetry run yoyo list
- Perform all migrations, with verbose output:
poetry run yoyo apply -vvv --batch
- Rollback all migrations (requires interactive prompting):
poetry run yoyo rollback -vvv --all
- Also possible to perform migrations using code
- Reset all yoyo tables in the database (if yoyo cli was run in the psycopg3
client test).
- docker exec into one of the cockroachdb instances (see above)
\c test_app
: Connect to thetest_app
database\dt
: List tablesDROP TABLE IF EXISTS _yoyo_log, _yoyo_migration, _yoyo_version, yoyo_lock
docker compose -f ./python-sqlalchemy-core/docker-compose.yml up --build
- (WIP) see some logs...
docker compose -f ./python-sqlalchemy-core/docker-compose.yml down
docker compose -f ./dbcluster/docker-compose.yml down
: Stop the cluster- Does not destroy the cluster or its data.
- Running
docker compose -f ./dbcluster/docker-compose.yml up
again will revive the cluster.
docker compose -f ./dbcluster/docker-compose-postgres.yml down
: Stop postgres
This removes the entire cluster and its data.
- Stop the cluster (see above).
sudo ./dbcluster/scripts/destroy-cluster.sh
: Remove the certs and docker volumes that were created.