无法找出如何使用Python SDK修改Azure Loadbalancer后端池

2024-09-29 20:28:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我在Azure(ARM)中配置了一个负载平衡器,我有两个后端池:prod、stage。通过GUI,当我想将一个登台服务器提升到生产环境时,我会将它从暂存池中移除,并将其放入prod池中。我很困惑这一切是如何工作的,因为当我配置堆栈时,我首先配置负载平衡器,当我配置VM将连接到的NIC时,我将该NIC放在我想要的后端池中。但是,当我想将虚拟机移动到另一个池时,我不再在NIC级别这样做了。我必须在负载平衡器上这样做。在

使用pythonsdk,如果查询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]

打印输出(dir(stage))为:

^{pr2}$

所以当我看到“后端配置”的时候,我觉得我在想什么。当查看我的选项时(通过键入以下内容):

print(stage.backend_ip_configurations)

它返回一个对象数组:

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

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

beip = stage.backend_ip_configurations[0].id

当我看到我对“beip”的选择时,这就是我的死胡同。在

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

打印输出(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。在


Tags: clientbackendformatdirguiprodnetworkazure
2条回答

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

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

Resource references are currently inconvenient to follow, because there's no get() that allow you to fetch using a reference id. So you need to parse the names of the resources that make up the id.

In this case, you need to parse the resource id to get the names 'myresourcegroup', 'app-green04-nic' and 'ipconfig1'. With these, you'll be able to access the NIC with network_client.network_interfaces.get('myresourcegroup, 'app-green04-nic') .

Looking at LoadBalancerOperations, it looks like you should be able to do a get(), modify the contents of the load_balancer.backend_address_pools[0].backend_ip_configurations by adding/modifying/removing ResourceId objects, and then call create_or_update() with the modified LoadBalancer object.

Obtaining a resource id to create a ResourceId is easy, no need to build the string yourself, you can usually just call a get() method and Azure will populate and return the id for you. For example, to get the resource id for a NIC configuration:

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

You can't index by name on those collections, so you'll need to iterate to find the one you are looking for.

如果您有任何进一步的担心,请随时告诉我们。在

看看这个视频和GitHub repo,它演示了如何通过REST和Python修改池成员资格。在

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

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

注意在下面的/src路径具有Python版本

https://github.com/cicorias/AzureLoadbalancedPoolUpdater

方法和顺序

实际上,您是从NIC的方向而不是从负载平衡器的角度出发。更新(PUT)针对的是“NIC”。在

总的顺序是
  1. 获取虚拟机及其网卡
  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

相关问题 更多 >

    热门问题