应用程序组简介Introduction to Application Groups

Service Fabric 的群集 Resource Manager 通常通过将负载(通过指标表示)平均分散到整个群集来管理群集资源。Service Fabric's Cluster Resource Manager typically manages cluster resources by spreading the load (represented via Metrics) evenly throughout the cluster. Service Fabric 管理群集中节点的容量,并通过容量管理整个群集。Service Fabric manages the capacity of the nodes in the cluster and the cluster as a whole via capacity. 指标和容量非常适用于许多种工作负荷,但大量使用不同 Service Fabric 应用程序实例的模式有时还有其他要求。Metrics and capacity work great for many workloads, but patterns that make heavy use of different Service Fabric Application Instances sometimes bring in additional requirements. 例如,如果想要:For example you may want to:

  • 为某些命名应用程序实例内的服务保留群集节点上的某些容量Reserve some capacity on the nodes in the cluster for the services within some named application instance
  • 限制在其上运行命名应用程序实例内的服务的节点总数,而不是分散到整个群集上Limit the total number of nodes that the services within a named application instance run on (instead of spreading them out over the entire cluster)
  • 定义命名应用程序实例本身的容量,限制其中的服务数量或服务的资源总消耗量Define capacities on the named application instance itself to limit the number of services or total resource consumption of the services inside it

为了满足这些要求,Service Fabric 群集资源管理器支持名为应用程序组的功能。To meet these requirements, the Service Fabric Cluster Resource Manager supports a feature called Application Groups.

限制最大节点数Limiting the maximum number of nodes

应用程序容量的最简单用例出现在需要将应用程序实例限制为特定的最大节点数时。The simplest use case for Application capacity is when an application instance needs to be limited to a certain maximum number of nodes. 这样会将该应用程序实例中的所有服务整合到一定数量的计算机上。This consolidates all services within that application instance onto a set number of machines. 尝试预测该命名应用程序实例内的服务所使用的物理资源或设置其上限时,整合会非常有用。Consolidation is useful when you're trying to predict or cap physical resource use by the services within that named application instance.

下图显示已定义和未定义最大节点数的应用程序实例:The following image shows an application instance with and without a maximum number of nodes defined:

定义最大节点数的应用程序实例

在左侧的示例中,应用程序未定义最大节点数,并且有三个服务。In the left example, the application doesn't have a maximum number of nodes defined, and it has three services. 群集资源管理器已将所有副本分散到六个可用节点,从而在群集中实现最佳均衡(默认行为)。The Cluster Resource Manager has spread out all replicas across six available nodes to achieve the best balance in the cluster (the default behavior). 在右侧示例中,可以看到限制在 3 个节点上的同一应用程序。In the right example, we see the same application limited to three nodes.

控制此行为的参数称为 MaximumNodes。The parameter that controls this behavior is called MaximumNodes. 可在创建应用程序期间设置此参数,或针对已运行的应用程序实例更新此参数。This parameter can be set during application creation, or updated for an application instance that was already running.

PowershellPowershell

New-ServiceFabricApplication -ApplicationName fabric:/AppName -ApplicationTypeName AppType1 -ApplicationTypeVersion 1.0.0.0 -MaximumNodes 3
Update-ServiceFabricApplication -ApplicationName fabric:/AppName -MaximumNodes 5

C#C#

ApplicationDescription ad = new ApplicationDescription();
ad.ApplicationName = new Uri("fabric:/AppName");
ad.ApplicationTypeName = "AppType1";
ad.ApplicationTypeVersion = "1.0.0.0";
ad.MaximumNodes = 3;
await fc.ApplicationManager.CreateApplicationAsync(ad);

ApplicationUpdateDescription adUpdate = new ApplicationUpdateDescription(new Uri("fabric:/AppName"));
adUpdate.MaximumNodes = 5;
await fc.ApplicationManager.UpdateApplicationAsync(adUpdate);

在节点集中,群集资源管理器并不能保证会将哪些服务对象放在一起或者会使用哪些节点。Within the set of nodes, the Cluster Resource Manager doesn't guarantee which service objects get placed together or which nodes get used.

应用程序指标、负载和容量Application Metrics, Load, and Capacity

利用应用程序组,还可以定义与给定命名应用程序实例关联的指标,以及该应用程序实例在这些指标上拥有的容量。Application Groups also allow you to define metrics associated with a given named application instance, and that application instance's capacity for those metrics. 利用应用程序指标,可以跟踪、保留和限制该应用程序实例中服务的资源消耗量。Application metrics allow you to track, reserve, and limit the resource consumption of the services inside that application instance.

对于每个应用程序指标,可设置 2 个值:For each application metric, there are two values that can be set:

  • 应用程序总容量 - 此设置代表特定指标的应用程序总容量。Total Application Capacity - This setting represents the total capacity of the application for a particular metric. 群集 Resource Manager 不允许在此应用程序实例中创建任何新服务,这会造成总负载超出此值。The Cluster Resource Manager disallows the creation of any new services within this application instance that would cause total load to exceed this value. 例如,假设应用程序实例的容量为 10,已有负载为 5。For example, let's say the application instance had a capacity of 10 and already had load of five. 不允许创建总默认负载为 10 的服务。The creation of a service with a total default load of 10 would be disallowed.
  • 最大节点容量 – 此设置指定单个节点上应用程序的最大总负载。Maximum Node Capacity - This setting specifies the maximum total load for the application on a single node. 如果负载超过此容量,则群集资源管理器会将副本移到其他节点,降低负载。If load goes over this capacity, the Cluster Resource Manager moves replicas to other nodes so that the load decreases.

Powershell:Powershell:

New-ServiceFabricApplication -ApplicationName fabric:/AppName -ApplicationTypeName AppType1 -ApplicationTypeVersion 1.0.0.0 -Metrics @("MetricName:Metric1,MaximumNodeCapacity:100,MaximumApplicationCapacity:1000")

C#:C#:

ApplicationDescription ad = new ApplicationDescription();
ad.ApplicationName = new Uri("fabric:/AppName");
ad.ApplicationTypeName = "AppType1";
ad.ApplicationTypeVersion = "1.0.0.0";

var appMetric = new ApplicationMetricDescription();
appMetric.Name = "Metric1";
appMetric.TotalApplicationCapacity = 1000;
appMetric.MaximumNodeCapacity = 100;
ad.Metrics.Add(appMetric);
await fc.ApplicationManager.CreateApplicationAsync(ad);

保留容量Reserving Capacity

应用程序组的另一个常见用途是确保针对给定应用程序实例保留群集中的资源。Another common use for application groups is to ensure that resources within the cluster are reserved for a given application instance. 创建应用程序实例时,会始终保留空间。The space is always reserved when the application instance is created.

即使出现以下情况,也会在群集中立即为应用程序保留空间:Reserving space in the cluster for the application happens immediately even when:

  • 应用程序实例已创建,但其中尚不具有任何服务the application instance is created but doesn't have any services within it yet
  • 应用程序实例中的服务数每次都会发生更改the number of services within the application instance changes every time
  • 服务存在,但不会消耗资源the services exist but aren't consuming the resources

为应用程序实例保留资源需要指定两个附加参数:MinimumNodesNodeReservationCapacityReserving resources for an application instance requires specifying two additional parameters: MinimumNodes and NodeReservationCapacity

  • MinimumNodes - 定义应用程序实例应在其上运行的最小节点数。MinimumNodes - Defines the minimum number of nodes that the application instance should run on.
  • NodeReservationCapacity - 此设置是应用程序的各项指标。NodeReservationCapacity - This setting is per metric for the application. 应用程序中的服务在节点上运行,该值是为该节点上的该应用程序保留的该指标的量。The value is the amount of that metric reserved for the application on any node where that the services in that application run.

结合使用 MinimumNodes 和 NodeReservationCapacity 可以保证为群集中的应用程序保留最小负载 。Combining MinimumNodes and NodeReservationCapacity guarantees a minimum load reservation for the application within the cluster. 如果群集中存在的剩余容量比所需要的总预留容量小,则无法创建应用程序。If there's less remaining capacity in the cluster than the total reservation required, creation of the application fails.

下面是容量预留的一个示例:Let's look at an example of capacity reservation:

定义保留容量的应用程序实例

在左侧的示例中,应用程序中未定义任何应用程序容量。In the left example, applications do not have any Application Capacity defined. 群集 Resource Manager 根据一般规则平衡所有内容。The Cluster Resource Manager balances everything according to normal rules.

在右侧的示例中,假设 Application1 是使用以下设置创建的:In the example on the right, let's say that Application1 was created with the following settings:

  • MinimumNodes 设置为 2MinimumNodes set to two
  • 应用程序指标定义为An application Metric defined with
    • NodeReservationCapacity 为 20NodeReservationCapacity of 20

PowershellPowershell

New-ServiceFabricApplication -ApplicationName fabric:/AppName -ApplicationTypeName AppType1 -ApplicationTypeVersion 1.0.0.0 -MinimumNodes 2 -Metrics @("MetricName:Metric1,NodeReservationCapacity:20")

C#C#

ApplicationDescription ad = new ApplicationDescription();
ad.ApplicationName = new Uri("fabric:/AppName");
ad.ApplicationTypeName = "AppType1";
ad.ApplicationTypeVersion = "1.0.0.0";
ad.MinimumNodes = 2;

var appMetric = new ApplicationMetricDescription();
appMetric.Name = "Metric1";
appMetric.NodeReservationCapacity = 20;

ad.Metrics.Add(appMetric);

await fc.ApplicationManager.CreateApplicationAsync(ad);

Service Fabric 为 Application1 保留两个节点上的容量,并不允许 Application2 的服务使用该容量,即使此时 Application1 中的服务唯有消耗负载。Service Fabric reserves capacity on two nodes for Application1, and doesn't allow services from Application2 to consume that capacity even if there are no load is being consumed by the services inside Application1 at the time. 我们认为这些保留的应用程序容量是根据该节点上和群集中的剩余容量消耗和计算的。This reserved application capacity is considered consumed and counts against the remaining capacity on that node and within the cluster. 可以立即从剩余的群集容量中扣除预留,但是仅当特定节点上至少放置一个服务对象时,才从该节点的容量扣除预留的消耗量。The reservation is deducted from the remaining cluster capacity immediately, however the reserved consumption is deducted from the capacity of a specific node only when at least one service object is placed on it. 使用后面这种预留方式可以获得弹性并改善资源利用率,因为只会根据需要在节点上预留资源。This later reservation allows for flexibility and better resource utilization since resources are only reserved on nodes when needed.

获取应用程序负载信息Obtaining the application load information

对于每个应用程序,如果具有为一个或多个指标定义的应用程序容量,可以获取其服务的副本报告的聚合负载的相关信息。For each application that has an Application Capacity defined for one or more metrics you can obtain the information about the aggregate load reported by replicas of its services.

Powershell:Powershell:

Get-ServiceFabricApplicationLoadInformation -ApplicationName fabric:/MyApplication1

C#C#

var v = await fc.QueryManager.GetApplicationLoadInformationAsync("fabric:/MyApplication1");
var metrics = v.ApplicationLoadMetricInformation;
foreach (ApplicationLoadMetricInformation metric in metrics)
{
    Console.WriteLine(metric.ApplicationCapacity);  //total capacity for this metric in this application instance
    Console.WriteLine(metric.ReservationCapacity);  //reserved capacity for this metric in this application instance
    Console.WriteLine(metric.ApplicationLoad);  //current load for this metric in this application instance
}

ApplicationLoad 查询返回针对应用程序所指定应用程序容量的基本信息。The ApplicationLoad query returns the basic information about Application Capacity that was specified for the application. 此信息包括最小节点数和最大节点数信息,以及应用程序当前使用的节点数。This information includes the Minimum Nodes and Maximum Nodes info, and the number that the application is currently occupying. 还包括有关每个应用程序负载指标的信息,包括:It also includes information about each application load metric, including:

  • 指标名称:指标的名称。Metric Name: Name of the metric.
  • 预留容量:在群集中为此应用程序保留的群集容量。Reservation Capacity: Cluster Capacity that is reserved in the cluster for this Application.
  • 应用程序负载:此应用程序的子副本的总负载。Application Load: Total Load of this Application's child replicas.
  • 应用程序容量:允许的最大应用程序负载值。Application Capacity: Maximum permitted value of Application Load.

删除应用程序容量Removing Application Capacity

为应用程序设置应用程序容量参数后,可以使用更新应用程序 API 或 PowerShell cmdlet 来删除这些参数。Once the Application Capacity parameters are set for an application, they can be removed using Update Application APIs or PowerShell cmdlets. 例如:For example:

Update-ServiceFabricApplication -Name fabric:/MyApplication1 -RemoveApplicationCapacity

此命令从应用程序实例中删除所有应用程序容量管理参数。This command removes all Application capacity management parameters from the application instance. 其中包括 MinimumNodes、MaximumNodes 和应用程序的指标(如果有)。This includes MinimumNodes, MaximumNodes, and the Application's metrics, if any. 该命令立即生效。The effect of the command is immediate. 此命令完成后,群集资源管理器会使用默认行为管理应用程序。After this command completes, the Cluster Resource Manager uses the default behavior for managing applications. 可以通过 Update-ServiceFabricApplication/System.Fabric.FabricClient.ApplicationManagementClient.UpdateApplicationAsync() 再次指定应用程序容量参数。Application Capacity parameters can be specified again via Update-ServiceFabricApplication/System.Fabric.FabricClient.ApplicationManagementClient.UpdateApplicationAsync().

应用程序容量的限制Restrictions on Application Capacity

必须遵守应用程序容量参数的几项限制。There are several restrictions on Application Capacity parameters that must be respected. 如果存在验证错误,则不会发生任何更改。If there are validation errors no changes take place.

  • 所有整数参数必须为非负数。All integer parameters must be non-negative numbers.
  • MinimumNodes 不得大于 MaximumNodes。MinimumNodes must never be greater than MaximumNodes.
  • 如果已定义负载指标的容量,则这些容量必须遵守以下规则:If capacities for a load metric are defined, then they must follow these rules:
    • 节点预留容量不得大于最大节点容量。Node Reservation Capacity must not be greater than Maximum Node Capacity. 例如,不能将节点上指标“CPU”的容量限制为 2 个单位,也不能尝试在每个节点上保留 3 个单位。For example, you cannot limit the capacity for the metric "CPU" on the node to two units and try to reserve three units on each node.
    • 如果已指定 MaximumNodes,则 MaximumNodes 和最大节点容量的积不得大于应用程序容量总计。If MaximumNodes is specified, then the product of MaximumNodes and Maximum Node Capacity must not be greater than Total Application Capacity. 例如,如果将负载指标“CPU”的最大节点容量设置为 8,For example, let's say the Maximum Node Capacity for load metric "CPU" is set to eight. 将最大节点数设置为 10。Let's also say you set the Maximum Nodes to 10. 在这种情况下,此负载指标的应用程序容量总计必须大于 80。In this case, Total Application Capacity must be greater than 80 for this load metric.

在创建和更新应用程序的过程中,都会强制限制。The restrictions are enforced both during application creation and updates.

在哪些情况下不应使用应用程序容量How not to use Application Capacity

  • 不要尝试使用应用程序组功能将应用程序限制在特定节点子集。 Do not try to use the Application Group features to constrain the application to a specific subset of nodes. 换言之,可以指定应用程序最多在 5 个节点上运行,但不能指定群集中特定的 5 个节点。In other words, you can specify that the application runs on at most five nodes, but not which specific five nodes in the cluster. 可使用服务放置约束完成将应用程序限制在特定节点。Constraining an application to specific nodes can be achieved using placement constraints for services.
  • 不要尝试通过使用应用程序容量确保将同一应用程序中的两个服务放在同一节点上。Do not try to use the Application Capacity to ensure that two services from the same application are placed on the same nodes. 改用关联放置约束Instead use affinity or placement constraints.

后续步骤Next steps