This document provides instructions on how to deploy the example implementation of the pattern using a heartbeat mechanism to switch a route's next hop as part of Patterns for using floating IP addresses in Compute Engine using Terraform.
This pattern deploys two nginx webservers utilizing 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 running nginx. It also contains a static VPC route with the floating IP address as a destination and the primary instance as a next hop. keepalived
is running on the nginx instances. When keepalived
on the secondary instance detects a failure of the primary instance, it calls a Cloud Function that updates the static VPC route to use the secondary instance as the next hop.
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, to allow VRRP for keepalived between the nginx instances and to allow connecting by using SSH through IAP
- A route using the floating IP address as destination and the primary nginx instance as next hop.
- A Cloud Function that dynamically updates the route and is called when
keepalived
notices a state change - A Cloud Storage bucket and an object containing the code for the function
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.
-
In the Google Cloud Console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Cloud project. Learn how to confirm that billing is enabled for your project.
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.
-
(Optional) Activate Cloud Shell.
-
If you don't use Cloud Shell, install the required components unless they are already installed on your local host:
-
Install Terraform version 0.15.0 or later.
-
-
Authenticate to Google Cloud by running
gcloud auth application-default login
. Alternatively use a service account as described in the Terraform Google provider documentation. -
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
.
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.
-
In the code that you downloaded, enter the
6-routes-keepalived
subdirectory:cd floating-ip-patterns/6-routes-keepalived
. -
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
andzone
are set to deploy all resources in theus-central1-c
zone by default, but you can deploy in a region of your choice.
-
-
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. -
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" vrrp_password = "mysecretpassword"
The value that you assign to each variable must match the type of that variable as declared in
variables.tf
or the following table. -
Initialize Terraform:
terraform init
Wait until you see the following message:
Terraform has been successfully initialized!
-
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.
-
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
andterraform plan
again.
When no further changes are necessary in the configuration, deploy the resources:
-
Run the following command:
terraform apply
Terraform displays a list of the resources that will be created.
-
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 the pattern that uses a heartbeat mechanism to switch a route's next hop.
-
In your browser, go to the VM instances page for your project in the Google Cloud Console.
-
In the list of virtual machine instances, click SSH in the row of the instance named
client
. A separate window is opened that connects to the example client VM for this deployment. -
On the client VM, run:
curl 10.200.1.1
If you changed the
floating_ip
variable in your Terraform variables file, replace10.200.1.1
with the floating IP address you have chosen.You should see
This is server 1
as the primary instance serves all requests. -
If you run the
curl
command repeatedly you can see that requests all requests are served by the primary instance.
Optionally, to test a failure case:
- In the list of virtual machine instances, click SSH in the row of the instance with the name
nginx-primary
- On the
nginx-primary
VM, stop thenginx
service by running:sudo service nginx stop
- After a few seconds, run the
curl
command from above repeatedly on theclient
instance and all requests should returnThis is server 2
. The static route now points to the secondary instance.
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.
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.
-
Run the following command:
terraform destroy
Terraform displays a list of the resources that will be destroyed.
-
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!
Name | Description | Type | Default | Required |
---|---|---|---|---|
bucket_location | Location to deploy Cloud Storage bucket containing Cloud Function | string |
"US" |
no |
floating_ip | Floating IP address | string |
"10.200.1.1" |
no |
network_name | VPC Network name | string |
"ip-failover" |
no |
primary_ip | IP address of the primary VM instance | string |
"10.100.2.1" |
no |
project_id | Google Cloud Project ID | string |
n/a | yes |
region | Google Cloud Region used to deploy resources | string |
"us-central1" |
no |
route_name | Route name used for the floating IP route | string |
"floating-ip-route" |
no |
secondary_ip | IP address of the backup VM instance | string |
"10.100.2.2" |
no |
subnet_name | VPC Network name | string |
"ip-failover-subnet" |
no |
subnet_range | IP address range used for the subnet | string |
"10.100.0.0/16" |
no |
vrrp_password | Password used for VRRP between instances | any |
n/a | yes |
zone | Google Cloud Zone used to deploy resources | string |
"us-central1-c" |
no |