2

我在 Azure (ARM) 中配置了负载均衡器,并且有 2 个后端池:prod、stage。通过 GUI,当我想将登台服务器提升到生产环境时,我将其从舞台池中删除并将其放入产品池中。我对这一切是如何工作的感到非常困惑,因为当我配置堆栈时,我首先配置负载平衡器,当我配置 VM 将附加到的 NIC 时,我将该 NIC 放置在我想要的后端池中。但是,当我想将 VM 移动到另一个池时,我不再在 NIC 级别执行此操作。我必须在负载均衡器上这样做。

使用 Python SDK,如果我查询 LB,我可以看到后端池中的 NIC,但似乎没有办法修改它。我还可以查询 NIC 并查看它与哪个后端池相关联,但同样无法修改(据我所知)。这是我到目前为止所拥有的:

# Create the client
network_client = azure.mgmt.network.NetworkResourceProviderClient(creds)

# Get all LBs
lbs = network_client.load_balancers

# select LB in question
lb = lbs.get(group,'cc-lb01')

# get all pools
pools = lb.load_balancer.backend_address_pools

# set variables for pools
prod = pools[0]
stage = pools[1]

print(dir(stage)) 的输出是:

['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_backend_ip_configurations', '_etag', '_id', '_load_balancing_rules', '_name', '_provisioning_state', 'backend_ip_configurations', 'etag', 'id', 'load_balancing_rules', 'name', 'provisioning_state']

所以当我看到'backend_ip_configurations'时,我以为我在做某事。当在那里查看我的选项时(通过输入):

print(stage.backend_ip_configurations)

它返回一个对象数组:

[<azure.mgmt.network.networkresourceprovider.ResourceId object at 0x03C9C090>]

该数组中只有一项,因此我将该项设置为一个变量:

beip = stage.backend_ip_configurations[0].id

当我看到“beip”有什么选择时,这就是我的死胡同。

/subscriptions/xxx-xxx-xxx/resourceGroups/myresourcegroup/providers/Microsoft.Network/networkInterfaces/app-green04-nic/ipConfigurations/ipconfig1

print(dir(beip)) 的输出是:

['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']

我无法弄清楚如何查看后端池中的 NIC 并修改该池,而不是通过 GUI。

4

2 回答 2

2

观看此视频和 GitHub 存储库,其中演示了如何通过 REST 和 Python 修改池成员资格。

最后,它是关于获取通过更新发送的正确 json

https://mix.office.com/watch/f4cvoa3cnfoe

注意以下/src路径具有Python版本

https://github.com/cicorias/AzureLoadbalancedPoolUpdate r

方法和顺序

您实际上是从 NIC 方向进行的,而不是负载均衡器。更新 (PUT) 是针对“NIC”的。

一般顺序是
  1. 获取 VM 及其 NIC
  2. 获取 NIC 与后端池的关系
  3. loadBalancerBackendAddressPools留空删除
  4. 通过将资源 ID 放入 `loadBalancerBackendAddressPools' 数组中添加

通过 PUT 创建 REST 请求在这里。

def build_request(vm_object, nic_object, load_balancer=None):
"""
:param vm_object : azure.mgmt.compute.VirtualMachine
:param nic_object : azure.mgmt.network.networkresourceprovider.NetworkInterface
:param load_balancer : azure.mgmt.network.LoadBalancer
:return: dict
"""
if load_balancer == None:
    backend_pool = []
else:
    backend_pool = [{ 'id' : load_balancer.load_balancer.backend_address_pools[0].id }]

request = {
    'properties': {
        'virtualMachine' : {
            'id' : vm_object.virtual_machine.id
            },
        'ipConfigurations' : [{ #may have to build by hand
            'properties' : {
                'loadBalancerBackendAddressPools' : backend_pool,
                'subnet' : {
                    'id' :  nic_object.ip_configurations[0].subnet.id
                    }
                },
            'name' : nic_object.ip_configurations[0].name,
            'id' : nic_object.ip_configurations[0].id
             }]
        },
    'id' : nic_object.id,
    'name' : nic_object.name,
    'location' : vm_object.virtual_machine.location,
    'type' : 'Microsoft.Network/networkInterfaces'
    }


return request
于 2015-11-28T20:25:32.683 回答
1

Hugues 在以下链接的 github 上回答了同样的问题:https ://github.com/Azure/azure-sdk-for-python/issues/471

如果任何其他社区成员有类似的询问,请在此处引用答案:

资源引用目前不方便遵循,因为没有 get() 允许您使用引用 id 获取。所以需要解析构成id的资源名称。

在这种情况下,您需要解析资源 id 以获取名称“myresourcegroup”、“app-green04-nic”和“ipconfig1”。有了这些,您将能够使用 network_client.network_interfaces.get('myresourcegroup, 'app-green04-nic') 访问 NIC。

查看 LoadBalancerOperations,看起来您应该能够执行 get(),通过添加/修改/删除 ResourceId 对象来修改 load_balancer.backend_address_pools[0].backend_ip_configurations 的内容,然后使用修改后的 LoadBalancer 调用 create_or_update()目的。

获取资源 id 以创建 ResourceId 很容易,无需自己构建字符串,通常只需调用 get() 方法,Azure 将为您填充并返回 id。例如,要获取 NIC 配置的资源 id:

result = network_client.network_interfaces.get('myresourcegroup', 'app-green04-nic')
result.network_interface.ip_configurations[0].name
#ipconfig1
result.network_interface.ip_configurations[0].id
#/subscriptions/xxx-xxx-xxx/resourceGroups/myresourcegroup/providers/Microsoft.Network/networkInterfaces/app-green04-nic/ipConfigurations/ipconfig1

您无法按名称对这些集合进行索引,因此您需要迭代以找到您要查找的集合。

如果您有任何进一步的疑虑,请随时告诉我们。

于 2015-09-14T21:31:04.103 回答