All Products
Search
Document Center

Auto Scaling:Use Alibaba Cloud CLI to execute rolling update tasks

Last Updated:Aug 07, 2024

Alibaba Cloud Command Line Interface (CLI) is a management tool that is developed based on Alibaba Cloud OpenAPI. You can use Alibaba Cloud CLI to call Alibaba Cloud APIs to manage and scale Alibaba Cloud services in a flexible manner. This topic describes how to use Alibaba Cloud CLI to execute rolling update tasks.

Prerequisites

  • Alibaba Cloud CLI is installed. For more information, see What is Alibaba Cloud CLI?

  • A scaling group is created and Elastic Compute Service (ECS) instances are added to the scaling group.

  • The instance configuration source of the scaling group is a scaling configuration. If you want to update the images of ECS instances in the scaling group, this prerequisite must be met.

  • CloudOps Orchestration Service (OOS) packages are created. If you want to install OOS packages on ECS instances in the scaling group, this prerequisite must be met. For more information, see Manage custom software on multiple ECS instances at a time.

Background information

Rolling update tasks can be used to update the configurations of multiple ECS instances at the same time. For more information, see Rolling update.

Procedure

The following section describes how to use Alibaba Cloud CLI to update images, run scripts, and install OOS packages on ECS instances in a scaling group. You must perform the following steps:

Step 1: Create a RAM user and grant permissions to the RAM user

  1. Log on to the Resource Access Management (RAM) console.

  2. Create a RAM user.

    In this example, a RAM user named clitest is created. For more information, see Create a RAM user.

    1. In the left-side navigation pane, choose Identities > Users.

    2. On the Users page, click Create User.

    3. On the Create User page, configure parameters as prompted and click OK.

      The following table describes the parameter settings used in this example.

      Parameter

      Description

      Logon Name

      example@sample.com

      Display Name

      example

      Access Mode

      Select Open API Access.

      An AccessKey pair is automatically created for the RAM user. The RAM user can call API operations or use SDKs to access Alibaba Cloud resources.

    4. On the User Information page, click Download CSV File.

      Note

      The AccessKey secret is displayed only when you create an AccessKey pair, and cannot be queried. We recommend that you save the AccessKey secret for subsequent use. If an AccessKey pair is leaked or lost, you must create a new AccessKey pair.

  3. Grant the RAM user permissions to manage resources.

    1. In the left-side navigation pane, choose Identities > Users.

    2. Find the clitest user that you created and click Add Permissions in the Actions column.

    3. In the Grant Permission panel, select the permissions required to execute the rolling update task and click Grant permissions.

      The following table describes the parameter settings used in this example.

      Parameter

      Description

      Resource Scope

      Set the value to Account.

      Principal

      Set the value to example@sample.com.

      Policy

      Select the following system policies:

      • AliyunECSFullAccess: grants permissions to manage ECS resources, such as ECS instances.

      • AliyunESSFullAccess: grants permissions to manage Auto Scaling resources, such as scaling groups.

      • AliyunOOSFullAccess: grants permissions to manage OOS resources, such as executions.CloudOps Orchestration Service

      • AliyunOSSFullAccess: grants permissions to manage Object Storage Service (OSS) resources, such as buckets.

Step 2: Configure and verify Alibaba Cloud CLI

For information about the configuration parameters, see Configure Alibaba Cloud CLI.

  1. Open Alibaba Cloud CLI on your on-premises computer.

  2. Configure Alibaba Cloud CLI.

    1. Run the following command to open the configuration file:

      aliyun configure
    2. Enter the AccessKey ID and AccessKey secret as prompted.

      cli-config

  3. Run the following command to check whether Alibaba Cloud CLI is available:

    aliyun ecs DescribeRegions

    This command is used to query the regions in which Alibaba Cloud CLI is supported. If region information is returned, the command is successfully run. The following figure shows a sample command output.cli-example

Step 3: Use Alibaba Cloud CLI to execute a rolling update task

In this step, sample commands are used to show how to update images, run scripts, and install OOS packages on ECS instances in a scaling group.

  1. Run an Alibaba Cloud CLI command to execute a rolling update task.

    For information about the OOS template parameters in the code, see Template parameters.

    • The following sample code provides an example on how to update the images of ECS instances in the scaling group to Alibaba Cloud Linux 2.1903 LTS 64-bit:

      aliyun oos StartExecution --TemplateName ACS-ESS-RollingUpdateByReplaceSystemDiskInScalingGroup --Parameters "{
              \"invokeType\": \"invoke\",
              \"scalingGroupId\": \"asg-bp18p2yfxow2dloq****\",
              \"scalingConfigurationId\": \"asc-bp1bx8mzur534edp****\",
              \"imageId\": \"aliyun_2_1903_x64_20G_alibase_20200529.vhd\",
              \"sourceImageId\": \"centos_7_8_x64_20G_alibase_20200717.vhd\",
              \"OOSAssumeRole\": \"\",
              \"enterProcess\": [
                \"ScaleIn\",
                \"ScaleOut\",
                \"HealthCheck\",
                \"AlarmNotification\",
                \"ScheduledAction\"
              ],
              \"exitProcess\":  [
                \"ScaleIn\",
                \"ScaleOut\",
                \"HealthCheck\",
                \"AlarmNotification\",
                \"ScheduledAction\"
              ],
              \"batchNumber\": 2,
              \"batchPauseOption\": \"Automatic\"
            }"
    • The following sample code provides an example on how to run the df -h and ifconfig shell commands on the ECS instances in the scaling group to view the disks and network configurations of the ECS instances:

      aliyun oos StartExecution --TemplateName ACS-ESS-RollingUpdateByRunCommandInScalingGroup --Parameters "{
              \"invokeType\": \"invoke\",
              \"scalingGroupId\": \"asg-bp18p2yfxow2dloq****\",
              \"commandType\": \"RunShellScript\",
              \"invokeScript\": \"df -h\nifconfig\",
              \"rollbackScript\": \"df -h\nifconfig\",
              \"OOSAssumeRole\": \"\",
              \"exitProcess\": [
                \"ScaleIn\",
                \"ScaleOut\",
                \"HealthCheck\",
                \"AlarmNotification\",
                \"ScheduledAction\"
              ],
              \"enterProcess\": [
                \"ScaleIn\",
                \"ScaleOut\",
                \"HealthCheck\",
                \"AlarmNotification\",
                \"ScheduledAction\"
              ],
              \"batchNumber\": 2,
              \"batchPauseOption\": \"Automatic\"
            }"
    • The following sample code provides an example on how to install the WordPress software package created in an OOS template on the ECS instances in the scaling group:

      aliyun oos StartExecution --TemplateName ACS-ESS-RollingUpdateByConfigureOOSPackage --Parameters "{
              \"invokeType\": \"invoke\",
              \"scalingGroupId\": \"asg-bp18p2yfxow2dloq****\",
              \"packageName\": \"wordpress\",
              \"packageVersion\": \"v4\",
              \"action\": \"install\",
              \"OOSAssumeRole\": \"\",
              \"enterProcess\": [
                \"ScaleIn\",
                \"ScaleOut\",
                \"HealthCheck\",
                \"AlarmNotification\",
                \"ScheduledAction\"
              ],
              \"exitProcess\": [
                \"ScaleIn\",
                \"ScaleOut\",
                \"HealthCheck\",
                \"AlarmNotification\",
                \"ScheduledAction\"
              ],
              \"batchNumber\": 2,
              \"batchPauseOption\": \"Automatic\"
            }"
  2. View the execution details.

    When you run CLI commands to execute rolling update tasks, executions are automatically created in OOS. You can find the desired execution based on the returned execution ID and view the details of the execution, such as the execution result and output. The following example shows how to obtain the execution ID and view the execution details:

    1. Find the execution ID of the rolling update task in the command output.

      The following figure shows a sample execution ID.exec-id

    2. Run the following Alibaba Cloud CLI command to view the execution details:

      aliyun oos ListExecutions --ExecutionId exec-40e2e17ef7e04****

      The following figure shows the details of a sample execution.exec-outputs

Execute rollback tasks to handle exceptions in rolling update tasks

If exceptions occur when rolling update tasks are executed, or if you want to use previous configurations after rolling update tasks are executed, you can execute rollback tasks to restore the configurations of ECS instances. In this section, sample commands are used to show how to roll back rolling update tasks that are executed.

  1. Find the execution ID of the rolling update task in the command output.

    The following figure shows a sample execution ID.exec-id

  2. Run an Alibaba Cloud CLI command to execute a rollback task.

    For information about the OOS template parameters in the code, see Template parameters.

    Note

    When you execute a rollback task, OOS automatically finds the ECS instances that need to be rolled back based on the rolling update task, and suspends or resumes scaling processes in the scaling group. Therefore, you may not need to configure some parameters.

    • The following sample code provides an example on how to roll back the images of the ECS instances in the scaling group to CentOS 7.8 64-bit:

      aliyun oos StartExecution --TemplateName ACS-ESS-RollingUpdateByReplaceSystemDiskInScalingGroup --Parameters "{
              \"invokeType\": \"rollback\",
              \"scalingGroupId\": \"asg-bp18p2yfxow2dloq****\",
              \"scalingConfigurationId\": \"asc-bp1bx8mzur534edp****\",
              \"sourceImageId\": \"centos_7_8_x64_20G_alibase_20200717.vhd\",
              \"sourceExecutionId\": \"exec-83dba59be77d430****\",
              \"OOSAssumeRole\": \"\",
              \"batchNumber\": 2,
              \"batchPauseOption\": \"Automatic\"
            }"
    • The following sample code provides an example on how to run the rollback script on the ECS instances:

Note

The df -h and ifconfig shell commands are still used. You can change the script based on your business requirements.

aliyun oos StartExecution --TemplateName ACS-ESS-RollingUpdateByRunCommandInScalingGroup --Parameters "{
        \"invokeType\": \"rollback\",
        \"commandType\": \"RunShellScript\",
        \"rollbackScript\": \"df -h\nifconfig\",
        \"scalingGroupId\": \"asg-bp18p2yfxow2dloq****\",
        \"sourceExecutionId\": \"exec-40e2e17ef7e046****\",
        \"OOSAssumeRole\": \"\",
        \"batchNumber\": 2,
        \"batchPauseOption\": \"Automatic\"
      }"
  • The following sample code provides an example on how to roll back the WordPress software package created in the OOS template on the ECS instance to its previous version:

    aliyun oos StartExecution --TemplateName ACS-ESS-RollingUpdateByConfigureOOSPackage --Parameters "{
            \"invokeType\": \"rollback\",
            \"scalingGroupId\": \"asg-bp18p2yfxow2dloq****\",
            \"packageVersion\": \"v3\",
            \"packageName\": \"wordpress\",
            \"sourceExecutionId\": \"exec-f4e61f2f21fe490****\",
            \"OOSAssumeRole\": \"\",
            \"batchNumber\": 2,
            \"batchPauseOption\": \"Automatic\"
          }"
  • View the execution details.

    When you run CLI commands to execute rollback tasks, executions are automatically created in OOS. You can also use the methods in Step 3 to view the details of an execution, such as the execution result and output.

Template parameters

The following tables describe the parameters of the OOS public templates that are used in the preceding examples.

Table 1. Parameters in the ACS-ESS-RollingUpdateByReplaceSystemDiskInScalingGroup template

Parameter

Description

invokeType

The type of the task. Valid values:

  • invoke: rolling update task

  • rollback: rollback task

scalingGroupId

The ID of the scaling group in which you want to execute the task.

scalingConfigurationId

The ID of the active scaling configuration in the scaling group.

imageId

The ID of the image with which you want to replace the current image.

sourceImageId

The ID of the image that you want to use for the rollback task.

OOSAssumeRole

The RAM role that you want to use to execute the task. Default value: OOSServiceRole.

enterProcess

The scaling process that you want to suspend before the task is executed.

exitProcess

The scaling process that you want to resume when the task is complete.

batchNumber

The number of batches into which the ECS instances in the scaling group are divided. The task is executed in batches. Each batch contains at least one ECS instance.

batchPauseOption

Specifies whether and how to suspend the task. Valid values:

  • Automatic: The task is executed without interruptions.

  • FirstBatchPause: The task is suspended when the first batch of executions is complete.

  • EveryBatchPause: The task is suspended when each batch of executions is complete.

sourceExecutionId

The execution ID of the source rolling update task when the rollback task is executed.

Note

You can log on to the OOS console to view more parameters. For example, to view the parameters supported in the China (Hangzhou) region, see ACS-ESS-RollingUpdateByReplaceSystemDiskInScalingGroup.

Table 2. Parameters in the ACS-ESS-RollingUpdateByRunCommandInScalingGroup template

Parameter

Description

invokeType

The type of the task. Valid values:

  • invoke: rolling update task

  • rollback: rollback task

scalingGroupId

The ID of the scaling group in which you want to execute the task.

commandType

The type of the script that you want to execute. A value of RunShellScript specifies a shell script.

invokeScript

The script that you want to execute on ECS instances during the rolling update task.

rollbackScript

The script that you want to execute on ECS instances during the rollback task.

OOSAssumeRole

The RAM role that you want to use to execute the task. Default value: OOSServiceRole.

enterProcess

The scaling process that you want to suspend before the task is executed.

exitProcess

The scaling process that you want to resume when the task is complete.

batchNumber

The number of batches into which the ECS instances in the scaling group are divided. The task is executed in batches. Each batch contains at least one ECS instance.

batchPauseOption

Specifies whether and how to suspend the task. Valid values:

  • Automatic: The task is executed without interruptions.

  • FirstBatchPause: The task is suspended when the first batch of executions is complete.

  • EveryBatchPause: The task is suspended when each batch of executions is complete.

sourceExecutionId

The execution ID of the source rolling update task when the rollback task is executed.

Note

You can log on to the OOS console to view more parameters. For example, to view the parameters supported in the China (Hangzhou) region, see ACS-ESS-RollingUpdateByRunCommandInScalingGroup.

Table 3. Parameters in the ACS-ESS-RollingUpdateByConfigureOOSPackage template

Parameter

Description

invokeType

The type of the task. Valid values:

  • invoke: rolling update task

  • rollback: rollback task

scalingGroupId

The ID of the scaling group in which you want to execute the task.

packageName

The name of the software package.

packageVersion

The version of the software package.

action

The mode in which you want to configure the software package. Valid values:

  • install

  • uninstall

Default value: install.

OOSAssumeRole

The RAM role that you want to use to execute the task. Default value: OOSServiceRole.

enterProcess

The scaling process that you want to suspend before the task is executed.

exitProcess

The scaling process that you want to resume when the task is complete.

batchNumber

The number of batches into which the ECS instances in the scaling group are divided. The task is executed in batches. Each batch contains at least one ECS instance.

batchPauseOption

Specifies whether and how to suspend the task. Valid values:

  • Automatic: The task is executed without interruptions.

  • FirstBatchPause: The task is suspended when the first batch of executions is complete.

  • EveryBatchPause: The task is suspended when each batch of executions is complete.

sourceExecutionId

The execution ID of the source rolling update task when the rollback task is executed.

Note

You can log on to the OOS console to view more parameters. For example, to view the parameters supported in the China (Hangzhou) region, see ACS-ESS-RollingUpdateByConfigureOOSPackage.