Jump Start Solution: Load balanced managed VMs

Last reviewed 2023-04-10 UTC

This guide helps you understand, deploy, and use the Load balanced managed VMs Jump Start Solution, which demonstrates how to create a virtual machine cluster with a load balancer, make VMs globally available, and instantaneously manage traffic.

You can deploy the solution to help you do the following:

  • Create redundant versions of an application that is hosted on multiple VMs.
  • Automatically scale the number of VMs to meet user demand.
  • Automatically heal failing copies of an application.
  • Distribute traffic to multiple locations.
  • Migrate an existing load-balanced implementation to the cloud with minor modifications (lift and shift).

This document is intended for developers who have some background with load balancers. It assumes that you're familiar with basic cloud concepts, though not necessarily Google Cloud. Experience with Terraform is helpful.

Objectives

This solution guide helps you do the following:

  • Learn about load balancer features and configurations, including auto-scaling and auto-healing.
  • Deploy two or more VMs that can potentially serve an application, and use a load balancer to manage traffic.
  • Modify the deployment location and the number of nodes.
  • Understand load balancer design considerations.

Architecture

This solution deploys a group of VMs that are managed by a load balancer. The following diagram shows the architecture of the Google Cloud resources:

Load balanced VMs jump start solution
diagram.

Request flow

The following is the request processing flow of the topology that the load balanced managed VMs solution deploys. The steps in the flow are numbered as shown in the preceding architecture diagram.

  1. The user makes a request to the application, which is deployed on Compute Engine. The request first lands on Cloud Load Balancing.

  2. Cloud Load Balancing distributes traffic to the Compute Engine managed instance group (MIG), which scales the number of instances based on traffic volume.

Components and configuration

The architecture includes the following components:

Component Product description Purpose in this solution
Compute Engine A secure and customizable compute service that lets you create and run virtual machines on Google's infrastructure. Multiple virtual machines in a MIG create redundant versions of a prospective application.
Cloud Load Balancing A service that provides high performance, scalable load balancing on Google Cloud. Process incoming user requests, and distribute to nodes based on configured settings.

Cost

For an estimate of the cost of the Google Cloud resources that the load balanced managed VMs solution uses, see the precalculated estimate in the Google Cloud Pricing Calculator.

Use the estimate as a starting point to calculate the cost of your deployment. You can modify the estimate to reflect any configuration changes that you plan to make for the resources that are used in the solution.

The precalculated estimate is based on assumptions for certain factors, including the following:

  • The Google Cloud locations where the resources are deployed.
  • The amount of time that the resources are used.

Deploy the solution

This section guides you through the process of deploying the solution.

Create or choose a Google Cloud project

When you deploy the solution, you choose the Google Cloud project where the resources are deployed. When you're deciding whether to use an existing project or to create a new project, consider the following factors:

  • If you create a project for the solution, then when you no longer need the deployment, you can delete the project and avoid continued billing. If you use an existing project, you must delete the deployment when you no longer need it.
  • Using a new project can help avoid conflicts with previously provisioned resources, such as resources that are used for production workloads.

If you want to deploy the solution in a new project, create the project before you begin the deployment.

To create a project, complete the following steps:

  1. In the Google Cloud console, go to the project selector page.

    Go to project selector

  2. To begin creating a Google Cloud project, click Create project.

  3. Name your project. Make a note of your generated project ID.

  4. Edit the other fields as needed.

  5. To create the project, click Create.

Get the required IAM permissions

To start the deployment process, you need the Identity and Access Management (IAM) permissions that are listed in the following table. If you have the roles/owner basic role for the project in which you plan to deploy the solution, then you already have all the necessary permissions. If you don't have the roles/owner role, then ask your administrator to grant these permissions (or the roles that include these permissions) to you.

IAM permission required Predefined role that includes the required permissions

serviceusage.services.enable

Service Usage Admin
(roles/serviceusage.serviceUsageAdmin)

iam.serviceAccounts.create

Service Account Admin
(roles/iam.serviceAccountAdmin)

resourcemanager.projects.setIamPolicy

Project IAM Admin
(roles/resourcemanager.projectIamAdmin)
config.deployments.create
config.deployments.list
Cloud Infrastructure Manager Admin
(roles/config.admin)

Service account created for the solution

If you start the deployment process through the console, Google creates a service account to deploy the solution on your behalf (and to delete the deployment later if you choose). This service account is assigned certain IAM permissions temporarily; that is, the permissions are revoked automatically after the solution deployment and deletion operations are completed. Google recommends that after you delete the deployment, you delete the service account, as described later in this guide.

View the roles assigned to the service account

These roles are listed here in case your administrator needs this information.

  • roles/compute.instanceAdmin.v1
  • roles/editor
  • roles/iam.serviceAccountActor
  • roles/iam.serviceAccountUser

Choose a deployment method

To help you deploy this solution with minimal effort, a Terraform configuration is provided in GitHub. The Terraform configuration defines all the Google Cloud resources that are required for the solution.

You can deploy the solution by using one of the following methods:

  • Through the console: Use this method if you want to try the solution with the default configuration and see how it works. Cloud Build deploys all the resources that are required for the solution. When you no longer need the deployed solution, you can delete it through the console. Any resources that you create after you deploy the solution might need to be deleted separately.

    To use this deployment method, follow the instructions in Deploy through the console.

  • Using the Terraform CLI: Use this method if you want to customize the solution or if you want to automate the provisioning and management of the resources by using the infrastructure as code (IaC) approach. Download the Terraform configuration from GitHub, optionally customize the code as necessary, and then deploy the solution by using the Terraform CLI. After you deploy the solution, you can continue to use Terraform to manage the solution.

    To use this deployment method, follow the instructions in Deploy using the Terraform CLI.

Deploy through the console

Complete the following steps to deploy the preconfigured solution.

  1. In the Google Cloud Jump Start Solutions catalog, go to the Load balanced managed VMs page.

  2. Review the information that's provided on the page, such as the estimated cost of the solution and the estimated deployment time.

  3. When you're ready to start deploying the solution, click Deploy.

    A step-by-step interactive guide is displayed.

  4. Complete the steps in the interactive guide:

    1. Select a project where you want to create resources that are deployed by the solution and click Continue.

    2. In the Deployment name field, type a name you have not previously used in this project.

    3. Optionally, add an identifying label to the deployment. (Solution indicator and deployment name labels are automatically added.) You can use labels to organize resources by criteria such as cost center, environment, or state.

      For more information about labels, see Creating and managing labels

    4. From the Region and Zone drop-down lists, select the desired location where resources will be created.

      For more information about regions and zones, see Geography and regions

    5. In the Number of nodes field, type the minimum number of virtual machines in the MIG. The load balancer is configured to scale the number of virtual machines based on user traffic volume. For this deployment, you can use the default value of 2 nodes.

      For more information about creating multiple VMs, see Basic scenarios for creating managed instance groups (MIGs).

    6. Click Continue.

  5. When you've finished specifying options, click Deploy.

    The Solution deployments page is displayed. The Status field on this page shows Deploying.

  6. Wait for the solution to be deployed.

    If the deployment fails, the Status field shows Failed. You can use the Cloud Build log to diagnose the errors. For more information, see Errors when deploying from the console

    After the deployment is completed, the Status field changes to Deployed.

  7. To view the Google Cloud resources that are deployed and their configuration, take an interactive tour.

    Start the tour

You deployed the example solution, viewed the load balancer configuration, and viewed the application site that is served by VMs. To learn about design recommendations to address your organization's unique load balancing needs, see Design recommendations.

When you no longer need the solution, you can delete the deployment to avoid continued billing for the Google Cloud resources. For more information, see Delete the deployment.

Deploy using the Terraform CLI

This section describes how you can customize the solution or automate the provisioning and management of the solution by using the Terraform CLI. Solutions that you deploy by using the Terraform CLI are not displayed in the Solution deployments page in the Google Cloud console.

Set up the Terraform client

You can run Terraform either in Cloud Shell or on your local host. This guide describes how to run Terraform in Cloud Shell, which has Terraform preinstalled and configured to authenticate with Google Cloud.

The Terraform code for this solution is available in a GitHub repository.

  1. Clone the GitHub repository to Cloud Shell.

    Open in Cloud Shell

    A prompt is displayed to confirm downloading the GitHub repository to Cloud Shell.

  2. Click Confirm.

    Cloud Shell is launched in a separate browser tab, and the Terraform code is downloaded to the $HOME/cloudshell_open directory of your Cloud Shell environment.

  3. In Cloud Shell, check whether the current working directory is $HOME/cloudshell_open/terraform-google-load-balanced-vms/. This is the directory that contains the Terraform configuration files for the solution. If you need to change to that directory, run the following command:

    cd $HOME/cloudshell_open/terraform-google-load-balanced-vms/
    
  4. Initialize Terraform by running the following command:

    terraform init
    

    Wait until you see the following message:

    Terraform has been successfully initialized!
    

Configure the Terraform variables

The Terraform code that you downloaded includes variables that you can use to customize the deployment based on your requirements. For example, you can specify the Google Cloud project and the region where you want the solution to be deployed.

  1. Make sure that the current working directory is $HOME/cloudshell_open/terraform-google-load-balanced-vms/. If it isn't, go to that directory.

  2. In the same directory, create a text file named terraform.tfvars.

  3. In the terraform.tfvars file, copy the following code snippet, and set values for the required variables.

    • Follow the instructions that are provided as comments in the code snippet.
    • This code snippet includes only the variables for which you must set values. The Terraform configuration includes other variables that have default values. To review all the variables and the default values, see the variables.tf file that's available in the $HOME/cloudshell_open/terraform-google-load-balanced-vms/ directory.
    • Make sure that each value that you set in the terraform.tfvars file matches the variable type as declared in the variables.tf file. For example, if the type that’s defined for a variable in the variables.tf file is bool, then you must specify true or false as the value of that variable in the terraform.tfvars file.
    # This is an example of the terraform.tfvars file.
    # The values that you set in this file must match the variable types, as declared in variables.tf.
    # The values in this file override any defaults in variables.tf.
    
    # ID of the project in which you want to deploy the solution
    project_id = "PROJECT_ID"
    
    # Google Cloud region where you want to deploy the solution
    # Example: us-central1
    region = "REGION"
    
    # Google Cloud zone where you want to deploy the solution
    # Example: us-central1-a
    zone = "ZONE"
    
    # The number of Cloud Compute nodes you want to deploy (minimum of 2)
    # Example: 2
    nodes = "NODES"
    
    # The name of this particular deployment, will get added as a prefix to most resources
    # Example: load-balanced-vms
    deployment_name = "DEPLOYMENT_NAME"
    
    # The following variables have default values. You can set your own values or remove them to accept the defaults
    
    # A set of key/value label pairs to assign to the resources that are deployed by this solution
    # Example: {"team"="monitoring", "environment"="test"}
    labels = {"KEY1"="VALUE1",..."KEYn"="VALUEn"}
    
    # Whether to enable underlying APIs
    # Example: true
    enable_apis = "ENABLE_APIS"
    
    # If you want to deploy to an existing network, enter your network details in the following variables:
    
    # VPC network to deploy VMs in. A VPC will be created if not specified
    network_id = "NETWORK_ID"
    
    # Subnetwork to deploy VMs in. A Subnetwork will be created if not specified
    subnet_self_link = "SUBNET_SELF_LINK"
    
    #Shared VPC host project ID, if a Shared VPC is provided via network_id
    network_project_id = "NETWORK_PROJECT_ID"
    

For information about the values that you can assign to the required variables, see the following:

  • project_id, region, and zone are required. For information about the values that you can use for these variables, see the following:
  • The other variables have default values. You might change some of them (for example, deployment_name and labels).

Validate and review the Terraform configuration

  1. Make sure that the current working directory is $HOME/cloudshell_open/terraform-google-load-balanced-vms/. If it isn't, go to that directory.

  2. Verify that the Terraform configuration has no errors:

    terraform validate
    

    If the command returns any errors, make the required corrections in the configuration and then run the terraform validate command again. Repeat this step until the command returns the following message:

    Success! The configuration is valid.
    
  3. Review the resources that are defined in the configuration:

    terraform plan
    
  4. If you didn't create the terraform.tfvars file as described earlier, Terraform prompts you to enter values for the variables that don't have default values. Enter the required values.

    The output of the terraform plan command is a list of the resources that Terraform provisions when you apply the configuration.

    If you want to make any changes, edit the configuration and then run the terraform validate and terraform plan commands again.

Provision the resources

When no further changes are necessary in the Terraform configuration, deploy the resources.

  1. Make sure that the current working directory is $HOME/cloudshell_open/terraform-google-load-balanced-vms/. If it isn't, go to that directory.

  2. Apply the Terraform configuration:

    terraform apply
    
  3. If you didn't create the terraform.tfvars file as described earlier, Terraform prompts you to enter values for the variables that don't have default values. Enter the required values.

    Terraform displays a list of the resources that will be created.

  4. When you're prompted to perform the actions, enter yes.

    Terraform displays messages showing the progress of the deployment.

    If the deployment can't be completed, Terraform displays the errors that caused the failure. Review the error messages and update the configuration to fix the errors. Then run the terraform apply command again. For help with troubleshooting Terraform errors, see Errors when deploying the solution using the Terraform CLI.

    After all the resources are created, Terraform displays the following message:

    Apply complete!
    

    The following additional output is displayed:

    Outputs:
    console_page_for_load_balancer = "https://console.cloud.google.com/net-services/loadbalancing/details/http/<DEPLOYMENT_NAME>-lb-url-map?project=<PROJECT_ID>"
    load_balancer_endpoint = "<VALUE>"
    
  5. To view the Google Cloud resources that are deployed and their configuration, take an interactive tour.

    Start the tour

When you no longer need the solution, you can delete the deployment to avoid continued billing for the Google Cloud resources. For more information, see Delete the deployment.

Design recommendations

This section provides recommendations for using the load balanced managed VMs solution to develop an architecture that meets your requirements for security, reliability, cost, and performance.

For a high level overview of best practices, see Patterns for scalable and resilient apps .

Security

Implement the recommendations in the following guides to help secure your architecture:

For example, your architecture might have the following requirements:

  • You might require security features that are only available on a specific operating system. For more information, see Operating system details

  • You might need to fine-tune subnet details in a custom network. For more information about creating networks, see Create and manage VPC networks

Reliability

Use the following guidelines to create reliable services:

For example, you might fine-tune your VM health check details to ensure that timing is in line with your organization's commitments to customers. For more information about configuring health checks, see Set up an application health check and autohealing .

Performance

Optimize performance by adhering to the following best practices:

For example, the application that you deploy might require specific hardware requirements. For more information about configuring disk, memory, and CPU details on Compute Engine, see Machine families resource and comparison guide .

Cost

Use the following best practices to optimize the cost of your workflows: