Azure Functions 部署槽Azure Functions deployment slots

Azure Functions 部署槽使函数应用能够运行不同的实例(称作“槽”)。Azure Functions deployment slots allow your function app to run different instances called "slots". 槽是通过公用终结点公开的不同环境。Slots are different environments exposed via a publicly available endpoint. 一个应用实例始终映射到生产槽,你可以按需交换分配给某个槽的实例。One app instance is always mapped to the production slot, and you can swap instances assigned to a slot on demand. 在应用服务计划下运行的函数应用可以有多个槽,而在消耗计划下运行的函数应用只允许有一个槽。Function apps running under the Apps Service plan may have multiple slots, while under the Consumption plan only one slot is allowed.

下面反映了交换槽对函数的影响:The following reflect how functions are affected by swapping slots:

  • 流量重定向是无缝的;不会因为交换而丢弃任何请求。Traffic redirection is seamless; no requests are dropped because of a swap.
  • 如果交换期间某个函数正在运行,则执行将会继续,后续触发器将路由到交换的应用实例。If a function is running during a swap, execution continues and the next triggers are routed to the swapped app instance.

为何要使用槽?Why use slots?

使用部署槽可获得多种优势。There are a number of advantages to using deployment slots. 以下方案描述了槽的常见用途:The following scenarios describe common uses for slots:

  • 满足不同用途的不同环境 :使用不同的槽可以在交换到生产槽或过渡槽之前区分应用实例。Different environments for different purposes : Using different slots gives you the opportunity to differentiate app instances before swapping to production or a staging slot.
  • 预热 :部署到某个槽而不是直接部署到生产环境可让应用在上线之前进行预热。Prewarming : Deploying to a slot instead of directly to production allows the app to warm up before going live. 此外,使用槽可以降低 HTTP 触发的工作负荷的延迟。Additionally, using slots reduces latency for HTTP-triggered workloads. 实例在部署之前会预热,这可以减少新部署的函数的冷启动。Instances are warmed up before deployment, which reduces the cold start for newly deployed functions.
  • 轻松回退 :与生产环境交换后,以前包含过渡应用的槽现在包含以前的生产应用。Easy fallbacks : After a swap with production, the slot with a previously staged app now has the previous production app. 如果交换到生产槽的更改不符合预期,你可以立即反向交换,以恢复“上次已知正常的实例”。If the changes swapped into the production slot aren't as you expect, you can immediately reverse the swap to get your "last known good instance" back.

交换操作Swap operations

在交换期间,一个槽被视为源,另一个槽被视为目标。During a swap, one slot is considered the source and the other the target. 源槽包含已应用到目标槽的应用程序实例。The source slot has the instance of the application that is applied to the target slot. 以下步骤可确保在交换期间目标槽不会发生停机:The following steps ensure the target slot doesn't experience downtime during a swap:

  1. 应用设置: 目标槽的设置将应用到源槽的所有实例。Apply settings: Settings from the target slot are applied to all instances of the source slot. 例如,生产设置将应用到过渡实例。For example, the production settings are applied to the staging instance. 应用的设置包括以下类别:The applied settings include the following categories:

  2. 等待重启和可用性: 交换操作会等待源槽中的每个实例完成其重启并可用于请求。Wait for restarts and availability: The swap waits for every instance in the source slot to complete its restart and to be available for requests. 如果有任何实例无法重启,交换操作将会还原对源槽所做的所有更改,并停止操作。If any instance fails to restart, the swap operation reverts all changes to the source slot and stops the operation.

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

  4. 重复操作: 源槽包含先前位于目标槽中的预交换应用后,通过应用所有设置并重启源槽的实例来完成相同的操作。Repeat operation: Now that the source slot has the pre-swap app previously in the target slot, complete the same operation by applying all settings and restarting the instances for the source slot.

请记住以下几点:Keep in mind the following points:

  • 在执行交换操作期间的任何时候,已交换应用的初始化将在源槽上发生。At any point of the swap operation, initialization of the swapped apps happens on the source slot. 准备源槽时,无论交换是成功还是失败,目标槽都会保持联机。The target slot remains online while the source slot is prepared, whether 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.

  • 在启动交换之前,必须将与事件源和绑定相关的设置配置为 。Settings related to event sources and bindings must be configured as deployment slot settings before you start a swap . 提前将这些设置标记为“粘性”可确保将事件和输出定向到正确的实例。Marking them as "sticky" ahead of time ensures events and outputs are directed to the proper instance.

管理设置Manage settings

从另一个部署槽克隆配置时,可以编辑克隆的配置。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 *

标有星号 (*) 的功能计划取消交换。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 OnAlways On
  • 诊断设置Diagnostic settings
  • 跨域资源共享 (CORS)Cross-origin resource sharing (CORS)

备注

应用于不交换的设置的某些应用设置也不交换。Certain app settings that apply to unswapped settings are also not swapped. 例如,由于诊断设置不会交换,因此相关的应用设置(如 WEBSITE_HTTPLOGGING_RETENTION_DAYSDIAGNOSTICS_AZUREBLOBRETENTIONDAYS)也不会交换,即使它们未显示为槽设置也是如此。For example, since diagnostic 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.

创建部署设置Create a deployment setting

可将设置标记为部署设置,使之具有“粘性”。You can mark settings as a deployment setting, which makes it "sticky". 粘性设置不会与应用实例一起交换。A sticky setting doesn't swap with the app instance.

如果在一个槽中创建部署设置,请确保在交换所涉及的任何其他槽中创建具有唯一值的相同设置。If you create a deployment setting in one slot, make sure to create the same setting with a unique value in any other slot that is involved in a swap. 这样,设置的值不会更改,同时设置名称可在不同的槽之间保持一致。This way, while a setting's value doesn't change, the setting names remain consistent among slots. 此名称一致性可确保代码不会尝试访问已在一个槽中定义,但未在另一个槽中定义的设置。This name consistency ensures your code doesn't try to access a setting that is defined in one slot but not another.

使用以下步骤创建部署设置:Use the following steps to create a deployment setting:

  1. 在函数应用中导航到“部署槽” ,然后选择槽名称。Navigate to Deployment slots in the function app, and then select the slot name.

    在 Azure 门户中查找槽。

  2. 选择“配置” ,然后选择要粘滞到当前槽的设置名称。Select Configuration , and then select the setting name you want to stick with the current slot.

    在 Azure 门户中配置槽的应用程序设置。

  3. 选择“部署槽设置”,然后选择“确定” 。Select Deployment slot setting , and then select OK .

    配置部署槽设置。

  4. 设置部分消失后,选择“保存” 以保留更改Once setting section disappears, select Save to keep the changes

    保存部署槽设置。

部署Deployment

创建槽时,槽是空的。Slots are empty when you create a slot. 可以使用任何支持的部署技术将应用程序部署到槽中。You can use any of the supported deployment technologies to deploy your application to a slot.

扩展Scaling

所有槽将缩放到与生产槽中相同的辅助角色数。All slots scale to the same number of workers as the production slot.

  • 对于消耗计划,槽将随着函数应用的缩放而缩放。For Consumption plans, the slot scales as the function app scales.
  • 对于应用服务计划,应用会缩放到固定数量的辅助角色。For App Service plans, the app scales to a fixed number of workers. 槽在与应用计划相同的数目的辅助角色上运行。Slots run on the same number of workers as the app plan.

添加槽Add a slot

可以通过 CLI 或门户添加槽。You can add a slot via the CLI or through the portal. 以下步骤演示如何在门户中创建新槽:The following steps demonstrate how to create a new slot in the portal:

  1. 导航到函数应用。Navigate to your function app.

  2. 选择“部署槽” ,然后选择“+ 添加槽” 。Select Deployment slots , and then select + Add Slot .

    添加 Azure Functions 部署槽。

  3. 键入槽名称,然后选择“添加” 。Type the name of the slot and select Add .

    为 Azure Functions 部署槽命名。

交换槽Swap slots

可以通过 CLI 或门户交换槽。You can swap slots via the CLI or through the portal. 以下步骤演示如何在门户中交换槽:The following steps demonstrate how to swap slots in the portal:

  1. 导航到函数应用。Navigate to the function app.

  2. 选择“部署槽” ,然后选择“交换” 。Select Deployment slots , and then select Swap .

    屏幕截图显示了“部署槽位”页面,其中选择了“添加槽”操作。

  3. 验证交换的配置设置,并选择“交换” Verify the configuration settings for your swap and select Swap

    交换部署槽。

执行交换操作可能需要花费一段时间。The operation may take a moment while the swap operation is executing.

回滚交换Roll back a swap

如果交换导致出错,或者你想要“撤消”交换,可以回滚到初始状态。If a swap results in an error or you simply want to "undo" a swap, you can roll back to the initial state. 若要恢复到交换前的状态,请再次执行交换以反向交换。To return to the pre-swapped state, do another swap to reverse the swap.

删除槽Remove a slot

可以通过 CLI 或门户删除槽。You can remove a slot via the CLI or through the portal. 以下步骤演示如何在门户中删除槽:The following steps demonstrate how to remove a slot in the portal:

  1. 在函数应用中导航到“部署槽” ,然后选择槽名称。Navigate to Deployment slots in the function app, and then select the slot name.

    在 Azure 门户中查找槽。

  2. 选择“删除” 。Select Delete .

    屏幕截图显示了“概述”页面,其中选择了“删除”操作。

  3. 键入要删除的部署槽的名称,然后选择“删除” 。Type the name of the deployment slot you want to delete, and then select Delete .

    在 Azure 门户中删除部署槽。

  4. 关闭删除确认窗格。Close the delete confirmation pane.

    部署槽删除确认。

自动槽管理Automate slot management

使用 Azure CLI 可以针对槽自动执行以下操作:Using the Azure CLI, you can automate the following actions for a slot:

更改应用服务计划Change App Service plan

对于在应用服务计划下运行的函数应用,可以更改槽的基础应用服务计划。With a function app that is running under an App Service plan, you can change the underlying App Service plan for a slot.

备注

无法在消耗计划下更改槽的应用服务计划。You can't change a slot's App Service plan under the Consumption plan.

使用以下步骤更改槽的应用服务计划:Use the following steps to change a slot's App Service plan:

  1. 在函数应用中导航到“部署槽” ,然后选择槽名称。Navigate to Deployment slots in the function app, and then select the slot name.

    在 Azure 门户中查找槽。

  2. 在“应用服务计划”下,选择“更改应用服务计划” 。Under App Service plan , select Change App Service plan .

  3. 选择要升级到的计划,或创建新的计划。Select the plan you want to upgrade to, or create a new plan.

    在 Azure 门户中更改应用服务计划。

  4. 选择“确定” 。Select OK .

限制Limitations

Azure Functions 部署槽存在以下限制:Azure Functions deployment slots have the following limitations:

  • 应用可用的槽数取决于计划。The number of slots available to an app depends on the plan. 消耗计划仅允许一个部署槽。The Consumption plan is only allowed one deployment slot. 在应用服务计划中运行的应用可以使用更多的槽。Additional slots are available for apps running under the App Service plan.
  • 交换某个槽会重置其 AzureWebJobsSecretStorageType 应用设置等于 files 的应用的密钥。Swapping a slot resets keys for apps that have an AzureWebJobsSecretStorageType app setting equal to files.

支持级别Support levels

部署槽有两个支持级别:There are two levels of support for deployment slots:

  • 正式版 (GA) :完全受支持,并已获批在生产环境中使用。General availability (GA) : Fully supported and approved for production use.
  • 预览 :尚不支持,但将来有望达到正式版的支持状态。Preview : Not yet supported, but is expected to reach GA status in the future.
OS/托管计划OS/Hosting plan 支持级别Level of support
Windows 消耗计划Windows Consumption 正式版General availability
Windows 高级计划Windows Premium 正式版General availability
Windows 专用计划Windows Dedicated 正式版General availability

后续步骤Next steps