Deploy Master-Standby Redis Instance
Application Scenario
Distributed Cache Service (DCS) is a high-performance, highly available in-memory database service provided by Huawei Cloud, supporting mainstream cache engines such as Redis and Memcached. DCS service provides multiple instance specifications and deployment modes, including single-node, master-standby, and cluster, meeting cache requirements for different scales and scenarios.
Master-standby Redis instance is the high availability deployment mode in DCS service, implementing data redundancy and automatic failover through master-standby architecture, ensuring high availability of cache services. Master-standby instances support automatic fault detection and failover. When the master node fails, the standby node will automatically take over the service, ensuring business continuity. This best practice will introduce how to use Terraform to automatically deploy DCS master-standby Redis instances, including VPC creation, instance configuration, backup policy, and whitelist management.
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. Create VPC
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_name
cidr: VPC CIDR block, assigned by referencing the input variable vpc_cidr
3. 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 VPC resource (huaweicloud_vpc.test) ID
name: Subnet name, assigned by referencing the input variable subnet_name
cidr: Subnet CIDR block, prioritizes using input variable, if empty then calculated using cidrsubnet function
gateway_ip: Gateway IP, prioritizes using input variable, if empty then calculated using cidrhost function
4. 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 query results are used to create DCS instances:
Parameter Description:
count: Data source creation count, used to control whether to execute the availability zones list query data source, only creates data source when
var.availability_zoneslength is less than 1
5. Query DCS 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 query results are used to create DCS instances:
Parameter Description:
count: Data source creation count, used to control whether to execute the DCS flavors list query data source, only creates data source when
var.instance_flavor_idis emptycache_mode: Cache mode, assigned by referencing the input variable instance_cache_mode, set to "ha" for master-standby mode
capacity: Capacity, assigned by referencing the input variable instance_capacity
engine_version: Engine version, assigned by referencing the input variable instance_engine_version
6. Create DCS Master-Standby Redis Instance
Add the following script to the TF file (e.g., main.tf) to instruct Terraform to create a DCS instance resource:
Parameter Description:
name: Instance name, assigned by referencing the input variable instance_name
engine: Engine type, set to "Redis"
enterprise_project_id: Enterprise project ID, assigned by referencing the input variable enterprise_project_id
engine_version: Engine version, assigned by referencing the input variable instance_engine_version
capacity: Capacity, assigned by referencing the input variable instance_capacity
flavor: Flavor, prioritizes using input variable, if empty then uses the first result from DCS flavors list query data source
availability_zones: Availability zones list, prioritizes using input variable, if empty then uses the first two results from availability zones list query data source
vpc_id: VPC ID, assigned by referencing the VPC resource (huaweicloud_vpc.test) ID
subnet_id: Subnet ID, assigned by referencing the VPC subnet resource (huaweicloud_vpc_subnet.test) ID
password: Password, assigned by referencing the input variable instance_password
backup_policy.backup_type: Backup type, assigned by referencing the backup_type field in backup policy
backup_policy.save_days: Save days, assigned by referencing the save_days field in backup policy
backup_policy.period_type: Period type, assigned by referencing the period_type field in backup policy
backup_policy.backup_at: Backup time, assigned by referencing the backup_at field in backup policy
backup_policy.begin_at: Begin time, assigned by referencing the begin_at field in backup policy
whitelists.group_name: Whitelist group name, assigned by referencing the group_name field in whitelist list
whitelists.ip_address: IP address list, assigned by referencing the ip_address field in whitelist list
parameters.id: Parameter ID, assigned by referencing the id field in parameter list
parameters.name: Parameter name, assigned by referencing the name field in parameter list
parameters.value: Parameter value, assigned by referencing the value field in parameter list
tags: Tags, assigned by referencing the input variable instance_tags
rename_commands: Rename commands, assigned by referencing the input variable instance_rename_commands
charging_mode: Charging mode, assigned by referencing the input variable charging_mode
period_unit: Charging period unit, assigned by referencing the input variable period_unit
period: Charging period, assigned by referencing the input variable period
auto_renew: Auto-renewal, assigned by referencing the input variable auto_renew
lifecycle.ignore_changes: Lifecycle ignore changes, ignores changes to flavor and availability_zones
7. 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="instance_name=my-redis"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.
8. 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 the DCS master-standby Redis instanceRun
terraform showto view the details of the created DCS master-standby Redis instance
Reference Information
Last updated