为可缩放的应用程序创建虚拟机和存储帐户Create a virtual machine and storage account for a scalable application

本教程是一个系列中的第一部分。This tutorial is part one of a series. 本教程演示如何部署使用 Azure 存储帐户上传和下载大量随机数据的应用程序。This tutorial shows you deploy an application that uploads and download large amounts of random data with an Azure storage account. 完成后,会有一个控制台应用程序在虚拟机上运行,用于将大量数据上传和下载到存储帐户。When you're finished, you have a console application running on a virtual machine that you upload and download large amounts of data to a storage account.

在该系列的第一部分中,你会学习如何:In part one of the series, you learn how to:

  • 创建存储帐户Create a storage account
  • 创建虚拟机Create a virtual machine
  • 配置自定义脚本扩展Configure a custom script extension

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

备注

本文进行了更新,以便使用新的 Azure PowerShell Az 模块。This article has been updated to use the new Azure PowerShell Az module. 你仍然可以使用 AzureRM 模块,至少在 2020 年 12 月之前,它将继续接收 bug 修补程序。You can still use the AzureRM module, which will continue to receive bug fixes until at least December 2020. 若要详细了解新的 Az 模块和 AzureRM 兼容性,请参阅新 Azure Powershell Az 模块简介To learn more about the new Az module and AzureRM compatibility, see Introducing the new Azure PowerShell Az module. 有关 Az 模块安装说明,请参阅安装 Azure PowerShellFor Az module installation instructions, see Install Azure PowerShell.

在本地安装并使用 PowerShell,本教程需要 Azure PowerShell 模块 Az 版本 0.7 或更高版本。Install and use the PowerShell locally, this tutorial requires the Azure PowerShell module Az version 0.7 or later. 运行 Get-Module -ListAvailable Az 即可查找版本。Run Get-Module -ListAvailable Az to find the version. 如果需要升级,请参阅安装 Azure PowerShell 模块If you need to upgrade, see Install Azure PowerShell module. 此外,还需要运行 Connect-AzAccount -EnvironmentName AzureChinaCloud 以创建与 Azure 的连接。You also need to run Connect-AzAccount -EnvironmentName AzureChinaCloud to create a connection with Azure.

创建资源组Create a resource group

使用 New-AzResourceGroup 创建 Azure 资源组。Create an Azure resource group with New-AzResourceGroup. 资源组是在其中部署和管理 Azure 资源的逻辑容器。A resource group is a logical container into which Azure resources are deployed and managed.

New-AzResourceGroup -Name myResourceGroup -Location ChinaNorth

创建存储帐户Create a storage account

此示例将 50 个大文件上传到 Azure 存储帐户中的 blob 容器。The sample uploads 50 large files to a blob container in an Azure Storage account. 存储帐户提供唯一的命名空间来存储和访问 Azure 存储数据对象。A storage account provides a unique namespace to store and access your Azure storage data objects. 使用 New-AzStorageAccount 命令在创建的资源组中创建存储帐户。Create a storage account in the resource group you created by using the New-AzStorageAccount command.

在以下命令中,请将 <blob_storage_account> 占位符替换成自己的 Blob 存储帐户的全局唯一名称。In the following command, substitute your own globally unique name for the Blob storage account where you see the <blob_storage_account> placeholder.

$storageAccount = New-AzStorageAccount -ResourceGroupName myResourceGroup `
  -Name "<blob_storage_account>" `
  -Location "China North" `
  -SkuName Standard_LRS `
  -Kind Storage `

创建虚拟机Create a virtual machine

创建虚拟机配置。Create a virtual machine configuration. 此配置包括部署虚拟机时使用的设置,例如虚拟机映像、大小和身份验证配置。This configuration includes the settings that are used when deploying the virtual machine such as a virtual machine image, size, and authentication configuration. 运行此步骤时,会提示输入凭据。When running this step, you are prompted for credentials. 输入的值将配置为用于虚拟机的用户名和密码。The values that you enter are configured as the user name and password for the virtual machine.

使用 New-AzVM 创建虚拟机。Create the virtual machine with New-AzVM.

# Variables for common values
$resourceGroup = "myResourceGroup"
$location = "China North"
$vmName = "myVM"

# Create user object
$cred = Get-Credential -Message "Enter a username and password for the virtual machine."

# Create a subnet configuration
$subnetConfig = New-AzVirtualNetworkSubnetConfig -Name mySubnet -AddressPrefix 192.168.1.0/24

# Create a virtual network
$vnet = New-AzVirtualNetwork -ResourceGroupName $resourceGroup -Location $location `
  -Name MYvNET -AddressPrefix 192.168.0.0/16 -Subnet $subnetConfig

# Create a public IP address and specify a DNS name
$pip = New-AzPublicIpAddress -ResourceGroupName $resourceGroup -Location $location `
  -Name "mypublicdns$(Get-Random)" -AllocationMethod Static -IdleTimeoutInMinutes 4

# Create a virtual network card and associate with public IP address
$nic = New-AzNetworkInterface -Name myNic -ResourceGroupName $resourceGroup -Location $location `
  -SubnetId $vnet.Subnets[0].Id -PublicIpAddressId $pip.Id

# Create a virtual machine configuration
$vmConfig = New-AzVMConfig -VMName myVM -VMSize Standard_DS14_v2 | `
    Set-AzVMOperatingSystem -Windows -ComputerName myVM -Credential $cred | `
    Set-AzVMSourceImage -PublisherName MicrosoftWindowsServer -Offer WindowsServer `
    -Skus 2016-Datacenter -Version latest | Add-AzVMNetworkInterface -Id $nic.Id

# Create a virtual machine
New-AzVM -ResourceGroupName $resourceGroup -Location $location -VM $vmConfig

Write-host "Your public IP address is $($pip.IpAddress)"

部署配置Deploy configuration

在本教程中,必须在虚拟机上安装一些必备组件。For this tutorial, there are pre-requisites that must be installed on the virtual machine. 自定义脚本扩展用于运行完成以下任务的 PowerShell 脚本:The custom script extension is used to run a PowerShell script that completes the following tasks:

  • 安装 .NET Core 2.0Install .NET core 2.0
  • 安装 chocolateyInstall chocolatey
  • 安装 GITInstall GIT
  • 克隆示例存储库Clone the sample repo
  • 还原 NuGet 包Restore NuGet packages
  • 使用随机数据创建 50 个 1 GB 的文件Creates 50 1-GB files with random data

运行以下 cmdlet,完成虚拟机的配置。Run the following cmdlet to finalize configuration of the virtual machine. 完成此步骤需要 5-15 分钟。This step takes 5-15 minutes to complete.

# Start a CustomScript extension to use a simple PowerShell script to install .NET core, dependencies, and pre-create the files to upload.
Set-AzVMCustomScriptExtension -ResourceGroupName myResourceGroup `
    -VMName myVM `
    -Location "China North" `
    -FileUri https://raw.githubusercontent.com/azure-samples/storage-dotnet-perf-scale-app/master/setup_env.ps1 `
    -Run 'setup_env.ps1' `
    -Name DemoScriptExtension

后续步骤Next steps

本系列的第一部分中介绍了如何创建存储帐户、部署虚拟机以及如何为虚拟机配置所需的必备组件,例如如何:In part one of the series, you learned about creating a storage account, deploying a virtual machine and configuring the virtual machine with the required pre-requisites such as how to:

  • 创建存储帐户Create a storage account
  • 创建虚拟机Create a virtual machine
  • 配置自定义脚本扩展Configure a custom script extension

进入本系列的第二部分,使用指数重试和并行方式将大量数据上传到存储帐户。Advance to part two of the series to upload large amounts of data to a storage account using exponential retry and parallelism.