This topic describes how to use a lifecycle hook to put ECS instances into the wait state and then use an Operation Orchestration Service (OOS) template to automatically add or remove the instances to or from the whitelist of a PolarDB cluster.

Prerequisites

  • An Alibaba Cloud account is created. To create an Alibaba Cloud account, go to the account registration page.
  • A scaling group is created and enabled.
  • A PolarDB cluster is created.
  • A RAM role is created for OOS. For more information, see Grant RAM permission for OOS.
    Note The OOSServiceRole RAM role is used in this example. You can also use other custom RAM roles, but you must make sure that the used RAM role has the permissions required to execute OOS templates.

Background information

A scaling group can be associated with Server Load Balancer (SLB) or ApsaraDB for RDS (RDS) instances, but cannot be associated with PolarDB clusters. If your business data is stored on a PolarDB cluster, you must manually add or remove your ECS instances to or from the whitelist of the PolarDB cluster. This is time-consuming and inefficient. You can use lifecycle hooks and OOS templates to automatically add or remove ECS instances to or from the whitelist of a PolarDB cluster.

Procedure

The following example uses the ACS-ESS-LifeCycleModifyPolarDBIPWhitelist public template to demonstrate how to add ECS instances to the whitelist of a PolarDB cluster during scale-out events. Perform the following steps:
Note If you want to remove ECS instances from the whitelist of a PolarDB cluster during scale-in events, you can create lifecycle hooks that are applicable to scale-in events and then trigger the scale-in events.

Step 1: Grant OOS permissions to the RAM user

You must be granted the permissions required to execute OOS templates. Resources of ECS, Auto Scaling, and PolarDB are involved when the O&M operations specified in the ACS-ESS-LifeCycleModifyPolarDBIPWhitelist template are performed.

  1. Log on to the RAM console.
  2. Create a policy.
    1. In the left-side navigation pane, choose Permissions > Policies.
    2. In the upper-left corner of the Policies page, click Create Policy.
    3. On the Create Custom Policy page, configure parameters for the policy and click OK.
      The following table describes the parameters used in this example. Use the default values for parameters that are not mentioned in the table.
      Parameter Description
      Policy Name Enter ESSHookPolicyForPolarDBWhitelist.
      Configuration Mode Select Script.
      Policy Document Enter the following content:
      {
          "Version": "1",
          "Statement": [
              {
                  "Action": [
                      "ecs:DescribeInstances"
                  ],
                  "Resource": "*",
                  "Effect": "Allow"
              },
              {
                  "Action": [
                      "polardb:ModifyDBClusterAccessWhitelist"
                  ],
                  "Resource": "*",
                  "Effect": "Allow"
              },
              {
                  "Action": [
                      "ess:CompleteLifecycleAction"
                  ],
                  "Resource": "*",
                  "Effect": "Allow"
              }
          ]
      }
  3. Attach the policy to the OOSServiceRole RAM role.
    1. In the left-side navigation pane, click RAM Roles.
    2. Find the OOSServiceRole RAM role and click Add Permissions in the Actions column.
      Attach the policy to the OOSServiceRole RAM role assumed by OOS to complete the authorization.
    3. In the Add Permissions panel, configure the parameters and click OK.
      The following table describes the parameters used in this example. Use the default values for parameters that are not mentioned in the table.
      Parameter Description
      Authorization Select Alibaba Cloud account all resources.
      Select Policy Select Custom Policy and then the ESSHookPolicyForPolarDBWhitelist policy.

Step 2: Create a lifecycle hook for scale-out events and trigger a scale-out event

  1. Log on to the Auto Scaling console.
  2. In the left-side navigation pane, click Scaling Groups.
  3. In the top navigation bar, select a region.
  4. Find the scaling group and use one of the following methods to open the details page of the scaling group:
    • Click the ID of the scaling group in the Scaling Group Name/ID column.
    • In the Actions column corresponding to the scaling group, click Details.
  5. Create a lifecycle hook for scale-out events.
    1. In the left-side navigation pane, click Lifecycle Hooks.
    2. In the upper-left corner of the Lifecycle Hooks page, click Create Lifecycle Hook.
    3. Configure parameters for the lifecycle hook and click OK.
      The following table describes the parameters used in this example. Use the default values for parameters that are not mentioned in the table.
      Parameter Description
      Name Enter ESSHookForAddPolarDBWhitelist.
      Applicable Scaling Activity Type Select Scale-out Event.
      Timeout Period Enter a proper value, such as 300.
      Note The timeout period is the period of time during which customized operations are performed. If the period is short, the operations may fail to be performed. Evaluate and set a proper timeout period.
      Execution Policy Select Continue.
      Notification Method Configure the following settings:
      • Notification method: Select OOS Template.
      • OOS template type: Select Public Templates.
      • Public template: Select ACS-ESS-LifeCycleModifyPolarDBIPWhitelist from the drop-down list.
      The following section describes the parameters for ACS-ESS-LifeCycleModifyPolarDBIPWhitelist:
      • dbClusterId: Enter the PolarDB cluster ID.
      • modifyMode: Select Append. This value applies to scale-out events and allows ECS instances to be added to the whitelist of the PolarDB cluster.
      • Permissions: Select OOSServiceRole. In Step 1, the OOSServiceRole RAM role is granted permissions of ECS, Auto Scaling, and PolarDB. OOS owns the preceding permissions after it assumes the RAM role.
  6. Trigger a scale-out event.
    In this example, a scale-out event is manually triggered by executing a scaling rule. You can also trigger scale-out events by using scheduled or event-triggered tasks.
    Note Lifecycle hooks take effect when scaling activities are manually triggered by executing scaling rules. Lifecycle hooks do not take effect when you manually add or remove ECS instances to or from a scaling group.
    1. In the left-side navigation pane, click Scaling Rules.
    2. In the upper-left corner of the Scaling Rules page, click Create Scaling Rule.
    3. In the Create Scaling Rule dialog box, configure the parameters and click OK.
      The following table describes the parameters used in this example. Use the default values for parameters that are not mentioned in the table.
      Parameter Description
      Rule Name Enter Add1.
      Rule Type Select Simple Scaling Rule.
      Operation Set this parameter to Add 1 Instances.
    4. On the Scaling Rules page, find the created Add1 scaling rule and click Execute in the Actions column.
    5. In the Execute Scaling Rule message, click OK.
    After the scaling rule is executed, an ECS instance is automatically created. The ESSHookForAddPolarDBWhitelist lifecycle hook in the scaling group puts the ECS instance into the wait state. Auto Scaling automatically notifies OOS to perform the O&M operations specified in the ACS-ESS-LifeCycleModifyPolarDBIPWhitelist template on the ECS instance.

Step 3: View the whitelist of the PolarDB cluster

  1. Log on to the PolarDB console.
  2. In the left-side navigation pane, click Clusters.
  3. Find the PolarDB cluster and click its ID in the Cluster ID/Name column.
  4. In the left-side navigation pane, choose Settings and Management > Whitelists.
    The following figure shows that the private IP address of the ECS instance is added to the whitelist of the PolarDB cluster as specified in the ACS-ESS-LifeCycleModifyPolarDBIPWhitelist template.whitelist-polardb

    If the ECS instance is created, but its private IP address is not added to the whitelist of the PolarDB cluster, go to the OOS console to view the execution result of O&M tasks. For more information, see Step 4 (optional): View the OOS execution status.

Step 4 (optional): View the OOS execution status

  1. Log on to the OOS console.
  2. In the left-side navigation pane, click Executions.
  3. Find the execution by time and click Details in the Actions column.
  4. On the page that appears, click the Advanced View tab.
    The execution status is displayed on the Execution Result tab.成功
    If the execution fails, the error message is also displayed on the Execution Result tab.报错信息

FAQ

If an O&M task fails to be executed, find the cause based on the error message in the execution result. The following section describes the common error messages and their solutions:
  • Error message: Forbidden.Unauthorized message: A required authorization for the specified action is not supplied.

    Solution: Check whether you have granted the required permissions to the OOSServiceRole RAM role, such as the sample permissions in Step 1. You must grant required permissions to the OOSServiceRole RAM role to make sure that OOS can manage the resources involved in OOS templates.

  • Error message: Forbidden.RAM message: User not authorized to operate on the specified resource, or this API doesn't support RAM.

    Solution: Check whether you have granted the required permissions to the OOSServiceRole RAM role, such as the sample permissions in Step 1. You must grant required permissions to the OOSServiceRole RAM role to make sure that OOS can manage the resources involved in OOS templates.

  • Error message: LifecycleHookIdAndLifecycleActionToken.Invalid message: The specified lifecycleActionToken and lifecycleActionId you provided does not match any in process lifecycle action.

    Solution: Estimate the timeout period of the lifecycle hook to make sure that the O&M task specified in the OOS template can be complete within the timeout period.