Deploy VIP and Associate Instance
Application Scenario
Virtual IP (VIP) is a network technology in Huawei Cloud VPC used to achieve high availability, which can bind a virtual IP address to multiple ECS instances, implementing load balancing and failover. Through VIP association, high availability deployment of business can be achieved. When an ECS instance fails, traffic can automatically switch to other healthy instances. This best practice will introduce how to use Terraform to automatically deploy VIP associations, including creating ECS instances, VIP resources, and their association configurations.
Related Resources/Data Sources
This best practice involves the following main resources and data sources:
Data Sources
Resources
Resource/Data Source Dependencies
Operation Steps
1. Script Preparation
Prepare the TF file (e.g., main.tf) in the specified workspace for writing the current best practice script, ensuring that it (or other TF files in the same directory) contains the provider version declaration and Huawei Cloud authentication information required for deploying resources. Refer to the "Preparation Before Deploying Huawei Cloud Resources" document for configuration introduction.
2. Prerequisite Resource Preparation
This best practice requires creating prerequisite resources such as VPC, subnets, security groups, and ECS instances first. Please follow the following steps in the "Deploy Basic Elastic Cloud Server" best practice for preparation:
Step 2: Query availability zones required for ECS instance resource creation through data sources
Step 3: Query flavors required for ECS instance resource creation through data sources
Step 4: Query images required for ECS instance resource creation through data sources
Step 5: Create VPC resource
Step 6: Create VPC subnet resource
Step 7: Create security group resource
Step 8: Create ECS instance
After completing the above steps, continue with the subsequent steps of this best practice.
3. Create VIP Resource
Add the following script to the TF file to instruct Terraform to create a VIP resource:
Parameter Description:
network_id: Network ID that the VIP belongs to, referencing the ID of the previously created subnet resource
4. Create VIP Association Resource
Add the following script to the TF file to instruct Terraform to create a VIP association resource:
Parameter Description:
vip_id: VIP ID, referencing the ID of the previously created VIP resource
port_ids: List of port IDs to associate, using try function to get the network port ID of the ECS instance, using empty list if retrieval fails
5. Preset Input Parameters Required for Resource Deployment (Optional)
In this practice, some resources and data sources use input variables to assign values to configuration content. These input parameters need to be manually entered during subsequent deployments. At the same time, Terraform provides a method to preset these configurations through .tfvars files, which can avoid repeated input during each execution.
Create a terraform.tfvars file in the working directory with the following example content:
Usage:
Save the above content as
terraform.tfvarsfile in the working directory (this file name allows users to automatically import the content of thistfvarsfile when executing terraform commands; for other names,.autoneeds to be added before tfvars, such asvariables.auto.tfvars)Modify parameter values as needed
When executing
terraform planorterraform apply, Terraform will automatically read the variable values from this file
In addition to using terraform.tfvars file, variable values can also be set in the following ways:
Command line parameters:
terraform apply -var="vpc_name=my-vpc" -var="subnet_name=my-subnet"Environment variables:
export TF_VAR_vpc_name=my-vpcCustom named variable files:
terraform apply -var-file="custom.tfvars"
Note: If the same variable is set in multiple ways, Terraform will use the variable value according to the following priority: command line parameters > variable files > environment variables > default values.
6. Initialize and Apply Terraform Configuration
After completing the above script configuration, execute the following steps to create resources:
Run
terraform initto initialize the environmentRun
terraform planto view the resource creation planAfter confirming the resource plan is correct, run
terraform applyto start creating VIP associationsRun
terraform showto view the created VIP association details
Reference Information
Last updated