快速入门:使用 Azure CLI 创建虚拟网络Quickstart: Create a virtual network using the Azure CLI

虚拟网络能让 Azure 资源(例如虚拟机 (VM))彼此之间私下通信以及与 Internet 进行通信。A virtual network enables Azure resources, like virtual machines (VMs), to communicate privately with each other, and with the internet. 本快速入门介绍如何创建虚拟网络。In this quickstart, you learn how to create a virtual network. 创建虚拟网络后,将两个 VM 部署到该虚拟网络中。After creating a virtual network, you deploy two VMs into the virtual network. 然后可以从 Internet 连接到 VM,并通过新的虚拟网络进行私下通信。You then connect to the VMs from the internet, and communicate privately over the new virtual network.

先决条件Prerequisites

如果还没有 Azure 订阅,请现在就创建一个试用帐户If you don't have an Azure subscription, create a trial account now.

备注

在 Azure China 中使用 Azure CLI 2.0 之前,请首先运行 az cloud set -n AzureChinaCloud 更改云环境。Before you can use Azure CLI 2.0 in Azure China, please run az cloud set -n AzureChinaCloud first to change the cloud environment. 如果要切换回全局 Azure,请再次运行 az cloud set -n AzureCloudIf you want to switch back to Global Azure, run az cloud set -n AzureCloud again.

如果决定在本地安装并使用 Azure CLI,本快速入门要求使用 Azure CLI 2.0.28 或更高版本。If you decide to install and use Azure CLI locally instead, this quickstart requires you to use Azure CLI version 2.0.28 or later. 若要查找已安装的版本,请运行 az --versionTo find your installed version, run az --version. 有关安装或升级信息,请参阅安装 Azure CLISee Install Azure CLI for install or upgrade info.

创建资源组和虚拟网络Create a resource group and a virtual network

在创建虚拟网络之前,必须创建一个资源组用于托管该虚拟网络。Before you can create a virtual network, you have to create a resource group to host the virtual network. 使用 az group create 创建资源组。Create a resource group with az group create. 此示例在 chinaeast 位置创建一个名为 myResourceGroup 的资源组 :This example creates a resource group named myResourceGroup in the chinaeast location:

az group create --name myResourceGroup --location chinaeast

使用 az network vnet create 创建虚拟网络。Create a virtual network with az network vnet create. 此示例创建名为 myVirtualNetwork 的默认虚拟网络,它具有一个名为 default 的子网 :This example creates a default virtual network named myVirtualNetwork with one subnet named default:

az network vnet create \
  --name myVirtualNetwork \
  --resource-group myResourceGroup \
  --subnet-name default

创建虚拟机Create virtual machines

在虚拟网络中创建两个 VM。Create two VMs in the virtual network.

创建第一个 VMCreate the first VM

使用 az vm create 创建 VM。Create a VM with az vm create. 如果默认密钥位置中尚不存在 SSH 密钥,该命令会创建它们。If SSH keys don't already exist in a default key location, the command creates them. 若要使用特定的一组密钥,请使用 --ssh-key-value 选项。To use a specific set of keys, use the --ssh-key-value option. --no-wait 选项会在后台创建 VM,因此可继续执行下一步。The --no-wait option creates the VM in the background, so that you can continue to the next step. 此示例创建名为 myVm1 的 VM :This example creates a VM named myVm1:

az vm create \
  --resource-group myResourceGroup \
  --name myVm1 \
  --image UbuntuLTS \
  --generate-ssh-keys \
  --no-wait

创建第二个 VMCreate the second VM

由于已在上一步骤中使用了 --no-wait 选项,因此可以继续并创建名为 myVm2 的第二个 VM 。Since you used the --no-wait option in the previous step, you can go ahead and create the second VM named myVm2.

az vm create \
  --resource-group myResourceGroup \
  --name myVm2 \
  --image UbuntuLTS \
  --generate-ssh-keys

Azure CLI 输出消息Azure CLI output message

创建 VM 可能需要数分钟的时间。The VMs take a few minutes to create. Azure 创建 VM 后,Azure CLI 会返回如下输出:After Azure creates the VMs, the Azure CLI returns output like this:

{
  "fqdns": "",
  "id": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/myResourceGroup/providers/Microsoft.Compute/virtualMachines/myVm2",
  "location": "chinaeast",
  "macAddress": "00-0D-3A-23-9A-49",
  "powerState": "VM running",
  "privateIpAddress": "10.0.0.5",
  "publicIpAddress": "40.68.254.142",
  "resourceGroup": "myResourceGroup"
  "zones": ""
}

记下 publicIpAddress。Take note of the publicIpAddress. 在下一步中,此地址将用于从 Internet 连接到 VM。You will use this address to connect to the VM from the internet in the next step.

从 Internet 连接到 VMConnect to a VM from the internet

在此命令中,将 <publicIpAddress> 替换为 myVm2 VM 的公共 IP 地址 :In this command, replace <publicIpAddress> with the public IP address of your myVm2 VM:

ssh <publicIpAddress>

VM 之间进行通信Communicate between VMs

若要确认 myVm2 VM 和 myVm1 VM 之间的私下通信,请输入此命令 :To confirm private communication between the myVm2 and myVm1 VMs, enter this command:

ping myVm1 -c 4

将从 10.0.0.4 收到四条回复 。You'll receive four replies from 10.0.0.4.

退出与 myVm2 VM 的 SSH 会话。Exit the SSH session with the myVm2 VM.

清理资源Clean up resources

如果不再需要资源组及其所有资源,可以使用 az group delete 将其删除:When no longer needed, you can use az group delete to remove the resource group and all the resources it has:

az group delete --name myResourceGroup --yes

后续步骤Next steps

在本快速入门中,你创建了默认的虚拟网络和两个 VM。In this quickstart, you created a default virtual network and two VMs. 从 Internet 连接到了其中一个 VM,并在两个 VM 之间进行了私下通信。You connected to one VM from the internet and communicated privately between the two VMs. Azure 可让 VM 之间进行不受限制的私下通信。Azure lets unrestricted private communication between VMs. 默认情况下,Azure 仅允许从 Internet 到 Windows VM 的入站远程桌面连接。By default, Azure only lets inbound remote desktop connections to Windows VMs from the internet. 转到下一篇文章,详细了解如何配置不同类型的 VM 网络通信:Advance to the next article to learn more about configuring different types of VM network communications: