Determine Azure Web Role's instance IP address

家住魔仙堡 提交于 2019-12-11 03:46:50

问题


The IP address I am looking for is not the VIP, nor the private IP address that is given to the role instance, but the public IP address that you can assign to each instance using this configuration:

  <NetworkConfiguration>
    <AddressAssignments>
      <InstanceAddress roleName="WebRole1">
        <PublicIPs>
          <PublicIP name="public" />
        </PublicIPs>
      </InstanceAddress>
    </AddressAssignments>
  </NetworkConfiguration>

Using Powershell Cmdlets, one is then able to find the IP address given to each instance like this:

PS C:\> Get-AzureService -ServiceName "rr-testservice" | Get-AzureRole -InstanceDetails

Which will output the results, like this:

InstanceErrorCode            :
InstanceFaultDomain          : 0
InstanceName                 : WebRole1_IN_0
InstanceSize                 : Small
InstanceStateDetails         :
InstanceStatus               : ReadyRole
InstanceUpgradeDomain        : 0
RoleName                     : WebRole1
DeploymentID                 : 69a82ec9bb094c31a1b79021c0f3bbf2
IPAddress                    : 100.79.164.151
PublicIPAddress              : 137.135.135.186
PublicIPName                 : public
PublicIPIdleTimeoutInMinutes :
PublicIPDomainNameLabel      :
PublicIPFqdns                : {}
ServiceName                  : rr-testservice
OperationDescription         : Get-AzureRole
OperationId                  : 00400634-f65d-095b-947f-411e69b9a053
OperationStatus              : Succeeded

Where in this case the IP addresses 137.135.135.186 and 137.135.133.191 are the ones I am looking for to retrieve in .net / c# (in the role instance itself)

Any advice on this would be greatly appreciated.

Thanks!


回答1:


You could make use of Azure Management Libraries which are essentially a wrapper over Azure Service Management API like PowerShell Cmdlets.

I wrote a simple console application where I made use of these libraries to fetch the IP address of all instances of my cloud service:

    static void GetCloudServiceDetails()
    {
        var subscriptionId = "<your azure subscription id>";
        var managementCertDataFromPublishSettingsFile = "<management cert data from a publish settings file>";
        var cert = new X509Certificate2(Convert.FromBase64String(managementCertDataFromPublishSettingsFile));
        var credentials = new CertificateCloudCredentials(subscriptionId, cert);
        var computeManagementClient = new ComputeManagementClient(credentials);
        var cloudServiceName = "<your cloud service name>";
        var cloudServiceDetails = computeManagementClient.HostedServices.GetDetailed(cloudServiceName);
        var deployments = cloudServiceDetails.Deployments;
        foreach (var deployment in deployments)
        {
            Console.WriteLine("Deployment Slot: " + deployment.DeploymentSlot);
            Console.WriteLine("-----------------------------------------------");
            foreach(var instance in deployment.RoleInstances)
            {
                Console.WriteLine("Instance name: " + instance.InstanceName + "; IP Address: " + string.Join(", ", instance.PublicIPs.Select(c => c.Address.ToString())));
            }
        }
    }

You could very well use this code for your Cloud Service.



来源:https://stackoverflow.com/questions/32036049/determine-azure-web-roles-instance-ip-address

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!