Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to scale Azure app services programmatically

I am looking for a way to programmatically scale up/down an Azure app service instance. This question has been asked before (e.g. in the link below) several months back and the answer was not possible at the time, so I am just wondering if something has changed and/or some new feature is available now.

Is possible to programmatically scale Azure instances?

Please note that we would like to do this on "Azure app services", not the old style "Azure cloud services".

The reason that we would like to do the scaling programmatically is so that we can control it using on our custom metrics. We did not find a way to publish our custom metrics to Azure and it can then be used by Azure to do the autoscaling either.

We did find that the Azure autoscale rule can accept an Azure storage queue length, so theoretically we can control the queue length by adding/removing messages to the queue, but it is kind of a hack and also it only works if the queue is created in the classic Azure web portal, not the new Azure portal.

like image 667
qcenow Avatar asked Mar 12 '17 13:03

qcenow


People also ask

How do I scale my Azure App Service?

Scale up your pricing tierIn your browser, open the Azure portal. In your App Service app page, from the left menu, select Scale Up (App Service plan). Choose your tier, and then select Apply. Select the different categories (for example, Production) and also See additional options to show more tiers.

Is Azure App Service Auto scalable?

Azure App Service has built-in autoscaling. Autoscale settings apply to all of the apps within an App Service. See Scale instance count manually or automatically and Scale up an app in Azure App Service.

Which of the below is possible to scale automatically in app service?

Azure App Service environments support autoscaling. You can autoscale individual worker pools based on metrics or schedule. Autoscaling optimizes your resource utilization by automatically growing and shrinking an App Service environment to fit your budget and or load profile.

How the app services can be scaled?

Scale-up (Vertical scaling) This scale-up is done by changing the pricing tier of the app service plan. Additionally, based on the pricing tier, different useful features are available with the azure app service like custom domains and certificates, staging slots, autoscaling, instances, daily backups, and many more.


1 Answers

Is it possible to scale Azure app services programmatically

Yes, we can do that use REST API or SDK. I test the REST API using the fiddler, details please refer to the snapshot, for how to get the authorization, please refer to the document.

Header info:

enter image description here

Body info:

enter image description here

If C# code is possible, please have a try to use
Microsoft.Azure.Management.WebSites to scale Azure app services. More detail info about SDK please refer to the packages.config file. How to registry Azure AD App and how to get Application ID, secretKey and tenantId please refer to the document. The following is the demo code.

  var subscriptionId = "Your subscrption";
  var appId = "Registried Azure Application Id";
  var secretKey = "Secret Key";
  var tenantId = "tenant Id";
  var resourceGroup = "resource group name";
  var servicePlanName = "service plan name";
  var context = new AuthenticationContext("https://login.windows.net/" + tenantId);
  ClientCredential clientCredential = new ClientCredential(appId, secretKey);
  var tokenResponse = context.AcquireTokenAsync("https://management.azure.com/", clientCredential).Result;
  var accessToken = tokenResponse.AccessToken;
  TokenCredentials credential = new TokenCredentials(accessToken);
  var webSiteManagementClient = new Microsoft.Azure.Management.WebSites.WebSiteManagementClient(credential);
  webSiteManagementClient.SubscriptionId = subscriptionId;
  var servicePlan = webSiteManagementClient.AppServicePlans.ListByResourceGroupWithHttpMessagesAsync(resourceGroup).Result.Body.Where(x=>x.Name.Equals(servicePlanName)).FirstOrDefault();
   //scale up/down
    servicePlan.Sku.Family = "P"; 
    servicePlan.Sku.Name = "P1";
    servicePlan.Sku.Size = "P1";
    servicePlan.Sku.Tier = "Premium";
    servicePlan.Sku.Capacity = 2; // scale out: number of instances 
   var updateResult = webSiteManagementClient.AppServicePlans.CreateOrUpdateWithHttpMessagesAsync(resourceGroup, servicePlanName, servicePlan).Result;

packages.config file:

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="Microsoft.Azure.Management.Websites" version="1.6.0-preview" targetFramework="net462" />
  <package id="Microsoft.IdentityModel.Clients.ActiveDirectory" version="3.13.8" targetFramework="net462" />
  <package id="Microsoft.Rest.ClientRuntime" version="2.3.5" targetFramework="net462" />
  <package id="Microsoft.Rest.ClientRuntime.Azure" version="3.3.5" targetFramework="net462" />
  <package id="Newtonsoft.Json" version="6.0.8" targetFramework="net462" />
</packages>

Check the result from the portal.

enter image description here

Note: If the Azure Service plan is updated, it will apply to all of WebApps in the Service plan.

like image 174
Tom Sun - MSFT Avatar answered Sep 23 '22 01:09

Tom Sun - MSFT