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

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

如果将 web 应用、Linux 上的 web 应用、移动后端或 API 应用部署到Azure App Service,则在标准高级独立应用服务计划层中运行时,可以使用单独的部署槽而不是默认的生产槽。When you deploy your web app, web app on Linux, mobile back end, or API app to Azure App Service, you can use a separate deployment slot instead of the default production slot when you're running in the Standard, Premium, or Isolated App Service plan tier. 部署槽是具有自己的主机名的实时应用。Deployment slots are live apps with their own host names. 两个部署槽(包括生产槽)之间的应用内容与配置元素可以交换。App content and configurations elements can be swapped between two deployment slots, including the production slot.

将应用程序部署到非生产槽具有以下优点:Deploying your application to a non-production 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 makes sure 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 because of swap operations. 在不需要预交换验证时,可以通过配置自动交换来自动执行整个工作流。You can automate this entire workflow by configuring auto swap when pre-swap validation isn't needed.
  • 交换后,具有以前分阶段应用的槽现在具有以前的生产应用。After a swap, the slot with previously staged app now has the previous production app. 如果交换到生产槽的更改与预期不同,可以立即执行同一交换来收回“上一已知的良好站点”。If the changes swapped into the production slot aren't as you expect, 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. 使用部署槽位不会产生额外的费用。There's no additional charge for using 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, make sure that the target tier supports the number of slots your app already uses. 例如,如果您的应用程序具有五个以上的槽,则无法将其缩小到标准层,因为标准级别仅支持五个部署槽。For example, if your app has more than five slots, you can't scale it down to the Standard tier, because the Standard tier supports only five deployment slots.

添加槽位Add a slot

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

  1. Azure 门户中,搜索并选择 "应用服务" 并选择应用。in the Azure portal, search for and select App Services and select your app.

    搜索应用服务

  2. 在左窗格中,选择 "部署槽" > "添加槽"。In the left pane, select Deployment slots > Add Slot.

    添加新部署槽

    备注

    如果应用尚未处于 "标准"、"高级" 或 "独立" 层中,你将收到一条消息,指示用于启用过渡发布的支持层。If the app isn't already in the Standard, Premium, or Isolated tier, you receive a message that indicates the supported tiers for enabling staged publishing. 此时,可以选择 "升级" 并在应用程序的 "缩放" 选项卡上继续操作。At this point, you have the option to select Upgrade and go to the Scale tab of your app before continuing.

  3. 在 "添加槽" 对话框中,为槽指定一个名称,并选择是否要从另一个部署槽克隆应用配置。In the Add a slot dialog box, give the slot a name, and select whether to clone an app configuration from another deployment slot. 选择 "添加" 以继续。Select Add to continue.

    配置源

    你可以从任何现有槽克隆配置。You can clone a configuration from any existing slot. 可以克隆的设置包括应用设置、连接字符串、语言框架版本、Web 套接字、HTTP 版本和平台位数。Settings that can be cloned include app settings, connection strings, language framework versions, web sockets, HTTP version, and platform bitness.

  4. 添加槽后,选择 "关闭" 以关闭对话框。After the slot is added, select Close to close the dialog box. 新槽现在显示在 "部署槽" 页上。The new slot is now shown on the Deployment slots page. 默认情况下,新槽的流量% 设置为0,所有客户流量都路由到生产槽。By default, Traffic % is set to 0 for the new slot, with all customer traffic routed to the production slot.

  5. 选择新的部署槽位以打开该槽的资源页。Select the new deployment slot to open that slot's resource page.

    部署槽位标题

    过渡槽具有管理页面,就像任何其他应用服务应用一样。The staging slot has a management page just like any other App Service app. 可以更改此槽的配置。You can change the slot's configuration. 槽的名称将出现在页面顶部,提醒你正在查看部署槽位。The name of the slot is shown at the top of the page to remind you that you're viewing the deployment slot.

  6. 选择槽资源页上的 "应用 URL"。Select the app URL on the slot's resource page. 部署槽具有其自己的主机名,并且也是一个实时应用。The deployment slot has its own host name and is also a live app. 若要限制对部署槽的公共访问权限,请参阅AZURE APP SERVICE IP 限制To limit public access to the deployment slot, see Azure App Service IP restrictions.

即使从其他槽克隆设置,新部署槽位也无内容。The new deployment slot has no content, even if you clone the settings from a different slot. 例如,可以通过 Git 发布到此槽For example, you can publish to this slot with Git. 可以从其他存储库分支或不同的存储库部署到槽。You can deploy to the slot from a different repository branch or a different repository.

交换期间发生的情况What happens during a swap

交换操作步骤Swap operation steps

将两个槽(通常从暂存槽到生产槽)交换时,应用服务会执行以下操作,以确保目标槽不会遇到停机时间:When you swap two slots (usually from a staging slot into the production slot), App Service does the following to ensure that the target slot doesn't experience downtime:

  1. 将以下设置从目标槽(例如,生产槽)应用到源槽的所有实例:Apply the following settings from the target slot (for example, the production slot) to all instances of the source slot:

    其中的任何情况都会触发源槽中的所有实例重新启动。Any of these cases trigger all instances in the source slot to restart. 交换和预览期间,会标记第一个阶段的结束。During swap with preview, this marks the end of the first phase. 交换操作已暂停,你可以通过目标槽的设置验证源槽是否正常工作。The swap operation is paused, and you can validate that the source slot works correctly with the target slot's settings.

  2. 等待源槽中的每个实例完成重新启动。Wait for every instance in the source slot to complete its restart. 如果任何实例无法重新启动,则交换操作会将所有更改恢复到源槽并停止操作。If any instance fails to restart, the swap operation reverts all changes to the source slot and stops the operation.

  3. 如果启用了本地缓存,请在源槽的每个实例上向应用程序根("/")发出 HTTP 请求,以触发本地缓存初始化。If local cache is enabled, trigger local cache initialization by making an HTTP request to the application root ("/") on each instance of the source slot. 等待,直到每个实例返回任何 HTTP 响应。Wait until each instance returns any HTTP response. 本地缓存初始化会导致每个实例上重新启动一次。Local cache initialization causes another restart on each instance.

  4. 如果使用自定义预热启用自动交换,则会通过对源槽的每个实例发出 HTTP 请求来触发应用程序根("/"),触发应用程序启动If auto swap is enabled with custom warm-up, trigger Application Initiation by making an HTTP request to the application root ("/") on each instance of the source slot.

    如果未指定 applicationInitialization,将触发对每个实例上的源槽的应用程序根的 HTTP 请求。If applicationInitialization isn't specified, trigger an HTTP request to the application root of the source slot on each instance.

    如果实例返回任何 HTTP 响应,则会将其视为准备好。If an instance returns any HTTP response, it's considered to be warmed up.

  5. 如果源槽上的所有实例都已成功准备好,则通过切换两个槽的路由规则来交换两个槽。If all instances on the source slot are warmed up successfully, swap the two slots by switching the routing rules for the two slots. 执行此步骤后,目标槽(例如,生产槽)包含先前在源槽中准备好的应用。After this step, the target slot (for example, the production slot) has the app that's previously warmed up in the source slot.

  6. 现在源槽具有以前在目标槽中的预交换应用,请通过应用所有设置并重新启动实例来执行相同的操作。Now that the source slot has the pre-swap app previously in the target slot, perform the same operation by applying all settings and restarting the instances.

在交换操作的任意一点,初始化交换应用的所有工作都将在源槽上发生。At any point of the swap operation, all work of initializing the swapped apps happens on the source slot. 当源槽正在准备并准备好时,目标槽保持联机,而不考虑交换成功或失败的位置。The target slot remains online while the source slot is being prepared and warmed up, regardless of where the swap succeeds or fails. 若要将过渡槽交换到生产槽,请确保生产槽始终是目标槽。To swap a staging slot with the production slot, make sure that the production slot is always the target slot. 这样,交换操作不会影响生产应用。This way, the swap operation doesn't affect your production app.

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

从另一个部署槽克隆配置时,可以编辑克隆的配置。When you clone configuration from another deployment slot, the cloned configuration is editable. 某些配置元素会在交换(而不是特定于插槽)后跟随内容,而其他配置元素会在交换(特定于槽)后保留在同一插槽中。Some configuration elements follow the content across a swap (not slot specific), whereas other configuration elements 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
  • 公用证书Public certificates
  • WebJobs 内容WebJobs content
  • 混合连接 *Hybrid connections *
  • 虚拟网络集成 *Virtual network integration *
  • 服务终结点 *Service endpoints *
  • Azure 内容分发网络 *Azure Content Delivery Network *

标有星号(*)的功能计划为 unswapped。Features marked with an asterisk (*) are planned to be unswapped.

不交换的设置Settings that aren't swapped:

  • 发布终结点Publishing endpoints
  • 自定义域名Custom domain names
  • 非公共证书和 TLS/SSL 设置Non-public certificates and TLS/SSL settings
  • 缩放设置Scale settings
  • Web 作业计划程序WebJobs schedulers
  • IP 限制IP restrictions
  • 始终可用Always On
  • 诊断日志设置Diagnostic log settings
  • 跨域资源共享(CORS)Cross-origin resource sharing (CORS)

备注

适用于 unswapped 设置的某些应用设置也不会进行交换。Certain app settings that apply to unswapped settings are also not swapped. 例如,由于未交换诊断日志设置,因此也不会交换诸如 WEBSITE_HTTPLOGGING_RETENTION_DAYSDIAGNOSTICS_AZUREBLOBRETENTIONDAYS 之类的相关应用程序设置,即使它们不显示为槽设置也是如此。For example, since diagnostic log settings are not swapped, related app settings like WEBSITE_HTTPLOGGING_RETENTION_DAYS and DIAGNOSTICS_AZUREBLOBRETENTIONDAYS are also not swapped, even if they don't show up as slot settings.

若要将应用设置或连接字符串配置为坚持使用特定插槽(不交换),请前往该槽的配置页。To configure an app setting or connection string to stick to a specific slot (not swapped), go to the Configuration page for that slot. 添加或编辑设置,然后选择 "部署槽设置"。Add or edit a setting, and then select deployment slot setting. 如果选中此复选框,则会告诉应用程序该设置不可交换。Selecting this check box tells App Service that the setting is not swappable.

槽设置

交换两个槽Swap two slots

可以在应用的 "部署槽" 页和 "概述" 页上交换部署槽位。You can swap deployment slots on your app's Deployment slots page and the Overview page. 有关槽交换的技术详细信息,请参阅交换过程中发生的情况For technical details on the slot swap, see What happens during swap.

重要

在将应用从部署槽交换到生产环境之前,请确保 "生产" 是目标槽,并且源槽中的所有设置都已准确配置为要在生产中使用它们。Before you swap an app from a deployment slot into production, make sure that production is your target slot and that all settings in the source slot are configured exactly as you want to have them in production.

若要交换部署槽:To swap deployment slots:

  1. 转到应用的部署槽位页面,并选择 "交换"。Go to your app's Deployment slots page and select Swap.

    交换按钮

    "交换" 对话框显示所选源槽和目标槽中要更改的设置。The Swap dialog box shows settings in the selected source and target slots that will be changed.

  2. 选择所需的“源”和“目标”槽。Select the desired Source and Target slots. 目标通常是生产槽。Usually, the target is the production slot. 此外,选择 "源更改" 和 "目标更改" 选项卡,并验证是否需要配置更改。Also, select the Source Changes and Target Changes tabs and verify that the configuration changes are expected. 完成后,可以通过选择 "交换" 来立即交换槽。When you're finished, you can swap the slots immediately by selecting Swap.

    完成交换

    若要查看在实际发生交换之前目标槽如何以新设置运行,请不要选择 "交换",但请按照 "带预览的交换" 中的说明进行操作。To see how your target slot would run with the new settings before the swap actually happens, don't select Swap, but follow the instructions in Swap with preview.

  3. 完成后,选择 "关闭" 以关闭该对话框。When you're finished, close the dialog box by selecting Close.

如果有任何问题,请参阅排查交换问题。If you have any problems, see Troubleshoot swaps.

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

作为目标槽交换到生产环境之前,请验证应用是否以交换的设置运行。Before you swap into production as the target slot, validate that the app runs with the swapped settings. 源槽还会在交换完成前准备好,这是任务关键型应用程序所必需的。The source slot is also warmed up before the swap completion, which is desirable for mission-critical applications.

当你执行带预览的交换时,应用服务执行相同的交换操作,但在第一步后暂停。When you perform a swap with preview, App Service performs the same swap operation but pauses after the first step. 然后,你可以在完成交换前验证过渡槽上的结果。You can then verify the result on the staging slot before completing the swap.

如果取消交换,应用服务会将配置元素重新应用到源槽。If you cancel the swap, App Service reapplies configuration elements to the source slot.

若要交换预览版:To swap with preview:

  1. 按照交换部署槽位中的步骤操作,并选择 "执行带预览的交换"。Follow the steps in Swap deployment slots but select Perform swap with preview.

    带预览的交换

    此对话框显示了源槽中的配置在阶段1中的更改,以及源槽和目标槽在阶段2中的变化情况。The dialog box shows you how the configuration in the source slot changes in phase 1, and how the source and target slot change in phase 2.

  2. 准备好开始交换时,请选择 "开始交换"。When you're ready to start the swap, select Start Swap.

    阶段1完成后,会在对话框中收到通知。When phase 1 finishes, you're notified in the dialog box. 转到 https://<app_name>-<source-slot-name>.azurewebsites.net预览源槽中的交换。Preview the swap in the source slot by going to https://<app_name>-<source-slot-name>.azurewebsites.net.

  3. 准备完成挂起的交换后,选择 "完成交换"操作并选择 "完成交换"。When you're ready to complete the pending swap, select Complete Swap in Swap action and select Complete Swap.

    若要取消挂起的交换,请选择 "取消交换"。To cancel a pending swap, select Cancel Swap instead.

  4. 完成后,选择 "关闭" 以关闭该对话框。When you're finished, close the dialog box by selecting Close.

如果有任何问题,请参阅排查交换问题。If you have any problems, see Troubleshoot swaps.

若要自动执行多阶段交换,请参阅使用 PowerShell 进行自动化操作To automate a multi-phase swap, see Automate with PowerShell.

回滚交换Roll back a swap

如果在槽交换后目标槽(例如,生产槽)中发生任何错误,请通过立即交换相同的两个槽以将槽恢复到其交换前状态。If any errors occur in the target slot (for example, the production slot) after a slot swap, restore the slots to their pre-swap states by swapping the same two slots immediately.

配置自动交换Configure auto swap

备注

在 Linux 上的 web 应用中不支持自动交换。Auto swap isn't supported in web apps on Linux.

自动交换简化了 Azure DevOps 方案,在这些方案中,你希望在应用程序的客户无冷启动和零停机时间的情况下持续部署你的应用程序。Auto swap streamlines Azure DevOps scenarios where you want to deploy your app continuously with zero cold starts and zero downtime for customers of the app. 如果从槽到生产环境启用了自动交换,则每次将代码更改推送到该槽时,应用服务会在源槽中准备好后,自动将该应用交换到生产中。When auto swap is enabled from a slot into production, every time you push your code changes to that slot, App Service automatically swaps the app into production after it's warmed up in the source slot.

备注

在为生产槽配置自动交换之前,请考虑在非生产目标槽上测试自动交换。Before you configure auto swap for the production slot, consider testing auto swap on a non-production target slot.

配置自动交换:To configure auto swap:

  1. 中转到应用的资源页。Go to your app's resource page. 选择 "部署槽位" > <所需的源槽,> > 配置 > "常规设置"。Select Deployment slots > <desired source slot> > Configuration > General settings.

  2. 启用自动交换时,请选择 "打开"For Auto swap enabled, select On. 然后,为自动交换部署槽选择所需的目标槽,并在命令栏上选择 "保存"。Then select the desired target slot for Auto swap deployment slot, and select Save on the command bar.

    用于配置自动交换的选项

  3. 执行代码推送到源槽。Execute a code push to the source slot. 短时间之后会自动交换,并且更新将反映在目标槽的 URL 上。Auto swap happens after a short time, and the update is reflected at your target slot's URL.

如果有任何问题,请参阅排查交换问题。If you have any problems, see Troubleshoot swaps.

指定自定义预热Specify custom warm-up

某些应用可能需要自定义的预热操作才能进行交换。Some apps might require custom warm-up actions before the swap. Web.config 中的 applicationInitialization 配置元素允许你指定自定义初始化操作。The applicationInitialization configuration element in web.config lets you specify custom initialization actions. 交换操作将等待此自定义预热完成,然后再交换目标槽。The swap operation waits for this custom warm-up to finish before swapping with the target slot. 下面是 web.config 片段的示例。Here's a sample web.config fragment.

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

有关自定义 applicationInitialization 元素的详细信息,请参阅最常见的部署槽交换失败及其修复方法For more information on customizing the applicationInitialization element, see Most common deployment slot swap failures and how to fix them.

你还可以通过以下一个或两个应用设置自定义预热行为:You can also customize the warm-up behavior with one or both of the following app settings:

  • WEBSITE_SWAP_WARMUP_PING_PATH:用于预热站点的 ping 的路径。WEBSITE_SWAP_WARMUP_PING_PATH: The path to ping to warm up your site. 通过指定以斜杠开头的自定义路径作为值来添加此应用设置。Add this app setting by specifying a custom path that begins with a slash as the value. 示例为 /statuscheckAn example is /statuscheck. 默认值是 /The default value is /.
  • WEBSITE_SWAP_WARMUP_PING_STATUSES:预热操作的有效 HTTP 响应代码。WEBSITE_SWAP_WARMUP_PING_STATUSES: Valid HTTP response codes for the warm-up operation. 使用以逗号分隔的 HTTP 代码列表添加此应用设置。Add this app setting with a comma-separated list of HTTP codes. 200,202 的示例。An example is 200,202 . 如果返回的状态代码不在列表中,则预热和交换操作将停止。If the returned status code isn't in the list, the warmup and swap operations are stopped. 默认情况下,所有响应代码都是有效的。By default, all response codes are valid.

备注

<applicationInitialization> 配置元素是每个应用程序启动的一部分,而两个预热行为应用设置仅适用于槽交换。The <applicationInitialization> configuration element is part of each app start-up, whereas the two warm-up behavior app settings apply only to slot swaps.

如果有任何问题,请参阅排查交换问题。If you have any problems, see Troubleshoot swaps.

监视交换Monitor a swap

如果交换操作需要很长时间才能完成,可在活动日志中获取有关交换操作的信息。If the swap operation takes a long time to complete, you can get information on the swap operation in the activity log.

在门户的 "资源" 页上的左窗格中,选择 "活动日志"。On your app's resource page in the portal, in the left pane, select Activity log.

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

路由流量Route traffic

默认情况下,应用生产 URL (http://<app_name>.azurewebsites.net) 的所有客户端请求都将路由到生产槽。By default, all client requests to the app's production URL (http://<app_name>.azurewebsites.net) are routed to the production slot. 可以将部分流量路由到另一个槽。You can route a portion of the traffic to another slot. 如果需要用户对新更新的反馈,但是还没有准备好将其发布到生产环境中,那么这个功能非常有用。This feature is useful if you need user feedback for a new update, but you're not ready to release it to production.

自动路由生产流量Route production traffic automatically

自动路由生产流量:To route production traffic automatically:

  1. 请在应用的资源页上,选择 "部署槽位"。Go to your app's resource page and select Deployment slots.

  2. 在要路由到的槽的“流量 %”列中,指定一个百分比(介于 0 到 100 之间)以表示要路由的总流量。In the Traffic % column of the slot you want to route to, specify a percentage (between 0 and 100) to represent the amount of total traffic you want to route. 选择“保存”。Select Save.

    设置流量百分比

保存设置后,指定百分比的客户端将随机路由到非生产槽。After the setting is saved, the specified percentage of clients is randomly routed to the non-production slot.

将客户端自动路由到特定的槽后,它将在该客户端会话的生命周期内 "固定" 到该槽。After a client is automatically routed to a specific slot, it's "pinned" to that slot for the life of that client session. 在客户端浏览器上,可以通过查看 HTTP 标头中的 x-ms-routing-name cookie 来查看会话固定到哪个槽。On the client browser, you can see which slot your session is pinned to by looking at the x-ms-routing-name cookie in your HTTP headers. 路由到“暂存”槽的请求具有 cookie x-ms-routing-name=stagingA request that's routed to the "staging" slot has the cookie x-ms-routing-name=staging. 路由到生产槽的请求具有 cookie x-ms-routing-name=selfA request that's routed to the production slot has the cookie x-ms-routing-name=self.

备注

在 Azure 门户旁边,还可以使用 Azure CLI 中的az webapp traffic-routing set命令,设置 DevOps 管道或其他自动化系统等 CI/CD 工具中的路由百分比。Next to the Azure portal, you can also use the az webapp traffic-routing set command in the Azure CLI to set the routing percentages from CI/CD tools like DevOps pipelines or other automation systems.

手动路由生产流量Route production traffic manually

除了自动流量路由以外,应用服务也可以将请求路由到特定槽。In addition to automatic traffic routing, App Service can route requests to a specific slot. 如果希望用户能够选择加入或退出 beta 应用,这会很有用。This is useful when you want your users to be able to opt in to or opt out of your beta app. 若要手动路由生产流量,请使用 x-ms-routing-name 查询参数。To route production traffic manually, you use the x-ms-routing-name query parameter.

例如,若要让用户选择退出 beta 应用,可以在网页上放置此链接:To let users opt out of your beta app, for example, you can put this link on your webpage:

<a href="<webappname>.azurewebsites.net/?x-ms-routing-name=self">Go back to production app</a>

字符串 x-ms-routing-name=self 指定生产槽。The string x-ms-routing-name=self specifies the production slot. 客户端浏览器访问链接后,会重定向到生产槽。After the client browser accesses the link, it's redirected to the production slot. 每个后续请求都具有将会话固定到生产槽的 x-ms-routing-name=self cookie。Every subsequent request has the x-ms-routing-name=self cookie that pins the session to the production slot.

若要让用户选择加入 beta 应用,请将相同的查询参数设置为非生产槽的名称。To let users opt in to your beta app, set the same query parameter to the name of the non-production slot. 下面是一个示例:Here's an example:

<webappname>.azurewebsites.net/?x-ms-routing-name=staging

默认情况下,将为新的槽给定路由规则 0%,以灰色显示。By default, new slots are given a routing rule of 0%, shown in grey. 如果将此值显式设置为 0% (显示为黑色文本),则用户可以通过使用 x-ms-routing-name 查询参数手动访问过渡槽。When you explicitly set this value to 0% (shown in black text), your users can access the staging slot manually by using the x-ms-routing-name query parameter. 但不会自动将它们路由到插槽,因为路由百分比设置为0。But they won't be routed to the slot automatically because the routing percentage is set to 0. 这是一种高级方案,你可以在其中 "隐藏" 过渡槽,同时允许内部团队测试槽上的更改。This is an advanced scenario where you can "hide" your staging slot from the public while allowing internal teams to test changes on the slot.

删除槽Delete a slot

搜索并选择应用。Search for and select your app. 选择要删除 > 概述 * > <槽的*部署槽位Select Deployment slots > <slot to delete> > Overview. 在命令栏上选择 "删除"。Select Delete on the command bar.

删除部署槽

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

备注

本文进行了更新,以便使用新的 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.

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.

有关安装和配置 Azure PowerShell 的信息以及使用 Azure 订阅对 Azure PowerShell 进行身份验证的信息,请参阅如何安装和配置 Microsoft Azure PowerShellFor information on installing and configuring Azure PowerShell, and on authenticating Azure PowerShell with your Azure subscription, see How to install and configure Microsoft Azure PowerShell.


创建 Web 应用Create a web app

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

创建槽Create a slot

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

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

$ParametersObject = @{targetSlot  = "[slot name – e.g. “production”]"}
Invoke-AzResourceAction -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 the source slot configuration

Invoke-AzResourceAction -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-AzResourceAction -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-AzLog -ResourceGroup [resource group name] -StartTime 2018-03-07 -Caller SlotSwapJobProcessor  

删除槽Delete a slot

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

资源管理器模板自动执行Automate with Resource Manager templates

Azure 资源管理器模板是声明性的 JSON 文件,用于自动部署和配置 Azure 资源。Azure Resource Manager templates are declarative JSON files used to automate the deployment and configuration of Azure resources. 若要通过使用资源管理器模板来交换槽,你将在 " microsoft/站点/槽" 和 " microsoft 网站/站点" 资源上设置两个属性:To swap slots by using Resource Manager templates, you will set two properties on the Microsoft.Web/sites/slots and Microsoft.Web/sites resources:

  • buildVersion:这是一个字符串属性,表示槽中部署的应用的当前版本。buildVersion: this is a string property which represents the current version of the app deployed in the slot. 例如: "v1"、"1.0.0.1" 或 "20T11:53: 25.2887393-07: 00"。For example: "v1", "1.0.0.1", or "2019-09-20T11:53:25.2887393-07:00".
  • targetBuildVersion:这是一个字符串属性,它指定了槽应使用的 buildVersiontargetBuildVersion: this is a string property that specifies what buildVersion the slot should have. 如果 targetBuildVersion 不等于当前 buildVersion,则会通过查找具有指定 buildVersion的槽来触发交换操作。If the targetBuildVersion does not equal the current buildVersion, then this will trigger the swap operation by finding the slot which has the specified buildVersion.

示例资源管理器模板Example Resource Manager template

以下资源管理器模板将更新过渡槽的 buildVersion 并在生产槽上设置 targetBuildVersionThe following Resource Manager template will update the buildVersion of the staging slot and set the targetBuildVersion on the production slot. 这将交换两个槽。This will swap the two slots. 该模板假设已有使用名为 "过渡" 的槽创建的 webapp。The template assumes you already have a webapp created with a slot named "staging".

{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "my_site_name": {
            "defaultValue": "SwapAPIDemo",
            "type": "String"
        },
        "sites_buildVersion": {
            "defaultValue": "v1",
            "type": "String"
        }
    },
    "resources": [
        {
            "type": "Microsoft.Web/sites/slots",
            "apiVersion": "2018-02-01",
            "name": "[concat(parameters('my_site_name'), '/staging')]",
            "location": "East US",
            "kind": "app",
            "properties": {
                "buildVersion": "[parameters('sites_buildVersion')]"
            }
        },
        {
            "type": "Microsoft.Web/sites",
            "apiVersion": "2018-02-01",
            "name": "[parameters('my_site_name')]",
            "location": "East US",
            "kind": "app",
            "dependsOn": [
                "[resourceId('Microsoft.Web/sites/slots', parameters('my_site_name'), 'staging')]"
            ],
            "properties": {
                "targetBuildVersion": "[parameters('sites_buildVersion')]"
            }
        }        
    ]
}

此资源管理器模板是幂等的,这意味着它可以重复执行,并生成相同的槽状态。This Resource Manager template is idempotent, meaning that it can be executed repeatedly and produce the same state of the slots. 第一次执行后,targetBuildVersion 将与当前 buildVersion匹配,因此不会触发交换。After the first execution, targetBuildVersion will match the current buildVersion, so a swap will not be triggered.

通过 CLI 自动执行Automate with the CLI

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

交换疑难解答Troubleshoot swaps

如果槽交换期间发生任何错误,则会将其记录在D:\home\LogFiles\eventlog.xml中。If any error occurs during a slot swap, it's logged in D:\home\LogFiles\eventlog.xml. 它还记录在特定于应用程序的错误日志中。It's also logged in the application-specific error log.

下面是一些常见的交换错误:Here are some common swap errors:

  • 对应用程序根的 HTTP 请求已超时。An HTTP request to the application root is timed. 交换操作会等待每个 HTTP 请求90秒,并重试最多5次。The swap operation waits for 90 seconds for each HTTP request, and retries up to 5 times. 如果所有重试均已超时,则交换操作将停止。If all retries are timed out, the swap operation is stopped.

  • 当应用内容超出为本地缓存指定的本地磁盘配额时,本地缓存初始化可能会失败。Local cache initialization might fail when the app content exceeds the local disk quota specified for the local cache. 有关详细信息,请参阅本地缓存概述For more information, see Local cache overview.

  • 自定义预热期间,会在内部发出 HTTP 请求(不通过外部 URL)。During custom warm-up, the HTTP requests are made internally (without going through the external URL). 在 web.config 中, 它们可能会失败,并会出现某些 URL 重写规则。例如,重定向域名或强制 HTTPS 的规则可能会阻止预热请求到达应用代码。They can fail with certain URL rewrite rules in Web.config. For example, rules for redirecting domain names or enforcing HTTPS can prevent warm-up requests from reaching the app code. 若要解决此问题,请添加以下两个条件来修改重写规则:To work around this issue, modify your rewrite rules by adding the following two conditions:

    <conditions>
      <add input="{WARMUP_REQUEST}" pattern="1" negate="true" />
      <add input="{REMOTE_ADDR}" pattern="^100?\." negate="true" />
      ...
    </conditions>
    
  • 如果没有自定义的预热,URL 重写规则仍可阻止 HTTP 请求。Without a custom warm-up, the URL rewrite rules can still block HTTP requests. 若要解决此问题,请添加以下条件来修改重写规则:To work around this issue, modify your rewrite rules by adding the following condition:

    <conditions>
      <add input="{REMOTE_ADDR}" pattern="^100?\." negate="true" />
      ...
    </conditions>
    
  • 某些IP 限制规则可能会阻止交换操作向你的应用程序发送 HTTP 请求。Some IP restriction rules might prevent the swap operation from sending HTTP requests to your app. 10. 开头且 100. 的 IPv4 地址范围在部署内部。IPv4 address ranges that start with 10. and 100. are internal to your deployment. 你应允许它们连接到你的应用程序。You should allow them to connect to your app.

  • 槽交换后,应用可能会遇到意外的重新启动。After slot swaps, the app may experience unexpected restarts. 这是因为,在交换之后,主机名绑定配置会不同步,而不会导致重新启动。This is because after a swap, the hostname binding configuration goes out of sync, which by itself doesn't cause restarts. 但是,某些基础存储事件(例如存储卷故障转移)可以检测到这些差异并强制重新启动所有工作进程。However, certain underlying storage events (such as storage volume failovers) may detect these discrepancies and force all worker processes to restart. 若要最大程度地减少这种重新启动类型,请在所有槽上设置 " WEBSITE_ADD_SITENAME_BINDINGS_IN_APPHOST_CONFIG=1 应用" 设置To minimize these types of restarts, set the WEBSITE_ADD_SITENAME_BINDINGS_IN_APPHOST_CONFIG=1 app setting on all slots. 但是,此应用设置不适用于WINDOWS COMMUNICATION FOUNDATION (WCF)应用。However, this app setting does not work with Windows Communication Foundation (WCF) apps.

后续步骤Next steps

阻止对非生产槽进行访问Block access to non-production slots