All Products
Search
Document Center

Container Service for Kubernetes:Call an API to query cost data

Last Updated:Jul 11, 2024

If you want to obtain information about resource usage and cost distribution in your clusters from multiple dimensions, you can enable the cost insights feature of Container Service for Kubernetes (ACK). After you enable this feature, cost data is reported to and stored in Managed Service for Prometheus. You can call HTTP APIs to query the cost data. ACK can provide suggestions on cost optimization based on the data. The data can also be used in further development.

Prerequisites

Usage notes

If you are not the owner of the cluster but you are authorized to manage the cluster by the owner, ack-metrics-adapter-api-service may not have the required permissions. When you call the Kubernetes API or HTTP API exposed by your Service to query cost insights data of ACK, you need to grant role-based access control (RBAC) permissions to the user or user group in the credentials of the request that is sent to the API server.

Show authorization 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 pane, choose Security > Role.

  3. On the Role tab, click Edit YAML in the Actions column of arms-pilot-prom-spec-ns-k8s.

  4. In the View in YAML panel, modify the rules parameter based on the following content and click OK.

      - apiGroups:
          - ''
        resources:
          - services
        verbs:
          - get

Methods to query cost data

Method

API

Description

Call the Cost API

Cost API (old version): queries the estimated cost in real time.

You can call the Cost API to query the real-time estimated cost and resource usage of a pod.

Call the Cost V2 API

Cost V2 API: queries the estimated cost in real time.

The Cost V2 API is compatible with the Cost API and allows you to specify a time range and filter workloads by pod name, namespace, label, controller, or controller type. You can analyze the real-time cost trend based on the returned cost data.

Call the Allocation API

Allocation API: queries cost allocation data.

The Allocation API allocates the payment amounts in the bills of the cluster to workloads in the cluster. The data structure in the response of the Allocation API is the same as the data structure in the response of the Cost V2 API. You can use the cost allocation data for accounting.

What to do next

  • ACK provides the resource profiling and idle resource optimization features. For more information, see Resource profiling and Idle resource optimization.

  • After you extract insights from cost data and implement cost optimization strategies, you can use the cost control feature to control the cost governance lifecycle. For more information, see Cost control.