In this lab, you create a Google Compute Engine instance and deploy the Bookshelf application to it using a Linux shell startup script. You continue to use existing storage services you used earlier in the course, including Google Cloud Datastore, and Google Cloud Storage.
To complete this lab, you need:
Access to a supported Internet browser:
A Google Cloud Platform project
A Source Code repository containing the lab code
A Google Cloud Storage bucket
In this lab, you:
In this lab, you explore Google Compute Engine using the Bookshelf sample application. As in the case of Google Container Engine, Compute Engine is only used in this lab to host the frontend Python web component of the application. You continue to use the existing Google Cloud Platform storage services used earlier in the course - Cloud Datastore to host structured data for books, and Cloud Storage to host book cover images.
Compute Engine lets you create and run virtual machines (known as instances), block storage disk devices, and networks on Google infrastructure. Compute Engine instances are created based on a combination of boot image and machine type. No upfront investment is required to get started and you can run thousands of virtual CPUs on infrastructure that is designed to be fast, and offer strong, consistent performance. Compute Engine instances and resources are used as the basis of many other services in Google Cloud Platform. For example, as you observed earlier in the course, Container Engine leverages Compute Engine instances.
Running Bookshelf across a cluster of multiple instances involves a few more steps than an equivalent scalable system in Container Engine. To keep things simple for this introductory lab, you deploy Bookshelf on a single Compute Engine instance. If you would like to learn more about how to deploy Bookshelf, or similar software across multiple instances, then please refer to this getting started guide for Python.
The following diagram illustrates the high-level components and resources that make up the version of Bookshelf you deploy in this lab.
You use a startup script to automate the installation and configuration of Bookshelf on your instance. Startup scripts provide a quick, and easy to use form of configuration management. Alternatively, you could use more sophisticated and open-source configuration management tools such as Puppet, Chef, Salt, and Ansible. Google Cloud Platform also supports automated creation and management of resources using Google Cloud Deployment Manager.
As you work through this lab, consider the development, operational, and support implications of deploying Bookshelf in the different environments you have tested in this course.
Review the source code for the startup script you use to deploy Bookshelf.
To review the code:
Open the Google Cloud Platform Console, and if necessary, select the cp100 project.
Click Tools > Source Repositories > Source Code.
Click compute-engine > startup-scripts > startup-script.sh.
If you are not used to reading and writing Bash shell scripts, then this file may seem a little daunting at first. Each of the main steps in the script are commented so you can read through it and follow the general flow. In this step we highlight some of the sections of the script that relate specifically to Compute Engine and to the deployment of Bookshelf.
Notice on line 20 that the
PROJECTID variable is assigned the output of a command. In this case the
curl command is used to query your project ID from the Compute Engine metadata stored in your project.
The project ID is then used on line 45 to clone the source code for Bookshelf from your cloud repository.
Earlier in the course, you manually ran a command to install a number of requirements related to deploying Bookshelf to App Engine. On line 52 of the script these requirements are automatically installed.
The remainder of the script configures a supervisor system that manages the Bookshelf process. Most of these lines are not specific to Compute Engine and might be found in similar scripts to deploy an application in a Linux environment.
One possible disadvantage of using a startup script in this way, is that it takes a relatively long time (minutes) to start the application. A potential solution might be to create an image with most of the required software already installed and then apply any configuration changes required using a startup script.
Leave the Cloud Platform Console window open.
Create a Compute Engine instance to host a copy of Bookshelf and use a startup script to deploy the application. Configure a firewall rule to allow access to the external IP address and port used to expose the web application.
To create an instance:
Return to the Cloud Platform Console, and in the top right corner of the console window, click the Activate Google Cloud Shell button ().
Type the following command to change to the directory containing the code for this lab.
As with Container Engine, you need to configure the Cloud SDK in Cloud Shell with a default zone in which to create your container cluster. Type the following command to view a list of available zones.
gcloud compute zones list
The output lists the available zones names, the region they are based in, and status.
Select a zone from the region geographically closest to your location.
Type the following command to configure the Cloud SDK with a default zone and substitute your chosen value where indicated in the following command.
gcloud config set compute/zone <ZONE>
For example, if you chose the zone us-east1-b then the command would look like the following.
gcloud config set compute/zone us-east1-b
Before you type the command in this step to create a Compute Engine instance, it is useful to review the various flags and options used:
bookshelf:This is the name of your instance.
--image-project:These flags specify the name of the Compute Engine image used to create the instance. In this case, the image is based on the Debian 8 Linux operating system.
--machine-type:This flag specifies the CPU and memory resources that will be allocated to the instance. Note that Compute Engine offers a wide variety of preconfigured machine types and you can even create your own custom machine type.
--scopes:The scopes flag is used to pass in a list of scopes that should be assigned to a Compute Engine instance. Scopes are used to limit your access to only the methods and user information that your application requires.
--metadata-from-file:This flag supplies some custom metadata for this particular Compute Engine instance. In this case, the metadata supplies the contents of the startup script you reviewed earlier in the lab.
--tags:The tags flag allows you to assign arbitrary tags to describe your instance. Tags can be used to associate an instance with other resources, and in this case, is used to establish a relationship with a firewall rule to allow access to the Bookshelf application.
Type the following command to create a Compute Engine instance called bookshelf.
gcloud compute instances create bookshelf \ --image-family=debian-8 \ --image-project=debian-cloud \ --machine-type=g1-small \ --scopes userinfo-email,cloud-platform \ --metadata-from-file startup-script=startup-scripts/startup-script.sh \ --tags http-server
After some delay, the output describes the progress of creating your Compute Engine instance. Once the command completes, it summarizes the details of the new instance, including the external IP address that you will use to connect to the Bookshelf application.
You also need to create a firewall rule to allow access to the Bookshelf application on port 8080 from outside your Compute Engine network. Once again, before you type the command in this step to create the rule, it is useful to review the various flags and options used:
default-allow-http-8080:This is the name of your rule. Note that the term ‘default' refers to the name of the network where the rule applies. This is a naming convention that is typically used when creating Compute Engine firewall rules, but you are free to come up with an alternative naming convention.
--allow:This flag is used to specify the protocol and port for which you are configuring access.
--source-ranges:This flag specifies what IP addresses can connect to the allowed port(s). In this case a setting
0.0.0.0/0allows connections from any IP address.
--target-tags:This flag indicates which tags the rule will apply to. Recall that you applied a tag to the Compute Engine instance you created earlier in this section. The tag specified in the rule must match the tag applied to the instance(s), or you will not be able to access the application.
--description:Used to supply a human-readable description of what the rule does. This flag is optional.
Type the following command (without the trailing newline characters) to create a firewall rule named default-allow-http-8080.
gcloud compute firewall-rules create default-allow-http-8080 \ --allow tcp:8080 \ --source-ranges 0.0.0.0/0 \ --target-tags http-server \ --description "Allow port 8080 access to http-server"
After a brief delay, the output summarizes the details of the new rule.
As you observed when reviewing the startup script earlier in the lab, there is lot of work that needs to be done by the operating system before the Bookshelf application is ready to use.
You can review the progress of the startup script by requesting the output of operating system from the serial port using the API or command line tools.
Type the following command to get the output of the serial port for the cp100 instance.
gcloud compute instances get-serial-port-output bookshelf
You should repeat this command until you see a message similar to the following sample output, indicating that the startup script has finished running, before proceeding to the next step.
<time-stamp> cp100 systemd:
in 1.247s (kernel) + 1min 54.620s (userspace) = 1min 55.867s.
Type the following command to close your Cloud Shell window.
You need to examine the external IP address assigned to the bookshelf instance in order to test the application.
Click Compute > Compute Engine > VM instances.
Notice that the details for bookshelf are summarized in the list of VM instances, including a link to the external IP address in the External IP column.
In the External IP column, click the IP address for the bookshelf instance.
A new browser tab will open but you will not be able to view Bookshelf just yet.
In the address bar, append the port number, 8080, you specified in the firewall rule.
For example, if your external IP address is 220.127.116.11, then you should modify the rule to look like the following.
Leave the Bookshelf tab and the Cloud Platform Console open.
Generate some simple test data in the Bookshelf application, including a sample cover image, to verify that it is working as expected.
To test Bookshelf:
Return to the Bookshelf browser tab. You may, or may not see existing book details in the bookshelf, based on any previous labs you may have completed earlier in the course.
Click Add Book.
In the ‘Add book' page:
A page loads with the details you recorded for the new book, in addition to the sample cover image.
Close the Bookshelf browser tab, but leave the Cloud Platform Console open.
As in the case of Container Engine, and before you start working through the ‘clean up' section of this lab, it is useful to take a few moments to consider the major differences between this version of Bookshelf with the App Engine version. Can you think of any others that are important to you?
Can you think of any advantages of running the different versions of Bookshelf you have tried on Container Engine compared to Compute Engine? You can find some suggestions after the comparison table.
App Engine standard runtime
Sandboxed version of Python 2.7
Custom Python runtime running in Debian Linux
Access to Datastore
Access to Cloud Storage
Potential advantages of running the different versions of Bookshelf on Container Engine over Compute Engine include:
Can you think of any others?
For more information on comparing compute options, see: https://cloud.google.com/docs/choosing-a-compute-option#comparing_options.
To clean up the resources used in the lab:
RIn the Google Cloud Platform Console's left-side menu, click Compute > Compute Engine.
Click the check box to the left of the bookshelf instance. This will activate the Delete button at the top of the page.
Click Delete to remove the the instance.
In the Google Cloud Platform Console's left-side menu, click Networking > VPC Network.
Click Firewall rules.
In the Firewall rules section, check the default-allow-http-8080 rule, and then click Delete.
Close all browser tabs.
©Google, Inc. or its affiliates. All rights reserved. Do not distribute.