Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
150 views
in Technique[技术] by (71.8m points)

c# - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...