我在 Azure (ARM) 中配置了一个负载均衡器,并且我有 2 个后端池:prod、stage。通过 GUI,当我想将暂存服务器提升到生产环境时,我将其从暂存池中移除并将其放入生产池中。我对这一切是如何工作的感到非常困惑,因为当我配置堆栈时,我首先配置负载均衡器,当我配置虚拟机将附加到的 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
当我看到“哔”的选项时,这就是我的死胡同。
/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']
除了通过 GUI 之外,我不知道如何查看后端池中有哪些 NIC 并修改该池。
请您参考如下方法:
观看此视频和 GitHub 存储库,其中演示了如何通过 REST 和 Python 处理修改池成员资格。
最后,它是关于获取通过更新发送的正确 json
https://mix.office.com/watch/f4cvoa3cnfoe
注意下面/src路径下有Python版本
https://github.com/cicorias/AzureLoadbalancedPoolUpdate r
方法和顺序
您实际上是从 NIC 方向而不是负载均衡器着手的。更新 (PUT) 针对“NIC”。
一般命令是- 获取 VM 及其 NIC
- 获取网卡与后端池的关系
- 通过将
loadBalancerBackendAddressPools留空来移除 - 通过将资源 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
