您现在访问的是微软AZURE全球版技术文档网站,若需要访问由世纪互联运营的MICROSOFT AZURE中国区技术文档网站,请访问 https://docs.azure.cn.

设置 Azure 应用服务中的过渡环境Set up staging environments in Azure App Service

将 Web 应用、Linux 上的 Web 应用、移动后端和 API 应用部署到 应用服务时,如果应用在“标准”或“高级”应用服务计划层下运行,则可以部署到单独的部署槽而不是默认的生产槽。When you deploy your web app, web app on Linux, mobile back end, and API app to App Service, you can deploy to a separate deployment slot instead of the default production slot when running in the Standard or Premium App Service plan tier. 部署槽实际上是具有自身主机名的实时应用。Deployment slots are actually live apps with their own hostnames. 两个部署槽(包括生产槽)之间的应用内容与配置元素可以交换。App content and configurations elements can be swapped between two deployment slots, including the production slot. 将应用程序部署到部署槽具有以下优点:Deploying your application to a deployment slot has the following benefits:

  • 可以在分阶段部署槽中验证应用更改,然后将其与生产槽交换。You can validate app changes in a staging deployment slot before swapping it with the production slot.
  • 首先将应用部署到槽,然后将其交换到生产,这确保槽的所有实例都已准备好,然后交换到生产。Deploying an app to a slot first and swapping it into production ensures that all instances of the slot are warmed up before being swapped into production. 部署应用时,这样可避免停机。This eliminates downtime when you deploy your app. 流量重定向是无缝的,且不会因交换操作而删除任何请求。The traffic redirection is seamless, and no requests are dropped as a result of swap operations. 当不需要预交换验证时,可以通过配置自动交换来自动化这整个工作流。This entire workflow can be automated by configuring Auto Swap when pre-swap validation is not needed.
  • 交换后,具有以前分阶段应用的槽现在具有以前的生产应用。After a swap, the slot with previously staged app now has the previous production app. 如果交换到生产槽的更改与预期不同,可以立即执行同一交换来收回“上一已知的良好站点”。If the changes swapped into the production slot are not as you expected, you can perform the same swap immediately to get your "last known good site" back.

每种应用服务计划层支持不同数量的部署槽。Each App Service plan tier supports a different number of deployment slots. 若要了解应用层支持的槽数,请参阅应用服务限制To find out the number of slots your app's tier supports, see App Service Limits. 若要将应用缩放到其他层,目标层必须支持应用业已使用的槽数。To scale your app to a different tier, the target tier must support the number of slots your app already uses. 例如,如果应用有 5 个以上的槽,则不能向下缩放到“标准”层,因为“标准”层只支持 5 个部署槽。For example, if your app has more than 5 slots, you cannot scale it down to Standard tier, because Standard tier only supports 5 deployment slots.

添加部署槽Add a deployment slot

必须在“标准”或“高级”层下运行应用,才能启用多个部署槽。The app must be running in the Standard or Premium tier in order for you to enable multiple deployment slots.

  1. Azure 门户中,打开应用的资源边栏选项卡In the Azure Portal, open your app's resource blade.
  2. 选择“部署槽”选项,并单击“添加槽”。Choose the Deployment slots option, then click Add Slot.

    添加新部署槽

    备注

    如果应用尚未处于“标准”或“高级”层,则会收到消息,指示启用过渡支持的层。If the app is not already in the Standard or Premium tier, you will receive a message indicating the supported tiers for enabling staged publishing. 此时,可选择“升级”,并导航到应用的“缩放”选项卡,并继续。At this point, you have the option to select Upgrade and navigate to the Scale tab of your app before continuing.

  3. 在“添加槽”边栏选项卡中,为槽提供一个名称,并选择是否要从其他现有部署槽中克隆应用配置。In the Add a slot blade, give the slot a name, and select whether to clone app configuration from another existing deployment slot. 单击复选标记以继续。Click the check mark to continue.

    配置源

    第一次添加槽时,只有两种选择:从生产中的默认槽克隆配置或者完全不进行克隆。The first time you add a slot, you only have two choices: clone configuration from the default slot in production or not at all. 创建多个插槽后,可以从生产槽以外的槽克隆配置:After you have created several slots, you will be able to clone configuration from a slot other than the one in production:

    配置源

  4. 在应用的资源边栏选项卡中,单击“部署槽”,并单击部署槽打开该槽的资源边栏选项卡,它包含一组度量值和配置(类似任何其他应用)。In your app's resource blade, click Deployment slots, then click a deployment slot to open that slot's resource blade, with a set of metrics and configuration just like any other app. 槽的名称将出现在边栏选项卡顶部,提醒你正在查看部署槽。The name of the slot is shown at the top of the blade to remind you that you are viewing the deployment slot.

    部署槽标题

  5. 单击此槽边栏选项卡中的应用 URL。Click the app URL in the slot's blade. 请注意,部署槽有其自己的主机名,同时它也是动态应用。Notice the deployment slot has its own hostname and is also a live app. 若要限制对部署槽的公共访问权限,请参阅应用服务 Web 应用 – 阻止对非生产部署槽的 Web 访问To limit public access to the deployment slot, see App Service Web App – block web access to non-production deployment slots.

创建部署槽后没有任何内容。There is no content after deployment slot creation. 可以从其他存储库分支或完全不同的存储库部署到槽。You can deploy to the slot from a different repository branch, or an altogether different repository. 还可以更改此槽的配置。You can also change the slot's configuration. 使用与内容更新部署槽关联的发布配置文件或部署凭证。Use the publish profile or deployment credentials associated with the deployment slot for content updates. 例如,可以使用 git 发布到此槽For example, you can publish to this slot with git.

交换哪些设置?Which settings are swapped?

从另一个部署槽克隆配置时,可以编辑克隆的配置。When you clone configuration from another deployment slot, the cloned configuration is editable. 此外,某些配置元素在交换时遵循内容(不特定于位置),而其他配置元素会在交换之后保留在同一个位置(特定于位置)。Furthermore, some configuration elements will follow the content across a swap (not slot specific) while other configuration elements will stay in the same slot after a swap (slot specific). 以下列表显示交换槽时会更改的设置。The following lists show the settings that change when you swap slots.

已交换的设置Settings that are swapped:

  • 常规设置 - 例如 Framework 版本、32/64 位、Web 套接字General settings - such as framework version, 32/64-bit, Web sockets
  • 应用设置(可以配置为停在槽中)App settings (can be configured to stick to a slot)
  • 连接字符串(可以配置为停在槽中)Connection strings (can be configured to stick to a slot)
  • 处理程序映射Handler mappings
  • 监视和诊断设置Monitoring and diagnostic settings
  • WebJobs 内容WebJobs content

不交换的设置Settings that are not swapped:

  • 发布终结点Publishing endpoints
  • 自定义域名Custom Domain Names
  • SSL 证书和绑定SSL certificates and bindings
  • 缩放设置Scale settings
  • Web 作业计划程序WebJobs schedulers

要将应用设置或连接字符串配置为停在某个槽中(不交换),请访问特定槽的“应用程序设置”边栏选项卡,然后针对应该位于该槽中的配置元素选中“槽设置”框。To configure an app setting or connection string to stick to a slot (not swapped), access the Application Settings blade for a specific slot, then select the Slot Setting box for the configuration elements that should stick the slot. 如果将配置元素标记为特定于槽,则无法在所有与该应用关联的部署槽之间交换该元素。Marking a configuration element as slot specific has the effect of establishing that element as not swappable across all the deployment slots associated with the app.

槽设置

交换部署槽Swap deployment slots

在应用的资源边栏选项卡的“概述”或“部署槽”中,可交换部署槽。You can swap deployment slots in the Overview or Deployment slots view of your app's resource blade.

重要

将应用从部署槽交换到生产之前,请确保所有非槽特定的设置已完全根据希望它在交换目标中的位置明确地进行配置。Before you swap an app from a deployment slot into production, make sure that all non-slot specific settings are configured exactly as you want to have it in the swap target.

  1. 若要交换部署槽,请在应用命令栏或部署槽命令栏中,单击“交换”按钮。To swap deployment slots, click the Swap button in the command bar of the app or in the command bar of a deployment slot.

    “交换”按钮

  2. 确保正确设置交换源和交换目标。Make sure that the swap source and swap target are set properly. 交换目标通常是生产槽。Usually, the swap target is the production slot. 单击“确定”完成操作。Click OK to complete the operation. 操作完成后,即已交换部署槽。When the operation finishes, the deployment slots have been swapped.

    完成交换

    有关“带预览的交换”交换类型,请参阅带预览的交换(多阶段交换)For the Swap with preview swap type, see Swap with preview (multi-phase swap).

带预览的交换(多阶段交换)Swap with preview (multi-phase swap)

带预览的交换(或多阶段交换)可简化特定于槽的配置元素(如连接字符串)的验证。Swap with preview, or multi-phase swap, simplify validation of slot-specific configuration elements, such as connection strings. 对于任务关键型工作负荷,在应用生产槽的配置时,请验证应用的行为是否符合预期,并且必须在将应用交换到生产之前执行此验证。For mission-critical workloads, you want to validate that the app behaves as expected when the production slot's configuration is applied, and you must perform such validation before the app is swapped into production. 带预览的交换正是需要的。Swap with preview is what you need.

备注

Linux 上的 Web 应用不支持带预览的交换。Swap with preview is not supported in web apps on Linux.

如果使用“带预览的交换”选项(请参阅交换部署槽),应用服务将执行以下操作:When you use the Swap with preview option (see Swap deployment slots), App Service does the following:

  • 目标槽保持不变,该槽上的现有工作负荷(如生产)不会受影响。Keeps the destination slot unchanged so existing workload on that slot (such as production) is not impacted.
  • 将目标槽的配置元素应用到源槽,包括特定于槽的连接字符串和应用设置。Applies the configuration elements of the destination slot to the source slot, including the slot-specific connection strings and app settings.
  • 使用前面提到的这些配置元素,重启源槽上的工作进程。Restarts the worker processes on the source slot using these aforementioned configuration elements.
  • 完成交换后:将准备好的源槽移到目标槽。When you complete the swap: Moves the pre-warmed-up source slot into the destination slot. 目标槽会按手动交换的方式移动到源槽。The destination slot is moved into the source slot as in a manual swap.
  • 取消交换时:重新将源槽的配置元素应用到源槽。When you cancel the swap: Reapplies the configuration elements of the source slot to the source slot.

可预览应用具体如何使用目标槽配置。You can preview exactly how the app will behave with the destination slot's configuration. 完成验证后,可通过单独的步骤完成交换。Once you complete validation, you complete the swap in a separate step. 此步骤具有额外优势,源槽已通过所需的配置提前准备好,因此客户端不会遇到停机的情况。This step has the added advantage that the source slot is already warmed up with the desired configuration, and clients don't experience any downtime.

“适用于部署槽的 Azure PowerShell cmdlet”部分中提供了可用于多阶段交换的 Azure PowerShell cmdlet 示例。Samples for the Azure PowerShell cmdlets available for multi-phase swap are included in the Azure PowerShell cmdlets for deployment slots section.

配置自动交换Configure Auto Swap

自动交换简化了 DevOps 方案,在此方案中,可连续部署应用,无需冷启动且不会给应用的最终客户造成停机。Auto Swap streamlines DevOps scenarios where you want to continuously deploy your app with zero cold start and zero downtime for end customers of the app. 将部署槽配置为自动交换到生产槽后,每次将代码更新推送到该槽时,应用服务会在其已在该槽上做好准备之后,自动将该应用交换到生产槽。When a deployment slot is configured for Auto Swap into production, every time you push your code update to that slot, App Service will automatically swap the app into production after it has already warmed up in the slot.

重要

为某个槽启用自动交换时,请确保槽配置与针对目标槽(通常是生产槽)的配置完全相同。When you enable Auto Swap for a slot, make sure the slot configuration is exactly the configuration intended for the target slot (usually the production slot).

备注

Linux 上的 Web 应用中不支持自动交换。Auto Swap is not supported in web apps on Linux.

为槽配置自动交换很容易。Configuring Auto Swap for a slot is easy. 执行以下步骤:Follow these steps:

  1. 在“部署槽”中,选择非生产槽,并在该槽的资源边栏选项卡中选择“应用程序设置”。In Deployment Slots, select a non-production slot, and choose Application Settings in that slot's resource blade.

  2. 针对“自动交换”选择“打开”,在“自动交换槽”中选择所需的目标槽,并在命令栏中单击“保存”。Select On for Auto Swap, select the desired target slot in Auto Swap Slot, and click Save in the command bar. 确保槽的配置与针对目标槽的配置完全相同。Make sure configuration for the slot is exactly the configuration intended for the target slot.

    操作完成后,“通知”选项卡会闪烁并显示绿色的“成功”字样。The Notifications tab flashes a green SUCCESS once the operation is complete.

    备注

    若要测试应用的自动交换,可在“自动交换槽”中选择非生产目标槽,以便先熟悉这个功能。To test Auto Swap for your app, you can first select a non-production target slot in Auto Swap Slot to become familiar with the feature.

  3. 向该部署槽执行代码推送。Execute a code push to that deployment slot. 不久之后,自动交换就会发生,而更新将反映在目标槽的 URL 上。Auto Swap happens after a short time and the update is reflected at your target slot's URL.

在交换后回退生产应用Roll back a production app after swap

如果在槽交换后在生产中发现任何错误,请立即通过交换相同的两个槽来将槽回滚到交换前状态。If any errors are identified in production after a slot swap, roll the slots back to their pre-swap states by swapping the same two slots immediately.

交换前的自定义准备工作Custom warm-up before swap

某些应用可能需要自定义的准备操作。Some apps may require custom warm-up actions. web.config 中的 applicationInitialization 配置元素允许指定收到请求之前要执行的自定义初始化操作。The applicationInitialization configuration element in web.config allows you to specify custom initialization actions to be performed before a request is received. 交换操作会等待此自定义准备操作完成。The swap operation waits for this custom warm-up to complete. 以下是 web.config 片段的示例。Here is a sample web.config fragment.

<system.webServer>
    <applicationInitialization>
        <add initializationPage="/" hostName="[app hostname]" />
        <add initializationPage="/Home/About" hostname="[app hostname]" />
    </applicationInitialization>
</system.webServer>

监视交换进度Monitor swap progress

有时候(例如,进行交换的应用的预热时间长),交换操作需要一些时间才能完成。Sometimes, the swap operation takes some time to complete, such as when the app that is swapped has a long warm-up time. 有关交换操作的详细信息,可参阅 Azure 门户中的活动日志You can get more information on swap operations in the Activity Log in the Azure portal.

在门户的应用页的左侧导航中,选择“活动日志”。In your app page of the portal, in the left-hand navigation, select Activity log.

交换操作在日志查询中显示为 SlotsswapA swap operation appears in the log query as Slotsswap. 可以将其展开,然后选择一个子操作或错误来查看详细信息。You can expand it and select one of the suboperations or errors to see the details.

槽交换的活动日志

删除部署槽Delete a deployment slot

在部署槽的边栏选项卡中,打开部署槽的边栏选项卡,单击“概述”(默认页),并在命令栏中单击“删除”。In the blade for a deployment slot, open the deployment slot's blade, click Overview (the default page), and click Delete in the command bar.

删除部署槽

使用 Azure PowerShell 进行自动化操作Automate with Azure PowerShell

Azure PowerShell 是一个模块,可提供通过 Windows PowerShell 管理 Azure 的 cmdlet,包括对管理 Azure 应用服务的部署槽的支持。Azure PowerShell is a module that provides cmdlets to manage Azure through Windows PowerShell, including support for managing deployment slots in Azure App Service.


创建 Web 应用Create a web app

New-AzureRmWebApp -ResourceGroupName [resource group name] -Name [app name] -Location [location] -AppServicePlan [app service plan name]

创建部署槽Create a deployment slot

New-AzureRmWebAppSlot -ResourceGroupName [resource group name] -Name [app name] -Slot [deployment slot name] -AppServicePlan [app service plan name]

启动带预览的交换(多阶段交换)并将目标槽配置应用到源槽Initiate a swap with preview (multi-phase swap) and apply destination slot configuration to source slot

$ParametersObject = @{targetSlot  = "[slot name – e.g. “production”]"}
Invoke-AzureRmResourceAction -ResourceGroupName [resource group name] -ResourceType Microsoft.Web/sites/slots -ResourceName [app name]/[slot name] -Action applySlotConfig -Parameters $ParametersObject -ApiVersion 2015-07-01

取消挂起的交换(带预览的交换)并还原源槽配置Cancel a pending swap (swap with review) and restore source slot configuration

Invoke-AzureRmResourceAction -ResourceGroupName [resource group name] -ResourceType Microsoft.Web/sites/slots -ResourceName [app name]/[slot name] -Action resetSlotConfig -ApiVersion 2015-07-01

交换部署槽Swap deployment slots

$ParametersObject = @{targetSlot  = "[slot name – e.g. “production”]"}
Invoke-AzureRmResourceAction -ResourceGroupName [resource group name] -ResourceType Microsoft.Web/sites/slots -ResourceName [app name]/[slot name] -Action slotsswap -Parameters $ParametersObject -ApiVersion 2015-07-01

在活动日志中监视交换事件Monitor swap events in the Activity Log

Get-AzureRmLog -ResourceGroup [resource group name] -StartTime 2018-03-07 -Caller SlotSwapJobProcessor  

删除部署槽Delete deployment slot

Remove-AzureRmResource -ResourceGroupName [resource group name] -ResourceType Microsoft.Web/sites/slots –Name [app name]/[slot name] -ApiVersion 2015-07-01

使用 Azure CLI 进行自动化操作Automate with Azure CLI

有关用于部署槽的 Azure CLI 命令,请参阅 az webapp deployment slotFor Azure CLI commands for deployment slots, see az webapp deployment slot.

后续步骤Next steps

Azure 应用服务 Web 应用 – 阻止对非生产部署槽的 Web 访问Azure App Service Web App – block web access to non-production deployment slots
Linux 应用服务简介Introduction to App Service on Linux
Microsoft Azure 免费试用Microsoft Azure Free Trial