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:
|
filter | string | No | The filter options for workloads. The following types of filter options are supported:
You can specify multiple filter options of the same type. Separate filter options with commas (,). Example: You can also specify multiple types of filter options. Separate different types of filter options with plus signs ( |
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 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:
|
format | string | No | The format of the queried costs. The following types of format options are supported:
|
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:
Log on to the ACK console. In the left-side navigation pane, click Clusters.
On the Clusters page, find the cluster that you want to manage and click its name. In the left-side navigation pane, choose .
On the Helm page, find ack-alibaba-cloud-metrics-adapter and click Update in the Actions column.
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.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
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 .