Skip to content

Latest commit

 

History

History
254 lines (170 loc) · 7.79 KB

compute_quickstart-en.md

File metadata and controls

254 lines (170 loc) · 7.79 KB

Compute Engine Quickstart

Build a to-do app with MongoDB

In this quickstart, you'll use Compute Engine to create a two-tier application. The frontend VM runs a Node.js to-do web app, and the backend VM runs MongoDB.

This tutorial will walk you through:

  • Creating and configuring two VMs
  • Setting up firewall rules
  • Using SSH to install packages on your VMs

Project setup

Google Cloud Platform organizes resources into projects. This allows you to collect all the related resources for a single application in one place.

Navigate to Compute Engine

Open the menu on the left side of the console.

Then, select the Compute Engine section.

Create instances

You will create 2 instances to be backend and frontend servers for the application.

Create the backend instance

First, create the backend instance that runs MongoDB. This server stores the to-do items.

Click the Create instance button.

  • Select a name and zone for this instance.

  • Select f1-micro. This will incur fewer charges.

  • Select Ubuntu 14.04 LTS as your boot disk image for this tutorial.

  • In the Firewall selector, select Allow HTTP traffic. This opens port 80 (HTTP) to access the app.

  • Click the Create button to create the instance.

Note: Once the instance is created your billing account will start being charged according to the GCE pricing. You will remove the instance later to avoid extra charges.

Create the frontend instance

While the backend VM is spinning up, create the frontend instance that runs the Node.js to-do application. Use the same configuration as the backend instance.

Setup

You'll install a MongoDB database on the backend instance to save your data.

The SSH buttons in the table will open up an SSH session to your instance in a separate window.

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

Open the Cloud Shell

Open Cloud Shell by clicking icon in the navigation bar at the top of the console.

Wait for the instance creation to finish

The instances need to finish creating before the tutorial can proceed. The activity can be tracked by clicking the notification menu from the navigation bar at the top.

Connect to your backend instance

Connect to the instance

Enter the following command to SSH into the VM. If this is your first time using SSH from Cloud Shell, you will need to create a private key. Enter the zone and name of the instance you created.

gcloud compute --project "{{project-id}}" ssh --zone <backend-zone> <backend-name>

Install the backend database

Now that you have an SSH connection to the backend instance, use the following commands to install the backend database:

Update packages and install MongoDB. When asked if you want to continue, type Y:

sudo apt-get update
sudo apt-get install mongodb

Run the database

The MongoDB service started when you installed it. You must stop it so you can change how it runs.

sudo service mongodb stop

Create a directory for MongoDB and then run the MongoDB service in the background on port 80.

sudo mkdir $HOME/db
sudo mongod --dbpath $HOME/db --port 80 --fork --logpath /var/tmp/mongodb

After, exit the SSH session using the exit command:

exit

Connect to your frontend instance

Install and run the web app on your frontend VM

The backend server is running, so it is time to install the frontend web application.

Connect to the instance

Enter the following command to SSH into the VM. Enter the zone and name of the instance you created.

gcloud compute --project "{{project-id}}" ssh --zone <frontend-zone> <frontend-name>

Install the dependencies

Now that you're connected to your frontend instance, update packages and install git, Node.js and npm. When asked if you want to continue, type Y:

sudo apt-get update
curl -sL https://deb.nodesource.com/setup_6.x | sudo -E bash -
sudo apt-get install git nodejs

Install and run the frontend web app

Clone the sample application and install application dependencies.

git clone https://github.com/GoogleCloudPlatform/todomvc-mongodb.git
cd todomvc-mongodb; npm install
sed -i -e 's/8080/80/g' server.js

Start the to-do web application with the following command, entering the internal ip addresses for the instances you created.

sudo nohup nodejs server.js --be_ip <backend-internal-ip> --fe_ip <frontend-internal-ip> &

After, exit the SSH session using the exit command:

exit

Visit your application

Visit your webserver at the IP address listed in the External IP column next to your frontend instance.

Cleanup

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

Conclusion

You're done!

Here's what you can do next: