在虚拟机规模集上部署应用程序Deploy your application on virtual machine scale sets

若要在规模集中的虚拟机 (VM) 实例上运行应用程序,首先需要安装应用程序组件和所需文件。To run applications on virtual machine (VM) instances in a scale set, you first need to install the application components and required files. 本文介绍如何为规模集中的实例生成自定义 VM 映像,或在现有 VM 实例上自动运行安装脚本。This article introduces ways to build a custom VM image for instances in a scale set, or automatically run install scripts on existing VM instances. 本文还将介绍如何跨规模集管理应用程序或 OS 更新。You also learn how to manage application or OS updates across a scale set.

生成自定义 VM 映像Build a custom VM image

使用 Azure 平台映像之一在规模集中创建实例时,不会安装或配置其他软件。When you use one of the Azure platform images to create the instances in your scale set, no additional software is installed or configured. 可以自动安装这些组件,但这会增加将 VM 实例预配到规模集所需的时间。You can automate the install of these components, however that adds to the time it takes to provision VM instances to your scale sets. 如果将多个配置更改应用到 VM 实例,那么这些配置脚本和任务会产生管理开销。If you apply many configuration changes to the VM instances, there is management overhead with those configuration scripts and tasks.

若要减少配置管理和预配 VM 的时间,可以创建自定义 VM 映像,使其在规模集中预配实例后立即准备好运行应用程序。To reduce the configuration management and time to provision a VM, you can create a custom VM image that is ready to run your application as soon as an instance is provisioned in the scale set. 若要深入了解如何通过规模集创建和使用自定义 VM 映像,请参阅以下教程:For more information on how to create and use a custom VM image with a scale set, see the following tutorials:

使用自定义脚本扩展安装应用Install an app with the Custom Script Extension

自定义脚本扩展在 Azure VM 上下载和执行脚本。The Custom Script Extension downloads and executes scripts on Azure VMs. 此扩展适用于部署后配置、软件安装或其他任何配置/管理任务。This extension is useful for post deployment configuration, software installation, or any other configuration / management task. 可以从 Azure 存储或 GitHub 下载脚本,或者在扩展运行时将脚本提供给 Azure 门户。Scripts can be downloaded from Azure storage or GitHub, or provided to the Azure portal at extension run-time. 有关如何使用自定义脚本扩展安装应用的详细信息,请参阅以下教程:For more information on how to install an app with a Custom Script Extension, see the following tutorials:

使用 PowerShell DSC 将应用安装到 Windows VMInstall an app to a Windows VM with PowerShell DSC

PowerShell Desired State Configuration (DSC) 是一个管理平台,用于定义目标计算机的配置。PowerShell Desired State Configuration (DSC) is a management platform to define the configuration of target machines. DSC 配置定义要在计算机上安装的内容,以及如何配置主机。DSC configurations define what to install on a machine and how to configure the host. 本地配置管理器 (LCM) 引擎在每个目标节点上运行,此类节点根据推送的配置处理请求的操作。A Local Configuration Manager (LCM) engine runs on each target node that processes requested actions based on pushed configurations.

通过 PowerShell DSC 扩展,可使用 PowerShell 在规模集中自定义 VM 实例。The PowerShell DSC extension lets you customize VM instances in a scale set with PowerShell. 下面的示例:The following example:

DSC 扩展会应用于名为 myResourceGroup 的资源组中的 myScaleSet VM 实例 。The DSC extension is applied to the myScaleSet VM instances in the resource group named myResourceGroup. 按如下所示输入自己的名称:Enter your own names as follows:

# Define the script for your Desired Configuration to download and run
$dscConfig = @{
  "wmfVersion" = "latest";
  "configuration" = @{
    "url" = "https://github.com/Azure-Samples/compute-automation-configurations/raw/master/dsc.zip";
    "script" = "configure-http.ps1";
    "function" = "WebsiteTest";

# Get information about the scale set
$vmss = Get-AzVmss `
                -ResourceGroupName "myResourceGroup" `
                -VMScaleSetName "myScaleSet"

# Add the Desired State Configuration extension to install IIS and configure basic website
$vmss = Add-AzVmssExtension `
    -VirtualMachineScaleSet $vmss `
    -Publisher Microsoft.Powershell `
    -Type DSC `
    -TypeHandlerVersion 2.24 `
    -Name "DSC" `
    -Setting $dscConfig

# Update the scale set and apply the Desired State Configuration extension to the VM instances
Update-AzVmss `
    -ResourceGroupName "myResourceGroup" `
    -Name "myScaleSet"  `
    -VirtualMachineScaleSet $vmss

如果规模集上的升级策略为 手动,则使用 Update-AzVmssInstance 更新 VM 实例。If the upgrade policy on your scale set is manual, update your VM instances with Update-AzVmssInstance. 此 cmdlet 会将更新的规模集配置应用于 VM 实例,并安装应用程序。This cmdlet applies the updated scale set configuration to the VM instances and installs your application.

使用 cloud-init 将应用安装到 Linux VMInstall an app to a Linux VM with cloud-init

Cloud-init 是一种广泛使用的方法,用于在首次启动 Linux VM 时对其进行自定义。Cloud-init is a widely used approach to customize a Linux VM as it boots for the first time. 可使用 cloud-init 来安装程序包和写入文件,或者配置用户和安全性。You can use cloud-init to install packages and write files, or to configure users and security. 在初始启动期间运行 cloud-init 时,无需额外的步骤且无需代理来应用配置。As cloud-init runs during the initial boot process, there are no additional steps or required agents to apply your configuration.

Cloud-init 还支持不同的发行版。Cloud-init also works across distributions. 例如,不要使用 apt-get 安装或 yum 安装来安装包。For example, you don't use apt-get install or yum install to install a package. 可定义要安装的程序包的列表。Instead you can define a list of packages to install. Cloud-init 将为所选发行版自动使用本机包管理工具。Cloud-init automatically uses the native package management tool for the distro you select.

有关详细信息,包括示例 cloud-init.txt 文件,请参阅使用 cloud-init 自定义 Azure VMFor more information, including an example cloud-init.txt file, see Use cloud-init to customize Azure VMs.

若要创建规模集并使用 cloud-init 文件,请将 --custom-data 参数添加到 az vmss create 命令并指定 cloud-init 文件的名称。To create a scale set and use a cloud-init file, add the --custom-data parameter to the az vmss create command and specify the name of a cloud-init file. 以下示例会在 myResourceGroup 中创建名为 myScaleSet 的规模集,并配置包含名为 cloud-init.txt 的文件的 VM 实例 。The following example creates a scale set named myScaleSet in myResourceGroup and configures VM instances with a file named cloud-init.txt. 按如下所示输入自己的名称:Enter your own names as follows:

az vmss create \
  --resource-group myResourceGroup \
  --name myScaleSet \
  --image UbuntuLTS \
  --upgrade-policy-mode automatic \
  --custom-data cloud-init.txt \
  --admin-username azureuser \

使用 OS 更新安装应用程序Install applications with OS updates

新的 OS 版本可用时,可使用或生成新的自定义映像并将 OS 升级部署到规模集中。When new OS releases are available, you can use or build a new custom image and deploy OS upgrades to a scale set. 每个 VM 实例均会升级到指定的最新映像。Each VM instance is upgraded to the latest image that you specify. 可使用预安装了应用程序的自定义映像、自定义脚本扩展或 PowerShell DSC 使应用程序在你执行升级时自动可用。You can use a custom image with the application pre-installed, the Custom Script Extension, or PowerShell DSC to have your application automatically available as you perform the upgrade. 执行此过程时,可能需要为应用程序维护制定计划,确保不存在版本兼容问题。You may need to plan for application maintenance as you perform this process to ensure that there are no version compatibility issues.

如果使用预安装了应用程序的自定义 VM 映像,则可将应用程序更新与部署管道集成,以便生成新的映像并在规模集中部署 OS 升级。If you use a custom VM image with the application pre-installed, you could integrate the application updates with a deployment pipeline to build the new images and deploy OS upgrades across the scale set. 此方法可使管道选取最新的应用程序版本,创建和验证 VM 映像,然后升级规模集中的 VM 实例。This approach allows the pipeline to pick up the latest application builds, create and validate a VM image, then upgrade the VM instances in the scale set. 若要运行跨自定义 VM 映像生成并部署应用程序更新的部署管道,可使用 SpinnakerJenkins 等平台。To run a deployment pipeline that builds and deploys application updates across custom VM images, you could use platform such as Spinnaker or Jenkins.

后续步骤Next steps

生成应用程序并将其部署到规模集时,可参阅规模集设计概述As you build and deploy applications to your scale sets, you can review the Scale Set Design Overview. 若要深入了解如何管理规模集,请参阅使用 PowerShell 管理规模集For more information on how to manage your scale set, see Use PowerShell to manage your scale set.