All Products
Search
Document Center

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

Last Updated:Jul 23, 2024

You can call the Cost V2 API to query the estimated workload costs of a cluster in real time. When you call the Cost V2 API, you can specify a time range and filter workloads by pod name, namespace, label, controller, or controller type (such as ReplicaSet, DaemonSet, and Job). You can analyze the real-time cost trend based on the returned cost data.

Prerequisites

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

Request parameters and response parameters

Request syntax

GET /v2/cost

Request parameters

Parameter

Type

Required

Description

window

string

Yes

The time range to be queried. The following formats are supported:

  • Keywords. Examples: today, week, month, yesterday, lastweek, and lastmonth.

  • Durations. Examples: 30m, 12h, and 7d.

  • RFC3339 dates. Specify a pair of comma-separated dates in RFC3339 format. Example: 2024-03-26T00:00:00Z,2024-03-27T00:00:00Z.

  • UNIX timestamps. Specify a pair of comma-separated UNIX timestamps (in seconds). Example: 1711382400,1711468800.

filter

string

No

The filter options for workloads. The following types of filter options are supported:

  • namespace

  • controllerName

  • controllerKind (such as ReplicaSet, DaemonSet, and Job)

  • pod

  • label

You can specify multiple filter options of the same type. Separate filter options with commas (,). Example: namespace:"default","kube-system".

You can also specify multiple types of filter options. Separate different types of filter options with plus signs (+). Example: namespace:"default"+label[app]:"nginx".

step

string

No

The interval that is used to split the time range. The step parameter specifies the interval of a cost set. Example: 1d or 1w. The default value is the value of the window parameter.

Note

A cost set includes the cost data of a Kubernetes object, such as a pod, within the interval.

aggregate

string

No

The aggregate options for the returned results from different dimensions. The following types of aggregate options are supported:

  • namespace

  • controller

  • controllerKind

format

string

No

The format of the queried costs. The following types of format options are supported:

  • json (default value)

  • csv

Response parameters

Parameter

Type

Description

properties

object

Information about the Kubernetes object.

pod

string

The name of the pod.

namespace

string

The namespace to which the pod belongs.

controllerKind

string

The type of the controller.

controller

string

The name of the controller.

start

string

The start time of the cost set.

end

string

The end time of the cost set.

cpuCoreRequestAverage

float

The average number of CPU cores requested by the pod.

cpuCoreUsageAverage

float

The average number of CPU cores used by the pod.

ramByteRequestAverage

float

The average amount of memory requested by the pod.

ramByteUsageAverage

float

The average amount of memory used by the pod.

cost

float

The estimated cost of the pod. The default CPU weight used by cost estimation policies is 100%. To specify custom resource weights, refer to Cost estimation policies.

costRatio

float

The cost ratio of the pod. This parameter indicates the ratio of the pod cost to the total cluster cost.

customCost

float

The custom cost.

(Optional) Step 1: Specify custom resource weights

The default CPU weight of the cost estimation policy applied to the Cost V2 API is 100%. To specify custom resource weights, perform the following steps:

  1. Log on to the ACK console. In the left-side navigation pane, click Clusters.

  2. On the Clusters page, find the cluster that you want to manage and click its name. In the left-side navigation pane, choose Applications > Helm.

  3. On the Helm page, find ack-alibaba-cloud-metrics-adapter and click Update in the Actions column.

  4. Modify the AlibabaCloudMetricsAdapter.costWeights parameter based on your business requirements. The default value of this field is {"cpu": "1.0", "memory": "0.0"}. Then, click OK.

    image

    To specify a CPU weight and a memory weight for the cost estimation policy, refer to Cost estimation policies.

Step 2: Query cost data

In this section, curl is used to access the Kubernetes API. For more information, see Use the Kubernetes API.

Example 1: Query the estimated cost of a DaemonSet within the previous day

Command

curl -G -k --cert ./client-cert.pem --key ./client-key.pem \
-d 'window=yesterday' \
-d 'filter=namespace:"kube-system"+controllerKind:"DaemonSet"+label[app]:"terway-eniip"' \
"$APISERVER/api/v1/namespaces/kube-system/services/ack-metrics-adapter-api-service:8080/proxy/v2/cost" | jq .

Sample success responses in JSON format

View the sample response

{
  "data": [
    {
      "kube-system/terway-eniip-88286": {
        "name": "kube-system/terway-eniip-88286",
        "properties": {
          "controller": "terway-eniip",
          "controllerKind": "DaemonSet",
          "namespace": "kube-system",
          "pod": "terway-eniip-88286"
        },
        "start": "2024-03-25T00:00:00+08:00",
        "end": "2024-03-26T00:00:00+08:00",
        "cpuCoreRequestAverage": 0.35,
        "cpuCoreUsageAverage": 0.001,
        "ramByteRequestAverage": 209715200,
        "ramByteUsageAverage": 65815511.04,
        "cost": 1.622,
        "costRatio": 0.016,
        "customCost": 0.972
      },
      "kube-system/terway-eniip-fqxk8": {
        "name": "kube-system/terway-eniip-fqxk8",
        "properties": {
          "controller": "terway-eniip",
          "controllerKind": "DaemonSet",
          "namespace": "kube-system",
          "pod": "terway-eniip-fqxk8"
        },
        "start": "2024-03-25T00:00:00+08:00",
        "end": "2024-03-26T00:00:00+08:00",
        "cpuCoreRequestAverage": 0.35,
        "cpuCoreUsageAverage": 0.001,
        "ramByteRequestAverage": 209715200,
        "ramByteUsageAverage": 72787722.24,
        "cost": 1.622,
        "costRatio": 0.016,
        "customCost": 0.972
      },
      "kube-system/terway-eniip-xjsr8": {
        "name": "kube-system/terway-eniip-xjsr8",
        "properties": {
          "controller": "terway-eniip",
          "controllerKind": "DaemonSet",
          "namespace": "kube-system",
          "pod": "terway-eniip-xjsr8"
        },
        "start": "2024-03-25T00:00:00+08:00",
        "end": "2024-03-26T00:00:00+08:00",
        "cpuCoreRequestAverage": 0.35,
        "cpuCoreUsageAverage": 0.001,
        "ramByteRequestAverage": 209715200,
        "ramByteUsageAverage": 53661380.923,
        "cost": 0.843,
        "costRatio": 0.008,
        "customCost": 0.505
      }
    }
  ]
}

Example 2: Query the estimated cost of a pod within 3 hours (split by hour)

Command

curl -G -k --cert ./client-cert.pem --key ./client-key.pem \
-d 'window=2024-03-24T00:00:00Z,2024-03-24T03:00:00Z' \
-d 'step=1h' \
-d 'filter=namespace:"kube-system"+pod:"terway-eniip-kz68n"' \
"$APISERVER/api/v1/namespaces/kube-system/services/ack-metrics-adapter-api-service:8080/proxy/v2/cost" | jq .

Sample success responses in JSON format

View the sample response

{
  "data": [
    {
      "kube-system/terway-eniip-kz68n": {
        "name": "kube-system/terway-eniip-kz68n",
        "properties": {
          "controller": "terway-eniip",
          "controllerKind": "DaemonSet",
          "namespace": "kube-system",
          "pod": "terway-eniip-kz68n"
        },
        "start": "2024-03-24T00:00:00Z",
        "end": "2024-03-24T01:00:00Z",
        "cpuCoreRequestAverage": 0.35,
        "cpuCoreUsageAverage": 0.001,
        "ramByteRequestAverage": 209715200,
        "ramByteUsageAverage": 63583378.285,
        "cost": 0.075,
        "costRatio": 0.025,
        "customCost": 0.045
      }
    },
    {
      "kube-system/terway-eniip-kz68n": {
        "name": "kube-system/terway-eniip-kz68n",
        "properties": {
          "controller": "terway-eniip",
          "controllerKind": "DaemonSet",
          "namespace": "kube-system",
          "pod": "terway-eniip-kz68n"
        },
        "start": "2024-03-24T01:00:00Z",
        "end": "2024-03-24T02:00:00Z",
        "cpuCoreRequestAverage": 0.35,
        "cpuCoreUsageAverage": 0.001,
        "ramByteRequestAverage": 209715200,
        "ramByteUsageAverage": 63111168,
        "cost": 0.075,
        "costRatio": 0.025,
        "customCost": 0.045
      }
    },
    {
      "kube-system/terway-eniip-kz68n": {
        "name": "kube-system/terway-eniip-kz68n",
        "properties": {
          "controller": "terway-eniip",
          "controllerKind": "DaemonSet",
          "namespace": "kube-system",
          "pod": "terway-eniip-kz68n"
        },
        "start": "2024-03-24T02:00:00Z",
        "end": "2024-03-24T03:00:00Z",
        "cpuCoreRequestAverage": 0.35,
        "cpuCoreUsageAverage": 0.001,
        "ramByteRequestAverage": 209715200,
        "ramByteUsageAverage": 63430656,
        "cost": 0.075,
        "costRatio": 0.025,
        "customCost": 0.045
      }
    }
  ]
}