【问题标题】:Determine Azure Web Role's instance IP address确定 Azure Web 角色的实例 IP 地址
【发布时间】:2015-08-16 14:14:36
【问题描述】:

我要查找的 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# 中检索的地址 strong>(在角色实例本身中)

对此的任何建议将不胜感激。

谢谢!

【问题讨论】:

    标签: c# .net azure azure-cloud-services


    【解决方案1】:

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

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

    【讨论】:

    • 有点用。但是,我觉得当您应该能够通过 RoleEnvironment 或类似的方式获取公共 IP 端点时,您需要使用管理 API 来获取公共 IP 端点,这有点怪怪的。
    • 我认为 RoleEnvironment 不会起作用,因为如果我没记错的话,公共 IP 会分配给位于您的角色实例前面的负载均衡器。
    • 确实有道理。
    猜你喜欢
    • 2013-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-04
    • 1970-01-01
    • 2016-09-30
    • 1970-01-01
    相关资源
    最近更新 更多