Azure Functions 的缩放和托管Azure Functions scale and hosting

在 Azure 中创建函数应用时,必须为应用选择托管计划。When you create a function app in Azure, you must choose a hosting plan for your app. 有三个托管计划可用于 Azure Functions:消耗计划应用服务计划There are three hosting plans available for Azure Functions: Consumption plan and App Service plan.

选择的托管计划决定了以下行为:The hosting plan you choose dictates the following behaviors:

  • 函数应用的缩放方式。How your function app is scaled.
  • 每个函数应用实例可用的资源。The resources available to each function app instance.
  • 对 VNET 连接等高级功能的支持。Support for advanced features, such as VNET connectivity.

消耗计划在代码运行时自动添加计算能力。Consumption plans automatically add compute power when your code is running. 应用在需要处理负载时会扩展,在代码停止运行时会缩减。Your app is scaled out when needed to handle load, and scaled down when code stops running. 此外,对于消耗计划,无需提前支付空闲 VM 或预留容量的费用。For the Consumption plan, you also don't have to pay for idle VMs or reserve capacity in advance.

选择应用服务计划可以利用你管理的专用基础结构。App Service plan allows you to take advantage of dedicated infrastructure, which you manage. 函数应用不会基于事件进行缩放,这意味着,它永远不会缩减为零。Your function app doesn't scale based on events, which means is never scales down to zero. (要求启用 Always On。)(Requires that Always on is enabled.)

托管计划支持Hosting plan support

功能支持划分为以下两个类别:Feature support falls into the following two categories:

  • 正式版 (GA):完全受支持并获批在生产中使用。 Generally available (GA): fully supported and approved for production use.
  • 预览版:尚不完全受支持,但已获批在生产环境中使用。 Preview: not yet fully supported and approved for production use.

下表指出了在 Windows 或 Linux 上运行应用时,目前对三个托管计划的支持级别:The following table indicates the current level of support for the three hosting plans, when running on either Windows or Linux:

消耗计划Consumption plan 专用计划Dedicated plan
WindowsWindows GAGA GAGA
LinuxLinux GAGA GAGA

消耗计划Consumption plan

使用消耗计划时,会根据传入事件数自动添加和删除 Azure Functions 主机实例。When you're using the Consumption plan, instances of the Azure Functions host are dynamically added and removed based on the number of incoming events. 这个无服务器计划会自动缩放,仅在函数运行时,才会产生计算资源费用。This serverless plan scales automatically, and you're charged for compute resources only when your functions are running. 在消耗计划中,函数执行在可配置的时间段后超时。On a Consumption plan, a function execution times out after a configurable period of time.

账单将基于执行数量、执行时间和所用内存。Billing is based on number of executions, execution time, and memory used. 账单是基于函数应用内的所有函数聚合而生成的。Billing is aggregated across all functions within a function app. 有关详细信息,请参阅 Azure Functions 定价页For more information, see the Azure Functions pricing page.

消耗计划是默认的托管计划,它提供了以下优势:The Consumption plan is the default hosting plan and offers the following benefits:

  • 仅当函数正在运行时才产生费用Pay only when your functions are running
  • 可自动扩展,即使是在负载较高期间Scale out automatically, even during periods of high load

可将同一区域中的函数应用分配到同一个消耗计划。Function apps in the same region can be assigned to the same Consumption plan. 在同一个消耗计划中运行多个应用不会产生负面影响。There's no downside or impact to having multiple apps running in the same Consumption plan. 将多个应用分配到同一个消耗计划不会影响每个应用的复原能力、可伸缩性或可靠性。Assigning multiple apps to the same consumption plan has no impact on resilience, scalability, or reliability of each app.

若要详细了解在消耗计划中运行时如何估算成本,请参阅了解消耗计划成本To learn more about how to estimate costs when running in a Consumption plan, see Understanding Consumption plan costs.

专用(应用服务)计划Dedicated (App Service) plan

函数应用也可以像其他应用服务应用(基本、标准和隔离 SKU)一样在相同的专用 VM 上运行。Your function apps can also run on the same dedicated VMs as other App Service apps (Basic, Standard, and Isolated SKUs).

对于以下情况,可以考虑使用应用服务计划:Consider an App Service plan in the following situations:

  • 具有已运行其他应用服务实例的、未充分利用的现成 VM。You have existing, underutilized VMs that are already running other App Service instances.
  • 需要提供用于运行函数的自定义映像。You want to provide a custom image on which to run your functions.

应用服务计划中函数应用的费用与其他应用服务资源(例如 Web 应用)的费用相同。You pay the same for function apps in an App Service Plan as you would for other App Service resources, like web apps. 如需详细了解如何使用应用服务计划,请参阅 Azure 应用服务计划深入概述For details about how the App Service plan works, see the Azure App Service plans in-depth overview.

借助应用服务计划,可通过添加更多 VM 实例手动进行横向扩展。With an App Service plan, you can manually scale out by adding more VM instances. 也可以启用自动缩放。You can also enable autoscale. 有关详细信息,请参阅手动或自动缩放实例计数For more information, see Scale instance count manually or automatically. 还可以通过选择不同的应用服务计划来进行增加。You can also scale up by choosing a different App Service plan. 有关详细信息,请参阅增加 Azure 中的应用For more information, see Scale up an app in Azure.

在应用服务计划上运行 JavaScript 函数时,应选择具有较少 vCPU 的计划。When running JavaScript functions on an App Service plan, you should choose a plan that has fewer vCPUs. 有关详细信息,请参阅选择单核应用服务计划For more information, see Choose single-core App Service plans.

Always OnAlways On

如果在应用服务计划上运行,应启用 AlwaysOn 设置,使函数应用能正常运行 。If you run on an App Service plan, you should enable the Always on setting so that your function app runs correctly. 在应用服务计划中,如果函数运行时处于不活动状态,几分钟后就会进入空闲状态,因此只有 HTTP 触发器才能“唤醒”函数。On an App Service plan, the functions runtime goes idle after a few minutes of inactivity, so only HTTP triggers will "wake up" your functions. 只能对应用服务计划使用始终可用。Always on is available only on an App Service plan. 在消耗计划中,平台会自动激活函数应用。On a Consumption plan, the platform activates function apps automatically.

函数应用超时持续时间Function app timeout duration

函数应用的超时持续时间可通过 host.json 项目文件中的 functionTimeout 属性定义。The timeout duration of a function app is defined by the functionTimeout property in the host.json project file. 下表显示两种计划和两种运行时版本的默认值和最大值(以分钟为单位):The following table shows the default and maximum values in minutes for both plans and in both runtime versions:

计划Plan 运行时版本Runtime Version 默认Default 最大值Maximum
消耗Consumption 1.x1.x 55 10 个10
消耗Consumption 2.x2.x 55 10 个10
消耗Consumption 3.x(预览版)3.x (preview) 55 10 个10
应用服务App Service 1.x1.x 无限制Unlimited 无限制Unlimited
应用服务App Service 2.x2.x 3030 无限制Unlimited
应用服务App Service 3.x(预览版)3.x (preview) 3030 无限制Unlimited

Note

不管函数应用超时设置如何,230 秒是 HTTP 触发的函数在响应请求时需要的最长时间。Regardless of the function app timeout setting, 230 seconds is the maximum amount of time that an HTTP triggered function can take to respond to a request. 这起因于 Azure 负载均衡器的默认空闲超时This is because of the default idle timeout of Azure Load Balancer. 对于处理时间较长的情况,考虑使用 Durable Functions 异步模式延迟实际工作并返回即时响应For longer processing times, consider using the Durable Functions async pattern or defer the actual work and return an immediate response.

即使启用了 AlwaysOn,各函数的执行超时也由 host.json 项目文件中的 functionTimeout 设置控制。Even with Always On enabled, the execution timeout for individual functions is controlled by the functionTimeout setting in the host.json project file.

确定现有应用程序的托管计划Determine the hosting plan of an existing application

要确定你的函数应用所使用的托管计划,请在 Azure 门户中参阅函数应用的“概览”选项卡中的“应用服务计划/定价层” 。To determine the hosting plan used by your function app, see App Service plan / pricing tier in the Overview tab for the function app in the Azure portal. 对于应用服务计划,还指明了定价层。For App Service plans, the pricing tier is also indicated.

在门户中查看缩放计划

还可以使用 Azure CLI 来确定计划,如下所示:You can also use the Azure CLI to determine the plan, as follows:

appServicePlanId=$(az functionapp show --name <my_function_app_name> --resource-group <my_resource_group> --query appServicePlanId --output tsv)
az appservice plan list --query "[?id=='$appServicePlanId'].sku.tier" --output tsv

此命令的输出为 dynamic 时,函数应用采用消耗计划。When the output from this command is dynamic, your function app is in the Consumption plan. 所有其他值均表示应用服务计划的不同层。All other values indicate different tiers of an App Service plan.

存储帐户要求Storage account requirements

在任何计划中,函数应用需要一个支持 Azure Blob、队列、文件和表存储的常规 Azure 存储帐户。On any plan, a function app requires a general Azure Storage account, which supports Azure Blob, Queue, Files, and Table storage. 这是因为 Functions 依赖 Azure 存储来执行管理触发器和记录函数执行等操作,但某些存储帐户不支持队列和表。This is because Functions relies on Azure Storage for operations such as managing triggers and logging function executions, but some storage accounts do not support queues and tables. 这些帐户包括仅限 blob 的存储帐户(包括高级存储)和使用区域冗余存储空间复制的常规用途存储帐户,已在创建函数应用时将从现有的“存储帐户”选项中过滤掉 。These accounts, which include blob-only storage accounts (including premium storage) and general-purpose storage accounts with zone-redundant storage replication, are filtered-out from your existing Storage Account selections when you create a function app.

触发器和绑定也可以使用函数应用使用的相同存储帐户来存储应用程序数据。The same storage account used by your function app can also be used by your triggers and bindings to store your application data. 但是,对于存储密集型操作,应使用单独的存储帐户。However, for storage-intensive operations, you should use a separate storage account.

若要了解有关存储帐户类型的详细信息,请参阅 Azure 存储服务简介To learn more about storage account types, see Introducing the Azure Storage services.

消耗计划的工作原理How the consumption plans work

在消耗计划中,Azure Functions 基础结构通过根据触发函数的事件数添加额外的 Functions 主机实例来自动缩放 CPU 和内存资源。In the consumption plans, the Azure Functions infrastructure scales CPU and memory resources by adding additional instances of the Functions host, based on the number of events that its functions are triggered on. 消耗计划中 Functions 主机的每个实例限制为 1.5 GB 内存和 1 个 CPU。Each instance of the Functions host in the consumption plan is limited to 1.5 GB of memory and one CPU. 主机实例是整个函数应用,这意味着函数应用中的所有函数共享某个实例中的资源并同时缩放。An instance of the host is the entire function app, meaning all functions within a function app share resource within an instance and scale at the same time. 共享同一消耗计划的函数应用单独缩放。Function apps that share the same consumption plan are scaled independently.

函数代码文件存储在函数主要存储帐户中的 Azure 文件共享上。Function code files are stored on Azure Files shares on the function's main storage account. 删除函数应用的主存储帐户时,函数代码文件将被删除并且无法恢复。When you delete the main storage account of the function app, the function code files are deleted and cannot be recovered.

运行时缩放Runtime scaling

Azure Functions 使用名为“缩放控制器”的组件来监视事件率以及确定是要扩大或缩小。 Azure Functions uses a component called the scale controller to monitor the rate of events and determine whether to scale out or scale in. 缩放控制器针对每种触发器类型使用试探法。The scale controller uses heuristics for each trigger type. 例如,使用 Azure 队列存储触发器时,它会根据队列长度和最旧队列消息的期限进行缩放。For example, when you're using an Azure Queue storage trigger, it scales based on the queue length and the age of the oldest queue message.

Azure Functions 的缩放单位为函数应用。The unit of scale for Azure Functions is the function app. 横向扩展函数应用时,将分配额外的资源来运行 Azure Functions 主机的多个实例。When the function app is scaled out, additional resources are allocated to run multiple instances of the Azure Functions host. 相反,计算需求下降时,扩展控制器将删除函数主机实例。Conversely, as compute demand is reduced, the scale controller removes function host instances. 实例数最终会缩减为零,此时 Function App 中没有任何函数运行。The number of instances is eventually scaled down to zero when no functions are running within a function app.

用于监视事件和创建实例的扩展控制器

了解缩放行为Understanding scaling behaviors

缩放可根据多种因素而异,可根据选定的触发器和语言以不同的方式缩放。Scaling can vary on a number of factors, and scale differently based on the trigger and language selected. 需要注意缩放行为的以下几个细节:There are a few intricacies of scaling behaviors to be aware of:

  • 单个函数应用最多只能纵向扩展到 200 个实例。A single function app only scales up to a maximum of 200 instances. 不过,单个实例每次可以处理多个消息或请求,因此,对并发执行数没有规定的限制。A single instance may process more than one message or request at a time though, so there isn't a set limit on number of concurrent executions.
  • 对于 HTTP 触发器,将最多每隔 1 秒分配一次新实例。For HTTP triggers, new instances will only be allocated at most once every 1 second.
  • 对于非 HTTP 触发器,将最多每隔 30 秒分配一次新实例。For non-HTTP triggers, new instances will only be allocated at most once every 30 seconds.

不同触发器还可能有不同的缩放限制,如下所述:Different triggers may also have different scaling limits as well as documented below:

可缩放应用的最佳做法和模式Best practices and patterns for scalable apps

函数应用的许多方面会影响其缩放,包括主机配置、运行时占用空间和资源效率。There are many aspects of a function app that will impact how well it will scale, including host configuration, runtime footprint, and resource efficiency. 有关详细信息,请查看性能注意事项一文的“可扩展”部分For more information, see the scalability section of the performance considerations article. 还要注意随着函数应用的扩展,连接是如何实施的。You should also be aware of how connections behave as your function app scales. 有关详细信息,请参阅如何在 Azure Functions 中管理连接For more information, see How to manage connections in Azure Functions.

计费模式Billing model

不同计划的计费在 Azure Functions 定价页中有详细介绍。Billing for the different plans is described in detail on the Azure Functions pricing page. 使用量在 Function App 级别聚合,只会统计函数代码的执行时间。Usage is aggregated at the function app level and counts only the time that function code is executed. 以下是计费单位:The following are units for billing:

  • 以千兆字节/秒 (GB-s) 计量的资源消耗量 。Resource consumption in gigabyte-seconds (GB-s). 根据内存大小和函数应用中所有函数的执行时间组合计算得出。Computed as a combination of memory size and execution time for all functions within a function app.
  • 执行Executions. 每次为响应事件触发而执行函数时记为一次。Counted each time a function is executed in response to an event trigger.

帐单常见问题解答中可以找到有关如何了解消费帐单的有用查询和信息。Useful queries and information on how to understand your consumption bill can be found on the billing FAQ.

服务限制Service limits

下表指示了在各种托管计划中运行时适用于函数应用的限制:The following table indicates the limits that apply to function apps when running in the various hosting plans:

ResourceResource 消耗计划Consumption plan 高级计划Premium plan 应用服务计划1App Service plan1
向外扩展Scale out 事件驱动Event driven 事件驱动Event driven 手动/自动缩放Manual/autoscale
最大实例数Max instances 200200 100100 10-2010-20
默认超时持续时间(分钟)Default time out duration (min) 55 3030 302302
最大超时持续时间(分钟)Max time out duration (min) 10 个10 6060 不受限制3unbounded3
最大出站连接数(每个实例)Max outbound connections (per instance) 600 个处于活动状态(总共 1200 个)600 active (1200 total) unboundedunbounded unboundedunbounded
最大请求大小 (MB)4Max request size (MB)4 100100 100100 100100
最大查询字符串长度4Max query string length4 40964096 40964096 40964096
最大请求 URL 长度4Max request URL length4 81928192 81928192 81928192
每个实例的 ACUACU per instance 100100 210-840210-840 100-840100-840
最大内存(每个实例的 GB 数)Max memory (GB per instance) 1.51.5 3.5-143.5-14 1.75-141.75-14
每个计划的函数应用数Function apps per plan 100100 100100 不受限制5unbounded5
应用服务计划App Service plans 每个区域 100 个100 per region 每个资源组 100 个100 per resource group 每个资源组 100 个100 per resource group
存储6Storage6 1 GB1 GB 250 GB250 GB 50-1000 GB50-1000 GB
每个应用的自定义域数Custom domains per app 50075007 500500 500500
自定义域 SSL 支持Custom domain SSL support 包含无限制的 SNI SSL 连接unbounded SNI SSL connection included 包含无限制的 SNI SSL 连接和 1 个 IP SSL 连接unbounded SNI SSL and 1 IP SSL connections included 包含无限制的 SNI SSL 连接和 1 个 IP SSL 连接unbounded SNI SSL and 1 IP SSL connections included

1 有关各种应用服务计划选项的特定限制,请参阅应用服务计划限制1 For specific limits for the various App Service plan options, see the App Service plan limits.
2 默认情况下,应用服务计划中的 Functions 1.x 运行时的超时是无限制的。2 By default, the timeout for the Functions 1.x runtime in an App Service plan is unbounded.
3 需要将应用服务计划设置为 Always On3 Requires the App Service plan be set to Always On. 按标准费率付费。Pay at standard rates.
4 这些限制在主机中设置4 These limits are set in the host.
5 可以托管的函数应用的实际数目取决于应用的活动、计算机实例的大小和相应的资源利用率。5 The actual number of function apps that you can host depends on the activity of the apps, the size of the machine instances, and the corresponding resource utilization.
6 存储限制是同一应用服务计划中所有应用的临时存储中的总内容大小。6 The storage limit is the total content size in temporary storage across all apps in the same App Service plan. 消耗计划使用 Azure 文件存储作为临时存储。Consumption plan uses Azure Files for temporary storage.
7 当函数应用托管在消耗计划中时,仅支持 CNAME 选项。7 When your function app is hosted in a Consumption plan, only the CNAME option is supported. 对于应用服务计划中的函数应用,可以使用 CNAME 或 A 记录映射自定义域。For function apps in an App Service plan, you can map a custom domain using either a CNAME or an A record.