In this tutorial, you deploy an example logbook app that uses Node.js for its frontend and MySQL for its backend. At the end of the tutorial, your deployment has the following resources:
If you are new to Deployment Manager, see the Quickstart or the Step-by-Step Guide tutorials.
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.
- Read an overview of Compute Engine network load balancing.
- Read an overview of Docker containers.
Creating your resource templates
This example launches a deployment that contains a variety of Google Cloud resources. To start, you create templates that define these resources separately. Later, you call these templates in your final configuration. Your deployment contains these resources:
- A Compute Engine instance that hosts a MySQL database for the app.
- An instance template for frontend instances, which uses a Docker image for the Node.js app.
- A managed instance group, which uses the instance template to create two frontend instances.
- An autoscaler, which starts or stops additional frontend instances based on incoming traffic.
- A health check, which checks whether the frontend instances are available to do work.
- A network load balancer with a forwarding rule.
- A target pool for the managed instance group.
Creating the template for the MySQL backend
The backend of this app is a single Compute Engine instance running
a MySQL Docker container. The container_vm.py
template defines a
Compute Engine instance that can run Docker containers. To ensure that the
template follows the correct structure and contains all of the required
properties, you also need a schema file.
Copy the template below, or download it from the GitHub repository:
Download the schema file for the template.
The template has some undefined properties, such as containerImage
, which are
defined in later templates.
When you use container images on Compute Engine instances, you also need to
provide a manifest file that describes which container image to use. Create a
helper method called container_helper.py
to dynamically define the container
manifest:
Creating the template for the Node.js frontend
The frontend of the app runs Node.js, and lets users post messages to the web page. The frontend runs on a group of virtual machine instances, supported by an autoscaler and a load balancer. To create frontend templates, use the following instructions.
Create an instance template resource.
You need an instance template to create a managed instance group, which is a group of identical virtual machine (VM) instances that you control as a single entity.
Create a file named
container_instance_template.py
, and download the schema for the template:Create an autoscaler, managed instance group, and load balancer.
Next, create another template that uses the
container_instance_template.py
template and creates the rest of your frontend resources, including an autoscaler, load balancer, and managed instance group.
This template includes the following resources:
An instance template using the
container_instance_template.py
template.A managed instance group using the instance template, and an autoscaler that references the managed instance group. Using references ensures that Deployment Manager creates the resources in a specific order. In this case, the managed instance group is created before the autoscaler.
A network load balancer that contains the following resources:
- A forwarding rule with a single external IP address exposed to the Internet.
- A target pool containing the managed instance group you created earlier.
- A health check to attach to the target pool.
Create a file named frontend.py
, and download the schema for the template:
Download the schema file for the template.
Creating a unifying template
Finally, create a template that brings together both the back-end
and frontend templates. Create a file named nodejs.py
with the following
contents:
Download the schema file for the template.
Note that the frontend of your app is named env["deployment"]-frontend
, and
that your backend is also named similarly. When you deploy the app,
Deployment Manager will automatically replace env["deployment"]
with the
deployment name.
Creating your configuration
After you have all the templates ready, you can create a configuration that will
be used to deploy your resources. Create a configuration file named
nodejs.yaml
with the following contents:
Replace ZONE_TO_RUN
with the zone where you want your resources, such as
us-central1-a
.
Deploying your resources
Now, deploy your resources. Using the Google Cloud CLI, run:
gcloud deployment-manager deployments create advanced-configuration --config nodejs.yaml
When the deployment is complete, Deployment Manager shows a summary of the resources created, similar to the following:
Waiting for create operation-1468522101491-5379cf2344539-5961abe8-a500190c...done.
Create operation operation-1468522101491-5379cf2344539-5961abe8-a500190c completed successfully.
NAME TYPE STATE ERRORS
advanced-configuration-application-fw compute.v1.firewall COMPLETED []
advanced-configuration-backend compute.v1.instance COMPLETED []
advanced-configuration-frontend-as compute.v1.autoscaler COMPLETED []
advanced-configuration-frontend-hc compute.v1.httpHealthCheck COMPLETED []
advanced-configuration-frontend-igm compute.v1.instanceGroupManager COMPLETED []
advanced-configuration-frontend-it compute.v1.instanceTemplate COMPLETED []
advanced-configuration-frontend-lb compute.v1.forwardingRule COMPLETED []
advanced-configuration-frontend-tp compute.v1.targetPool COMPLETED []
Testing the app
To test your app, first get the external IP address that is serving traffic, by querying the forwarding rule:
$ gcloud compute forwarding-rules describe advanced-configuration-frontend-lb --region us-central1 IPAddress: 104.154.81.44 IPProtocol: TCP creationTimestamp: '2016-07-14T11:48:37.228-07:00' description: '' id: '9033201246750269546' kind: compute#forwardingRule name: advanced-configuration-frontend-lb portRange: 8080-8080 region: https://www.googleapis.com/compute/v1/projects/myproject/regions/us-central1 selfLink: https://www.googleapis.com/compute/v1/projects/myproject/regions/us-central1/forwardingRules/advanced-configuration-frontend-lb target: https://www.googleapis.com/compute/v1/projects/myproject/regions/us-central1/targetPools/advanced-configuration-frontend-tp
In this case, the external IP is 104.154.81.44
.
Next, in a browser, visit the external IP address with port 8080. For example,
if your external IP address is 104.154.81.44
, the URL would be:
http://104.154.81.44:8080
You should see a blank page, which is expected. Next, post a message to the page. Go to the following URL:
http://104.154.81.44:8080?msg=hellothere!
You will see confirmation that your message was added. Navigate back to the main URL, and the page should now have the message:
hellothere!
You now have a deployed app that can log messages sent to it.
Next steps
After you've completed this sample, you can:
- Build on this example by deploying instances with your own Docker containers. If you want to modify the Docker containers used in this tutorial, edit the Dockerfiles in the GitHub repository.
- See more example deployments in the Deployment Manager GitHub repository.
- Read more about templates and deployments.