This advanced example demonstrates how to build a logbook app that uses node.js for its frontend and MySQL for its backend. The template also creates and connects an HTTP load balancer that load balances across two zones, and an autoscaler to automatically scale the app.
This example assumes you are familiar with Docker containers, as well as Compute Engine resources, particularly HTTP load balancing, autoscaling, managed instance groups, and instance templates.
For more introductory tutorials, refer to the Getting started guide or the Step-by-step guide.
Before you begin
- If you want to use the command-line examples in this guide, install the `gcloud` command-line tool.
- If you want to use the API examples in this guide, set up API access.
- Familiar with Compute Engine HTTP load balancing.
- Familiar with Docker containers.
Creating your templates
This example launches a deployment with several types of resources. To start, you will create reusable templates that define these resources separately. Later on, you will use these templates in your final configuration.
At the end of this example, you will have a deployment that contains these resources:
- A single Compute Engine instance for the backend MySQL virtual machine.
- An instance template that uses a Docker image.
- Two autoscaled managed instance groups in two different zones, running the frontend node.js service.
- Another two autoscaled managed instance group serving static data.
- A health check and a HTTP load balancer to distributed traffic across the respective managed instance groups.
Creating the backend templates
The backend of this app is a single Compute Engine instance running a MySQL
Docker container. Create a template that defines a Compute Engine instance
that uses a container-optimized image. Name the file container_vm.[py|jinja]:
Jinja
Python
The template defines a number of variables, such as the containerImage and the
manifest, which will be filled in when you define your
configuration. This template alone just creates a
single virtual machine (VM) instance.
When you use container images on Compute Engine instances, you also need to
provide a manifest file (different from a Deployment Manager manifest) to
describe to Compute Engine which container image to use. Create a helper
method called container_helper.[py|jinja] to dynamically define the container
manifest:
Jinja
Python
Creating the frontend templates
The frontend of this app runs Node.js and allow users to post messages to the web page. There will be two managed instance groups that contain two instances each: a primary managed instance group, and a secondary managed instance group for load balancing.
To create these frontend templates, use the following instructions.
Create an instance template.
You need an Instance Template resource to create a managed instance group, which is a group of identical VM instances that are centrally managed. This example creates a managed instance group for the frontend node.js instances, but first, you must create the Instance Template.
Define a file named
container_instance_template.[py|jinja]:Jinja
Python
Create an autoscaled managed instance group.
Now that you have an instance template, you can define a template that uses the instance template to create an autoscaled managed instance group. Create a new file named
autoscaled_group.[py|jinja]with the following contents:Jinja
Python
Create the corresponding schema file:
Jinja
Python
Create resources using these templates.
Up to this point, you defined base templates that determine the properties of your resources. Using these templates, define the setup of your frontend. Create a new file named
service.[py|jinja]with the following contents:Jinja
Python
Create the corresponding schema file:
Jinja