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:
|
Dimension | Specify whether to filter cost insights data.
|
LabelSelector | Filter pods by application ( |
TimeUnit | The time range of the cost insights data that is queried. Valid values: |
Summary | If you set this parameter to |
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
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
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
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
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
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
References
The Cost V2 API allows you to specify a time range more flexibly and query real-time cost data by pod, namespace, label, controller, or controller kind. For more information, see Call the Cost V2 API to query cost data.
The Allocation API allows you to allocate business costs based on bills for accounting purposes. For more information, see Call the Allocation API to query cost insights data.