Use scaling configuration features to implement auto scaling

Updated at:
Copy as MD

For more elastic and flexible scaling, a scaling configuration lets you specify parameters such as a key pair, a tag, an instance RAM role, and instance user data. This topic describes and demonstrates how to use these four features.

Prerequisites

Before you start, make sure you have an Alibaba Cloud account. If you do not have an account, create one.

Background information

Auto Scaling (also known as ESS) allows you to automatically adjust the number of ECS instances or ECI instances in response to business demands. It also enables you to automatically deploy applications on these instances. Effective use of Auto Scaling reduces your server costs and management overhead.

A scaling configuration in Auto Scaling supports various features that let you efficiently and flexibly customize the settings of ECS or ECI instances. To help you understand and use Auto Scaling, this topic demonstrates how to use the features described in Scaling configuration features to implement automatic scaling and deployment. The demonstration includes enabling SSH key pair logon for instances, setting tags, attaching an instance RAM role, and running a custom script after an instance starts.

Scaling configuration features

The scaling configuration features described in this topic include key pairs, tags, instance RAM roles, and instance user data. The following table provides more details.

Feature

Description

How to use

Key Pair

Alibaba Cloud supports only 2048-bit RSA key pairs. Only ECS instances that run Linux support logon with an SSH key pair. When you create an SSH key pair, Alibaba Cloud saves the public key and returns the private key to you.

After Auto Scaling creates an ECS instance, the instance stores the public key of the SSH key pair. You can then use the private key on your local machine to log on to the automatically created ECS instance.

Note

Using an SSH key pair to log on to a Linux instance is faster and more secure than using a password. Simply select an SSH key pair when you create a scaling configuration. For more information, see SSH key pairs.

Tag

Tags help you identify resources and user groups. You can use tags to categorize ECS or ECI instances that serve the same purpose for easier searching and aggregation. For more information, see Tags.

Instance RAM Role

Resource Access Management (RAM) is an Alibaba Cloud service that allows you to manage user identities and resource access permissions. You can use RAM to create different roles and grant them different permissions on cloud services.

An instance RAM role allows an ECS instance to assume a role with specific permissions, granting the instance access to other Alibaba Cloud services. For more information, see Instance RAM roles.

Important

When you select an instance RAM role in a scaling configuration, ensure the role's trust policy allows instances to assume it. Otherwise, Auto Scaling cannot create instances.

The user data of the instance.

Both Windows and Linux instances support instance user data. For more information, see Overview of instance user data.

Note

Compared with open source IT infrastructure management tools like Terraform, using the native instance user data feature of Auto Scaling is faster and more secure. For usage notes and procedures, see Use instance user data to automatically configure ECS instances.

This feature provides the following main functions:

  • Customization: Runs as a custom script when an instance starts, allowing you to customize the instance's startup behavior.

  • Data passing: Passes information to an instance as regular data that you can reference within the instance.

  • Console: When you create a scaling configuration in the Auto Scaling console, prepare a script, Base64-encode it, and then pass it to the scaling configuration. The automatically created ECS instances run the script upon startup to implement application-level scale-out and scale-in. For more information, see Create a scaling configuration for ECS instances.

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

Step 1: Create the configuration items

Follow these steps to create the required instance RAM role, tags, key pair, and instance user data.

  1. Create a tag.

    For more information, see Create and bind tags.

  2. Create a key pair.

    For more information, see Create an SSH key pair.

  3. Create an instance 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 select an instance RAM role in a scaling configuration, ensure that the permission policy of the instance RAM role allows instances in the scaling group to assume the role. Otherwise, the scaling configuration cannot launch instances. For example, the RAM role AliyunECSImageExportDefaultRole is used to authorize image exports and allows all ECS instances of the current user to assume this role. Its trust policy is as follows:

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

    ecs.aliyuncs.com indicates that all ECS instances that belong to the current user can assume this RAM role.

  4. Generate the instance user data.

    For more information, see Overview of instance user data.

    In this example, a Shell script is created to write the string Hello World. The time is now{current time} to the /root/output10.txt file after the instance starts for the first time. The script is as follows:

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

    The shell script in this example is simple. You can customize the script to perform more complex tasks when an instance starts.

    After you Base64-encode the script, the content is as follows:

    IyEvYmluL3NoDQplY2hvICJIZWxsbyBXb3JsZC4gIFRoZSB0aW1lIGlzIG5vdyAkKGRhdGUgLVIpISIgfCB0ZWUgL3Jvb3Qvb3V0cHV0MTAudHh0 

Step 2: Apply the configuration items

Follow these steps to create a scaling group and a scaling configuration, and then apply the items created in Step 1.

  1. Create a scaling group.

    For more information, see Create a scaling group.

    When you create the scaling group, take note of the following parameters:

    Parameter

    Description

    Minimum Number of Instances

    Set this to 1. After the scaling group is enabled, Auto Scaling automatically creates one instance.

    Instance Configuration Source

    Select Create from Scratch.

    Network Type

    Select VPC.

    Note

    This parameter applies only to scaling groups for ECS instances and is not available for scaling groups for ECI instances.

    VPC

    Select the ID of an existing VPC.

    vSwitch

    Select a vSwitch.

  2. Create and enable a scaling configuration.

    For more information, see Create a scaling configuration for ECS instances.

    When you create the scaling configuration, take note of the following parameters:

    Section

    Parameter

    Description

    Instance and image

    Image

    Select Ubuntu 16.04 64-bit from the Public Image.

    Management Settings

    Logon Credential

    Select Key Pair.

    Key Pair

    Select the key pair that you created in Step 1: Create the configuration items.

    Tag

    Select the tag that you created in Step 1: Create the configuration items.

    Advanced Settings

    Instance RAM Role

    Select the instance RAM role that you created in Step 1: Create the configuration items.

    The user data of the instance.

    Enter the instance user data script that you prepared in Step 1: Create the configuration items.

  3. Enable the scaling group.

    For more information, see Enable a scaling group.

Step 3: Verify the custom configurations

Because you set Minimum Number of Instances to 1, Auto Scaling creates one instance (an ECS instance in this example) when the scaling group is enabled, which ensures that the group meets its minimum instance requirement.

  1. View the automatically created ECS instance.

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

  2. In the ECS Instance ID/Name column, click the instance ID to view the details of the ECS instance.

    The instance details show that the tag and instance RAM role from the scaling configuration have been applied. On the instance details page, go to the Tags section and verify that the instance has tags such as bw:ecs and acs:autoscaling:scalingGroupId:a***yn attached. This confirms the tag configuration was successful. Then, go to the Other Information section and check that RAM Role displays the name of the role you configured, such as AliyunECSImageImportDefaultR.... This confirms that the instance RAM role is successfully attached to the ECS instance.

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

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

    A successful logon, indicated by the following output, confirms that the SSH key pair was configured correctly.

    Using username "root".
    Authenticating with public key "imported-openssh-key"
    Welcome to Ubuntu 16.04.6 LTS (GNU/Linux 4.4.0-151-generic x86_64)
     * Documentation:  https://help.ubuntu.com
     * Management:     https://landscape.canonical.com
     * Support:        https://ubuntu.com/advantage
    Welcome to Alibaba Cloud Elastic Compute Service !
    root@ixxxxxxxxxx:~#
  4. Run the following command to view the content of the /root/output10.txt file:

    cat /root/output10.txt

    The following output shows the file content, which indicates that the instance user data configuration has taken effect:

    root@ixxx:~# cat /root/output10.txt
    Hello World.  The time is now Mon, 16 Sep 2019 11:01:26 +0800!
    root@ixxx:~#