快速入门:创建 NAT 网关 - Bicep
通过 Bicep 开始使用 Azure NAT 网关。 此 Bicep 文件部署虚拟网络、NAT 网关资源和 Ubuntu 虚拟机。 Ubuntu 虚拟机将部署到与 NAT 网关资源关联的子网。
Bicep 是一种特定于域的语言 (DSL),使用声明性语法来部署 Azure 资源。 它提供简明的语法、可靠的类型安全性以及对代码重用的支持。 Bicep 会针对你的 Azure 基础结构即代码解决方案提供最佳创作体验。
如果没有 Azure 订阅,可在开始前创建一个试用帐户。
本快速入门中使用的 Bicep 文件来自 Azure 快速入门模板。
此 Bicep 文件配置为创建:
虚拟网络
NAT 网关资源
Ubuntu 虚拟机
Ubuntu VM 部署到与 NAT 网关资源关联的子网。
@description('Name of the virtual machine')
param vmname string = 'myVM'
@description('Size of the virtual machine')
param vmsize string = 'Standard_D2s_v3'
@description('Name of the virtual network')
param vnetname string = 'myVnet'
@description('Name of the subnet for virtual network')
param subnetname string = 'mySubnet'
@description('Address space for virtual network')
param vnetaddressspace string = '192.168.0.0/16'
@description('Subnet prefix for virtual network')
param vnetsubnetprefix string = '192.168.0.0/24'
@description('Name of the NAT gateway')
param natgatewayname string = 'myNATgateway'
@description('Name of the virtual machine nic')
param networkinterfacename string = 'myvmNIC'
@description('Name of the NAT gateway public IP')
param publicipname string = 'myPublicIP'
@description('Name of the virtual machine NSG')
param nsgname string = 'myVMnsg'
@description('Name of the virtual machine public IP')
param publicipvmname string = 'myPublicIPVM'
@description('Name of the NAT gateway public IP')
param publicipprefixname string = 'myPublicIPPrefix'
@description('Administrator username for virtual machine')
param adminusername string
@description('Administrator password for virtual machine')
@secure()
param adminpassword string
@description('Name of resource group')
param location string = resourceGroup().location
resource nsg 'Microsoft.Network/networkSecurityGroups@2021-05-01' = {
name: nsgname
location: location
properties: {
securityRules: [
{
name: 'SSH'
properties: {
protocol: 'Tcp'
sourcePortRange: '*'
destinationPortRange: '22'
sourceAddressPrefix: '*'
destinationAddressPrefix: '*'
access: 'Allow'
priority: 300
direction: 'Inbound'
}
}
]
}
}
resource publicip 'Microsoft.Network/publicIPAddresses@2021-05-01' = {
name: publicipname
location: location
sku: {
name: 'Standard'
}
properties: {
publicIPAddressVersion: 'IPv4'
publicIPAllocationMethod: 'Static'
idleTimeoutInMinutes: 4
}
}
resource publicipvm 'Microsoft.Network/publicIPAddresses@2021-05-01' = {
name: publicipvmname
location: location
sku: {
name: 'Standard'
}
properties: {
publicIPAddressVersion: 'IPv4'
publicIPAllocationMethod: 'Static'
idleTimeoutInMinutes: 4
}
}
resource publicipprefix 'Microsoft.Network/publicIPPrefixes@2021-05-01' = {
name: publicipprefixname
location: location
sku: {
name: 'Standard'
}
properties: {
prefixLength: 31
publicIPAddressVersion: 'IPv4'
}
}
resource vm 'Microsoft.Compute/virtualMachines@2021-11-01' = {
name: vmname
location: location
properties: {
hardwareProfile: {
vmSize: vmsize
}
storageProfile: {
imageReference: {
publisher: 'Canonical'
offer: 'UbuntuServer'
sku: '18.04-LTS'
version: 'latest'
}
osDisk: {
osType: 'Linux'
name: '${vmname}_disk1'
createOption: 'FromImage'
caching: 'ReadWrite'
managedDisk: {
storageAccountType: 'Premium_LRS'
}
diskSizeGB: 30
}
}
osProfile: {
computerName: vmname
adminUsername: adminusername
adminPassword: adminpassword
linuxConfiguration: {
disablePasswordAuthentication: false
provisionVMAgent: true
}
allowExtensionOperations: true
}
networkProfile: {
networkInterfaces: [
{
id: networkinterface.id
}
]
}
}
}
resource vnet 'Microsoft.Network/virtualNetworks@2021-05-01' = {
name: vnetname
location: location
properties: {
addressSpace: {
addressPrefixes: [
vnetaddressspace
]
}
subnets: [
{
name: subnetname
properties: {
addressPrefix: vnetsubnetprefix
natGateway: {
id: natgateway.id
}
privateEndpointNetworkPolicies: 'Enabled'
privateLinkServiceNetworkPolicies: 'Enabled'
}
}
]
enableDdosProtection: false
enableVmProtection: false
}
}
resource natgateway 'Microsoft.Network/natGateways@2021-05-01' = {
name: natgatewayname
location: location
sku: {
name: 'Standard'
}
properties: {
idleTimeoutInMinutes: 4
publicIpAddresses: [
{
id: publicip.id
}
]
publicIpPrefixes: [
{
id: publicipprefix.id
}
]
}
}
resource mySubnet 'Microsoft.Network/virtualNetworks/subnets@2021-05-01' = {
parent: vnet
name: 'mySubnet'
properties: {
addressPrefix: vnetsubnetprefix
natGateway: {
id: natgateway.id
}
privateEndpointNetworkPolicies: 'Enabled'
privateLinkServiceNetworkPolicies: 'Enabled'
}
}
resource networkinterface 'Microsoft.Network/networkInterfaces@2021-05-01' = {
name: networkinterfacename
location: location
properties: {
ipConfigurations: [
{
name: 'ipconfig1'
properties: {
privateIPAddress: '192.168.0.4'
privateIPAllocationMethod: 'Dynamic'
publicIPAddress: {
id: publicipvm.id
}
subnet: {
id: mySubnet.id
}
primary: true
privateIPAddressVersion: 'IPv4'
}
}
]
enableAcceleratedNetworking: false
enableIPForwarding: false
networkSecurityGroup: {
id: nsg.id
}
}
}
该 Bicep 文件中定义了九个 Azure 资源:
Microsoft.Network/networkSecurityGroups :创建网络安全组。
Microsoft.Network/networkSecurityGroups/securityRules :创建安全规则。
Microsoft.Network/publicIPAddresses :创建公共 IP 地址。
Microsoft.Network/publicIPPrefixes :创建公共 IP 前缀。
Microsoft.Network/virtualNetworks :创建虚拟网络。
Microsoft.Network/natGateways :创建 NAT 网关资源。
Microsoft.Network/virtualNetworks/subnets :创建虚拟网络子网。
Microsoft.Network/networkinterfaces :创建网络接口。
将该 Bicep 文件另存为本地计算机上的 main.bicep。
使用 Azure CLI 或 Azure PowerShell 来部署该 Bicep 文件。
az group create --name exampleRG --location chinanorth2 az deployment group create --resource-group exampleRG --template-file main.bicep --parameters adminusername=<admin-name>
备注
将 <admin-name> 替换为虚拟机的管理员用户名。 系统还会提示输入 adminpassword。
部署完成后,应会看到一条指出部署成功的消息。
使用 Azure 门户、Azure CLI 或 Azure PowerShell 列出资源组中已部署的资源。
az resource list --resource-group exampleRG
如果不再需要资源组及其资源,请使用 Azure 门户、Azure CLI 或 Azure PowerShell 将其删除。
az group delete --name exampleRG
在本快速入门中,我们创建了:
NAT 网关资源
虚拟网络
Ubuntu 虚拟机
虚拟机部署到与 NAT 网关关联的虚拟网络子网。
若要详细了解 Azure NAT 网关和 Bicep,请继续阅读以下文章。