Sunday, December 10, 2017

GCP - Compute Engine - Quickstart

Build a to-do app with MEAN

15 minutes
In this quickstart, you'll use Compute Engine to create a two-tier application. The front-end VM runs a Node.js todo web app, and the back-end 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

Create a back-end VM

First, create the back-end VM that runs MongoDB. This server stores the to-do items.
  1. Click the Create instance button
    Click the Create instance button.
  2. Name the instance
    Enter a name (for example, "back-end") for this instance and press the Tab key to move to the next step
  3. Select the machine type
    Select f1-micro . This will incur fewer charges. Learn more about pricing 
  4. Select the boot disk image
    Select Ubuntu 14.04 LTS as your boot disk image for this tutorial.
  5. Open HTTP firewall port
    Open HTTP firewall port
    Check Allow HTTP traffic. This is an easy way to open firewall 80 (HTTP) so the front-end and back-end VMs can communicate
  6. Create the VM
    Create the VM
    Click Create to finish creating this instance

Create a front-end VM

While the back-end VM is spinning up, create the front-end VM that runs the Node.js todo application
  1. Create the front-end VM
    Click Create Instance to create the front-end VM
  2. Name the instance
    Enter a name (for example, "front-end") for this instance and press the Tab key to move to the next step
  3. Select the machine type
    Select f1-micro . This will incur fewer charges. Learn more about pricing 
  4. Select the boot disk image
    Select Ubuntu 14.04 LTS as your boot disk image for this tutorial.
  5. Open HTTP firewall port
    Open HTTP firewall port
    Check Allow HTTP traffic. This is an easy way to open firewall 80 (HTTP) so the front-end and back-end VMs can communicate
  6. Create the VM
    Create the VM
    Click Create to finish creating this instance

Install and run the back-end database (1)

  1. SSH into the VM
    SSH into the VM
    Next to your back-end VM, click SSH to open a terminal window in your browser
  2. Install and run the database
    Install and run the database
    Go to the terminal window and enter the commands to install and run MongoDB

Install and run the back-end database (terminal window)

Enter these commands to install and run MongoDB. You can use the keyboard to copy and paste each command line. Click Done when you finish entering all the commands to close the SSH window.
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 you enter the final command, click Done and then confirm that you want to leave the page to close the SSH browser window.
 

Install and run the web app on your front-end VM

The MongoDB back-end server is running, so it is time to install the front-end web application
  1. SSH into the VM
    SSH into the VM
    Next to your front-end VM, click SSH to open a terminal window in your browser
  2. Install and run the web app on your front-end VM
    Install and run the web app on your front-end VM
    Go to the terminal window and enter the commands to install and run the sample todo app

Install and run the web app on your front-end VM (terminal window)

Enter these commands to install the todo web application. You can use the keyboard to copy and paste each command line. Click Done when you finished entering all the commands to close the SSH window.
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

Start the todo web application. Note: The IP addresses below are internal IPs for communication between servers. You can find these IP addresses on each server’s details page.

$ nohup nodejs server.js --be_ip 10.142.0.2 --fe_ip 10.142.0.3 &

After you enter the final command, click Done and then confirm that you want to leave the page to close the SSH browser window.

You're done

Your app is running at http://35.185.117.209:8080
The quickstart servers continue running until you remove them. This can reduce your available quotas.

2 comments: