快速入门:使用 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.

如果没有 Azure 试用版订阅,请在开始前创建一个试用版订阅If you don't have an Azure trail subscription, create a trial subscription before you begin.

先决条件Prerequisites

  • 如果需要,请安装 Azure CLI 来运行 CLI 参考命令。If you prefer, install the Azure CLI to run CLI reference commands.

    • 如果使用的是本地安装,请使用 az login 命令登录到 Azure CLI。If you're using a local installation, sign in to the Azure CLI by using the az login command. 若要完成身份验证过程,请遵循终端中显示的步骤。To finish the authentication process, follow the steps displayed in your terminal. 有关其他登录选项,请参阅登录 Azure CLIFor additional sign-in options, see Sign in with the Azure CLI.

    • 出现提示时,请在首次使用时安装 Azure CLI 扩展。When you're prompted, install Azure CLI extensions on first use. 有关扩展详细信息,请参阅使用 Azure CLI 的扩展For more information about extensions, see Use extensions with the Azure CLI.

    • 运行 az version 以查找安装的版本和依赖库。Run az version to find the version and dependent libraries that are installed. 若要升级到最新版本,请运行 az upgradeTo upgrade to the latest version, run az upgrade.

备注

请先运行 az cloud set -n AzureChinaCloud 更改云环境,然后才能在 Azure 中国中使用 Azure CLI。Before you can use Azure CLI 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 Azure Public Cloud, run az cloud set -n AzureCloud again.

  • 本快速入门需要 Azure CLI 2.0.28 或更高版本。This quickstart requires version 2.0.28 or later of the Azure CLI.

创建资源组和虚拟网络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. 此示例在“中国东部”位置创建一个名为“CreateVNetQS-rg”的资源组:This example creates a resource group named CreateVNetQS-rg in the China East location:

az group create \
    --name CreateVNetQS-rg \
    --location chinaeast

使用 az network vnet create 创建虚拟网络。Create a virtual network with az network vnet create. 此示例创建一个名为“myVNet”的默认虚拟网络,其中包含一个名为“default”的子网 :This example creates a default virtual network named myVNet with one subnet named default:

az network vnet create \
  --name myVNet \
  --resource-group CreateVNetQS-rg \
  --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. 可以继续执行下一步。You can continue to the next step.

此示例创建一个名为“myVM1”的 VM:This example creates a VM named myVM1:

az vm create \
  --resource-group CreateVNetQS-rg \
  --name myVM1 \
  --image UbuntuLTS \
  --generate-ssh-keys \
  --public-ip-address myPublicIP-myVM1 \
  --no-wait

创建第二个 VMCreate the second VM

你在上一步中使用了 --no-wait 选项。You used the --no-wait option in the previous step. 你可以继续创建名为“myVM2”的第二个 VM。You can go ahead and create the second VM named myVM2.

az vm create \
  --resource-group CreateVNetQS-rg \
  --name myVM2 \
  --image UbuntuLTS \
  --public-ip-address myPublicIP-myVM2 \
  --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/CreateVNetQS-rg/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": "CreateVNetQS-rg"
  "zones": ""
}

VM 公共 IPVM public IP

若要获取公共 IP 地址 myVM2,请使用 az network public-ip showTo get the public IP address myVM2, use az network public-ip show:

az network public-ip show \
  --resource-group CreateVNetQS-rg  \
  --name myPublicIP-myVM2 \
  --query ipAddress \
  --output tsv

从 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 CreateVNetQS-rg \
    --yes

后续步骤Next steps

在本快速入门:In this quickstart:

  • 已创建了默认虚拟网络和两个 VM。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.

VM 之间的专用通信在虚拟网络中不受限制。Private communication between VMs is unrestricted in a virtual network.

转到下一篇文章,详细了解如何配置不同类型的 VM 网络通信:Advance to the next article to learn more about configuring different types of VM network communications: