Associate instances with a cloud database
Instances in a scaling group, such as ECS and ECI (Elastic Container Instance) instances, are ephemeral and can be created or terminated at any time. For data persistence, store your data in a separate cloud database. This topic describes how to add instances from a scaling group to a cloud database's whitelist, enabling them to access the database.
Association methods
|
Method |
Instance types |
Cloud database types |
|
(Recommended) Method 1: Add instances and a cloud database to the same security group |
ECS, ECI |
|
|
ECS, ECI |
|
|
|
ECS |
|
(Recommended) Method 1: Use the same security group
By placing your instances and your cloud database in the same security group, you automatically add the IP addresses of all instances in the group to the database's whitelist. This allows all instances (ECS or ECI) in the security group to directly access the cloud database.
Configure the instance security group
-
The scaling configuration specifies the security group for instances in a scaling group. You can configure the security group when you create or modify a scaling configuration. For more information, see Create a scaling configuration (ECS instances) and Create a scaling configuration (ECI instances).
NoteIf you use a launch template for instance configurations, modify the security group in the launch template.
-
For existing instances in a scaling group, you can change the security group for ECS instances on the Security Group tab of the instance details page. You cannot change the security group for ECI instances. You must recreate the ECI instances.
Configure the database security group
Configure the cloud database to use the same security group that you configured for your instances.
|
Database type |
References |
|
RDS |
|
|
PolarDB |
|
|
Redis |
Add the public and private IP addresses of instances in batches by using a security group |
|
MongoDB |
Method 2: Associate a database with a scaling group
When you associate a cloud database with a scaling group, Auto Scaling automatically adds the IP addresses of instances in the group to the database's whitelist. This allows instances in the scaling group, including ECS and ECI instances, to directly access the cloud database.
Existing scaling group
Log on to the Auto Scaling console.
In the top navigation bar, select the region where Auto Scaling is activated.
In the left-side navigation pane, click Scaling Groups.
-
On the Scaling Groups page, find the scaling group that you want to manage and click Edit in the Actions column. The Edit Scaling Group dialog box appears.
-
In the Edit Scaling Group dialog box, find , and click Add Database. Configure the required parameters.
In the Database Information section, select the Type (RDS, Redis, or MongoDB), Database, and Association Mode. To create a new database instance, click the Create RDS Database, Create Redis Database, or Create MongoDB Database link at the bottom of the section.
-
Click OK to complete the association.
New scaling group
When you create a scaling group, in the section, specify the database that you want to associate.
In the Database Information section, set the Type, Database, and Association Mode for the database. You must select a Virtual Private Cloud (VPC) before you can select a database instance. You can click Add Database to add another entry, or click Delete to remove an existing entry.
For more information about how to create a scaling group, see Create an ECS scaling group and Create an ECI scaling group.
Method 3: Use a lifecycle hook and an OOS template
You can use a lifecycle hook to pause new ECS instances in a pending state upon launch. While pending, a predefined OOS (Operation Orchestration Service) template automatically runs to add the instances to a cloud database's whitelist. This process allows the ECS instances to directly access the cloud database. For more information, see the following topics:
-
Automatically add ECS instances to and remove them from the whitelist of a PolarDB cluster
-
Automatically add ECS instances to and remove them from the whitelist of a Redis instance
-
Automatically add ECS instances to and remove them from the whitelist of a MongoDB instance
-
Automatically add ECS instances to and remove them from the whitelist of an AnalyticDB cluster