Powered By GitBook
Compute Engine quickstart: Create a to-do app with MongoDB
Use Compute Engine to create a two-tier web application.
</walkthrough-tutorial-duration>
Take the interactive version of this tutorial, which runs in the Cloud Console:
​​
​
​
</walkthrough-alt>
In this quickstart, you'll use Compute Engine to create a two-tier web app. The frontend VM runs a Node.js to-do web app, and the backend VM runs MongoDB.
This tutorial guides you through the following:
    Creating and configuring two VMs
    Setting up firewall rules
    Using SSH to install packages on your VMs

Project setup

Google Cloud organizes resources into projects. This allows you to collect all of the related resources for a single application in one place.
Begin by creating a new project or selecting an existing project for this tutorial.
</walkthrough-project-billing-setup>
For details, see Creating a project.

Navigate to Compute Engine

Open the Navigation menu in the upper-left corner of the console, and then select Compute Engine.
</walkthrough-menu-navigation>

Create instances

You create two instances for the web app, one to be the backend server and one to be the frontend server.

Create the backend instance

First, create the backend instance, which runs MongoDB and stores the to-do items:
    1.
    In the VM instances section, click Create.
    2.
    Enter a name for this instance.
    3.
    Choose a region and a zone for this instance.
    4.
    In the Machine type menu, select micro, which specifies a lower-cost machine type. (Learn more about pricing.)
    5.
    In the Boot disk section, click Change, choose Google Drawfork Ubuntu 14.04 LTS, and click Select.
    6.
    In the Firewall selector section, select Allow HTTP traffic. This opens port 80 (HTTP) to access the app.
    7.
    Click Create to create the instance.
After the instance is created, your billing account will start being charged according to the Compute Engine pricing. You will remove the instance later to avoid extra charges.

Create the frontend instance

While the backend VM is starting, create the frontend instance, which runs the Node.js to-do app:
    1.
    In the VM instances toolbar, click Create Instance.
    2.
    Use the same configuration settings as the backend instance.
    3.
    Click Create to create the instance.

Opening Cloud Shell

For this tutorial, you connect using Cloud Shell, which is a built-in command-line tool for the console.

Open Cloud Shell

Open Cloud Shell by clicking the </walkthrough-cloud-shell-icon>Activate Cloud Shell button in the navigation bar in the upper-right corner of the console.

Wait for the instance creation to finish

The instances need to finish being created before the tutorial can proceed. To track the progress of this activity and others, click the Notifications button in the navigation bar in the upper-right corner of the console.

Set up the backend instance

In this section, you install a MongoDB database on the backend instance to save your data.

Connect to the instance

Connect to the VM using SSH:
1
gcloud compute --project "{{project_id}}" ssh --zone [backend-zone] [backend-name]
Copied!
Replace [backend-zone] and [backend-name] with the zone and name of the instance that you created.
If this is your first time using SSH from Cloud Shell, follow the instructions to create a private key.
It may take several minutes for the SSH key to propagate.

Install the backend database

Update packages:
1
sudo apt-get update
Copied!
Install the backend MongoDB database:
1
sudo apt-get install mongodb
Copied!
When asked if you want to continue, enter Y.

Run the database

The MongoDB service starts when you install it. You must stop it so that you can change how it runs:
1
sudo service mongodb stop
Copied!
Create a directory for MongoDB:
1
sudo mkdir $HOME/db
Copied!
Run the MongoDB service in the background on port 80:
1
sudo mongod --dbpath $HOME/db --port 80 --fork --logpath /var/tmp/mongodb
Copied!
Exit the SSH session and disconnect from the backend instance:
1
exit
Copied!

Set up the frontend instance

In this section, you install the dependencies for the web app and then install and run the frontend web app.

Connect to the instance

Connect to the VM with SSH:
1
gcloud compute --project "{{project_id}}" ssh --zone [frontend-zone] [frontend-name]
Copied!
Replace [frontend-zone] and [frontend-name] with the zone and name of the instance that you created.

Install the dependencies

Update packages:
1
sudo apt-get update
Copied!
Install git and npm:
1
curl -sL https://deb.nodesource.com/setup_6.x | sudo -E bash -
Copied!
Install Node.js:
1
sudo apt-get install git nodejs
Copied!
When asked if you want to continue, enter Y.

Install and run the frontend web app

Clone the sample app:
1
git clone https://github.com/GoogleCloudPlatform/todomvc-mongodb.git
Copied!
Install the app dependencies:
1
cd todomvc-mongodb; npm install
Copied!
1
sed -i -e 's/8080/80/g' server.js
Copied!
Start the to-do web app:
1
sudo nohup nodejs server.js --be_ip [backend-internal-ip] --fe_ip [frontend-internal-ip] &
Copied!
Replace [backend-internal-ip] and [frontend-internal-ip] with the internal IP addresses for the instances that you created. These IP addresses are listed for each instance in the VM instances table.
Exit the SSH session and disconnect from the frontend instance:
1
exit
Copied!

Visit the web app

Visit your webserver over an HTTP connection at the external IP address listed next to the frontend instance in the VM instances table. Use a URL in the form http://[external-ip-address].

Cleanup

To remove your instances, select the checkbox next to the instance names and click the Delete button.

Conclusion

</walkthrough-conclusion-trophy>
You're done!
Here are some suggestions for what you can do next:
Last modified 7mo ago