All Products
Search
Document Center

Container Service for Kubernetes:Call the Cost API to query cost data

Last Updated:Jul 11, 2024

You can call the Cost API to query the estimated real-time cost and resource monitoring data of a namespace or pod within a time period.

Prerequisites

The prerequisites described in the Overview of calling an API to query cost data topic are met.

Parameters for querying cost data

After you connect to your cluster, run the corresponding command to query cost data. The following table describes the parameters.

Parameter

Description

DimensionType

The dimension based on which cost insights data is queried. By default, cost insights data is queried by pod. Valid values:

  • Namespace: Cost insights data is queried by namespace.

  • Pod: Cost insights data is queried by pod.

Dimension

Specify whether to filter cost insights data.

  • When DimensionType is set to Namespace:

    • If you do not specify Dimension, the cost insights data of all namespaces is returned.

    • If you specify Dimension=A Namespace, only the cost insights data of Namespace A is returned.

    • If you specify LabelSelector, the cost insights data filtered by LabelSelector is returned.

  • When DimensionType is set to Pod:

    • If you do not specify Dimension, the cost insights data of all pods is returned.

    • If you specify Dimension=A Pod, only the cost insights data of Pod A is returned.

      Note

      You can also specify multiple pods and separate them with commas (,), such as Dimension=pod=node-exporter-7vtmj,pod=node-exporter-cn54q.

    • If you specify LabelSelector, the cost insights data filtered by LabelSelector is returned.

LabelSelector

Filter pods by application (app) label.

TimeUnit

The time range of the cost insights data that is queried. Valid values: hour, day, week, and month. Default value: hour.

Summary

If you set this parameter to true, the system returns the summarized cost insights data of all pods that match the specified conditions. Default value: false.

Cost data metrics

  • cpu: Unit: vCores.

  • gpu: Unit: count.

  • memory: Unit: KB.

  • perCorePricing: the unit price of vCores on the node that hosts the current pod or queried application. The currency unit of resources on the China site is ¥ and the currency unit of resources on the International site is $.

  • costRatio: the ratio of the cost of the current pod or queried application to the total cluster cost. For example, if the ratio is 0.2, 20% of the cluster cost is spent on the current pod or queried application.

  • cost: the cost of the current pod or queried application within the specified time range. The currency unit of resources on the China site is ¥ and the currency unit of resources on the International site is $.

  • customCost: the custom cost of the current pod or queried application within the specified time range.

Command for querying cost data

Example 1: Query the cost data of all pods in the cluster within the previous hour

Command

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

Expected output

View expected output

[
  {
    "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
    },
........

Example 2: Query the cost data of all pods in a namespace within the previous hour

Description

Query the cost data of all pods in the default namespace within the previous hour. Replace the namespace name with the actual value.

Command

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

Expected output

View expected output

[
  {
    "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
    },
.......

Example 3: Query the cost data of a pod within the previous hour

Description

Query the cost data of the pod named nginx-deployment-basic-75d6678cbb-lg8v5 within the previous hour. Replace the pod name with the actual value.

Command

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 .

Expected output

View expected output

[
  {
    "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
  }
]

Example 4: Query cost data by LabelSelector

Description

You can specify both LabelSelector and DimensionType as filter conditions. The logical relationship between LabelSelector and DimensionType is AND. The following example queries the cost data of all pods that have the app=sample-app label within the previous hour. Replace the application label with the actual value.

Command

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

Expected output

View expected output

[
  {
    "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
  }
]

Example 5: Query the cost data of pods within the previous day

Description

Query the cost data of the pod named nginx-deployment-basic-75d6678cbb-lg8v5 within the previous day. Replace the pod name with the actual value.

Command

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 .

Expected output

View expected output

[
  {
    "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
  }
]

Example 6: Query the cost data of a namespace

Description

Query the cost data of the default namespace. Replace the namespace name with the actual value.

Command

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

Expected output

View expected output

[
  {
    "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
  }
]

References