Deploy MySQL Read Replica Instance
Application Scenario
Relational Database Service (RDS) is a highly available, high-performance, and easily scalable relational database cloud service provided by Huawei Cloud, supporting multiple database engines such as MySQL, PostgreSQL, SQL Server, MariaDB, etc. RDS provides automatic backup, monitoring alerts, elastic scaling, read-write separation, and other functions, meeting the database requirements of enterprise applications.
Read replica instances are a special instance type provided by RDS for implementing read-write separation and load sharing. By creating read replica instances, read requests from the primary instance can be distributed to read-only instances, improving the overall performance of the database. Read replica instances maintain data synchronization with the primary instance, supporting real-time reading of data changes from the primary instance.
This best practice will introduce how to use Terraform to automatically deploy an RDS MySQL primary-standby instance and corresponding read replica instance, implementing high availability and read-write separation functions for the database.
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. Query Availability Zone Information Through Data Source
Add the following script to the TF file (e.g., main.tf) to instruct Terraform to perform a data source query, the results of which are used to create VPC subnets and RDS instances:
Parameter Description:
count: Data source creation count, used to control whether to execute the availability zone list query data source, only creates the data source when
var.availability_zonesis empty (i.e., execute availability zone list query)
3. Query RDS Flavor Information Through Data Source
Add the following script to the TF file (e.g., main.tf) to instruct Terraform to perform a data source query, the results of which are used to create RDS instances and read replica instances:
Parameter Description:
count: Data source creation count, used to control whether to execute the RDS flavor list query data source, creates corresponding number of data sources based on the length of
var.instance_flavors_filterdb_type: Database type, assigned by referencing the
db_typein the input variableinstance_flavors_filterdb_version: Database version, assigned by referencing the
db_versionin the input variableinstance_flavors_filterinstance_mode: Instance mode, assigned by referencing the
instance_modein the input variableinstance_flavors_filtergroup_type: Flavor group type, assigned by referencing the
group_typein the input variableinstance_flavors_filtervcpus: Number of CPU cores, assigned by referencing the
vcpusin the input variableinstance_flavors_filtermemory: Memory size, assigned by referencing the
memoryin the input variableinstance_flavors_filteravailability_zone: Availability zone, assigned based on the return results of the availability zone list query data source (data.huaweicloud_availability_zones)
4. Create VPC Network
Add the following script to the TF file (e.g., main.tf) to instruct Terraform to create a VPC resource:
Parameter Description:
name: VPC name, assigned by referencing the input variable
vpc_namecidr: VPC CIDR block, assigned by referencing the input variable
vpc_cidr
5. Create VPC Subnet
Add the following script to the TF file (e.g., main.tf) to instruct Terraform to create a VPC subnet resource:
Parameter Description:
vpc_id: VPC ID, assigned by referencing the ID of the VPC resource (huaweicloud_vpc)
name: Subnet name, assigned by referencing the input variable
subnet_namecidr: Subnet CIDR block, assigned by referencing the input variable
subnet_cidr, automatically calculated if emptygateway_ip: Subnet gateway IP address, assigned by referencing the input variable
gateway_ip, automatically calculated if emptyavailability_zone: Availability zone where the subnet is located, assigned based on the return results of the availability zone list query data source (data.huaweicloud_availability_zones)
6. Create Security Group
Add the following script to the TF file (e.g., main.tf) to instruct Terraform to create a security group resource:
Parameter Description:
name: Security group name, assigned by referencing the input variable
security_group_namedelete_default_rules: Whether to delete default rules, set to true to delete default security group rules
7. Create Security Group Rules
Add the following script to the TF file (e.g., main.tf) to instruct Terraform to create security group rule resources:
Parameter Description:
security_group_id: Security group ID, assigned by referencing the ID of the security group resource (huaweicloud_networking_secgroup)
direction: Rule direction, set to "ingress" for inbound rules
ethertype: Ethernet type, set to "IPv4"
remote_ip_prefix: Remote IP prefix, assigned by referencing the input variable
vpc_cidrports: Port number, assigned by referencing the input variable
instance_db_portprotocol: Protocol type, set to "tcp"
8. Create Random Password
Add the following script to the TF file (e.g., main.tf) to instruct Terraform to create a random password resource:
Parameter Description:
count: Resource creation count, used to control whether to execute random password resource creation, only creates the resource when
var.instance_passwordis emptylength: Password length, set to 12 characters
special: Whether to include special characters, set to true
override_special: Special character set, set to "!@%^*-_=+"
9. Create RDS Primary-Standby Instance
Add the following script to the TF file (e.g., main.tf) to instruct Terraform to create an RDS primary-standby instance resource:
Parameter Description:
name: RDS instance name, assigned by referencing the input variable
instance_nameflavor: RDS instance flavor, assigned by referencing the input variable
instance_flavor_id, if empty then assigned based on the return results of the RDS flavor list query data source (data.huaweicloud_rds_flavors)vpc_id: VPC ID, assigned by referencing the ID of the VPC resource (huaweicloud_vpc)
subnet_id: Subnet ID, assigned by referencing the ID of the VPC subnet resource (huaweicloud_vpc_subnet)
security_group_id: Security group ID, assigned by referencing the ID of the security group resource (huaweicloud_networking_secgroup)
availability_zone: Availability zone list, assigned based on the return results of the availability zone list query data source (data.huaweicloud_availability_zones)
ha_replication_mode: HA replication mode, assigned by referencing the input variable
ha_replication_modedb: Database configuration block, includes database type, version, port, and password
volume: Storage volume configuration block, includes storage type and size
backup_strategy: Backup strategy configuration block, includes backup time window and retention days
lifecycle: Lifecycle configuration block, used to ignore changes to specific parameters
10. Create RDS Read Replica Instance
Add the following script to the TF file (e.g., main.tf) to instruct Terraform to create an RDS read replica instance resource:
Parameter Description:
primary_instance_id: Primary instance ID, assigned by referencing the ID of the RDS instance resource (huaweicloud_rds_instance)
name: Read replica instance name, assigned by referencing the input variable
replica_instance_nameflavor: Read replica instance flavor, assigned by referencing the input variable
replica_instance_flavor_id, if empty then assigned based on the return results of the RDS flavor list query data source (data.huaweicloud_rds_flavors)availability_zone: Availability zone where the read replica instance is located, assigned based on the return results of the availability zone list query data source (data.huaweicloud_availability_zones)
volume: Storage volume configuration block, includes storage type and size
lifecycle: Lifecycle configuration block, used to ignore changes to specific parameters
11. 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.
12. 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 RDS MySQL primary-standby instances and read replica instancesRun
terraform showto view the created RDS MySQL primary-standby instances and read replica instances
Reference Information
Last updated