确定 Azure Web Role 的实例 IP 地址

Determine Azure Web Role's instance IP address

我要找的IP地址不是VIP,也不是给角色实例的私有IP地址,而是publicIP地址您可以使用此配置分配给每个实例:

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

使用 Powershell Cmdlet,然后可以找到分配给每个实例的 IP 地址,如下所示:

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

这将输出结果,如下所示:

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

在这种情况下,IP 地址 137.135.135.186137.135.133.191 是我要检索的 in .net / c#(在角色实例本身中)

如有任何建议,我们将不胜感激。

谢谢!

您可以使用 Azure Management Libraries,它本质上是 Azure Service Management API 的包装器,例如 PowerShell Cmdlet。

我编写了一个简单的控制台应用程序,我在其中使用这些库来获取我的云服务的所有实例的 IP 地址:

    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())));
            }
        }
    }

您可以很好地将此代码用于您的云服务。