All Products
Search
Document Center

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

Last Updated:Jan 24, 2025

You can call the Allocation API to query the cost allocation data of a cluster, which you can use for accounting during the financial governance cycle.

Prerequisites

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

Request and response parameters

Request syntax

GET /v2/allocation

Request parameters

Parameter

Type

Required

Description

window

string

Yes

The time range to be queried. The output of cost insights data must be used with the billing data of cloud services. The minimum time range to be queried is 24 hours. The following formats are accepted:

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

  • Examples: 24h and 7d.

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

  • UNIX timestamp: 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 (such as csi-plugin and node-exporter)

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

  • pod

  • label (such as [app]:"nginx")

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 filter options are supported:

  • namespace

  • controller

  • controllerKind

  • label:<name>

  • node

idle

boolean

No

Specifies whether to display or process idle costs. Default value: true.

shareIdle

boolean

No

Specifies whether to allocate idle costs to each resource. Default value: false.

shareSplit

string

No

The idle cost allocation policy. The following types of filter options are supported:

  • weighted (default)

  • even

idleByNode

boolean

No

Specifies whether to aggregate idle costs of each node. This parameter takes effect only if you set the aggregate parameter to node and the shareIdle parameter to false. Default value: false.

targetType

string

No

The target type of cost allocation. By default, costs for all resources associated with the cluster are allocated.

  • cluster (default value)

  • node

format

string

No

The format that is used to export costs The following types of filter options are supported:

  • json (default)

  • csv

Response parameters

Parameter

Type

Description

properties

object

The properties of the associated Kubernetes object.

pod

string

The name of the pod.

node

string

The name of the node.

namespace

string

The namespace.

controllerKind

string

The type of the controller.

controller

string

The name of the controller.

providerID

string

The Elastic Compute Service (ECS) instance ID of the node.

labels

map[string]string

The label of the pod.

Note

Prometheus allows you to use only underscores (_) in labels. If your label contains symbols, such as a hyphen (-) and aperiod (.), Prometheus converts the symbols into underscores (_).

start

string

The start time of the cost set.

end

string

The end time of the cost set.

cpuCoreRequestAverage

float

The average number of vCPUs requested.

cpuCoreUsageAverage

float

The average number of vCPUs in use.

ramByteRequestAverage

float

The average amount of memory requested.

ramByteUsageAverage

float

The average amount of memory in use.

cost

float

The cost allocated to the service from the total cluster cost.

costRatio

float

The ratio of the allocated cost to the total cluster cost.

customCost

float

The custom cost.

Query cost insights data

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

Example: Query the cost allocated to a DaemonSet from the total cluster cost of 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/allocation" | jq .

Sample success responses (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.039,
        "cost": 4.497,
        "costRatio": 0.016,
        "customCost": 0.972
      },
      "kube-system/terway-eniip-j8c87": {
        "name": "kube-system/terway-eniip-j8c87",
        "properties": {
          "controller": "terway-eniip",
          "controllerKind": "DaemonSet",
          "namespace": "kube-system",
          "pod": "terway-eniip-j8c87"
        },
        "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": 186293944.32,
        "cost": 6.504,
        "costRatio": 0.023,
        "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": 2.337,
        "costRatio": 0.008,
        "customCost": 0.505
      }
    }
  ]
}