简体   繁体   中英

How to change azure portal virtual machine scale set scaling Instance count from c# code?

I want to increase or decrease scaling instance count from my asp.net MVC website or through power-shell command so that I can call power-shell command from website c# code.

I have no idea how to that. It will be great if anyone can help me through this.

Below attached snap will give you exact idea for what I need to increase or decrease. 在此处输入图片说明

Judging from the picture what you actually want to do is to change the capacity (min, max, default) for the default autoscale profile for your Azure VM ScaleSet.

This is done with the Azure RM REST API.

Here is an example which will change your 'Instance Count' to 5:

Request 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 Method : POST

Request body:

{
  "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": []
      }
    ]
  }
}

Note: Names of your Autoscale Settings are 'globally' unique within your subscription. So you might want to give it an unique name, like cpuautoscale-myawsomescaleset0 instead of just cpuautoscale ; unless you plan on re-using the Autoscale setting on other ScaleSets, AppServices or CloudServices.

How to change azure portal virtual machine scale set scaling Instance count from c# code?

We could use the Azure Microsoft.Azure.Management.Fluent SDK to that directly.

Preparation:

Registry an app in the Azure AD and create service principal for accessing the resource. More detail please refer to the document . Then we could get the clientId, clientSecret,tenantId.

I also do a demo with it. You also could change the Tier,SKU name.

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;

在此处输入图片说明

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM