确定Azure Web角色的实例IP地址

本文关键字:实例 IP 地址 角色 Azure Web 确定 | 更新日期: 2023-09-27 18:08:05

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

  <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是我正在寻找在。net/c#中检索(在角色实例本身中)

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

谢谢!

确定Azure Web角色的实例IP地址

您可以使用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())));
            }
        }
    }

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