使用 Azure CLI 为虚拟机规模集附加并使用数据磁盘Attach and use data disks with a virtual machine scale set with the Azure CLI

此脚本创建虚拟机规模集并附加和准备数据磁盘。This script creates a virtual machine scale set and attaches and prepares data disks.

若要运行此示例,请安装最新版本的 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

# Create a resource group
az group create --name myResourceGroup --location chinanorth

# Create a scale set
# Network resources such as an Azure load balancer are automatically created
# Two data disks are created and attach - a 64Gb disk and a 128Gb disk
az vmss create `
  --resource-group myResourceGroup `
  --name myScaleSet `
  --image UbuntuLTS `
  --upgrade-policy-mode automatic `
  --admin-username azureuser `
  --generate-ssh-keys `
  --vm-sku Standard_DS1 `
  --data-disk-sizes-gb 64 128

# Attach an additional 128Gb data disk
az vmss disk attach `
  --resource-group myResourceGroup `
  --name myScaleSet `
  --size-gb 128

# Install the Azure Custom Script Extension to run a script that prepares the data disks
az vmss extension set `
  --publisher Microsoft.Azure.Extensions `
  --version 2.0 `
  --name CustomScript `
  --resource-group myResourceGroup `
  --vmss-name myScaleSet `
  --settings "{'fileUris':['https://raw.githubusercontent.com/Azure-Samples/compute-automation-configurations/master/prepare_vm_disks.sh'],'commandToExecute':'./prepare_vm_disks.sh'}"

清理部署Clean up deployment

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

az group delete --name myResourceGroup

脚本说明Script explanation

此脚本使用以下命令创建资源组、虚拟机规模集和所有相关资源。This script uses the following commands to create a resource group, virtual machine scale set, 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 vmss createaz vmss create 创建虚拟机规模集并将其连接到虚拟网络、子网和网络安全组。Creates the virtual machine scale set and connects it to the virtual network, subnet, and network security group. 负载均衡器也会被创建,以将流量分配到多个 VM 实例。A load balancer is also created to distribute traffic to multiple VM instances. 此命令还指定要使用的 VM 映像和管理凭据。This command also specifies the VM image to be used and administrative credentials.
az vmss disk attachaz vmss disk attach 创建数据磁盘并将其附加到虚拟机规模集。Creates and attaches a data disk to the virtual machine scale set.
az vmss extension setaz vmss extension set 安装 Azure 自定义脚本扩展以运行脚本,从而在每个 VM 实例上准备数据磁盘。Installs the Azure Custom Script Extension to run a script that prepares the data disks on each VM instance.
az group deleteaz group delete 删除资源组,包括所有嵌套的资源。Deletes a resource group including all nested resources.

后续步骤Next steps

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

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