Autoscaling Azure MySql Server using Azure Automation
Azure automation is a cloud service that helps in automating management tasks thereby saving time and preventing errors. We can also use Azure automation for several other tasks in the cloud as well, for on-premises configuration items. Don’t wait for a traffic spike to take down your app or site. With Scheduled AutoScale, you can respond before anything happens.
We had a standalone MySQL server that was running in production. When the workload used to increase/decrease, we had to manually increase/decrease the SKU tier. The process seemed hectic and time-consuming. That’s why we thought of autoscaling the MySQL server which led to better performance of the MySQL server and saved our time.
In this blog, I have explained how to set up an Azure automation account, create a runbook, publish a runbook, scheduling, etc.
Identifying loads and increasing or decreasing your computing capacity is critical to business and requires manual intervention and downtime to achieve. On the other hand, monitoring the environment requires continuous effort/resources.
In a managed database scenario, most services already provide built-in auto- scale down capability. In the case of Azure Database for MySQL, we can configure the autoscale capability from the storage front, however, the computation requires manual intervention to achieve this by increasing the SKU levels or automating the entire process.
To simplify the process, I am using Azure Automation account, Automation Runbook and PowerShell, and the whole process is explained step by step below.
Step-1: Create Azure Automation Account from the Azure portal for autoscaling the MySQL server.
Step-2: Since we are accessing Azure accounts (AZ. Accounts) and some Azure PowerShell modules to access MySQL (AZ. MySQL) resources, those modules are not included by default, so it can be accessed directly from the module gallery and will have to be imported.
You can view the “Module gallery” by clicking on the option under the “Shared Resource” section as shown.
AZ. Accounts module: Manages credentials and common configuration for all Azure modules. Connect to Azure with an authenticated account for use with cmdlets from the Az PowerShell modules.
AZ. MySQL module: Gets information about a server.
Step-3: In the next step, we need a runbook where we have to define the flow or script below the whole process for scale-up and down (conveniently I have created two separate rule books for both scale up and down).
I have chosen PowerShell as a runbook type, however, there are multiple other options like Python, Graphical, etc that can be used.
Step-4: I’ve included native code in the runbook console for changing or scaling your SKU values.
### Parameters ###[CmdletBinding()]param([Parameter(Mandatory=$True)][string]$ResourceGroupName,[Parameter(Mandatory=$True)][string]$ServerName,[Parameter(Mandatory=$False)][string]$SkuTier)# Track the execution date & time$StartDate=(GET-DATE)### Log-in to Azure with AZ (standard code) ###Write-Verbose -Message 'Connecting to Azure'# Name of the Azure Run As a connection$connectionName = "AzureRunAsConnection"try{# Get the connection "AzureRunAsConnection "$servicePrincipalConnection=Get-AutomationConnection -Name $connectionNameConnect-AzAccount `-ServicePrincipal `-TenantId $servicePrincipalConnection.TenantId `-ApplicationId $servicePrincipalConnection.ApplicationId `-CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint}
catch {if (!$servicePrincipalConnection){$ErrorMessage = "Connection $connectionName not found."throw $ErrorMessage}else {Write-Error -Message $_.Exceptionthrow $_.Exception}}### Setting SKU Tier for MySQLDatabase ###$CurrentSKU = (Get-AzMySqlServer -Name $ServerName -ResourceGroupName $ResourceGroupName).skunameIf($CurrentSKU -eq $SkuTier){# Validating the existing SKU valueWrite-Error "Cannot change pricing tier of $ServerName because the new SKU $SkuTier tier is equal to current SKU tier $CurrentSKU."return}else{try{# updating the existing SKU value with the new oneUpdate-AzMySqlServer -Name "$($ServerName)" -ResourceGroupName "$($ResourceGroupName)" -sku "$($SkuTier)"}catch{Write-Error -Message $_.Exception.Messagethrow $_.Exception}}
In this code, we have defined mandatory parameters for configuring azure resources for the MySql server.
We are using cmdlets for managing Azure resources directly from the PowerShell command line. Azure PowerShell is designed to make it easy to learn and get started with but provides powerful features for automation.
Log-in to Azure with AZ (standard code)
Try to connect azure with the help of Az.Accounts module and if servicePrincipalConnection is not found then throw the error message else write error .exception and throw the .exception.
Setting SKU Tier for MySQLDatabase
Using the Az.mysqlserver module gets the information about a server for example servername, resourcegroupname etc. If the CurrentSKU value is equal to the SkuTier then cannot change pricing tier of the server because the new SKU tier is equal to the current SKU tier. But CurrentSKU value is not equal to the SkuTier then updating the existing SKU value with the new one.
Step-5: Save and test by entering the required parameters as mentioned below.
Step-6: Once you’re done with the review, click on the “Publish” option to publish the runbook. Click on “Yes” to confirm.
Step-7: Your code is now ready to be published and linked with MySQL Alerts.
- Go To MySQL resource and create a new alert rule
- Choose the appropriate signal type (In my case I have selected CPU percent and defined the threshold value to 80%)
Step-8: Create Action Group and select User Defined Automation Runbook and select the one created above from the Runbook list.
Step-9: Please specify the required parameter in the parameter selection and complete.
Step-10: Configure the runbook and save after specifying the required parameter in the parameter selection.
Step-11: Finally, specify the alert description and severity that will help identify when this alert was triggered and take action.
Step-12: With all these steps, we increased the SKU tier and achieved our task.
Conclusion:
The MySQL server was running in production and the workloads led to an increase in the CPU utilization and the same was decreasing when the workload was reduced.
I tried searching for a solution to this scenario but there were not enough resources present online and so I decided to explore the service and checked how to increase/decrease SKU whenever required.
The above solution will help to identify the CPU threshold and take appropriate action based on the steps defined in Azure Automation Runbook. Also, it will help in tracking the changes as part of alerts for auditing purposes. This solution helps to reduce the upfront cost (which results from purchasing tier\sku’s resources) and take proactive action when identifying low-usage or high-usage CPUs; accordingly, the alert scales up\down depending on the configuration.
Originally published at http://blog.opstree.com on July 6, 2021.