By Yuanyi
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
Link: https://api.alibabacloud.com/api/Rds/2014-08-15/ModifyDBInstanceSpec
The following figure shows an example.
In the console, click Initiate Call to initiate an API call test.
After the API is called, go to the ApsaraDB RDS console to view instance information and check whether the call was successful.
After the API is debugged, download the Python project package from the console to obtain the API call script (alibabacloud_sample/sample.py).
Create a test directory, move the downloaded package to that directory, and then use the unzip command to decompress the package.
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.
export ALIBABA_CLOUD_ACCESS_KEY_ID="xxxxxxxxxxxxxxxxxxx"
export ALIBABA_CLOUD_ACCESS_KEY_SECRET="xxxxxxxxxxxxxxxxxxxxxx"
Run the source command to apply the environment variables.
source /etc/profile
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
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.
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.
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/
In this example, files in the /data2 directory are consistent with those in the scale-up project directory.
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
After modifying the parameter value, press the Esc
key and enter :wq
to save the file and exit the editor.
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.
Run the crontab -l
command to view the system scheduled task.
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.
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
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
PolarDB-X Disaster Recovery Capabilities: Cross-AZ, Cross-Region, and Cross-Cloud
Sevenfold Performance Improvement | Alibaba Cloud AnalyticDB Spark Vectorization Capability Analysis
Alibaba Clouder - February 7, 2018
Alibaba Developer - February 1, 2021
Alibaba Clouder - July 15, 2020
ApsaraDB - August 8, 2024
Alibaba Clouder - April 26, 2021
Alibaba Clouder - June 23, 2020
An on-demand database hosting service for SQL Server with automated monitoring, backup and disaster recovery capabilities
Learn MoreApsaraDB for HBase is a NoSQL database engine that is highly optimized and 100% compatible with the community edition of HBase.
Learn MoreVisualization, O&M-free orchestration, and Coordination of Stateful Application Scenarios
Learn MoreServerless Application Engine (SAE) is the world's first application-oriented serverless PaaS, providing a cost-effective and highly efficient one-stop application hosting solution.
Learn MoreMore Posts by ApsaraDB