Analyze Pod costs with custom tags

Updated at:

Tags are metadata that you can add to resources as key-value pairs to categorize and describe them. Container Compute Service (ACS) allows you to use annotations to bind custom Alibaba Cloud tags to Pods. This allows you to filter resources by tag for billing and cost analysis.

Background

Alibaba Cloud tags provide a flexible and customizable way to identify and categorize cloud resources. By tagging your cloud resources, you can manage and organize them more effectively. For more information, see What are tags?.

By default, Pods created by Container Compute Service (ACS) have the following tags:

System tag key

Description

acs:acc:cluster_id

The cluster ID.

acs:acc:cluster_name

The cluster name.

acs:acc:compute_class

The instance type.

User tag key

Description

ClusterId

The cluster ID.

NameSpace

The namespace in the cluster.

OwnerReferenceKind

The workload type, such as Deployment or Job.

OwnerReferenceName

The name of the workload, such as the Deployment name or Job name.

Note

Currently, ACS supports configuring tags only for features such as cost analysis.

Configuration

You can bind up to five custom tags to an ACS Pod by using the alibabacloud.com/custom-tags annotation. Separate a tag key from its value with a colon (:), and separate multiple tags with a comma (,).

Important
  • Add the annotation under the Pod's metadata. For example, when you create a Deployment, add the annotation under spec.template.metadata.

  • Custom tags take effect only when you add the annotation during the creation of an ACS Pod. Adding or modifying the annotation when updating an ACS Pod has no effect.

  • The key and value must comply with the tagging conventions.

  • Because the colon (:) is used as a delimiter, it cannot be part of the key or value.

  • Any content that does not conform to the key-value pair format is ignored.

Configuration example

The following example shows how to configure custom tags for Pods in a Deployment by using a YAML manifest.

  1. Create a file named nginx.yaml with the following content.

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: nginx
      labels:
        app: nginx
    spec:
      replicas: 2
      selector:
        matchLabels:
          app: nginx
      template:
        metadata:
          name: nginx-test
          labels:
            app: nginx
            alibabacloud.com/compute-class: general-purpose
            alibabacloud.com/compute-qos: default
          annotations:
            alibabacloud.com/custom-tags: "<tag_key1>:<tag_value1>,<tag_key2>:<tag_value2>"   #Bind custom tags
        spec:
          containers:
          - name: nginx
            image: registry.cn-hangzhou.aliyuncs.com/acs-sample/nginx:latest
            ports:
            - containerPort: 80

    The YAML above adds two tags, <tag_key1>:<tag_value1> and <tag_key2>:<tag_value2>, to the annotations section. Refer to Configuration instructions to add custom tag keys and values.

  2. Run the following command to view the Pod details. Replace <Pod name> with the actual Pod name, such as nginx-789f4xxxxx-vnjxz.

    kubectl describe pod <Pod name> | grep -A 10 Annotations

    Expected output:

    Annotations:      ProviderCreate: done
                      alibabacloud.com/client-token: edf29202-54ac-438e-9626-a1ca007xxxxx
                      alibabacloud.com/custom-tags: <tag_key1>:<tag_value1>,<tag_key2>:<tag_value2>
                      alibabacloud.com/instance-id: acs-2ze008giupcyaqbxxxxx
                      alibabacloud.com/pod-ephemeral-storage: 30Gi
                      alibabacloud.com/pod-use-spec: 0.25-0.5Gi
                      alibabacloud.com/request-id: A0EF3BF3-37E7-5A07-AC2D-68A0CFCxxxxx
                      alibabacloud.com/schedule-result: finished
                      alibabacloud.com/user-id: 14889995898xxxxx
                      kubernetes.io/pod-stream-port: 10250
                      kubernetes.io/preferred-scheduling-node: virtual-kubelet-cn-beijing-g/1

    As you can see, in Annotations, the newly added alibabacloud.com/custom-tags has taken effect.

  3. Go to the Billing Management page to use the tags for cost analysis.

    1. (Optional) Enable cost tags

      Designate your custom tags as cost tags to use them for cost analysis.

      1. Go to the Cost Tags page in Billing Management.

      2. On the Cost Tags page, read the instructions and click Next.

      3. Select the tags you want to use for cost allocation, and then click Next.

      4. Click Confirm Enablement.

    2. View cloud resources associated with cost tags

      1. Go to the Cost Analysis page in Billing Management.

      2. In the Filters pane on the right, click Tags.

      3. Select the cost tags that you enabled, such as <tag_key1>:<tag_value1>,<tag_key2>:<tag_value2>, to view their associated cost details.

      4. Click Apply Filters.

      Note
      • Cost Analysis supports selecting multiple tags. The relationship between multiple tags is a logical OR.

      • Tags added in the product console are synchronized to the cost tags list the next day (T+1).

      • If you determine a tag is no longer in use, you can choose not to enable it.

References

You can use tags to allocate the costs of your cloud resources. For more information, see Use tags for cost allocation.