如何更改azure门户虚拟机规模设置扩展c#代码的实例数?

时间:2018-02-20 12:39:43

标签: c# asp.net-mvc powershell azure azure-virtual-machine

我想从我的asp.net MVC网站或通过power-shell命令增加或减少扩展实例数,以便我可以从网站c#代码调用power-shell命令。

我不知道该怎么做。 如果有人能帮我解决这个问题,那就太好了。

下面附带的快照会让您准确了解我需要增加或减少的内容。 enter image description here

2 个答案:

答案 0 :(得分:1)

从图中判断,您实际想要做的是更改Azure VM ScaleSet的默认自动缩放配置文件的容量(最小值,最大值,默认值)。

这是通过Azure RM REST API完成的。

以下示例将“实例计数”更改为5:

请求URI https://management.azure.com/subscriptions/[YOUR_SUBSCRIPTION_ID]/resourceGroups/[YOUR_RESOURCE_GROUP_NAME/providers/microsoft.insights/autoscalesettings/cpuautoscale?api-version=2015-04-01

HTTP方法POST

请求正文:

{
  "id": "/subscriptions/[YOUR_SUBSCRIPTION_ID]/resourceGroups/[YOUR_RESOURCE_GROUP_NAME]/providers/microsoft.insights/autoscalesettings/cpuautoscale",
  "name": "cpuautoscale",
  "type": "Microsoft.Insights/autoscaleSettings",
  "location": [YOUR_AZURE_REGION],
  "tags": {
    "$type": "Microsoft.WindowsAzure.Management.Common.Storage.CasePreservedDictionary, Microsoft.WindowsAzure.Management.Common.Storage"
  },
  "properties": {
    "profiles": [
      {
        "name": "automatic",
        "capacity": {
          "minimum": "5",
          "maximum": "5",
          "default": "5"
        },
        "rules": []
      }
    ],
    "enabled": true,
    "name": "cpuautoscale",
    "targetResourceUri": "/subscriptions/[YOUR_SUBSCRIPTION_ID]/resourceGroups/[YOUR_RESOURCE_GROUP_NAME]/providers/Microsoft.Compute/virtualMachineScaleSets/[YOUR_SCALESET_NAME]",
    "notifications": [
      {
        "operation": "Scale",
        "email": {
          "sendToSubscriptionAdministrator": false,
          "sendToSubscriptionCoAdministrators": false,
          "customEmails": []
        },
        "webhooks": []
      }
    ]
  }
}

注意:您的自动定标设置的名称在您的订阅中是“全局”唯一的。因此,您可能希望为其指定一个唯一名称,例如cpuautoscale-myawsomescaleset0,而不仅仅是cpuautoscale;除非您计划在其他ScaleSets,AppServices或CloudServices上重新使用自动缩放设置。

答案 1 :(得分:0)

  

如何更改azure门户网站虚拟机规模设置扩展实例数量来自c#代码?

我们可以直接使用Azure Microsoft.Azure.Management.Fluent SDK。

<强>制备

在Azure AD中注册应用程序并创建用于访问资源的服务主体。更多细节请参考document。然后我们可以得到clientId,clientSecret,tenantId。

我也用它做了一个演示。您还可以更改 Tier,SKU名称。

var clientId = "clientId ";
var clientSecret = "clientSecret";
var tenantId = "tenant Id";
var resoureGroupName = "resource group name";
var vmScalesetName = "vm scale set name";
var subscriptionName = "subscriptionName";
var credentials = SdkContext.AzureCredentialsFactory.FromServicePrincipal(clientId, clientSecret,
            tenantId, AzureEnvironment.AzureGlobalCloud);
var azure = Azure
            .Configure()
            .WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic)
            .Authenticate(credentials)
            .WithDefaultSubscription();
var vMachineScaleSet = azure.VirtualMachineScaleSets.GetByResourceGroup(resoureGroupName, vmScalesetName);

var computeManagementClient =
            new ComputeManagementClient(credentials) {SubscriptionId = subscriptionName };
var update=  computeManagementClient.VirtualMachineScaleSets.CreateOrUpdateWithHttpMessagesAsync(resoureGroupName, vmScalesetName,
            new VirtualMachineScaleSetInner
            { 
               Location = vMachineScaleSet.RegionName,
               Sku = new Sku
               {
                   Capacity = 2, //set instance count
                   Name = vMachineScaleSet.Sku.Sku.Name,
                   Tier = vMachineScaleSet.Sku.Sku.Tier
               }
            }).Result;

enter image description here

<强> Packages.config

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="Microsoft.Azure.KeyVault.Core" version="1.0.0" targetFramework="net471" />
  <package id="Microsoft.Azure.Management.AppService.Fluent" version="1.6.0" targetFramework="net471" />
  <package id="Microsoft.Azure.Management.Batch.Fluent" version="1.6.0" targetFramework="net471" />
  <package id="Microsoft.Azure.Management.BatchAI.Fluent" version="1.6.0" targetFramework="net471" />
  <package id="Microsoft.Azure.Management.Cdn.Fluent" version="1.6.0" targetFramework="net471" />
  <package id="Microsoft.Azure.Management.Compute.Fluent" version="1.6.0" targetFramework="net471" />
  <package id="Microsoft.Azure.Management.ContainerInstance.Fluent" version="1.6.0" targetFramework="net471" />
  <package id="Microsoft.Azure.Management.ContainerRegistry.Fluent" version="1.6.0" targetFramework="net471" />
  <package id="Microsoft.Azure.Management.ContainerService.Fluent" version="1.6.0" targetFramework="net471" />
  <package id="Microsoft.Azure.Management.CosmosDB.Fluent" version="1.6.0" targetFramework="net471" />
  <package id="Microsoft.Azure.Management.Dns.Fluent" version="1.6.0" targetFramework="net471" />
  <package id="Microsoft.Azure.Management.Fluent" version="1.6.0" targetFramework="net471" />
  <package id="Microsoft.Azure.Management.Graph.RBAC.Fluent" version="1.6.0" targetFramework="net471" />
  <package id="Microsoft.Azure.Management.KeyVault.Fluent" version="1.6.0" targetFramework="net471" />
  <package id="Microsoft.Azure.Management.Locks.Fluent" version="1.6.0" targetFramework="net471" />
  <package id="Microsoft.Azure.Management.Monitor.Fluent" version="1.6.0" targetFramework="net471" />
  <package id="Microsoft.Azure.Management.Msi.Fluent" version="1.6.0" targetFramework="net471" />
  <package id="Microsoft.Azure.Management.Network.Fluent" version="1.6.0" targetFramework="net471" />
  <package id="Microsoft.Azure.Management.Redis.Fluent" version="1.6.0" targetFramework="net471" />
  <package id="Microsoft.Azure.Management.ResourceManager.Fluent" version="1.6.0" targetFramework="net471" />
  <package id="Microsoft.Azure.Management.Search.Fluent" version="1.6.0" targetFramework="net471" />
  <package id="Microsoft.Azure.Management.ServiceBus.Fluent" version="1.6.0" targetFramework="net471" />
  <package id="Microsoft.Azure.Management.Sql.Fluent" version="1.6.0" targetFramework="net471" />
  <package id="Microsoft.Azure.Management.Storage.Fluent" version="1.6.0" targetFramework="net471" />
  <package id="Microsoft.Azure.Management.TrafficManager.Fluent" version="1.6.0" targetFramework="net471" />
  <package id="Microsoft.Data.Edm" version="5.8.2" targetFramework="net471" />
  <package id="Microsoft.Data.OData" version="5.8.2" targetFramework="net471" />
  <package id="Microsoft.Data.Services.Client" version="5.8.2" targetFramework="net471" />
  <package id="Microsoft.IdentityModel.Clients.ActiveDirectory" version="2.28.3" targetFramework="net471" />
  <package id="Microsoft.Rest.ClientRuntime" version="2.3.9" targetFramework="net471" />
  <package id="Microsoft.Rest.ClientRuntime.Azure" version="3.3.10" targetFramework="net471" />
  <package id="Microsoft.Rest.ClientRuntime.Azure.Authentication" version="2.3.2" targetFramework="net471" />
  <package id="Newtonsoft.Json" version="6.0.8" targetFramework="net471" />
  <package id="System.ComponentModel.EventBasedAsync" version="4.0.11" targetFramework="net471" />
  <package id="System.Dynamic.Runtime" version="4.0.0" targetFramework="net471" />
  <package id="System.Linq.Queryable" version="4.0.0" targetFramework="net471" />
  <package id="System.Net.Requests" version="4.0.11" targetFramework="net471" />
  <package id="System.Spatial" version="5.8.2" targetFramework="net471" />
  <package id="WindowsAzure.Storage" version="8.1.4" targetFramework="net471" />
</packages>