适用于计算的 FinOps 最佳做法

本文概述了一系列经过验证的计算服务的 FinOps 实践。 它提供有关优化成本、提高效率和深入了解 Azure 中的计算资源的指南。 这些做法根据计算服务类型(例如虚拟机(VM)、Azure Kubernetes 服务(AKS)和 Azure Functions 进行分类。


Azure Kubernetes 服务

以下部分提供 AKS 群集的 Azure Resource Graph (ARG) 查询。 该查询可帮助你深入了解 VM。

查询 - AKS 集群

此 ARG 查询检索有关 Azure 环境中的 AKS 群集的详细信息。

类别

资源管理

查询

resources
| where type == "microsoft.containerservice/managedclusters"
| extend AgentPoolProfiles = properties.agentPoolProfiles
| mvexpand AgentPoolProfiles
| project
    id,
    ProfileName = tostring(AgentPoolProfiles.name),
    Sku = tostring(sku.name),
    Tier = tostring(sku.tier),
    mode = AgentPoolProfiles.mode,
    AutoScaleEnabled = AgentPoolProfiles.enableAutoScaling,
    SpotVM = AgentPoolProfiles.scaleSetPriority,
    VMSize = tostring(AgentPoolProfiles.vmSize),
    nodeCount = tostring(AgentPoolProfiles.['count']),
    minCount = tostring(AgentPoolProfiles.minCount),
    maxCount = tostring(AgentPoolProfiles.maxCount),
    ___location,
    resourceGroup,
    subscriptionId,
    AKSname = name

虚拟机

Azure 虚拟机(VM)是 Azure 提供 的多种按需可缩放计算资源之一。 通常,当您需要比其他选项更好地控制计算环境时,可以选择 VM。

Azure VM 提供虚拟化的灵活性,而无需购买和维护运行它的物理硬件。 但是,仍需要通过执行任务(例如配置、修补和安装运行的软件)来维护 VM。

相关资源:

解除分配虚拟机

建议:解除分配 VM,避免因未使用的计算资源而产生费用。 避免停止 VM 而不解除其分配。

关于非活动 VM

VM 具有两种非活动状态:已停止和已解除分配。

已停止的 VM 是从操作系统内部关闭的(例如,使用“关闭”命令)。 停止的 VM 已关闭,但 Azure 仍保留计算资源,例如 CPU 和内存。 由于计算资源是保留的,不能与其他 VM 一起使用,因此这些 VM 将继续产生计算费用。

已解除分配的 VM 是在 Azure 门户、CLI、PowerShell 或其他客户端工具中通过云管理 API 停止的。 解除分配 VM 时,Azure 会释放相应的计算资源。 由于计算资源已发布,因此这些 VM 不会产生计算费用;但是,请务必注意,停止和解除分配的 VM 都会继续产生与计算无关的费用,例如磁盘的存储费用。

标识已停止的 VM

使用以下 Azure Resource Graph (ARG) 查询来标识未解除分配的已停止 VM。 它检索有关其电源状态、位置、资源组和订阅 ID 的详细信息。

resources
| where type =~ 'microsoft.compute/virtualmachines'
| extend PowerState = tostring(properties.extended.instanceView.powerState.displayStatus)
| where PowerState !in =('VM deallocated', 'VM running')
| project
    ResourceId = id,
    PowerState,
    Region = ___location,
    ResourceGroupName = resourceGroup,
    SubscriptionId = subscriptionId

使用承诺折扣

建议:与标准成本相比,使用承诺折扣最高可节省 72% 的成本。

关于承诺折扣

承诺折扣是向承诺在指定期限或期限内使用 Azure 服务的组织提供的财务奖励,通常为一年或三年。 企业同意在特定期限内保持固定的使用量或支出(成本)额度,即可享受相较于标准定价的大幅折扣优惠(最高 72% 的折扣)。 折扣适用于符合条件的资源,帮助组织节省云成本,同时在预算中提供灵活性和可预测性。

若要详细了解承诺折扣,请参阅 费率优化功能

测量虚拟机使用承诺折扣的覆盖范围

使用以下 FinOps 中心查询来度量总体 VM 承诺折扣覆盖率。

Costs
| where ResourceType =~ 'Virtual machine'
| where x_SkuMeterCategory startswith 'Virtual Machines'
//
// Join with prices to filter out ineligible SKUs
| extend tmp_MeterKey = strcat(substring(ChargePeriodStart, 0, 7), x_SkuMeterId)
| project tmp_MeterKey, EffectiveCost, PricingCategory, CommitmentDiscountCategory, ResourceName, x_ResourceGroupName, SubAccountName, BillingCurrency
| join kind=leftouter (
    Prices
    | where x_SkuMeterCategory startswith 'Virtual Machines'
    | summarize sp = countif(x_SkuPriceType == 'SavingsPlan'), ri = countif(x_SkuPriceType == 'ReservedInstance')
        by tmp_MeterKey = strcat(substring(x_EffectivePeriodStart, 0, 7), x_SkuMeterId)
    | project tmp_MeterKey, x_CommitmentDiscountSpendEligibility = iff(sp == 0, 'Not Eligible', 'Eligible'), x_CommitmentDiscountUsageEligibility = iff(ri == 0, 'Not Eligible', 'Eligible')
) on tmp_MeterKey
| extend x_CommitmentDiscountUsageEligibility = iff(isempty(x_CommitmentDiscountUsageEligibility), '(missing prices)', x_CommitmentDiscountUsageEligibility)
| extend x_CommitmentDiscountSpendEligibility = iff(isempty(x_CommitmentDiscountSpendEligibility), '(missing prices)', x_CommitmentDiscountSpendEligibility)
//
// Sum costs
| summarize
    TotalCost = sum(EffectiveCost),
    OnDemandCost = sumif(EffectiveCost, PricingCategory == 'Standard'),
    SpotCost = sumif(EffectiveCost, PricingCategory == 'Dynamic'),
    CommittedCost = sumif(EffectiveCost, PricingCategory == 'Committed'),
    CommittedSpendCost = sumif(EffectiveCost, CommitmentDiscountCategory == 'Spend'),
    CommittedUsageCost = sumif(EffectiveCost, CommitmentDiscountCategory == 'Usage')
    by x_CommitmentDiscountUsageEligibility, x_CommitmentDiscountSpendEligibility, BillingCurrency
| extend OnDemandPercent = round(OnDemandCost / TotalCost * 100, 2)
| extend CoveragePercent = round(CommittedCost / TotalCost * 100, 2)
| extend CoverageUsagePercent = round(CommittedUsageCost / TotalCost * 100, 2)
| extend CoverageSpendPercent = round(CommittedSpendCost / TotalCost * 100, 2)
| order by CoveragePercent desc

使用以下查询来度量每个 VM 的成本明细,包括承诺折扣的覆盖范围。

Costs
| where ResourceType =~ 'Virtual machine'
| where x_SkuMeterCategory startswith 'Virtual Machines'
//
// Join with prices to filter out ineligible SKUs
| extend tmp_MeterKey = strcat(substring(ChargePeriodStart, 0, 7), x_SkuMeterId)
| project tmp_MeterKey, EffectiveCost, PricingCategory, CommitmentDiscountCategory, ResourceName, x_ResourceGroupName, SubAccountName, BillingCurrency
| join kind=leftouter (
    Prices
    | where x_SkuMeterCategory startswith 'Virtual Machines'
    | summarize sp = countif(x_SkuPriceType == 'SavingsPlan'), ri = countif(x_SkuPriceType == 'ReservedInstance')
        by tmp_MeterKey = strcat(substring(x_EffectivePeriodStart, 0, 7), x_SkuMeterId)
    | project tmp_MeterKey, x_CommitmentDiscountSpendEligibility = iff(sp == 0, 'Not Eligible', 'Eligible'), x_CommitmentDiscountUsageEligibility = iff(ri == 0, 'Not Eligible', 'Eligible')
) on tmp_MeterKey
| extend x_CommitmentDiscountUsageEligibility = iff(isempty(x_CommitmentDiscountUsageEligibility), '(missing prices)', x_CommitmentDiscountUsageEligibility)
| extend x_CommitmentDiscountSpendEligibility = iff(isempty(x_CommitmentDiscountSpendEligibility), '(missing prices)', x_CommitmentDiscountSpendEligibility)
//
// Sum costs by resource
| summarize
    TotalCost = sum(EffectiveCost),
    OnDemandCost = sumif(EffectiveCost, PricingCategory == 'Standard'),
    SpotCost = sumif(EffectiveCost, PricingCategory == 'Dynamic'),
    CommittedCost = sumif(EffectiveCost, PricingCategory == 'Committed'),
    CommittedSpendCost = sumif(EffectiveCost, CommitmentDiscountCategory == 'Spend'),
    CommittedUsageCost = sumif(EffectiveCost, CommitmentDiscountCategory == 'Usage')
    by ResourceName, x_ResourceGroupName, SubAccountName, x_CommitmentDiscountUsageEligibility, x_CommitmentDiscountSpendEligibility, BillingCurrency
| extend OnDemandPercent = round(OnDemandCost / TotalCost * 100, 2)
| extend CoveragePercent = round(CommittedCost / TotalCost * 100, 2)
| extend CoverageUsagePercent = round(CommittedUsageCost / TotalCost * 100, 2)
| extend CoverageSpendPercent = round(CommittedSpendCost / TotalCost * 100, 2)
| order by CoveragePercent desc

若要了解有关 FinOps 中心的详细信息,请参阅 FinOps 中心

查询 - 虚拟机规模集详细信息

此查询根据虚拟机规模集的 SKU、现成 VM 优先级和优先级混合策略分析 Azure 环境中的虚拟机规模集。 它提供了成本优化和资源管理策略的见解。

类别

资源管理

查询

resources
| where type =~ 'microsoft.compute/virtualmachinescalesets'
| extend SpotVMs = tostring(properties.virtualMachineProfile.priority)
| extend SpotPriorityMix = tostring(properties.priorityMixPolicy)
| extend SKU = tostring(sku.name)
| extend resourceGroup = strcat('/subscriptions/', subscriptionId, '/resourceGroups/', resourceGroup)
| project id, SKU, SpotVMs, SpotPriorityMix, subscriptionId, resourceGroup, ___location

查询 - 虚拟机处理器类型分析

此查询标识 Azure 环境中的 VM 使用的处理器类型(ARM、AMD 或 Intel)。 它有助于了解跨不同处理器体系结构分布 VM,这有助于优化工作负荷性能和成本效益。

类别

资源管理

查询

resources
| where type == 'microsoft.compute/virtualmachines'
| extend vmSize = properties.hardwareProfile.vmSize
| extend processorType = case(
    // ARM Processors
    vmSize has "Epsv5"
        or vmSize has "Epdsv5"
        or vmSize has "Dpsv5"
        or vmSize has "Dpdsv", "ARM",
    // AMD Processors
    vmSize has "Standard_D2a"
        or vmSize has "Standard_D4a"
        or vmSize has "Standard_D8a"
        or vmSize has "Standard_D16a"
        or vmSize has "Standard_D32a"
        or vmSize has "Standard_D48a"
        or vmSize has "Standard_D64a"
        or vmSize has "Standard_D96a"
        or vmSize has "Standard_D2as"
        or vmSize has "Standard_D4as"
        or vmSize has "Standard_D8as"
        or vmSize has "Standard_D16as"
        or vmSize has "Standard_D32as"
        or vmSize has "Standard_D48as"
        or vmSize has "Standard_D64as"
        or vmSize has "Standard_D96as", "AMD",
    "Intel"
)
| project vmName = name, processorType, vmSize, resourceGroup

提供反馈

请您给我们一个简短的评价,让我们知道我们做得怎么样。 我们将使用这些评审来改进和扩展 FinOps 工具和资源。

如果您在寻找特定的东西,可以为现有想法投票或者创建新想法。 与他人分享想法,以获得更多的选票。 我们专注于拥有最多选票的想法。


相关资源:

相关产品:

相关解决方案: