如何在Azure门户预留公网IP供后续使用及实现IP范围预留
解决方案
一、为虚拟机添加公网IP的替代方案
除了Azure门户操作,还有以下几种实用方式:
1. Azure CLI
直接通过命令行创建公网IP并关联到虚拟机的网络接口:
# 创建静态公网IP(可按需改为动态) az network public-ip create --resource-group <你的资源组名> --name <公网IP名称> --sku Standard --allocation-method Static # 将公网IP关联到虚拟机的网络接口 az network nic ip-config update --resource-group <你的资源组名> --nic-name <网卡名称> --name <IP配置名称> --public-ip-address <公网IP名称>
2. Azure PowerShell
用PowerShell完成创建与关联操作:
# 创建静态公网IP $publicIp = New-AzPublicIpAddress -ResourceGroupName <你的资源组名> -Name <公网IP名称> -Location <区域> -Sku Standard -AllocationMethod Static # 获取目标网络接口和IP配置 $nic = Get-AzNetworkInterface -ResourceGroupName <你的资源组名> -Name <网卡名称> $ipConfig = $nic.IpConfigurations[0] # 关联公网IP到IP配置 $ipConfig.PublicIpAddress = $publicIp Set-AzNetworkInterface -NetworkInterface $nic
3. Bicep/ARM模板
通过基础设施即代码定义公网IP及关联关系,示例Bicep片段:
resource publicIp 'Microsoft.Network/publicIPAddresses@2023-05-01' = { name: '<公网IP名称>' location: resourceGroup().location sku: { name: 'Standard' } properties: { publicIPAllocationMethod: 'Static' } } resource nic 'Microsoft.Network/networkInterfaces@2023-05-01' = { name: '<网卡名称>' location: resourceGroup().location properties: { ipConfigurations: [ { name: 'ipconfig1' properties: { privateIPAllocationMethod: 'Dynamic' publicIPAddress: { id: publicIp.id } subnet: { id: '<子网ID>' } } } ] } }
二、预留IP地址范围(公网IP前缀)的方法
这里指预留连续的公网IP段(公网IP前缀),操作如下:
前提条件
- 订阅需有对应配额,前缀支持/28(16个IP)、/27(32个IP)等规格(取决于区域支持)
- 必须使用Standard SKU(Basic SKU不支持前缀预留)
操作方式
1. Azure门户
- 搜索“公网IP前缀”,点击“创建”
- 选择资源组、区域、SKU(Standard)、前缀大小,填写名称后完成创建
- 创建后可从该前缀中分配静态公网IP给虚拟机网卡
2. Azure CLI
az network public-ip prefix create --resource-group <你的资源组名> --name <前缀名称> --location <区域> --sku Standard --length 28
(--length指定前缀长度,比如28对应16个公网IP)
3. Azure PowerShell
New-AzPublicIpPrefix -ResourceGroupName <你的资源组名> -Name <前缀名称> -Location <区域> -Sku Standard -PrefixLength 28
若需预留私网IP范围:创建虚拟网络时指定自定义地址空间(如10.0.0.0/16),后续子网从该空间划分;已创建的虚拟网络也可通过门户/CLI/PowerShell扩展地址空间。
内容的提问来源于stack exchange,提问作者samnoon
相关产品推荐
相关产品推荐

