教程:使用 Azure CLI 创建和管理 Linux VMTutorial: Create and Manage Linux VMs with the Azure CLI
Azure 虚拟机提供完全可配置的灵活计算环境。Azure virtual machines provide a fully configurable and flexible computing environment. 本教程介绍 Azure 虚拟机的基本部署项目,例如选择 VM 大小、选择 VM 映像和部署 VM。This tutorial covers basic Azure virtual machine deployment items such as selecting a VM size, selecting a VM image, and deploying a VM. 你将学习如何执行以下操作:You learn how to:
- 创建并连接到 VMCreate and connect to a VM
- 选择并使用 VM 映像Select and use VM images
- 查看和使用特定 VM 大小View and use specific VM sizes
- 调整 VM 的大小Resize a VM
- 查看并了解 VM 状态View and understand VM state
备注
请先运行 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 AzureCloud
。If you want to switch back to Azure Public Cloud, run az cloud set -n AzureCloud
again.
如果选择在本地安装并使用 CLI,本教程要求运行 Azure CLI 2.0.30 或更高版本。If you choose to install and use the CLI locally, this tutorial requires that you are running the Azure CLI version 2.0.30 or later. 运行 az --version
即可查找版本。Run az --version
to find the version. 如果需要进行安装或升级,请参阅安装 Azure CLI。If you need to install or upgrade, see Install Azure CLI.
创建资源组Create resource group
使用 az group create 命令创建资源组。Create a resource group with the az group create command.
Azure 资源组是在其中部署和管理 Azure 资源的逻辑容器。An Azure resource group is a logical container into which Azure resources are deployed and managed. 必须在创建虚拟机前创建资源组。A resource group must be created before a virtual machine. 在此示例中,在“chinaeast” 区域中创建了名为“myResourceGroupVM” 的资源组。In this example, a resource group named myResourceGroupVM is created in the chinaeast region.
az group create --name myResourceGroupVM --location chinaeast
创建或修改 VM 时指定资源组,本教程会对此进行演示。The resource group is specified when creating or modifying a VM, which can be seen throughout this tutorial.
创建虚拟机Create virtual machine
使用 az vm create 命令创建虚拟机。Create a virtual machine with the az vm create command.
创建虚拟机时,可使用多个选项,例如操作系统映像、磁盘大小调整和管理凭据。When you create a virtual machine, several options are available such as operating system image, disk sizing, and administrative credentials. 下面的示例创建一个名为 myVM、运行 Ubuntu Server 的 VM。The following example creates a VM named myVM that runs Ubuntu Server. 将在该 VM 上创建名为 azureuser 的用户帐户,并生成 SSH 密钥(如果这些密钥在默认密钥位置 ( ~/.ssh) 中不存在):A user account named azureuser is created on the VM, and SSH keys are generated if they do not exist in the default key location (~/.ssh):
az vm create \
--resource-group myResourceGroupVM \
--name myVM \
--image UbuntuLTS \
--admin-username azureuser \
--generate-ssh-keys
创建 VM 可能需要几分钟。It may take a few minutes to create the VM. 创建 VM 后,Azure CLI 会输出有关 VM 的信息。Once the VM has been created, the Azure CLI outputs information about the VM. 请记下 publicIpAddress
,可以使用此地址访问虚拟机。Take note of the publicIpAddress
, this address can be used to access the virtual machine..
{
"fqdns": "",
"id": "/subscriptions/d5b9d4b7-6fc1-0000-0000-000000000000/resourceGroups/myResourceGroupVM/providers/Microsoft.Compute/virtualMachines/myVM",
"location": "chinaeast",
"macAddress": "00-0D-3A-23-9A-49",
"powerState": "VM running",
"privateIpAddress": "10.0.0.4",
"publicIpAddress": "52.174.34.95",
"resourceGroup": "myResourceGroupVM"
}
连接到 VMConnect to VM
现在可以使用 SSH 从本地计算机连接到 VM。You can now connect to the VM with SSH from your local computer. 将示例 IP 地址替换为上一步骤中记下的 publicIpAddress
。Replace the example IP address with the publicIpAddress
noted in the previous step.
ssh azureuser@52.174.34.95
登录 VM 后,可以安装和配置应用程序。Once logged in to the VM, you can install and configure applications. 完成后,可按正常方式关闭 SSH 会话:When you are finished, you close the SSH session as normal:
exit
了解 VM 映像Understand VM images
Azure 市场包括许多可用于创建 VM 的映像。The Azure marketplace includes many images that can be used to create VMs. 在之前的步骤中,使用 Ubuntu 映像创建了虚拟机。In the previous steps, a virtual machine was created using an Ubuntu image. 在此步骤中,使用 Azure CLI 在市场中搜索 CentOS 映像,此映像稍后用于部署第二个虚拟机。In this step, the Azure CLI is used to search the marketplace for a CentOS image, which is then used to deploy a second virtual machine.
若要查看最常用映像的列表,请使用 az vm image list 命令。To see a list of the most commonly used images, use the az vm image list command.
az vm image list --output table
命令输出返回 Azure 上最常用的 VM 映像。The command output returns the most popular VM images on Azure.
Offer Publisher Sku Urn UrnAlias Version
------------- ---------------------- ------------------ -------------------------------------------------------------- ------------------- ---------
WindowsServer MicrosoftWindowsServer 2016-Datacenter MicrosoftWindowsServer:WindowsServer:2016-Datacenter:latest Win2016Datacenter latest
WindowsServer MicrosoftWindowsServer 2012-R2-Datacenter MicrosoftWindowsServer:WindowsServer:2012-R2-Datacenter:latest Win2012R2Datacenter latest
WindowsServer MicrosoftWindowsServer 2008-R2-SP1 MicrosoftWindowsServer:WindowsServer:2008-R2-SP1:latest Win2008R2SP1 latest
WindowsServer MicrosoftWindowsServer 2012-Datacenter MicrosoftWindowsServer:WindowsServer:2012-Datacenter:latest Win2012Datacenter latest
UbuntuServer Canonical 16.04-LTS Canonical:UbuntuServer:16.04-LTS:latest UbuntuLTS latest
CentOS OpenLogic 7.3 OpenLogic:CentOS:7.3:latest CentOS latest
openSUSE-Leap SUSE 42.2 SUSE:openSUSE-Leap:42.2:latest openSUSE-Leap latest
SLES SUSE 12-SP2 SUSE:SLES:12-SP2:latest SLES latest
Debian credativ 8 credativ:Debian:8:latest Debian latest
CoreOS CoreOS Stable CoreOS:CoreOS:Stable:latest CoreOS latest
可以通过添加 --all
参数查看完整列表。A full list can be seen by adding the --all
argument. 还可以按 --publisher
或 --offer
筛选映像列表。The image list can also be filtered by --publisher
or --offer
. 在此示例中,已在列表中筛选出其产品与“CentOS” 匹配的所有映像。In this example, the list is filtered for all images with an offer that matches CentOS.
az vm image list --offer CentOS --all --output table
部分输出:Partial output:
Offer Publisher Sku Urn Version
---------------- ---------------- ---- -------------------------------------- -----------
CentOS OpenLogic 6.5 OpenLogic:CentOS:6.5:6.5.201501 6.5.201501
CentOS OpenLogic 6.5 OpenLogic:CentOS:6.5:6.5.201503 6.5.201503
CentOS OpenLogic 6.5 OpenLogic:CentOS:6.5:6.5.201506 6.5.201506
CentOS OpenLogic 6.5 OpenLogic:CentOS:6.5:6.5.20150904 6.5.20150904
CentOS OpenLogic 6.5 OpenLogic:CentOS:6.5:6.5.20160309 6.5.20160309
CentOS OpenLogic 6.5 OpenLogic:CentOS:6.5:6.5.20170207 6.5.20170207
若要使用特定的映像部署 VM,请记下“Urn”列中的值,包括发布者、产品/服务、SKU,以及用于标识映像的版本号(可选)。 To deploy a VM using a specific image, take note of the value in the Urn column, which consists of the publisher, offer, SKU, and optionally a version number to identify the image. 指定映像时,可将映像版本号替换为“latest”,这会选择最新的发行版。When specifying the image, the image version number can be replaced with "latest", which selects the latest version of the distribution. 在此示例中,--image
参数用于指定最新版本的 CentOS 6.5 映像。In this example, the --image
argument is used to specify the latest version of a CentOS 6.5 image.
az vm create --resource-group myResourceGroupVM --name myVM2 --image OpenLogic:CentOS:6.5:latest --generate-ssh-keys
了解 VM 大小Understand VM sizes
虚拟机大小决定虚拟机可用计算资源(如 CPU、GPU 和内存)的数量。A virtual machine size determines the amount of compute resources such as CPU, GPU, and memory that are made available to the virtual machine. 需要根据预期的工作负载适当调整虚拟机的大小。Virtual machines need to be sized appropriately for the expected work load. 如果工作负荷增加,可调整现有虚拟机的大小。If workload increases, an existing virtual machine can be resized.
VM 大小VM Sizes
下表将大小分类成了多个用例。The following table categorizes sizes into use cases.
类型Type | 常见大小Common sizes | 说明Description |
---|---|---|
常规用途General purpose | B、Dsv3、Dv3、DSv2、Dv2、Av2B, Dsv3, Dv3, DSv2, Dv2, Av2 | CPU 与内存之比均衡。Balanced CPU-to-memory. 适用于开发/测试、小到中型应用程序和数据解决方案。Ideal for dev / test and small to medium applications and data solutions. |
计算优化Compute optimized | Fsv2Fsv2 | 高 CPU 与内存之比。High CPU-to-memory. 适用于中等流量的应用程序、网络设备和批处理。Good for medium traffic applications, network appliances, and batch processes. |
内存优化Memory optimized | Esv3、Ev3、M、DSv2、Dv2Esv3, Ev3, M, DSv2, Dv2 | 较高的内存核心比。High memory-to-core. 适用于关系数据库、中到大型缓存和内存中分析。Great for relational databases, medium to large caches, and in-memory analytics. |
GPUGPU | NCv3NCv3 | 专门针对大量图形绘制和视频编辑的 VM。Specialized VMs targeted for heavy graphic rendering and video editing. |
查找可用的 VM 大小Find available VM sizes
若要查看在特定区域可用的 VM 大小的列表,请使用 az vm list-sizes 命令。To see a list of VM sizes available in a particular region, use the az vm list-sizes command.
az vm list-sizes --location chinaeast --output table
部分输出:Partial output:
MaxDataDiskCount MemoryInMb Name NumberOfCores OsDiskSizeInMb ResourceDiskSizeInMb
------------------ ------------ ---------------------- --------------- ---------------- ----------------------
2 3584 Standard_DS1 1 1047552 7168
4 7168 Standard_DS2 2 1047552 14336
8 14336 Standard_DS3 4 1047552 28672
16 28672 Standard_DS4 8 1047552 57344
4 14336 Standard_DS11 2 1047552 28672
8 28672 Standard_DS12 4 1047552 57344
16 57344 Standard_DS13 8 1047552 114688
32 114688 Standard_DS14 16 1047552 229376
1 768 Standard_A0 1 1047552 20480
2 1792 Standard_A1 1 1047552 71680
4 3584 Standard_A2 2 1047552 138240
8 7168 Standard_A3 4 1047552 291840
4 14336 Standard_A5 2 1047552 138240
16 14336 Standard_A4 8 1047552 619520
8 28672 Standard_A6 4 1047552 291840
16 57344 Standard_A7 8 1047552 619520
创建具有特定大小的 VMCreate VM with specific size
在前面的 VM 创建示例中未提供大小,因此会使用默认大小。In the previous VM creation example, a size was not provided, which results in a default size. 可以在创建时使用 az vm create 和 --size
参数选择 VM 大小。A VM size can be selected at creation time using az vm create and the --size
argument.
az vm create \
--resource-group myResourceGroupVM \
--name myVM3 \
--image UbuntuLTS \
--size Standard_F4s \
--generate-ssh-keys
调整 VM 的大小Resize a VM
部署 VM 后,可调整其大小以增加或减少资源分配。After a VM has been deployed, it can be resized to increase or decrease resource allocation. 可通过 az vm show 查看 VM 的当前大小:You can view the current of size of a VM with az vm show:
az vm show --resource-group myResourceGroupVM --name myVM --query hardwareProfile.vmSize
调整 VM 大小之前,请检查所需的大小在当前 Azure 群集上是否可用。Before resizing a VM, check if the desired size is available on the current Azure cluster. az vm list-vm-resize-options 命令返回大小列表。The az vm list-vm-resize-options command returns the list of sizes.
az vm list-vm-resize-options --resource-group myResourceGroupVM --name myVM --query [].name
如果所需大小可用,则可从开机状态调整 VM 大小,但需在此操作期间重启 VM。If the desired size is available, the VM can be resized from a powered-on state, however it is rebooted during the operation. 使用 az vm resize 命令执行大小调整。Use the az vm resize command to perform the resize.
az vm resize --resource-group myResourceGroupVM --name myVM --size Standard_DS4_v2
如果所需大小在当前群集上不可用,则需解除分配 VM,然后才能执行调整大小操作。If the desired size is not on the current cluster, the VM needs to be deallocated before the resize operation can occur. 使用 az vm deallocate 命令停止和解除分配 VM。Use the az vm deallocate command to stop and deallocate the VM. 请注意,重新打开 VM 的电源时,临时磁盘上的所有数据可能已删除。Note, when the VM is powered back on, any data on the temp disk may be removed. 除非使用静态 IP 地址,否则公共 IP 地址也会更改。The public IP address also changes unless a static IP address is being used.
az vm deallocate --resource-group myResourceGroupVM --name myVM
解除分配后,可进行大小调整。Once deallocated, the resize can occur.
az vm resize --resource-group myResourceGroupVM --name myVM --size Standard_GS1
调整大小后,可以启动 VM。After the resize, the VM can be started.
az vm start --resource-group myResourceGroupVM --name myVM
VM 电源状态VM power states
Azure VM 可能会处于多种电源状态之一。An Azure VM can have one of many power states. 从虚拟机监控程序的角度来看,此状态表示 VM 的当前状态。This state represents the current state of the VM from the standpoint of the hypervisor.
电源状态Power states
电源状态Power State | 说明Description |
---|---|
正在启动Starting | 指示正在启动虚拟机。Indicates the virtual machine is being started. |
运行Running | 指示虚拟机正在运行。Indicates that the virtual machine is running. |
正在停止Stopping | 指示正在停止虚拟机。Indicates that the virtual machine is being stopped. |
已停止Stopped | 指示虚拟机已停止。Indicates that the virtual machine is stopped. 虚拟机处于停止状态时仍会产生计算费用。Virtual machines in the stopped state still incur compute charges. |
正在解除分配Deallocating | 指示正在解除分配虚拟机。Indicates that the virtual machine is being deallocated. |
已解除分配Deallocated | 指示虚拟机已从虚拟机监控程序中删除,但仍可在控制面板中使用。Indicates that the virtual machine is removed from the hypervisor but still available in the control plane. 处于“已解除分配”状态的虚拟机不会产生计算费用。Virtual machines in the Deallocated state do not incur compute charges. |
- | 指示虚拟机的电源状态未知。Indicates that the power state of the virtual machine is unknown. |
查找电源状态Find the power state
若要检索特定 VM 的状态,请使用 az vm get-instance-view 命令。To retrieve the state of a particular VM, use the az vm get-instance-view command. 请确保为虚拟机和资源组指定有效的名称。Be sure to specify a valid name for a virtual machine and resource group.
az vm get-instance-view \
--name myVM \
--resource-group myResourceGroupVM \
--query instanceView.statuses[1] --output table
输出:Output:
ode DisplayStatus Level
------------------ --------------- -------
PowerState/running VM running Info
若要检索订阅中所有 VM 的电源状态,请使用虚拟机 - 列出所有 API,并将参数 statusOnly 设置为 true。To retrieve the power state of all the VMs in your subscription, use the Virtual Machines - List All API with parameter statusOnly set to true.
管理任务Management tasks
在虚拟机生命周期中,可能需要运行管理任务,例如启动、停止或删除虚拟机。During the life-cycle of a virtual machine, you may want to run management tasks such as starting, stopping, or deleting a virtual machine. 此外,可能还需要创建脚本来自动执行重复或复杂的任务。Additionally, you may want to create scripts to automate repetitive or complex tasks. 使用 Azure CLI,可从命令行或脚本运行许多常见的管理任务。Using the Azure CLI, many common management tasks can be run from the command line or in scripts.
获取 IP 地址Get IP address
此命令返回虚拟机的私有 IP 地址和公共 IP 地址。This command returns the private and public IP addresses of a virtual machine.
az vm list-ip-addresses --resource-group myResourceGroupVM --name myVM --output table
停止虚拟机Stop virtual machine
az vm stop --resource-group myResourceGroupVM --name myVM
启动虚拟机Start virtual machine
az vm start --resource-group myResourceGroupVM --name myVM
删除资源组Delete resource group
删除资源组还会删除其包含的所有资源,例如 VM、虚拟网络和磁盘。Deleting a resource group also deletes all resources contained within, such as the VM, virtual network, and disk. --no-wait
参数会使光标返回提示符处,无需等待操作完成。The --no-wait
parameter returns control to the prompt without waiting for the operation to complete. --yes
参数将确认是否希望删除资源,而不会有额外提示。The --yes
parameter confirms that you wish to delete the resources without an additional prompt to do so.
az group delete --name myResourceGroupVM --no-wait --yes
后续步骤Next steps
在本教程中,你已学习 VM 创建和管理的基本知识,例如如何:In this tutorial, you learned about basic VM creation and management such as how to:
- 创建并连接到 VMCreate and connect to a VM
- 选择并使用 VM 映像Select and use VM images
- 查看和使用特定 VM 大小View and use specific VM sizes
- 调整 VM 的大小Resize a VM
- 查看并了解 VM 状态View and understand VM state
请转到下一教程,了解 VM 磁盘。Advance to the next tutorial to learn about VM disks.