This tutorial describes how to use an Auto Scaling lifecycle hook to suspend an ECS instance and a CloudOps Orchestration Service (OOS) template to automatically mount a NAS file system to a Linux ECS instance.
Prerequisites
A scaling group is created and enabled.
-
A NAS file system is created and a mount target is added. For more information, see Manage mount targets. The mount target must meet the following conditions:
-
If the scaling group uses a virtual private cloud (VPC), the mount target must also use a VPC. The mount target and the scaling group must be in the same VPC.
-
If the scaling group uses the classic network, the mount target must also use the classic network.
-
The mount protocol is NFS.
-
A RAM role is created for the OOS service. For this role, the trusted entity is set to Alibaba Cloud Service and the trusted service is set to CloudOps Orchestration Service. The role must have the permissions to execute OOS templates. For more information, see Set and grant a RAM role for OOS.
NoteThis topic uses OOSServiceRole as an example RAM role. You can also use a different custom RAM role.
Background information
A NAS file system is a file storage instance. After you mount the file system to an ECS instance, you can use the file storage just like a local file system. This improves data security and operational fault tolerance. You cannot associate a NAS file system when you create a scaling configuration. However, you can use a lifecycle hook and an OOS template to automatically mount a NAS file system to an ECS instance. This method is more efficient than creating an ECS instance and then manually mounting the file system.
Procedure
This tutorial uses the public OOS template ACS-ESS-LifeCycleAttachNASFileSystemToInstance as an example to show how to automatically mount a NAS file system to a Linux ECS instance during a scale-out event. The procedure is as follows:
Step 1: Grant OOS service permissions to the RAM role
You must have the required permissions to execute OOS templates. The operations and maintenance (O&M) operations defined in the ACS-ESS-LifeCycleAttachNASFileSystemToInstance template involve resources from Elastic Compute Service (ECS), Auto Scaling, and NAS.
Log on to the Resource Access Management (RAM) console.
-
Create an access policy.
In the navigation pane on the left, click .
Click Create Access Policy.
-
On the Create Policy page, select Script Editor as the configuration mode, set the other parameters, and then click OK.
The following table describes the parameters used in this tutorial. Keep the default values for other parameters.
Configuration item
Description
Name
Enter ESSHookPolicyForAttachNAS.
Policy Document
Enter the following content:
{ "Version": "1", "Statement": [ { "Action": [ "ecs:DescribeInvocations", "ecs:RunCommand", "ecs:DescribeInvocationResults", "ecs:DescribeInstances" ], "Resource": "*", "Effect": "Allow" }, { "Action": [ "ess:CompleteLifecycleAction" ], "Resource": "*", "Effect": "Allow" } ] }
-
Grant the access policy to OOSServiceRole.
In the navigation pane on the left, click .
Find OOSServiceRole and click Add Permissions in the Actions column.
Grant the required permissions to the OOSServiceRole RAM role.
-
On the Grant Permission page, specify the resource scope and permission configuration, and then click Confirm Add Authorization.
The following table describes the parameters used in this tutorial. Keep the default values for other parameters.
Configuration item
Description
Resource Scope
Select the Account level.
Policies
Add the custom policy ESSHookPolicyForAttachNAS.
Step 2: Create a lifecycle hook for scale-out activities and trigger a scale-out
Log on to the Auto Scaling console.
In the navigation pane on the left, click Scaling Groups.
In the top navigation bar, select a region.
Find the scaling group that you want to manage and open its details page in one of the following ways:
In the Scaling Group Name/ID column, click the ID of the scaling group.
In the Actions column, click Details.
-
Create a lifecycle hook for scale-out activities.
At the top of the page, click the Event and adhesive hook tab.
-
Click Create Lifecycle Hook.
-
Specify the lifecycle hook configuration and click OK.
The following table describes the parameters used in this tutorial. Keep the default values for other parameters.
Configuration item
Description
Name
Enter ESSHookForAttachNAS.
Scaling Activity Type
Select Scale-Out.
Timeout Period
Enter an appropriate timeout period, such as 300 seconds.
NoteThe timeout period is the time allowed for custom operations. If the timeout period is too short, the custom operations may fail. Evaluate the time required for your custom operations and set an appropriate timeout period.
Default Execution Policy
Select Continue.
Send Notification When Lifecycle Hook Takes Effect
Send notifications or automatically perform more operations in a specified way. The template settings are as follows:
-
Notification method: Select OOS Template.
-
OOS Template Type: Select Public Templates.
-
Public Template: Select ACS-ESS-LifeCycleAttachNASFileSystemToInstance.
The execution parameters for ACS-ESS-LifeCycleAttachNASFileSystemToInstance are configured as follows:
-
Mount Target Domain Name: Enter the mount target domain name of the NAS file system.
-
Mount Directory: The directory on the ECS instance where the file system is mounted, which defaults to /mnt.
-
File System Version: 0 indicates that the NFS v4 protocol is used to mount the file system, and 1 indicates that the NFS v3 protocol is used. In this example, 0 is used.
-
Rate Control: Two types of rate control are available: Concurrency Control and Batch Control. This tutorial uses the default type, Concurrency Control, as an example.
-
RAM Role For OOS: Select Specify RAM Role and Use Permissions Granted to This Role, and then select OOSServiceRole. In the Procedure, the OOSServiceRole RAM role is granted permissions to manage ECS, Auto Scaling, and NAS file system resources. The OOS service assumes this role to obtain the required permissions.
-
-
Trigger a scale-out.
This tutorial uses manual execution of a scaling rule as an example. You can also trigger a scale-out using a scheduled task or an event-triggered task.
NoteLifecycle hooks take effect when you manually execute a scaling rule to trigger a scaling activity. However, lifecycle hooks do not take effect when you manually add or remove ECS instances.
-
At the top of the page, click the Scaling Rules and Event-triggered Tasks tab.
-
On the Scaling Rules tab, click Create Scaling Rule.
-
Set the properties of the scaling rule, and then click OK.
The following table describes the parameters used in this tutorial. Keep the default values for other parameters.
Configuration item
Description
Rule Name
Enter Add1.
Rule Type
Select Simple Scaling Rule.
Operation
Set to add 1 instance.
-
In the list of scaling rules, find the newly created scaling rule Add1, and in the Actions column, click Recurrently.
-
Click OK.
After the scaling rule is executed, one ECS instance is automatically created. Because the ESSHookForAttachNAS lifecycle hook is created in the scaling group, the ECS instance is suspended. The OOS service is automatically notified to perform the O&M operations defined in the ACS-ESS-LifeCycleAttachNASFileSystemToInstance template.
-
-
Check if the automatically created ECS instance works as expected.
At the top of the page, click the Instance Management tab.
-
Find the automatically created ECS instance, and in the ECS Instance ID/Name column, click the instance ID.
-
In the Basic Information area, click Remote Connection.
-
After you log on to the ECS instance, run a command to check the mount result.
df -h | grep aliyunAs shown in the following figure, the result contains the mount path of the NAS file system. This indicates that the automatic mount is successful. You can also run
nfsstat -cto view the protocol version used.Client nfs v4indicates that the protocol version is v4.
If an ECS instance is created but the NAS file system is not mounted, you can go to the OOS console to view the task execution status. For more information, see (Optional) Step 3: View the OOS execution status.
(Optional) Step 3: View the OOS execution status
Log on to the OOS console.
In the navigation pane on the left, choose .
Find the execution based on the start time and click Details in the Actions column.
-
On the execution details page, view the execution information.
For example, you can view information such as the execution ID and execution status in the Basic Information section, or click a task node in the Execution Result section to view the execution details. For more information, see View task execution details.
NoteIf the execution fails, the related error message is also displayed on the execution details page.
FAQ
If an Operation and Maintenance (O&M) task fails, find the cause based on the error message in the execution result. For more information, see FAQ.
Common error messages are as follows:
Error message | Cause | Solution |
Forbidden.Unauthorized message: A required authorization for the specified action is not supplied. | Auto Scaling is not authorized to perform the specified action. | Verify that the required permissions are granted to the OOSServiceRole RAM role. |
Forbidden.RAM message: User not authorized to operate on the specified resource, or this API doesn't support RAM. | The RAM user or RAM role does not have the permissions to operate on the corresponding resource. | Verify that the OOSServiceRole RAM role has the required permissions. For example, you can grant the sample permissions for the OOS service to the RAM role. You must add operation permissions to the RAM role to ensure that the OOS service can manage the resources specified in the OOS template. |
LifecycleHookIdAndLifecycleActionToken.Invalid message: The specified lifecycleActionToken and lifecycleActionId you provided does not match any in process lifecycle action. | The ongoing lifecycle action has expired or has been aborted. | Evaluate the timeout period of the lifecycle hook to ensure that the O&M tasks defined in the OOS template can be completed within the timeout period. |