简体   繁体   中英

Azure, Cloud Service, scale with .NET

How can I use the Microsoft.Azure.Management.Compute to change instances number on Cloud Service ? Or any other .NET way to do it ?

Not really an answer to your question (as I have not used this library) but I will try to give you a general idea (in the hope that it helps you in finding the right methods in the library).

As you may already know that the number of instances for a Cloud Service Role is stored in the configuration file (*.cscfg) which is an XML file. Scaling a Cloud Service involves changing this configuration file.

What you would need to do is first get this configuration file. Once you get this configuration file, you would create an XML document out of it by reading the contents of the file.

Next you need to find <Instances> node in this XML document for the desired role that you wish to scale. You would update the value of count attribute of this node to the desired instance count.

Once you do that, you would need to apply this changed configuration. REST API operation that gets called is Change Deployment Configuration . This will initiate scaling of your Cloud Service.

Microsoft.Azure.Management.Compute client library is for Virtual Machines under Azure Resource Manager. For managing Azure Cloud Services, we could use this Management Libraries for .NET(Microsoft.WindowsAzure.Management.Compute) .

The following sample code works fine on my side, please refer to it.

    static void Main(string[] args)
    {
        var managementCertThumbprint = "{CertThumbprint}";
        var subscriptionid = "{subscriptionid}";
        var servicename = "fehancloud";

        X509Certificate2 managementCert = FindX509Certificate(managementCertThumbprint);

        SubscriptionCloudCredentials creds = new CertificateCloudCredentials(subscriptionid, managementCert);
        ComputeManagementClient cmclient = new ComputeManagementClient(creds);

        var detailed = cmclient.HostedServices.GetDetailed(servicename);

        var deployment = detailed.Deployments
            .First(_ => _.DeploymentSlot == DeploymentSlot.Production);

        var xConfig = XDocument.Parse(deployment.Configuration);

        Func<string, XName> n = (name) => XName.Get(name,
            "http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceConfiguration");

        Func<XDocument, string, int> getInstanceCount = (doc, rolename) =>
        {
            var role = doc.Root.Elements(n("Role")).FirstOrDefault(_ => _.Attribute("name").Value == rolename);
            if (role == null) return -1;
            var v = role.Element(n("Instances")).Attribute("count").Value;
            Console.WriteLine(int.Parse(v));
            return int.Parse(v);
        };

        Action<XDocument, string, int> setInstanceCount = (doc, rolename, newInstanceCount) =>
        {
            if (newInstanceCount < 1)
            {
                newInstanceCount = 1;
            }

            var role = doc.Root.Elements(n("Role")).FirstOrDefault(_ => _.Attribute("name").Value == rolename);
            role.Element(n("Instances")).Attribute("count").Value = newInstanceCount.ToString();
        };

        Action<XDocument, string, int> changeInstanceCount = (doc, rolename, deltaCount) =>
        {
            int oldCount = getInstanceCount(doc, rolename);
            var newCount = oldCount + deltaCount;
            setInstanceCount(doc, rolename, newCount);
        };

        changeInstanceCount(xConfig, "WorkerRole1", 2);

        var response = cmclient.Deployments.ChangeConfigurationBySlot(
            serviceName: detailed.ServiceName,
            deploymentSlot: deployment.DeploymentSlot,
            parameters: new DeploymentChangeConfigurationParameters()
            {
                Configuration = xConfig.ToString()
            });
    }

    private static X509Certificate2 FindX509Certificate(string managementCertThumbprint)
    {
        X509Store store = null;

        try
        {
            store = new X509Store(StoreName.My, StoreLocation.CurrentUser); //set StoreLocation based on your specified value 
            store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly);
            X509Certificate2Collection certs = store.Certificates.Find(
                findType: X509FindType.FindByThumbprint,
                findValue: managementCertThumbprint,
                validOnly: false);
            if (certs.Count == 0)
            {
                return null;
            }

            return certs[0];
        }
        finally
        {
            if (store != null) store.Close();
        }
    }

Note : I uploaded a management certificate.

packages.config

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="Hyak.Common" version="1.0.2" targetFramework="net45" />
  <package id="Microsoft.Azure.Common" version="2.0.4" targetFramework="net45" />
  <package id="Microsoft.Azure.Common.Dependencies" version="1.0.0" targetFramework="net45" />
  <package id="Microsoft.Bcl" version="1.1.9" targetFramework="net45" />
  <package id="Microsoft.Bcl.Async" version="1.0.168" targetFramework="net45" />
  <package id="Microsoft.Bcl.Build" version="1.0.14" targetFramework="net45" />
  <package id="Microsoft.IdentityModel.Clients.ActiveDirectory" version="3.13.8" targetFramework="net45" />
  <package id="Microsoft.Net.Http" version="2.2.22" targetFramework="net45" />
  <package id="Microsoft.WindowsAzure.Management.Compute" version="13.0.0" targetFramework="net45" />
  <package id="Newtonsoft.Json" version="6.0.4" targetFramework="net45" />
</packages>

After it executed changeInstanceCount method , you could see new instances are starting.

在此处输入图片说明

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