Before you begin
Before starting the tutorial, do the following:- Create a CockroachDB Cloud account.
- Create a Google Cloud account.
- Install the Google Cloud SDK.
- Install Docker Desktop.
Step 1. Create a CockroachDB Standard cluster
- . If this is your first CockroachDB Cloud organization, it will be credited with $400 in to get you started.
- On the Get Started page, click Create cluster.
- On the Select a plan page, select Standard.
- On the Cloud & Regions page, select a cloud provider (GCP or AWS).
- In the Regions section, select a region for the cluster. Refer to for the regions where CockroachDB Standard clusters can be deployed. To create a multi-region cluster, click Add region and select additional regions.
- Click Next: Capacity.
- On the Capacity page, keep the at the default value of 2 vCPUs. Click Next: Finalize.
- On the Finalize page, name your cluster. If an active free trial is listed in the right pane, you will not need to add a payment method, though you will need to do this by the to maintain your organization’s clusters. Click Create cluster. Your cluster will be created in a few seconds and the Create SQL user dialog will display.
Step 2. Set up your cluster connection
Once your cluster is created, the Connect to cluster-name dialog displays. Use the information provided in the dialog to set up your cluster connection for the SQL user that was created by default:- In your terminal, run the second command from the dialog to create a new
certsdirectory on your local machine and download the CA certificate to that directory:
cert file will be downloaded to ~/.postgresql/root.crt.
- Copy the connection string provided, which will be used in the next steps (and to connect to your cluster in the future).
This connection string contains your password, which will be provided only once. If you forget your password, you can reset it by going to the SQL Users page for the cluster, found at
https://cockroachlabs.cloud/cluster/<CLUSTER ID>/users.-
<usernameis the SQL user. By default, this is your CockroachDB Cloud account username. -
<passwordis the password for the SQL user. The password will be shown only once in the Connection info dialog after creating the cluster. -
<cluster-hostnameis the hostname of your CockroachDB Cloud cluster. -
<cluster-idis a unique string used to identify your cluster when downloading the CA certificate. For example,12a3bcde-4fa5-6789-1234-56bc7890d123. You can find these settings in the Connection parameters tab of the Connection info dialog.
cert file will be downloaded to ~/.postgresql/root.crt.
- Copy the connection string provided, which will be used in the next steps (and to connect to your cluster in the future).
https://cockroachlabs.cloud/cluster/<CLUSTER ID>/users.
-
<usernameis the SQL user. By default, this is your CockroachDB Cloud account username. -
<passwordis the password for the SQL user. The password will be shown only once in the Connection info dialog after creating the cluster. -
<cluster-hostnameis the hostname of your CockroachDB Cloud cluster. -
<cluster-idis a unique string used to identify your cluster when downloading the CA certificate. For example,12a3bcde-4fa5-6789-1234-56bc7890d123. You can find these settings in the Connection parameters tab of the Connection info dialog.
cert file will be downloaded to %APPDATA%/.postgresql/root.crt.
- Copy the connection string provided, which will be used in the next steps (and to connect to your cluster in the future).
https://cockroachlabs.cloud/cluster/<CLUSTER ID>/users.
-
<usernameis the SQL user. By default, this is your CockroachDB Cloud account username. -
<passwordis the password for the SQL user. The password will be shown only once in the Connection info dialog after creating the cluster. -
<cluster-hostnameis the hostname of your CockroachDB Cloud cluster. -
<cluster-idis a unique string used to identify your cluster when downloading the CA certificate. For example,12a3bcde-4fa5-6789-1234-56bc7890d123. You can find these settings in the Connection parameters tab of the Connection info dialog.
Step 3. Create a database
- If you haven’t already, .
-
Start the using the connection string you got from the CockroachDB Cloud Console earlier:
In the connection string copied from the CockroachDB Cloud Console, your username, password and cluster name are pre-populated. Replace the
<certs_dirplaceholder with the path to thecertsdirectory that you created earlier. -
In the SQL shell, create the
bankdatabase that your application will use: -
Exit the SQL shell:
Step 4. Get the application code
-
Clone the code’s GitHub repo:
-
Create a new folder named
certsat the top level of theexample-app-python-djangoproject, and then copy the root certificate that you downloaded for your cluster to the new folder. The project directory structure should look like this:
Step 5. Initialize the database and test the app locally
-
At the top level of the app’s project directory, create and then activate a virtual environment:
-
Install the required modules to the virtual environment:
-
Set the
DATABASE_URLenvironment variable to the connection string provided in the Connection info window of the CockroachDB Cloud Console, but with the root certificate located in the localcertsdirectory:This Django app uses thedj_database_urlmodule to configure the database connection from a connection URL. The module uses the value assigned to theDATABASE_URLenvironment variable for the connection.
In the Cloud Run deployment, we use the Google Cloud Secret Manager to define the
DATABASE_URL environment variable for the deployment.-
Execute the initial database schema migration:
This migration initializes the
bankdatabase with the tables defined inmodels.py, in addition to some other tables for the admin functionality included with Django’s starter application. -
Run the app:
The output should look like this:To perform simple reads and writes to the database, you can send HTTP requests to the application at http://0.0.0.0:8000/.
-
In a new terminal, use
curlto send a POST request to the application:This request inserts a new row into thecockroach_example_customerstable. -
Send a GET request to read from the
cockroach_example_customerstable:You can also query the table directly in the to see the changes: - Enter Ctrl+C to stop the application.
Step 6. Configure GCP
- In the terminal, authenticate the
gcloudcommand-line tool with your Google Cloud account:
gcloud is included with the Google Cloud SDK installation.-
Create a Google Cloud project for the application deployment:
You can specify a location for the project within your Google Cloud resources with the
--organization or --folder flags.-
Configure the CLI to use your Google Cloud account and the new project ID by default:
-
Set the
PROJECT_IDenvironment variable:For the rest of the tutorial, we usePROJECT_IDto refer to the project ID.
Step 7. Containerize the application and push it to the registry
-
Build the Docker image locally:
If there are no errors, the container built successfully.
-
Authenticate Docker with GCP’s Container Registry:
-
Enable the Container Registry API for the project:
-
Push the Docker image to the project’s registry.
Step 8. Create a secret for the database connection URI
-
Create a service account to manage the secrets for your project:
-
Enable the Secret Manager API for the project:
-
Create a secret for the connection string stored locally in the
DATABASE_URLenvironment variable, and bind the new service account to the secret.
Step 9. Deploy the application on Cloud Run
-
Enable the Cloud Run API for the project:
-
Create a Cloud Run service for the application:
Note the following:
-
The
--regionflag specifies the region of the CockroachDB node targeted in the connection string. -
The
--service-accountflag specifies thecockroach-labsservice account that you created earlier for the app deployment. -
The
--set-secretsflag sets theDATABASE_URLenvironment variable to thecockroach-connection-urisecret that you created earlier. -
The
--imageflag specifies the container image URL for thecrdb-sampleimage that you pushed to the container registry. If prompted, selectCloud Run (fully managed).
-
The
curl). For example:
By default, the sample application allows all hosts/domain names to serve the application.After testing, we recommend that you update the
ALLOWED_HOSTS property in settings.py to allow only a local testing URL and the Cloud Run service URL to serve the application.
