您现在访问的是微软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 mode. 部署槽实际上是具有自身主机名的实时应用。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 mode supports a different number of deployment slots. 若要了解应用模式支持的槽数,请参阅应用服务定价To find out the number of slots your app's mode supports, see App Service Pricing.

  • 如果应用具有多个槽,则无法更改模式。When your app has multiple slots, you cannot change the mode.
  • 缩放不适用于非生产槽。Scaling is not available for non-production slots.
  • 非生产槽不支持链接的资源管理。Linked resource management is not supported for non-production slots. 仅在 Azure 门户中,可以通过暂时会非生产槽移到其他应用服务计划模式避免对生产槽造成这种潜在影响。In the Azure Portal only, you can avoid this potential impact on a production slot by temporarily moving the non-production slot to a different App Service plan mode. 请注意,非生产槽必须先再次与生产槽共享相同的模式,才能交换这两个槽。Note that the non-production slot must once again share the same mode with the production slot before you can swap the two slots.

添加部署槽Add a deployment slot

必须在“标准”或“高级”模式下运行应用,才能启用多个部署槽。The app must be running in the Standard or Premium mode 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 mode, you will receive a message indicating the supported modes 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 will 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.

部署槽的配置Configuration for deployment slots

从另一个部署槽克隆配置时,可以编辑克隆的配置。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 configuration that will 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. 请注意,如果将配置元素标记为特定于槽会在将该元素建立为无法在所有与该应用关联的部署槽之间进行交换时产生影响。Note that 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 (e.g. 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 will not 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 the steps below:

  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 will flash 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 will happen after a short time and the update will be reflected at your target slot's URL.

在交换后回滚生产应用的步骤To rollback 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 will wait for this custom warm-up to complete. 以下是 web.config 片段的示例。Here is a sample web.config fragment.

    <add initializationPage="/" hostName="[app hostname]" />
    <add initializationPage="/Home/About" hostname="[app hostname]" />

删除部署槽To 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 cmdletAzure PowerShell cmdlets for deployment slots

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 review (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

删除部署槽Delete deployment slot

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

用于部署槽的 Azure 命令行界面 (Azure CLI) 命令Azure Command-Line Interface (Azure CLI) commands for Deployment Slots

Azure CLI 提供了适用于 Azure 的跨平台命令,包括对管理应用服务部署槽的支持。The Azure CLI provides cross-platform commands for working with Azure, including support for managing App Service deployment slots.

  • 有关安装和配置 Azure CLI 的说明(包括有关如何将 Azure CLI 连接到 Azure 订阅的信息),请参阅安装和配置 Azure CLIFor instructions on installing and configuring the Azure CLI, including information on how to connect Azure CLI to your Azure subscription, see Install and Configure the Azure CLI.
  • 若要在 Azure CLI 中列出可用于 Azure 应用服务的命令,请调用 azure site -hTo list the commands available for Azure App Service in the Azure CLI, call azure site -h.


若要了解部署槽的 Azure CLI 2.0 命令,请参阅 az appservice web 部署槽For Azure CLI 2.0 commands for deployment slots, see az appservice web deployment slot.

azure site listazure site list

若要了解当前订阅中的应用,请调用 azure site list,如以下示例所示。For information about the apps in the current subscription, call azure site list, as in the following example.

azure site list webappslotstest

azure site createazure site create

若要创建部署槽,请调用 azure site create,并指定现有应用的名称和要创建的槽的名称,如以下示例所示。To create a deployment slot, call azure site create and specify the name of an existing app and the name of the slot to create, as in the following example.

azure site create webappslotstest --slot staging

若要启用新槽源代码管理,请使用 --git 选项,如以下示例所示。To enable source control for the new slot, use the --git option, as in the following example.

azure site create --git webappslotstest --slot staging

azure site swapazure site swap

若要使更新的部署槽成为生产应用,请使用 azure site swap 命令执行交换操作,如以下示例所示。To make the updated deployment slot the production app, use the azure site swap command to perform a swap operation, as in the following example. 生产应用将不会停机,也不会进行冷启动。The production app will not experience any down time, nor will it undergo a cold start.

azure site swap webappslotstest

azure site deleteazure site delete

若要删除不再需要的部署槽,请使用 azure site delete 命令,如以下示例所示。To delete a deployment slot that is no longer needed, use the azure site delete command, as in the following example.

azure site delete webappslotstest --slot staging


查看实际应用中的 Web 应用。See a web app in action. 立即试用应用服务,创建短期的入门级应用 — 无需信用卡,也无需做出任何承诺。Try App Service immediately and create a short-lived starter app—no credit card required, no commitments.

后续步骤Next Steps

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