是否可以使用 arm 模板使用新的 lb 规则或健康探测更新现有的 azure 负载均衡器

Is it possible to update an existing azure load balancer with new lb rules or health probe using arm template

我尝试了上面的代码,但无法正确使用。而且,我可以看到它要求旧规则。我正在尝试在不影响旧规则的情况下更新新规则。

{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "lbName": {
            "type": "string"
        },
        "lbLocation": {
            "type": "string"
        },
        "vnetId": {
            "type": "string"
        },
        "subnetId": {
            "type": "string"
        },
        "healthProbes": {
            "type": "array",
            "defaultValue": [
                {
                    "name": "HealthProbe-TCP-80",
                    "protocol": "TCP",
                    "port": "80",
                    "intervalInSeconds": "15",
                    "numberOfProbes": "2",
                    "requestPath": null
                },
                {
                    "name": "HealthProbe-TCP-443",
                    "protocol": "TCP",
                    "port": "443",
                    "intervalInSeconds": "15",
                    "numberOfProbes": "2",
                    "requestPath": null
                },
                {
                    "name": "HealthProbe-HTTP-443",
                    "protocol": "HTTP",
                    "port": "443",
                    "intervalInSeconds": "15",
                    "numberOfProbes": "2",
                    "requestPath": "TestPath"
                }
            ]
        }
    },
    "variables": {
        "virtualNetworkName": "[parameters('vnetId')]",
        "subnetName": "[Parameters('subnetId')]",
        "frontEndIPConfigName": "LoadBalancerFrontEnd",
        "feId": "[concat(parameters('lbName'),'/frontendIPConfigurations/loadBalancerFrontend')]",
        "backendPoolConfigName": "qwertyBEPool",
        "beId": "[concat(parameters('lbName'), '/backendAddressPools/qwertyBEPool')]",
        "subnetRef": "[resourceId('Microsoft.Network/virtualNetworks/subnets', variables('virtualNetworkName'), variables('subnetName'))]"
    },
    "resources": [
        {
            "apiVersion": "2019-06-01",
            "name": "[parameters('lbName')]",
            "type": "Microsoft.Network/loadBalancers",
            "location": "[parameters('lbLocation')]",
            "properties": {
                "frontendIPConfigurations": [
                    {
                        "name": "LoadBalancerFrontEnd",
                        "properties": {
                            "subnet": {
                                "id": "[variables('subnetRef')]"
                            }
                        }
                    }
                ],
                "backendAddressPools": [
                    {
                        "name": "LoadBalancerBackend"
                    }
                ],
                "copy": [
                    {
                        "name": "probes",
                        "count": "[length(parameters('healthProbes'))]",
                        "input": {
                            "name": "[parameters('healthProbes')[copyIndex('probes')].name]",
                            "properties": {
                                "protocol": "[parameters('healthProbes')[copyIndex('probes')].protocol]",
                                "port": "[parameters('healthProbes')[copyIndex('probes')].port]",
                                "intervalInSeconds": "[parameters('healthProbes')[copyIndex('probes')].intervalInSeconds]",
                                "numberOfProbes": "[parameters('healthProbes')[copyIndex('probes')].numberOfProbes]",
                                "requestPath": "[parameters('healthProbes')[copyIndex('probes')].requestPath]"
                            }
                        }
                    }
                ]
            }
        }
    ]
}

update a resource in an Azure Resource Manager template,您需要在第二个模板中指定更新的资源,该模板使用 Microsoft.Resources/deployments 资源类型作为子模板链接或包含。

First, you must reference the resource once in the template to create it and then reference the resource by the same name to update it later. However, if two resources have the same name in a template, Resource Manager throws an exception. To avoid this error, specify the updated resource in a second template that's either linked or included as a subtemplate using the Microsoft.Resources/deployments resource type.

Second, you must either specify the name of the existing property to change or a new name for a property to add in the nested template. You must also specify the original properties and their original values. If you fail to provide the original properties and values, Resource Manager assumes you want to create a new resource and deletes the original resource.

例如,如果您已经从这个 201-2-vms-loadbalancer-lbrules template 创建了资源,您只需从 Load balancer----Settings---Export 下载现有模板模板。然后添加新的 loadBalancingRules 和探测器,编辑模板以满足您的要求,然后使用此命令重新部署编辑后的完整模板

New-AzResourceGroupDeployment -ResourceGroupName yourRG -mode Incremental -TemplateFile ".\template.json" -TemplateParameterFile ".\parameters.json"

例如,

 "loadBalancingRules": [
                    {
                        "name": "LBRule",
                        "properties": {
                            "frontendIPConfiguration": {
                                "id": "[concat(resourceId('Microsoft.Network/loadBalancers', parameters('loadBalancers_myLB_name')), '/frontendIPConfigurations/LoadBalancerFrontEnd')]"
                            },
                            "frontendPort": 80,
                            "backendPort": 80,
                            "enableFloatingIP": false,
                            "idleTimeoutInMinutes": 5,
                            "protocol": "Tcp",
                            "enableTcpReset": false,
                            "loadDistribution": "Default",
                            "backendAddressPool": {
                                "id": "[concat(resourceId('Microsoft.Network/loadBalancers', parameters('loadBalancers_myLB_name')), '/backendAddressPools/BackendPool1')]"
                            },
                            "probe": {
                                "id": "[concat(resourceId('Microsoft.Network/loadBalancers', parameters('loadBalancers_myLB_name')), '/probes/tcpProbe')]"
                            }
                        }
                    },
                        {
                        "name": "LBRule-new",
                        "properties": {
                            "frontendIPConfiguration": {
                                "id": "[concat(resourceId('Microsoft.Network/loadBalancers', parameters('loadBalancers_myLB_name')), '/frontendIPConfigurations/LoadBalancerFrontEnd')]"
                            },
                            "frontendPort": 8080,
                            "backendPort": 8080,
                            "enableFloatingIP": false,
                            "idleTimeoutInMinutes": 5,
                            "protocol": "Tcp",
                            "enableTcpReset": false,
                            "loadDistribution": "Default",
                            "backendAddressPool": {
                                "id": "[concat(resourceId('Microsoft.Network/loadBalancers', parameters('loadBalancers_myLB_name')), '/backendAddressPools/BackendPool1')]"
                            },
                            "probe": {
                                "id": "[concat(resourceId('Microsoft.Network/loadBalancers', parameters('loadBalancers_myLB_name')), '/probes/tcpProbe')]"
                            }
                        }
                    }
                ],
                "probes": [
                    {
                        "name": "tcpProbe",
                        "properties": {
                            "protocol": "Tcp",
                            "port": 80,
                            "intervalInSeconds": 5,
                            "numberOfProbes": 2
                        }
                    },
                      {
                        "name": "tcpProbe-new",
                        "properties": {
                            "protocol": "Tcp",
                            "port": 8080,
                            "intervalInSeconds": 5,
                            "numberOfProbes": 2
                        }
                    }
                ],
....

您将需要 运行 来自 ARM 模板的 PowerShell 或 CLI