使用虚拟硬盘创建 VMCreate a VM with a virtual hard disk

本示例使用 VHD 创建虚拟机。This example creates a virtual machine using a VHD. 本示例创建资源组、存储帐户、容器,并通过将 VHD 上传到容器来创建 VM。It creates a resource group, a storage account, and a container, then it creates a VM by uploading the VHD to the container. 本示例将 ssh 公钥替换为用户的公钥,因此用户可以访问 VM。It replaces the ssh public key with your public key so that you have access to the VM.

用户需要可引导 VHD。You'll need a bootable VHD. 脚本会查找 ~/sample.vhdThe script looks for ~/sample.vhd.

若要运行此示例,请安装最新版本的 Azure CLITo run this sample, install the latest version of the Azure CLI. 若要开始,请运行 az login 以创建与 Azure 的连接。To start, run az login to create a connection with Azure.

适用于 Azure CLI 的示例是针对 bash shell 编写的。Samples for the Azure CLI are written for the bash shell. 若要在 Windows PowerShell 或命令提示符中运行此示例,可能需要更改脚本的元素。To run this sample in Windows PowerShell or Command Prompt, you may need to change elements of the script.

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

示例脚本Sample script

#!/bin/bash

# Sign in the Azure China Cloud
az cloud set -n AzureChinaCloud
az login 

# Create a resource group
az group create -n myResourceGroup -l chinanorth

# Create the storage account to upload the vhd
az storage account create -g myResourceGroup -n mystorageaccount -l chinanorth --sku PREMIUM_LRS

# Get a storage key for the storage account
STORAGE_KEY=$(az storage account keys list -g myResourceGroup -n mystorageaccount --query "[?keyName=='key1'] | [0].value" -o tsv)

# Create the container for the vhd
az storage container create -n vhds --account-name mystorageaccount --account-key ${STORAGE_KEY}

# Upload the vhd to a blob
az storage blob upload -c vhds -f ~/sample.vhd -n sample.vhd --account-name mystorageaccount --account-key ${STORAGE_KEY}

# Create the vm from the vhd
az vm create -g myResourceGroup -n myVM --image "https://myStorageAccount.blob.core.chinacloudapi.cn/vhds/sample.vhd" \
        --os-type linux --admin-username deploy --generate-ssh-keys

# Update the deploy user with your ssh key
az vm user update --resource-group myResourceGroup -n custom-vm -u deploy --ssh-key-value "$(< ~/.ssh/id_rsa.pub)"

# Get public IP address for the VM
IP_ADDRESS=$(az vm list-ip-addresses -g az-cli-vhd -n custom-vm \
    --query "[0].virtualMachine.network.publicIpAddresses[0].ipAddress" -o tsv)

echo "You can now connect using 'ssh deploy@${IP_ADDRESS}'"

清理部署Clean up deployment

运行以下命令来删除资源组、VM 和所有相关资源。Run the following command to remove the resource group, VM, and all related resources.

az group delete -n az-cli-vhd

脚本说明Script explanation

此脚本使用以下命令创建资源组、虚拟机、可用性集、负载均衡器和所有相关资源。This script uses the following commands to create a resource group, virtual machine, availability set, load balancer, and all related resources. 表中的每条命令均链接到特定于命令的文档。Each command in the table links to command specific documentation.

命令Command 注释Notes
az group createaz group create 创建用于存储所有资源的资源组。Creates a resource group in which all resources are stored.
az storage account listaz storage account list 列出存储帐户Lists storage accounts
az storage account check-nameaz storage account check-name 检查存储帐户名称是否有效且目前还不存在Checks that a storage account name is valid and that it doesn't already exist
az storage account keys listaz storage account keys list 列出存储帐户的密钥Lists keys for the storage accounts
az storage blob existsaz storage blob exists 检查 Blob 是否存在Checks whether the blob exists
az storage container createaz storage container create 在存储帐户中创建一个容器。Creates a container in a storage account.
az storage blob uploadaz storage blob upload 通过上传 VHD,在容器中创建一个 Blob。Creates a blob in the container by uploading the VHD.
az vm listaz vm list --query 一起使用,用于检查 VM 名称是否已使用。Used with --query check whether the VM name is in use.
az vm createaz vm create 创建虚拟机。Creates the virtual machines.
az vm list-ip-addressesaz vm list-ip-addresses 获取已创建虚拟机的 IP 地址。Gets the IP address of the VM that was created.

后续步骤Next steps

有关 Azure CLI 的详细信息,请参阅 Azure CLI 文档For more information on the Azure CLI, see Azure CLI documentation.

可以在 Azure Linux VM 文档中找到其他虚拟机 CLI 脚本示例。Additional virtual machine CLI script samples can be found in the Azure Linux VM documentation.