In addition to default cost allocation strategies, the cost management suite allows you to create custom cost accounting policies. To define your own chargeback logic for cloud resource costs, create a configuration file that sets custom resource prices. This helps align cost data more closely with your business logic.
Use cases
Custom pricing provides fine-grained control over your IT costs. Common use cases include:
-
An internal operations team purchases underlying computing resources in bulk and resells them to different business teams.
-
Normalize costs at the business level by eliminating price variations that result from scheduling identical pods on nodes with different specifications.
-
To implement differentiated pricing, you must include the resource limit when calculating the pod's unit price.
To address these scenarios, the cost management suite provides a custom pricing template feature. You can edit a template to configure the unit price for each of a pod's resources, which are then aggregated to determine the pod's custom cost.
Currently, a pod's custom cost is available only through the API. After you enable and configure custom pricing, you can retrieve the cost from the customCost field in the API response. For details on using the API, see Retrieve cost data by API.
Prerequisites
-
The cost insights feature is enabled.
-
You have obtained the cluster's KubeConfig file and used kubectl to connect to the cluster.
Step 1: Enable custom pricing for cost-exporter
Log on to the ACK console. In the left navigation pane, click Clusters.
On the Clusters page, click the name of your cluster. In the left navigation pane, click .
-
At the top of the page, switch the namespace to kube-system. In the Actions column of the Deployment list, click More > View in YAML for ack-cost-exporter. In the
commandsection, add'--enable-custom-price=true'to enable the custom price feature. Then, click Update.spec: containers: - command: - /root/alibaba-cloud-price-exporter - '--listen-address=:9091' - '--ali-user-id=1251182063904492' - '--cluster-id=c66b9e33984194a219813f6a7d402cdba' - '--region-id=cn-hangzhou' - '--cluster-type=ManagedKubernetes' - '--cluster-profile=Default' - '--enable-custom-price=true' env: - name: DefaultIDCPricePerCPUCoreHour value: '0.2'
Step 2: Configure the pricing template
Procedure
-
Create a file named
price-template.yamlwith the following example content.Important-
The fields under the
specfield in a template are optional. If all configured fields underspecmatch the Pod's metadata, the Pod is considered a match for the template. A template with an emptyspeccan match all Pods. -
Templates are matched in order. If a pod matches multiple templates in the YAML file, the system uses the first matching template to calculate the pod's cost.
apiVersion: v1 kind: ConfigMap metadata: name: price-template namespace: kube-system data: template.yaml: | - name: template-nginx spec: labelSelector: app: nginx namespaceSelector: - default - kube-system resource: cpu: 500m memory: 512Mi cpu-overcommit-ratio: 2 memory-overcommit-ratio: 2 price: cpu: 0.2 memory: 0.03 - name: template-default price: cpu: 0.1 memory: 0.02This pricing template specifies the following rules:
-
The
template-nginxtemplate applies to pods that meet all of the following conditions:-
The label contains
app:nginx -
The pod is in the
defaultorkube-systemnamespace. -
The CPU request is 500m, and the limit is 1 Core.
-
The memory request is 512 MiB, and the limit is 1 GiB.
-
-
All other pods in the cluster use the
template-defaulttemplate.
The following table describes the parameters.
Parameter
Description
name
The name of the pricing template.
spec.labelSelector
A map of key-value pairs. A pod is considered a match if it has all the labels specified in this selector.
spec.namespaceSelector
A list of namespaces. The pod's namespace must be in this list to match.
spec.resource.cpu
The pod's CPU request.
spec.resource.memory
The pod's memory request.
spec.resource.cpu-overcommit-ratio
The pod's CPU overcommitment ratio, calculated as
CPU limit / CPU request.spec.resource.memory-overcommit-ratio
The pod's memory overcommitment ratio, calculated as
memory limit / memory request.price.cpu
The unit price for CPU, measured in cost per core-hour.
price.memory
The unit price for memory, measured in cost per GiB-hour.
-
-
Run the following command to create the
price-templateConfigMap in thekube-systemnamespace and apply your custom pricing configuration.kubectl apply -f price-template.yaml -n kube-system
Related documents
-
You can estimate pod costs based on cluster resource utilization by using single-resource (CPU or memory) or weighted (CPU and memory) models. For more information, see Cost estimation policies.
-
Send HTTP API requests to retrieve cost insights data for custom development. For more information, see Overview of calling an API to query cost data.