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
Google Cloud Platform organizes resources into projects. This allows you to collect all the related resources for a single application in one place.
Open the menu on the left side of the console.
Then, select the Compute Engine section.
You will create 2 instances to be backend and frontend servers for the application.
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.
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.
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 Cloud Shell by clicking icon in the navigation bar at the top of the console.
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.
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>
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
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
The backend server is running, so it is time to install the frontend web application.
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>
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
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 webserver at the IP address listed in the External IP column next to your frontend instance.
To remove your instances, select the checkbox next to your instance names and click the Delete button.
You're done!
Here's what you can do next:
-
Find Google Cloud Platform samples on GitHub.
-
Learn how to set up Load Balancing.
-
Learn how to transfer files to your Virtual Machine.
-
Learn how to run containers.