您现在访问的是微软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. 例如,如果应用有 5 个以上的槽,则不能向下缩放到“标准”层,因为“标准”层只支持 5 个部署槽。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, open your app's resource page.

  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 应用服务 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
  • 监视和诊断设置Monitoring and diagnostic settings
  • 公用证书Public certificates
  • WebJobs 内容WebJobs content
  • 混合连接 *Hybrid connections *
  • 虚拟网络集成 *Virtual network integration *
  • 服务终结点 *Service endpoints *
  • Azure 内容分发网络 *Azure Content Delivery Network *

标有星号 (*) 的功能已计划粘滞到槽。Features marked with an asterisk (*) are planned to be made sticky to the slot.

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

  • 发布终结点Publishing endpoints
  • 自定义域名Custom domain names
  • 私有证书和 SSL 绑定Private certificates and SSL bindings
  • 缩放设置Scale settings
  • Web 作业计划程序WebJobs schedulers
  • IP 限制IP restrictions
  • 始终可用Always On
  • 协议设置(HTTPS、TLS 版本、客户端证书)Protocol settings (HTTPS, TLS version, client certificates)
  • 诊断日志设置Diagnostic log settings
  • 跨域资源共享 (CORS)Cross-origin resource sharing (CORS)

若要将应用设置或连接字符串配置为粘滞到特定的槽(未交换),请转到该槽的“配置”页。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)

备注

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

在交换到用作目标槽的生产槽之前,请使用交换的设置验证应用的运行。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

使用自动交换时,某些应用可能需要在交换前自定义的预热操作。When you're using auto swap, 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,202An 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.

如果遇到任何问题,请参阅排查交换问题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.

手动路由生产流量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

转到应用的资源页。Go to your app's resource page. 选择“部署槽” > “<删除槽>” > “概述”。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

使用 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.

后续步骤Next steps

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