×
Community Blog Implement Scheduled Elastic Scaling of Serverless ApsaraDB RDS for SQL Server By Calling API

Implement Scheduled Elastic Scaling of Serverless ApsaraDB RDS for SQL Server By Calling API

The article explains how to use Python to schedule changes to the maximum and minimum RCUs of an ApsaraDB RDS instance to optimize cost and improve elastic RCU scaling performance.

By Yuanyi

1. Introduction

This article describes how to use Python to call the ApsaraDB RDS API to change the maximum and minimum numbers of RDS Capacity Units (RCUs) on a scheduled basis, increasing the maximum number before peak hours and decreasing the minimum number before off-peak hours. Not only does this save costs, but it also shortens the ramp-up period for elastic RCU scaling and avoids the impact on the business caused by slow scaling.

Recommended scenario: serverless ApsaraDB RDS for SQL Server instances

API name: ModifyDBInstanceSpec

API endpoint: https://www.alibabacloud.com/help/rds/apsaradb-rds-for-sql-server/api-rds-2014-08-15-modifydbinstancespec-sqlserver

2. Procedure

2.1 API Debugging

(1) Log on to the OpenAPI Explorer console to debug the API

Link: https://api.alibabacloud.com/api/Rds/2014-08-15/ModifyDBInstanceSpec

1

(2) View request parameters

Link: https://www.alibabacloud.com/help/rds/apsaradb-rds-for-sql-server/api-rds-2014-08-15-modifydbinstancespec-sqlserver

2

(3) Set the parameters based on the ApsaraDB RDS for SQL Server instance in question

The following figure shows an example.

3

(4) Initiate a test

In the console, click Initiate Call to initiate an API call test.

4

After the API is called, go to the ApsaraDB RDS console to view instance information and check whether the call was successful.

5

2.2 Prepare an Environment

(1) Download the Python project package.

After the API is debugged, download the Python project package from the console to obtain the API call script (alibabacloud_sample/sample.py).

6

7

(2) Decompress the Python project package.

Create a test directory, move the downloaded package to that directory, and then use the unzip command to decompress the package.

8

(3) Configure system environment variables.

Obtain the AccessKey ID and AccessKey secret of the Alibaba Cloud account under which the ApsaraDB RDS for SQL Server instance is managed. Configure the /etc/profile configuration file and add the AccessKey pair to system environment variables.

9

export ALIBABA_CLOUD_ACCESS_KEY_ID="xxxxxxxxxxxxxxxxxxx"
export  ALIBABA_CLOUD_ACCESS_KEY_SECRET="xxxxxxxxxxxxxxxxxxxxxx"

10

Run the source command to apply the environment variables.

source /etc/profile

(4) Install and run the required environment for script execution

In this example, a Python script is used to implement scheduling. Therefore, prepare a Python execution environment and install the Python modules that are required to call Alibaba Cloud SDKs. Install Python 3.8 or later, as shown in the following example.

python3 setup.py install

11

2.3 Scale-up Script and Scheduling

To achieve scheduled scale-up, you can configure a scheduled task in the Linux operating system so that each time peak hours arrive, a script is run to call an API to scale up RCUs for the serverless ApsaraDB RDS for SQL Server instance.

Run the crontab -e command to edit the crontab file. Write code to the file, as shown in the following example.

0 8 * * *  python3  /data/alibabacloud_sample/sample.py
The sample code indicates that the scale-up script is run at 8 o'clock every day. /data/alibabacloud_sample/sample.py is the path of the script.
####################################################
    The following content describes the meaning of each part in the sample code:
    0: the minute. The value 0 here indicates the top of the hour.
    8: the hour on a 24-hour clock. The value 8 here indicates eight o'clock in the morning.
    *: the day of the month. The asterisk (*) indicates all days of the month.
    *: the month. The asterisk (*) indicates all months.
    *: the day of the week. The asterisk (*) indicates all days of a week.

Save the script and exit the editor. If you are using the Vim editor, press the Esc key, enter :wq, and then press the Enter key to save the file and exit the editor.

12

The sample code runs a Python scale-up script at 8 o'clock every day to call the API to scale up the serverless ApsaraDB RDS for SQL Server instance. sample.py is the script to be run, which is obtained by decompressing the downloaded Python project package. You can modify the max_capacity and min_capacity values in the script to adjust the maximum and minimum numbers of RCUs for the serverless ApsaraDB RDS for SQL Server instance.

Note:

max_capacity: the maximum number of RCUs allowed. For example, if business traffic spikes and the maximum allowed number of RCUs is 14, the system gradually increases the number of RCUs to an appropriate value that is less than or equal to the upper limit specified by the max_capacity parameter.

min_capacity: the minimum number of RCUs allowed, namely, the lower limit for scaling down RCUs when the business traffic load decreases. In scenarios of scheduled RCU scaling, resource scaling is mainly implemented by modifying values of these two parameters.

Precaution:

To prevent other configurations of the instance from being modified due to misoperations, we recommend that you annotate the configurations other than the preceding two parameters.

2.4 Scale-down Script and Scheduling

(1) Prepare a scale-down script.

Copy all the Python project files used to implement scale-up to another directory. Rename the new directory as the Python project for scale-down.

cp -r /data/*  /data2/

13

In this example, files in the /data2 directory are consistent with those in the scale-up project directory.

14

Use the Vim editor to edit the sample.py file. Modify the value of the min_capacity parameter, which is the lower limit for scaling down RCUs. During RCU scale-down, this value indicates the value to which the number of RCUs should be reduced.

vim ./alibabacloud_sample/sample.py

15
16
17

After modifying the parameter value, press the Esc key and enter :wq to save the file and exit the editor.

(2) Schedule the scale-down script.

Similarly, to achieve scheduled scale-down, you can configure a scheduled task in the Linux operating system so that a script is run immediately after the peak hours to call an API to scale down RCUs for the ApsaraDB RDS for SQL Server instance.

Run the crontab -e command to edit the crontab file. Write code to the file, as shown in the following example.

0 18 * * *  python3  /data2/alibabacloud_sample/sample.py
The sample code indicates that the scale-up script is run at 18 o'clock every day. /data2/alibabacloud_sample/sample.py is the path of the script.
####################################################
    The following content describes the meaning of each part in the sample code:
    0: the minute. The value 0 here indicates the top of the hour.
    18: the hour on a 24-hour clock. The value 18 here indicates six o'clock in the afternoon.
    *: the day of the month. The asterisk (*) indicates all days of the month.
    *: the month. The asterisk (*) indicates all months.
    *: the day of the week. The asterisk (*) indicates all days of a week.

Save the script and exit the editor. If you are using the Vim editor, press the Esc key, enter :wq, and then press the Enter key to save the file and exit the editor.

18

Run the crontab -l command to view the system scheduled task.

19

(3) Summary

As described above, you can use Python scripts to call APIs to adjust the number of RCUs for a serverless ApsaraDB RDS for SQL Server instance on a scheduled basis. This avoids the impact on your business caused by slow scaling, which can take minutes, in some cases.

4. FAQ

4.1 Execution Error of python3 setup.py install

Create or modify the file by running the following command:

echo  "[global]\nindex-url = https://mirrors.aliyun.com/pypi/simple/\n[install]\ntrusted-host = mirrors.aliyun.com" > ~/.pip/pip.conf

4.2 ModuleNotFoundError:No module name '_ssl'

If an error ModuleNotFoundError: No module named '_ssl' occurs while executing the following Python command, you can refer to the following steps to resolve it.

python3  /data2/alibabacloud_sample/sample.py  

(1) Upgrade OpenSSL for the operating system. For more information, visit https://blog.csdn.net/qq_30359369/article/details/134954151
(2) Reinstall and compile Python 3. For more information, visit https://blog.csdn.net/Waller_/article/details/122252904
(3) Alternatively, refer to the Q&A on GitHub: https://github.com/codrsquad/portable-python/issues/41

0 1 0
Share on

ApsaraDB

448 posts | 95 followers

You may also like

Comments

ApsaraDB

448 posts | 95 followers

Related Products