Skip to content

Latest commit

 

History

History

1-ilb-active-active

Deploying Active-active load balancing pattern

This document provides instructions on how to deploy the example implementation of the Active-active load balancing pattern as part of Patterns for using floating IP addresses in Compute Engine using Terraform.

This pattern deploys two nginx webservers using a floating IP address. When you request the document root (/) from the floating IP address (the IP address of the internal TCP/UDP load balancer) you receive a response that identifies the first or second web server.

The following diagram shows the architecture that you deploy. It consists of two Compute Engine instances in a single instance group behind an internal TCP/UDP load balancer that distributes traffic equally between both instances.

Architecture for Active-active load-balancing pattern

Objectives

Provision the following resources in Google Cloud by using a Terraform template:

  • One VPC network and subnetwork that connects all resources
  • Two Compute Engine instances running nginx
  • A Compute Engine instance used as an internal client
  • A set of firewall rules to allow the client VM to reach the nginx instances using HTTP and to allow connecting by using SSH through IAP
  • An internal TCP load balancer distributing the traffic to the two nginx instances

Costs

This tutorial uses billable components of Google Cloud:

To generate a cost estimate based on your projected usage, use the pricing calculator.

When you finish this tutorial, you can avoid continued billing by deleting the resources you created. For more information, see Cleaning up.

Before you begin

  1. In the Google Cloud Console, on the project selector page, select or create a Google Cloud project.

  2. Make sure that billing is enabled for your Cloud project. Learn how to confirm that billing is enabled for your project.

Prepare your environment

You can complete this tutorial using Cloud Shell or your local host. Cloud Shell has Terraform pre-installed and set up to authenticate with Google Cloud.

  1. (Optional) Activate Cloud Shell.

  2. If you don't use Cloud Shell, install the required components unless they are already installed on your local host:

  3. Authenticate to Google Cloud by running gcloud auth application-default login. Alternatively use a service account as described in the Terraform Google provider documentation.

  4. If not already done, clone this repository to your local host or Cloud Shell by running git clone https://github.com/GoogleCloudPlatform/solutions-floating-ip-patterns-terraform.git.

Configuring 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 adjust the subnet CIDR ranges and specify the project where the resources should be deployed.

You can see the variables of this example in the variables.tf file or in the following table.

  1. In the code that you downloaded, enter the 1-ilb-active-active subdirectory: cd floating-ip-patterns/1-ilb-active-active.

  2. Identify the variables for which you need to assign values:

    • Variables that don't have a default value (for example, project_id).

    • Variables with a default value that you want to change.

      For example, region and zone are set to deploy all resources in the us-central1-c zone by default, but you can deploy in a region of your choice.

  3. Create a text file named terraform.tfvars.

    Terraform treats any file with this extension as a variable definitions file.

    If you don't create a terraform.tfvars or .tfvars.json file, Terraform uses the default values of the variables, if available. For variables that don't have a default value, Terraform prompts you to enter a value every time you run any Terraform command.

  4. In the terraform.tfvars file, assign appropriate values to the variables that you identified earlier.

    Example:

    region = "europe-west4"
    zone = "europe-west4-c"
    project_id = "my_project"
    

    The value that you assign to each variable must match the type of that variable as declared in variables.tf or the following table.

  5. Initialize Terraform:

    terraform init
    

    Wait until you see the following message:

    Terraform has been successfully initialized!
    
  6. Verify that the configuration has no errors:

    terraform validate
    

    If the command returns an error, make the required corrections in the configuration, and run terraform validate again.

    Repeat this step until the command returns the following message:

    Success! The configuration is valid.
    
  7. Review the resources defined in the configuration:

    terraform plan
    

    The output lists the resources that Terraform provisions when you apply the configuration.

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

Provisioning resources

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

  1. Run the following command:

    terraform apply
    

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

  2. At the prompt to perform the actions, enter yes.

    Terraform displays messages showing the progress of the deployment. After all the resources are created, Terraform displays the following message:

    Apply complete!
    

You have now deployed the example implementation for the active-active load balancing pattern.

Testing your deployment

  1. In your browser, go to the VM instances page for your project in the Google Cloud Console.

  2. In the list of virtual machine instances, click SSH in the row of the instance named client. A separate window opens that connects to the example client VM for this deployment.

  3. On the client VM, run:

    curl 10.100.3.1
    

    If you changed the floating_ip variable in your Terraform variables file, replace 10.100.3.1 with the floating IP address you have chosen.

    You should see This is server 1 or This is server 2 depending on which nginx instance received the request.

  4. If you run the curl command repeatedly you can see that requests are distributed between both nginx instances.

Optionally, to test a failure case:

  1. In the list of virtual machine instances, click SSH in the row of the instance named nginx-first.
  2. On the nginx-first VM, stop the nginx service by running:
    sudo service nginx stop
    
  3. After a few seconds, run the curl command from above repeatedly on the client instance and all requests should return This is server 2.

Adding, changing or removing resources

To add, change, or remove resources, edit the Terraform configuration, and then run the commands terraform validate, terraform plan, and terraform apply, in that order.

Cleaning up

To avoid incurring charges to your Google Cloud account for the resources you created in this tutorial, delete all the resources when you don't need them.

  1. Run the following command:

    terraform destroy
    

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

  2. At the prompt to perform the actions, enter yes.

    Terraform displays messages showing the progress. After all the resources are deleted, Terraform displays the following message:

    Destroy complete!
    

Variables

Name Description Type Default Required
floating_ip Floating IP address used by the load balancer string "10.100.3.1" no
project_id Google Cloud Project ID string n/a yes
region Google Cloud Region used to deploy resources string "us-central1" no
subnet_range IP address range used for the subnet string "10.100.0.0/16" no
zone Google Cloud Zone used to deploy resources string "us-central1-c" no