This page guides you through setting up a virtual environment for developing and debugging a global application. It is the third section of the Develop and Deploy a Global Application tutorial. In this section, you will set up a demo CockroachDB cluster, initialize the database, and set up a virtual development environment.
Before you begin
Complete the previous sections of the tutorial, ending with Create a Multi-Region Database Schema.
On your local machine:
- Install CockroachDB.
- Install Python 3 and Pipenv if necessary.
- Clone the
movr-flaskrepository. We'll reference the source code in this repository throughout the tutorial.
Set up a demo multi-region CockroachDB cluster
For debugging and development purposes, you can use the cockroach demo command. This command starts up a secure, nine-node demo cluster.
To set up the demo multi-region cluster, run
cockroach demo, with the--nodesand--demo-localityflags. The localities specified below assume GCP region names.cockroach demo \ --nodes=9 \ --demo-locality=region=gcp-us-east1:region=gcp-us-east1:region=gcp-us-east1:\ region=gcp-us-west1:region=gcp-us-west1:region=gcp-us-west1:\ region=gcp-europe-west1:\ region=gcp-europe-west1:region=gcp-europe-west1 \ --emptyKeep this terminal window open. Closing it will shut down the demo cluster.
In a new terminal, load the
dbinit.sqlscript to the demo database. This file contains themovrdatabase schema that we covered in Create a Multi-Region Database Schema.cockroach sql \ --url='postgres://demo:<demo_password>@127.0.0.1:26257?sslmode=require' \ -f dbinit.sqlVerify that the database schema loaded:
> SHOW TABLES;table_name +------------+ rides users vehicles (3 rows)
A production deployment should ideally have nodes on machines located in different areas of the world. You will deploy a multi-region CockroachDB cluster for this application using CockroachDB Standard in the final section of this tutorial series, Deploy a Global, Serverless Application.
Set up a virtual development environment
For debugging, use pipenv, a tool that manages dependencies with pip and creates virtual environments with virtualenv.
Initialize the project's virtual environment:
pipenvpipenvcreates aPipfilein the current directory, with the requirements needed for the app.Install the packages listed in the
Pipfile:pipenv installActivate the virtual environment:
pipenv shellFrom this shell, you can run any Python 3 application with the required dependencies that you listed in the
Pipfile, and the environment variables that you listed in the.envfile. You can exit the shell subprocess at any time with a simpleexitcommand.To test out the application, you can run the server file:
python server.pyTo run Python commands within the virtual environment, you do not need to add
3to the end of the command. For example, runningpython3 server.pyinstead of the above command results in an error.Navigate to the application's test URL, which defaults to http://127.0.0.1:5000/.
In production, it is often recommended to containerize your application and deploy it with a deployment orchestration tool like Kubernetes or with a serverless deployment service like Google Cloud Run. You will learn to deploy the application with Google Cloud Run in the final section of this tutorial series, Deploy a Global Application.
Next steps
Now that you've set up a development environment, you can learn about developing and debugging the application.