是否可以以编程方式扩展 Azure 应用服务 [英] Is it possible to scale Azure app services programmatically

查看:26
本文介绍了是否可以以编程方式扩展 Azure 应用服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种以编程方式扩展/缩减 Azure 应用服务实例的方法.几个月前(例如在下面的链接中)已经问过这个问题,当时无法回答,所以我只是想知道现在是否发生了某些变化和/或某些新功能可用.

身体信息:

如果可以使用C#代码,请尝试使用

<块引用>

注意:如果 Azure 服务计划更新,它将适用于服务计划中的所有 WebApp.

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.

解决方案

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:

Body info:

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.

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

这篇关于是否可以以编程方式扩展 Azure 应用服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆