All Products
Search
Document Center

Auto Scaling:Use scaling configuration features to enable automatic scaling

Last Updated:Sep 24, 2024

Auto Scaling supports various scaling configuration features to provide flexible and elastic scaling services, including key pair, tag, RAM role, and instance user data. This topic describes the scaling configuration features.

Prerequisites

An Alibaba Cloud account is created. To create an Alibaba Cloud account, go to the Sign up to Alibaba Cloud page.

Background information

Auto Scaling automatically adjusts the number of Elastic Compute Service (ECS) instances or elastic container instances to handle business workloads during peak or off-peak hours. Auto Scaling also supports automatic installation of applications on ECS instances or elastic container instances. If Auto Scaling is properly used, Auto Scaling helps reduce server costs and costs of managing and performing O&M on servers.

The scaling configuration component of Auto Scaling provides various features. You can create a scaling configuration to specify custom instance configurations in an efficient and flexible manner. To help you better understand and use Auto Scaling, this topic describes how to use the features that are described in the “Feature description” section to implement automatic scaling of instances and automatic installation of applications. For example, you can create a scaling configuration to automatically associate Resource Access Management (RAM) roles with instances, add tags to instances, configure logon key pairs, and run custom scripts upon instance startups.

Feature description

The following table describes the key pair, tag, RAM role, and instance user data features of the scaling configuration component.

Feature

Description

Operation

Key pair

Alibaba Cloud supports only 2048-bit RSA key pairs. Only Linux instances support Secure Shell (SSH) key pairs. After you create an SSH key pair, Alibaba Cloud provides you with the private key. Alibaba Cloud also stores the public key.

After Auto Scaling creates an ECS instance based on a scaling configuration, the ECS instance stores the public key of the specified SSH key pair. You can use the private key to log on to the ECS instance from your on-premises device.

Note

Compared with logons by using passwords, logons by using SSH key pairs are more efficient and secure. You can specify an SSH key pair when you create a scaling configuration. For more information, see Overview.

  • Auto Scaling console: When you create a scaling configuration, select an existing SSH key pair in the Key Pair field. For more information, see Create a scaling configuration of the ECS type.

  • Auto Scaling API: When you create a scaling configuration, use the KeyPairName parameter to specify an SSH key pair. For more information, see CreateScalingConfiguration.

Tag

The tag feature helps you identify resources and user groups with ease. Enterprises or individuals can use tags to categorize ECS instances or elastic container instances by purpose. This facilitates resource search and aggregation. For more information, see Tags.

RAM role

RAM is a service provided by Alibaba Cloud to manage user identities and resource access permissions. RAM allows you to create different roles and grant them different permissions on Alibaba Cloud services.

An ECS instance can assume a RAM role to inherit the permissions that are granted to the RAM role. For more information, see Overview.

Important

When you specify a RAM role in a scaling configuration, make sure that ECS or Elastic Container Instance is the trusted entity of the RAM role. Otherwise, Auto Scaling cannot create ECS instances or elastic container instances based on the scaling configuration.

Instance user data

Both Windows and Linux instances support the instance user data feature. For more information, see Instance user data.

Note

Compared with open source IT infrastructure management tools, such as Terraform, the instance user data feature of Auto Scaling is safer and more reliable. For information about the precautions of the instance user data feature and how to enable this feature, see Use the Instance User Data feature to automatically configure ECS instances.

The instance user data feature provides the following benefits:

  • Create custom features: You can use the instance user data feature to configure a custom instance script to manage instance startups. The custom instance script is automatically run when Auto Scaling starts instances.

  • Reference data: You can use the instance user data feature to pass data to instances and reference the data on the instances.

  • Auto Scaling console: When you create a scaling configuration, prepare a Base64-encoded custom script and pass the script to the scaling configuration in the Instance User Data field. ECS instances that are created based on the scaling configuration can run the script upon startups to automatically deploy applications. This way, you can enable application-level scaling. For more information, see Create a scaling configuration of the ECS type.

  • Auto Scaling API: When you create a scaling configuration, use the UserData parameter to pass instance user data. For more information, see CreateScalingConfiguration.

Step 1: Prepare feature settings

This section describes how to prepare an instance RAM role, tags, an SSH key pair, and the instance user data.

  1. Create tags.

    For more information, see Create or add a tag.

  2. Create an SSH key pair.

    For more information, see Create an SSH key pair.

  3. Create a RAM role.

    For more information, see Create a RAM role for a trusted Alibaba Cloud service. You can also use an existing RAM role. When you specify an existing RAM role in a scaling configuration, make sure that ECS or Elastic Container Instance is the trusted entity of the RAM role. Otherwise, Auto Scaling cannot create ECS instances or elastic container instances based on the scaling configuration. For example, RAM role AliyunECSImageExportDefaultRole grants the permission of exporting images. The trust policy of the RAM role allows all ECS instances in the current account to assume this RAM role. Sample code:

    {
        "Statement": [
            {
                "Action": "sts:AssumeRole",
                "Effect": "Allow",
                "Principal": {
                    "Service": [
                        "ecs.aliyuncs.com"
                    ]
                }
            }
        ],
        "Version": "1"
    }
    Note

    ecs.aliyuncs.com specifies that all ECS instances of the current account can assume the RAM role.

  4. Prepare user data.

    For more information, see Instance user data.

    The following code shows a sample Shell script that enables Auto Scaling to write Hello World. The time is now{current time} to the /root/output10.txt file upon the first instance startup. Sample script:

    #!/bin/sh
    echo "Hello World.  The time is now $(date -R)!" | tee /root/output10.txt
    Note

    The Shell script used in this example is simple and only for reference. You can create a more advanced custom script to implement more features upon instance startups based on your business requirements.

    The following code shows the result of the Base64 encoding of the preceding code:

    IyEvYmluL3NoDQplY2hvICJIZWxsbyBXb3JsZC4gIFRoZSB0aW1lIGlzIG5vdyAkKGRhdGUgLVIpISIgfCB0ZWUgL3Jvb3Qvb3V0cHV0MTAudHh0 

Step 2: Apply the feature settings

This section describes how to create a scaling group and a scaling configuration, and apply the feature settings that are prepared in Step 1: Prepare feature settings to the scaling configuration.

  1. Create a scaling group.

    For more information, see Manage scaling groups.

    The following table describes the parameters that you must configure when you create a scaling group.

    Parameter

    Description

    Minimum Number of Instances

    In this example, this parameter is set to 1. After you enable the scaling group, Auto Scaling automatically creates one ECS instance in the scaling group.

    Instance Configuration Source

    In this example, Create from Scratch is selected.

    Network Type

    In this example, VPC is selected.

    Note

    This parameter is available only for scaling groups of the ECS type.

    VPC

    Specify the ID of an existing virtual private cloud (VPC).

    vSwitch

    Select a vSwitch that resides in the specified VPC.

  2. Create and enable a scaling configuration.

    For more information, see Create a scaling configuration of the ECS type.

    The following table describes the parameters that you must configure when you create a scaling configuration.

    Section

    Parameter

    Description

    Instance and Image

    Select Image

    In this example, Ubuntu 16.04 64-bit below the Public Images field is selected.

    Management Settings

    Logon Credentials

    In this example, Key Pair is selected.

    Key pair

    Select the key pair that was created in Step 1: Prepare feature settings.

    Tag

    Select the tags that were created in Step 1: Prepare feature settings.

    Advanced Settings

    RAM Role

    Select the RAM role that was created in Step 1: Prepare feature settings.

    Instance User Data

    Pass the script that was prepared in Step 1: Prepare feature settings.

  3. Enable the scaling group.

    For more information, see Enable a scaling group.

Step 3: Verify the feature settings

In Step 2, the Minimum Number of Instances parameter was set to 1. After you enable the scaling group, Auto Scaling creates one ECS instance to ensure that the minimum number of instances is reached.

  1. View the automatically created ECS instance.

    For more information, see View the instances in a scaling group.

  2. In the row of the ECS instance, click the ID or name of the ECS instance whose details you want to view.

    The following figure shows an example of the instance details page. In the figure, the RAM role and tag settings of the ECS instance are in effect. 实例详情ECS实例详情-ram.png

  3. Use the SSH key pair to log on to the ECS instance.

    For more information, see Connect to a Linux instance by using an SSH key pair.

    The following figure shows an example of a successful logon. In the figure, the SSH key pair is in effect. 使用密钥对成功登录实例

  4. Run the following command to view the content of the /root/output10.txt file:

    cat /root/output10.txt

    The following figure shows an example of the file content. In this figure, the instance user data feature is in effect. 自定义数据配置生效