【问题标题】:Can public IP of VMScaleset instance be modified with static IP from a given public IpPrefix?是否可以使用给定公共 IpPrefix 的静态 IP 修改 VMScaleset 实例的公共 IP?
【发布时间】:2021-08-13 05:05:33
【问题描述】:

我需要从给定的 IpPublicPrefix 更新/设置 VMScaleset 实例的公共 IP 地址(以便客户将这些 IP 列入白名单)。 我尝试使用两个实例(具有虚拟网络、子网、网络接口)和 PublicIpPrefix 创建 VMScaleset,但 Azure doc 建议的代码根本不起作用。 https://docs.microsoft.com/en-us/powershell/module/azurerm.network/set-azurermnetworkinterface?view=azurermps-6.13.0

第一个问题:下面的代码不会返回我在上面创建的网络接口。这是 Azure API 中的错误吗? Get-AzureRmNetworkInterface -ResourceGroupName "ResourceGroup1" -Name "NetworkInterface1" 它只返回为 VM 创建的网络接口列表(不是从 VMSS 创建的),它不包括在 VMSS 创建期间创建的网络接口。

第二个问题:对于某些 cmets,VMSS 的 NetworkInterface 不会显示在 Azure 门户(搜索网络接口)或 AzureRM API 中,那么我们应该如何知道和更新 VMSS 或其实例的 NIC?

我一直在使用 AzureRm 模块 6.13.1

【问题讨论】:

    标签: azure azure-virtual-machine network-interface azure-public-ip


    【解决方案1】:

    对于第一个问题,规模集实例的公共 IP 不是 Azure 门户中的单独资源,我们无法使用Get-AzureRmNetworkInterface 获取网络接口信息。

    对于第二个问题,您可以通过 ARM 模板为每个虚拟机创建一个具有公共 IP 的规模集。您可以将 publicIpAddressConfiguration JSON 属性添加到规模集 ipConfigurations 部分。

    请注意,IpPublicPrefix 需要标准 SKU 负载平衡器和公共 IP 地址。这是一个工作示例。

    {
        "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
        "contentVersion": "1.0.0.0",
        "parameters": {
            "vmSku": {
                "type": "string",
                "defaultValue": "Standard_A1_v2",
                "metadata": {
                    "description": "Size of VMs in the VM Scale Set."
                }
            },
            "windowsOSVersion": {
                "type": "string",
                "defaultValue": "2019-Datacenter",
                "allowedValues": [
                    "2008-R2-SP1",
                    "2012-Datacenter",
                    "2012-R2-Datacenter",
                    "2016-Datacenter",
                    "2019-Datacenter"
                ],
                "metadata": {
                    "description": "The Windows version for the VM. This will pick a fully patched image of this given Windows version. Allowed values: 2008-R2-SP1, 2012-Datacenter, 2012-R2-Datacenter & 2016-Datacenter, 2019-Datacenter."
                }
            },
            "vmssName": {
                "type": "string",
                "minLength": 3,
                "maxLength": 61,
                "metadata": {
                    "description": "String used as a base for naming resources. Must be 3-61 characters in length and globally unique across Azure. A hash is prepended to this string for some resources, and resource-specific information is appended."
                }
            },
            "instanceCount": {
                "type": "int",
                "defaultValue": 3,
                "minValue": 1,
                "maxValue": 100,
                "metadata": {
                    "description": "Number of VM instances (100 or less)."
                }
            },
            "singlePlacementGroup": {
                "type": "bool",
                "defaultValue": true,
                "metadata": {
                    "description": "When true this limits the scale set to a single placement group, of max size 100 virtual machines. NOTE: If singlePlacementGroup is true, it may be modified to false. However, if singlePlacementGroup is false, it may not be modified to true."
                }
            },
            "adminUsername": {
                "type": "string",
                "defaultValue": "vmssadmin",
                "metadata": {
                    "description": "Admin username on all VMs."
                }
            },
            "adminPassword": {
                "type": "securestring",
                "metadata": {
                    "description": "Admin password on all VMs."
                }
            },
            
            "location": {
                "type": "string",
                "defaultValue": "[resourceGroup().location]",
                "metadata": {
                    "description": "Location for all resources."
                }
            },
            "platformFaultDomainCount": {
                "type": "int",
                "defaultValue": 1,
                "metadata": {
                    "description": "Fault Domain count for each placement group."
                }
            },
            "publicIPPrefixes_pubprefix_name": {
                "defaultValue": "vmsspublicprefix",
                "type": "string"
            }
        },
        "variables": {
            "namingInfix": "[toLower(substring(concat(parameters('vmssName'), uniqueString(resourceGroup().id)), 0, 9))]",
            "longNamingInfix": "[toLower(parameters('vmssName'))]",
            "addressPrefix": "10.0.0.0/16",
            "subnetPrefix": "10.0.0.0/24",
            "virtualNetworkName": "[concat(variables('namingInfix'), 'vnet')]",
            "publicIPAddressName": "[concat(variables('namingInfix'), 'pip')]",
            "subnetName": "[concat(variables('namingInfix'), 'subnet')]",
            "loadBalancerName": "[concat(variables('namingInfix'), 'lb')]",
            "publicIPAddressID": "[resourceId('Microsoft.Network/publicIPAddresses',variables('publicIPAddressName'))]",
            "lbProbeID": "[resourceId('Microsoft.Network/loadBalancers/probes',variables('loadBalancerName'), 'tcpProbe')]",
            "natPoolName": "[concat(variables('namingInfix'), 'natpool')]",
            "bePoolName": "[concat(variables('namingInfix'), 'bepool')]",
            "lbPoolID": "[resourceId('Microsoft.Network/loadBalancers/backendAddressPools',variables('loadBalancerName'),variables('bePoolName'))]",
            "natStartPort": 50000,
            "natEndPort": 50119,
            "natBackendPort": 3389,
            "nicName": "[concat(variables('namingInfix'), 'nic')]",
            "ipConfigName": "[concat(variables('namingInfix'), 'ipconfig')]",
            "frontEndIPConfigID": "[resourceId('Microsoft.Network/loadBalancers/frontendIPConfigurations',variables('loadBalancerName'),'loadBalancerFrontEnd')]",
            "osType": {
                "publisher": "MicrosoftWindowsServer",
                "offer": "WindowsServer",
                "sku": "[parameters('windowsOSVersion')]",
                "version": "latest"
            },
            "imageReference": "[variables('osType')]"
    
        },
        "resources": [
            {
                "type": "Microsoft.Network/loadBalancers",
                "apiVersion": "2020-06-01",
                "name": "[variables('loadBalancerName')]",
                "location": "[parameters('location')]",
                "dependsOn": [
                    "[resourceId('Microsoft.Network/publicIPAddresses', variables('publicIPAddressName'))]"
                ],
                "sku": {
                       "name": "Standard"
                      },
                "properties": {
                    "frontendIPConfigurations": [
                        {
                            "name": "LoadBalancerFrontEnd",
                            "properties": {
                                "publicIPAddress": {
                                    "id": "[variables('publicIPAddressID')]",
                                    "name": "Standard"
                                }
                            }
                        }
                    ],
                    "backendAddressPools": [
                        {
                            "name": "[variables('bePoolName')]"
                        }
                    ],
                    "inboundNatPools": [
                        {
                            "name": "[variables('natPoolName')]",
                            "properties": {
                                "frontendIPConfiguration": {
                                    "id": "[variables('frontEndIPConfigID')]"
                                },
                                "protocol": "Tcp",
                                "frontendPortRangeStart": "[variables('natStartPort')]",
                                "frontendPortRangeEnd": "[variables('natEndPort')]",
                                "backendPort": "[variables('natBackendPort')]"
                            }
                        }
                    ],
                    "loadBalancingRules": [
                        {
                            "name": "LBRule",
                            "properties": {
                                "frontendIPConfiguration": {
                                    "id": "[variables('frontEndIPConfigID')]"
                                },
                                "backendAddressPool": {
                                    "id": "[variables('lbPoolID')]"
                                },
                                "protocol": "Tcp",
                                "frontendPort": 80,
                                "backendPort": 80,
                                "enableFloatingIP": false,
                                "idleTimeoutInMinutes": 5,
                                "probe": {
                                    "id": "[variables('lbProbeID')]"
                                }
                            }
                        }
                    ],
                    "probes": [
                        {
                            "name": "tcpProbe",
                            "properties": {
                                "protocol": "Tcp",
                                "port": 80,
                                "intervalInSeconds": 5,
                                "numberOfProbes": 2
                            }
                        }
                    ]
                }
            },
    
            {
                "type": "Microsoft.Network/publicIPPrefixes",
                "apiVersion": "2020-11-01",
                "name": "[parameters('publicIPPrefixes_pubprefix_name')]",
                "location": "[parameters('location')]",
                "sku": {
                    "name": "Standard",
                    "tier": "Regional"
                },
                "properties": {
                    "prefixLength": 28,
                    "publicIPAddressVersion": "IPv4",
                    "ipTags": []
                }
            },
            {
                "type": "Microsoft.Compute/virtualMachineScaleSets",
                "apiVersion": "2020-06-01",
                "name": "[variables('namingInfix')]",
                "location": "[parameters('location')]",
                "sku": {
                    "name": "[parameters('vmSku')]",
                    "tier": "Standard",
                    "capacity": "[parameters('instanceCount')]"
                },
                "dependsOn": [
                    "[resourceId('Microsoft.Network/loadBalancers', variables('loadBalancerName'))]",
                    "[resourceId('Microsoft.Network/virtualNetworks', variables('virtualNetworkName'))]",
                    "[resourceId('Microsoft.Network/publicIPPrefixes',parameters('publicIPPrefixes_pubprefix_name'))]"
                ],
                "properties": {
                    "overprovision": true,
                    "upgradePolicy": {
                        "mode": "Automatic"
                    },
                    "singlePlacementGroup": "[parameters('singlePlacementGroup')]",
                    "platformFaultDomainCount": "[parameters('platformFaultDomainCount')]",
                    "virtualMachineProfile": {
                        "storageProfile": {
                            "osDisk": {
                                "caching": "ReadWrite",
                                "createOption": "FromImage"
                            },
                            "imageReference": "[variables('imageReference')]"
                        },
                        "osProfile": {
                            "computerNamePrefix": "[variables('namingInfix')]",
                            "adminUsername": "[parameters('adminUsername')]",
                            "adminPassword": "[parameters('adminPassword')]"
                        },
                        "networkProfile": {
                            "networkInterfaceConfigurations": [
                                {
                                    "name": "[variables('nicName')]",
                                    "properties": {
                                        "primary": true,
                                        "ipConfigurations": [
                                            {
                                                "name": "[variables('ipConfigName')]",
                                                "properties": {
                                                    "subnet": {
                                                        "id": "[resourceId('Microsoft.Network/virtualNetworks/subnets', variables('virtualNetworkName'), variables('subnetName'))]"
                                                    },
                                                    "loadBalancerBackendAddressPools": [
                                                        {
                                                            "id": "[variables('lbPoolID')]"
                                                        }
                                                    ],
                                                    "loadBalancerInboundNatPools": [
                                                        {
                                                            "id": "[resourceId('Microsoft.Network/loadBalancers/inboundNatPools', variables('loadBalancerName'),  variables('natPoolName'))]"
                                                        }
                                                    ],
    
                                                        "publicipaddressconfiguration": {
                                                            "name": "pub1",
                                                            "properties": {
                                                                "idleTimeoutInMinutes": 15,
                                                            "publicIPAddressVersion": "IPv4",
                                                            "publicIPPrefix":{
                                                                "id": "[resourceId('Microsoft.Network/publicIPPrefixes',parameters('publicIPPrefixes_pubprefix_name'))]"
                                                            }
                                                            }
                                                                        
                                                        }
    
                                                }
    
    
                                            }
                                        ]
                                    }
                                }
                            ]
                        }
    
                    }
                }
            },
            {
                "type": "Microsoft.Network/publicIPAddresses",
                "apiVersion": "2020-06-01",
                "name": "[variables('publicIPAddressName')]",
                "location": "[parameters('location')]",
                  "sku": {
                        "name": "Standard"      
                    },
                "properties": {
                    "publicIPAllocationMethod": "Static",
                    "dnsSettings": {
                        "domainNameLabel": "[variables('longNamingInfix')]"
                    }
                }
            },
            {
                "type": "Microsoft.Network/virtualNetworks",
                "apiVersion": "2020-06-01",
                "name": "[variables('virtualNetworkName')]",
                "location": "[parameters('location')]",
                "properties": {
                    "addressSpace": {
                        "addressPrefixes": [
                            "[variables('addressPrefix')]"
                        ]
                    },
                    "subnets": [
                        {
                            "name": "[variables('subnetName')]",
                            "properties": {
                                "addressPrefix": "[variables('subnetPrefix')]"
                            }
                        }
                    ]
                }
            },
            {
                "type": "Microsoft.Insights/autoscaleSettings",
                "apiVersion": "2015-04-01",
                "name": "autoscalehost",
                "location": "[parameters('location')]",
                "dependsOn": [
                    "[resourceId('Microsoft.Compute/virtualMachineScaleSets/', variables('namingInfix'))]"
                ],
                "properties": {
                    "name": "autoscalehost",
                    "targetResourceUri": "[resourceId('Microsoft.Compute/virtualMachineScaleSets', variables('namingInfix'))]",
                    "enabled": true,
                    "profiles": [
                        {
                            "name": "Profile1",
                            "capacity": {
                                "minimum": "1",
                                "maximum": "10",
                                "default": "1"
                            },
                            "rules": [
                                {
                                    "metricTrigger": {
                                        "metricName": "Percentage CPU",
                                        "metricResourceUri": "[resourceId('Microsoft.Compute/virtualMachineScaleSets', variables('namingInfix'))]",
                                        "timeGrain": "PT1M",
                                        "statistic": "Average",
                                        "timeWindow": "PT5M",
                                        "timeAggregation": "Average",
                                        "operator": "GreaterThan",
                                        "threshold": 50
                                    },
                                    "scaleAction": {
                                        "direction": "Increase",
                                        "type": "ChangeCount",
                                        "value": "1",
                                        "cooldown": "PT5M"
                                    }
                                },
                                {
                                    "metricTrigger": {
                                        "metricName": "Percentage CPU",
                                        "metricResourceUri": "[resourceId('Microsoft.Compute/virtualMachineScaleSets', variables('namingInfix'))]",
                                        "timeGrain": "PT1M",
                                        "statistic": "Average",
                                        "timeWindow": "PT5M",
                                        "timeAggregation": "Average",
                                        "operator": "LessThan",
                                        "threshold": 30
                                    },
                                    "scaleAction": {
                                        "direction": "Decrease",
                                        "type": "ChangeCount",
                                        "value": "1",
                                        "cooldown": "PT5M"
                                    }
                                }
                            ]
                        }
                    ]
                }
            }
        ]
    
    }
    

    此外,您还可以使用the REST API 获取虚拟机规模集中实例的指定公共 IP 地址。

    GET https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Compute/virtualMachineScaleSets/{virtualMachineScaleSetName}/virtualMachines/{virtualmachineIndex}/networkInterfaces/{networkInterfaceName}/ipconfigurations/{ipConfigurationName}/publicipaddresses/{publicIpAddressName}?api-version=2018-10-01
    

    【讨论】:

    • 如果我的回答对您有帮助,您可以接受它作为答案(点击答案旁边的复选标记,将其从灰色切换为填写。)。阅读meta.stackexchange.com/questions/5234/…这可能对其他社区成员有益。
    • 谢谢南希,不过,我尝试了一些不同的方法。单独创建网络接口并将其添加到虚拟网络。在创建虚拟网络时,它使用 2015 年的旧模板,这就是无法从 Azure PowerShell cmdlet 访问网络接口的原因。单独创建网络接口使用 2018 模板,可使用 Get-AzureRmNetworkInterface cmdlet 访问。
    • 第二部分是即使我在 VMSS 上设置了 publicIPPrefix,任何 VM 实例都会在重启后获得新的公共 IP,并在每次重启时不断变化。这不是我要找的。一旦每个 VM 实例获得新的公共 IP,它不应在其整个生命周期内更改(除非 VMSS 的 publicIPPrefix 发生更改)
    • 这不是真的。当我创建VMSS作为我上面的模板时,即使我重启了vmss实例或一次性重启了整个vmss,每个实例的公共IP地址都没有改变。
    • 你试过上面的模板吗?它解决了你的问题吗?
    猜你喜欢
    • 1970-01-01
    • 2015-02-15
    • 2016-04-06
    • 1970-01-01
    • 2023-03-03
    • 1970-01-01
    • 1970-01-01
    • 2016-12-02
    相关资源
    最近更新 更多