文档

通过Cost API获取成本数据

更新时间:

您可以通过Cost API查询集群指定命名空间或Pod在指定时间周期内的实时估算成本和资源监控数据。

前提条件

已参见通过API获取成本数据概述完成准备工作。

成本数据导出命令

连接集群后,您可以执行命令获取成本数据。获取成本数据的命令参数如下。

参数

描述

DimensionType

成本统计维度,默认为集群下所有Pod。取值:

  • Namespace:按照命名空间维度获取应用的成本。

  • Pod:按照Pod维度获取应用的成本。

Dimension

成本筛选值,若填写即指定请求某个维度值。

  • 当设置DimensionType为Namespace时,存在以下情况。

    • 若不填写Dimension,将返回所有命名空间的成本数据。

    • 若设置为Dimension=A Namespace时,则只返回A命名空间的成本数据。

    • 若设置LabelSelector,则会返回LabelSelector筛选后的数据。

  • 当设置DimensionType为Pod时,存在以下情况。

    • 若不填写Dimension,将返回所有Pod的成本数据。

    • 若设置为Dimension=A Pod时,则只返回A Pod的成本数据。

      说明

      您也可以选择多个Pod,使用英文半角逗号(,)隔开,例如Dimension=pod=node-exporter-7vtmj,pod=node-exporter-cn54q

    • 若设置LabelSelector,则会返回LabelSelector筛选后的数据。

LabelSelector

通过应用(app)标签筛选Pod,获取筛选后的成本数据。

TimeUnit

成本数据统计时间。可选值包括hourdayweekmonth,默认为hour

Summary

设置为true时,将返回筛选后的Pod数据总和。默认为false

成本数据指标

  • cpu:单位为核。

  • gpu:单位为卡。

  • memory:单位为KB。

  • perCorePricing:当前Pod或所查询应用所在的节点的单位CPU核的价格,中国站单位为¥,国际站单位为$。

  • costRatio:当前Pod或所查询应用占整个集群成本比例,例如值为0.2时,表明所选应用的成本占总集群成本的20%。

  • cost:当前Pod或所查询应用在查询时间范围内的成本,中国站单位为¥,国际站单位为$。

  • customCost:当前Pod或所查询应用在查询时间范围内的定价成本。

获取成本数据命令

示例一:获取集群所有Pod数据最近一小时的成本数据

获取命令

kubectl get --raw "/api/v1/namespaces/kube-system/services/ack-metrics-adapter-api-service:8080/proxy/cost"

预期输出

展开查看预期输出

[
  {
    "metadata": {
      "timestamp": "2023-02-14T03:27:18Z",
      "timeUnit": "hour",
      "DimensionType": "",
      "Dimension": "",
      "PodName": "arms-springboot-demo-subcomponent-7f94c7f597-5l8tc"
    },
    "request": {
      "cpu": 0.5,
      "memory": 0,
      "gpu": 0,
      "gpuMem": 0
    },
    "usage": {
      "cpu": 0.022,
      "memory": 1538784,
      "gpu": 0,
      "gpuMem": 0
    },
    "limit": {
      "cpu": 0,
      "memory": 0.5,
      "gpu": 0,
      "gpuMem": 0
    },
    "perCorePricing": 0.2,
    "costRatio": 0.010671219720414044,
    "cost": 0.1
  },
  {
    "metadata": {
      "timestamp": "2023-02-14T03:27:18Z",
      "timeUnit": "hour",
      "DimensionType": "",
      "Dimension": "",
      "PodName": "thanos-query-74cfcd459f-pf54n"
    },
    "request": {
      "cpu": 0.1,
      "memory": 131072,
      "gpu": 0,
      "gpuMem": 0
    },
    "usage": {
      "cpu": 0.001,
      "memory": 90780,
      "gpu": 0,
      "gpuMem": 0
    },
........

示例二:获取某一个命名空间所有Pod的数据最近一小时的成本数据

示例说明

获取default命名空间所有Pod最近一小时的成本数据。请替换命名空间取值为实际命名空间名称。

获取命令

kubectl get --raw "/api/v1/namespaces/kube-system/services/ack-metrics-adapter-api-service:8080/proxy/cost?DimensionType=Namespace&Dimension=default"| jq .

预期输出

展开查看预期输出

[
  {
    "metadata": {
      "timestamp": "2023-02-14T04:08:15Z",
      "timeUnit": "hour",
      "DimensionType": "Namespace",
      "Dimension": "default",
      "PodName": "test60-test-chart-v2-5cd85b946c-ntcct"
    },
    "request": {
      "cpu": 0,
      "memory": 0,
      "gpu": 0,
      "gpuMem": 0
    },
    "usage": {
      "cpu": 0,
      "memory": 18336,
      "gpu": 0,
      "gpuMem": 0
    },
    "limit": {
      "cpu": 0,
      "memory": 0,
      "gpu": 0,
      "gpuMem": 0
    },
    "perCorePricing": 0,
    "costRatio": 0,
    "cost": 0
  },
  {
    "metadata": {
      "timestamp": "2023-02-14T04:08:15Z",
      "timeUnit": "hour",
      "DimensionType": "Namespace",
      "Dimension": "default",
      "PodName": "nginx"
    },
    "request": {
      "cpu": 0,
      "memory": 0,
      "gpu": 0,
      "gpuMem": 0
    },
.......

示例三:获取某一个Pod最近一小时的成本数据

示例说明

获取名为nginx-deployment-basic-75d6678cbb-lg8v5的Pod的最近一小时的成本数据。请替换Pod名称为实际的Pod名称。

获取命令

kubectl get --raw "/api/v1/namespaces/kube-system/services/ack-metrics-adapter-api-service:8080/proxy/cost?DimensionType=Pod&Dimension=pod=nginx-deployment-basic-75d6678cbb-lg8v5"| jq .

预期输出

展开查看预期输出

[
  {
    "metadata": {
      "timestamp": "2023-02-14T04:09:50Z",
      "timeUnit": "hour",
      "DimensionType": "Pod",
      "Dimension": "pod=nginx-deployment-basic-75d6678cbb-lg8v5",
      "PodName": "nginx-deployment-basic-75d6678cbb-lg8v5"
    },
    "request": {
      "cpu": 0.5,
      "memory": 0,
      "gpu": 0,
      "gpuMem": 0
    },
    "usage": {
      "cpu": 0.002,
      "memory": 50148,
      "gpu": 0,
      "gpuMem": 0
    },
    "limit": {
      "cpu": 0,
      "memory": 0.5,
      "gpu": 0,
      "gpuMem": 0
    },
    "perCorePricing": 0.2,
    "costRatio": 0.010671219720414044,
    "cost": 0.1
  }
]

示例四:通过LabelSelector筛选的方式获取成本数据

示例说明

LabelSelector与DimensionType可以同时作为筛选条件使用,两个条件为“且”的关系。以下示例为获取带有Labelapp=sample-app的所有Pod的最近一小时的成本数据。请将应用标签替换为实际标签。

获取命令

kubectl get --raw "/api/v1/namespaces/kube-system/services/ack-metrics-adapter-api-service:8080/proxy/cost?LabelSelector=app=nginx"| jq .

预期输出

展开查看预期输出

[
  {
    "metadata": {
      "timestamp": "2023-02-14T04:10:24Z",
      "timeUnit": "hour",
      "DimensionType": "",
      "Dimension": "",
      "PodName": "nginx-deployment-basic-75d6678cbb-lg8v5"
    },
    "request": {
      "cpu": 0.5,
      "memory": 0,
      "gpu": 0,
      "gpuMem": 0
    },
    "usage": {
      "cpu": 0.002,
      "memory": 50148,
      "gpu": 0,
      "gpuMem": 0
    },
    "limit": {
      "cpu": 0,
      "memory": 0.5,
      "gpu": 0,
      "gpuMem": 0
    },
    "perCorePricing": 0.2,
    "costRatio": 0.010671219720414044,
    "cost": 0.1
  },
  {
    "metadata": {
      "timestamp": "2023-02-14T04:10:24Z",
      "timeUnit": "hour",
      "DimensionType": "",
      "Dimension": "",
      "PodName": "nginx-675b8d6f54-vqxvd"
    },
    "request": {
      "cpu": 0.25,
      "memory": 524288,
      "gpu": 0,
      "gpuMem": 0
    },
    "usage": {
      "cpu": 0,
      "memory": 13548,
      "gpu": 0,
      "gpuMem": 0
    },
    "limit": {
      "cpu": 0,
      "memory": 0,
      "gpu": 0,
      "gpuMem": 0
    },
    "perCorePricing": 0.2,
    "costRatio": 0.005335609860207022,
    "cost": 0.05
  },
  {
    "metadata": {
      "timestamp": "2023-02-14T04:10:25Z",
      "timeUnit": "hour",
      "DimensionType": "",
      "Dimension": "",
      "PodName": "nginx-deployment-basic-7456bcd48d-r87rz"
    },
    "request": {
      "cpu": 0,
      "memory": 0,
      "gpu": 0,
      "gpuMem": 0
    },
    "usage": {
      "cpu": 0,
      "memory": 223492,
      "gpu": 0,
      "gpuMem": 0
    },
    "limit": {
      "cpu": 0,
      "memory": 0,
      "gpu": 0,
      "gpuMem": 0
    },
    "perCorePricing": 0,
    "costRatio": 0,
    "cost": 0
  }
]

示例五:获取最近1天的Pod数据

示例说明

获取名为nginx-deployment-basic-75d6678cbb-lg8v5的Pod的最近一天的成本数据。请替换Pod名称为实际Pod名称。

获取命令

kubectl get --raw "/api/v1/namespaces/kube-system/services/ack-metrics-adapter-api-service:8080/proxy/cost?DimensionType=Pod&Dimension=pod=nginx-deployment-basic-75d6678cbb-lg8v5&TimeUnit=day"| jq .

预期输出

展开查看预期输出

[
  {
    "metadata": {
      "timestamp": "2023-02-14T04:11:04Z",
      "timeUnit": "day",
      "DimensionType": "Pod",
      "Dimension": "pod=nginx-deployment-basic-75d6678cbb-lg8v5",
      "PodName": "nginx-deployment-basic-75d6678cbb-lg8v5"
    },
    "request": {
      "cpu": 0.5,
      "memory": 0,
      "gpu": 0,
      "gpuMem": 0
    },
    "usage": {
      "cpu": 0.001,
      "memory": 50196,
      "gpu": 0,
      "gpuMem": 0
    },
    "limit": {
      "cpu": 0,
      "memory": 0.5,
      "gpu": 0,
      "gpuMem": 0
    },
    "perCorePricing": 0.2,
    "costRatio": 0.010571112959515542,
    "cost": 2.402
  }
]

示例六:获取某一命名空间的成本数据总量

示例说明

获取default命名空间的成本数据总量。请替换命名空间名称为实际命名空间。

获取命令

kubectl get --raw "/api/v1/namespaces/kube-system/services/ack-metrics-adapter-api-service:8080/proxy/cost?DimensionType=Namespace&Dimension=default&Summary=true"| jq .

预期输出

展开查看预期输出

[
  {
    "metadata": {
      "timestamp": "2023-02-14T04:12:48Z",
      "timeUnit": "hour",
      "DimensionType": "Namespace",
      "Dimension": "default",
      "PodName": ""
    },
    "request": {
      "cpu": 12,
      "memory": 7045120,
      "gpu": 0,
      "gpuMem": 0
    },
    "usage": {
      "cpu": 0.053,
      "memory": 4901208,
      "gpu": 0,
      "gpuMem": 0
    },
    "limit": {
      "cpu": 0,
      "memory": 6553600,
      "gpu": 0,
      "gpuMem": 0
    },
    "perCorePricing": 4.061,
    "costRatio": 0.01609432143753053,
    "cost": 3.657
  }
]

相关文档

  • Cost V2 API支持设定更灵活的时间范围,提供Pod、Namespace、Label、Controller、Controller Kind等维度的实时估算成本查询,请参见通过Cost V2 API获取

  • Allocation API结合了账单数据,提供业务分摊账单的费用,可用于对账,请参见通过Allocation API获取

  • 本页导读 (1)