搭配 Amazon Managed Service for Prometheus 使用分割成本分配資料 - AWS 資料匯出

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

搭配 Amazon Managed Service for Prometheus 使用分割成本分配資料

分割 Amazon EKS 的成本資料需要您從叢集收集和存放指標,包括記憶體和 CPU 用量。Amazon Managed Service for Prometheus 可用於此目的。

當您選擇加入分割成本分配資料,且您的 Amazon Managed Service for Prometheus 工作區開始接收兩個必要的指標 (container_cpu_usage_seconds_totalcontainer_memory_working_set_bytes) 時,分割成本分配資料會識別指標並自動使用它們。

注意

兩個必要的指標 (container_cpu_usage_seconds_totalcontainer_memory_working_set_bytes) 存在於預設 Prometheus 抓取組態中,以及 AWS 受管收集器隨附的預設組態中。不過,如果您自訂這些組態,請勿重新標記、修改或移除 container_cpu_usage_seconds_totalcontainer_memory_working_set_bytes指標中的下列標籤:namenamespacepod。如果您重新標記、修改或移除這些標籤,可能會影響指標的擷取。

您可以使用 Amazon Managed Service for Prometheus 從單一區域中的單一使用帳戶收集 EKS 指標。Amazon Managed Service for Prometheus 工作區必須位於該帳戶和區域中。您需要針對要監控成本的每個用量帳戶和區域一個 Amazon Managed Service for Prometheus 執行個體。您可以在 Amazon Managed Service for Prometheus 工作區中收集多個叢集的指標,只要它們位於相同的用量帳戶和區域即可。

下列各節說明如何將正確的指標從 EKS 叢集傳送至 Amazon Managed Service for Prometheus 工作區。

先決條件

使用 Amazon Managed Service for Prometheus 搭配分割成本分配資料的先決條件:

  • 您需要在 AWS Billing and Cost Management 主控台中啟用分割成本分配資料。如需詳細資訊,請參閱啟用分割成本分配資料。選擇分割成本分配資料,會在每個用量帳戶中建立服務連結角色,以查詢該帳戶中 Amazon EKS 叢集指標的 Amazon Managed Service for Prometheus。如需詳細資訊,請參閱分割成本分配資料的服務連結角色

  • 您需要要追蹤分割成本分配資料的 EKS 叢集。這可以是現有的叢集,也可以建立新的叢集。如需詳細資訊,請參閱《Amazon EKS 使用者指南》中的建立 Amazon EKS 叢集

    注意

    您將需要 EKS cluster ARNsecurity group IDs和至少兩個 subnet IDs(在不同可用區域中),才能在後續步驟中使用。

    (選用) 將 EKS 叢集的身分驗證模式設定為 APIAPI_AND_CONFIG_MAP

  • 您需要在與 EKS 叢集相同的帳戶和區域中使用 Amazon Managed Service for Prometheus 執行個體。如果您還沒有,您可以建立一個。如需建立 Amazon Managed Service for Prometheus 執行個體的詳細資訊,請參閱《Amazon Managed Service for Prometheus 使用者指南》中的建立工作區

    注意

    您需要Amazon Managed Service for Prometheus workspace ARN在後續步驟中使用 。

將 EKS 指標轉送至 Amazon Managed Service for Prometheus

擁有 EKS 叢集和 Amazon Managed Service for Prometheus 執行個體後,您可以將指標從叢集轉送到執行個體。您可以透過兩種方式傳送指標。

選項 1:使用 AWS 受管收集器

使用 AWS 受管收集器 (抓取器) 是將指標從 EKS 叢集傳送至 Amazon Managed Service for Prometheus 執行個體的最簡單方法。下列程序會逐步引導您建立 AWS 受管收集器。如需詳細資訊,請參閱《Amazon Managed Service for Prometheus 使用者指南》中的AWS 受管收集器

注意

AWS 受管收集器的最短抓取間隔為 30 秒。如果您有短期 Pod,建議將抓取器間隔設定為 15 秒。若要使用 15 秒的抓取間隔,請使用選項 2 建立您自己的 Prometheus 代理程式。

建立 AWS 受管收集器有三個步驟:

  1. 建立抓取器組態。

  2. 建立抓取器。

  3. 設定您的 EKS 叢集以允許抓取器存取指標。

步驟 1:建立抓取器組態

若要建立抓取器,您必須具有抓取器組態。您可以使用預設組態,或建立自己的組態。以下是取得抓取器組態的三種方式:

  • 透過呼叫 ,使用 AWS CLI 取得預設組態:

    aws amp get-default-scraper-configuration
  • 建立您自己的組態。如需詳細資訊,請參閱《Amazon Managed Service for Prometheus 使用者指南》中的 Scraper 組態說明。

  • 複製 Amazon Managed Service for Prometheus 使用者指南中相同 Scraper 組態指示中提供的範例組態。

您可以編輯抓取器組態、修改抓取間隔,或篩選抓取的指標。

若要篩選抓取的指標,只包含分割成本分配資料所需的兩個指標,請使用下列抓取器組態:

scrape_configs: - bearer_token_file: /var/run/secrets/kubernetes.io/serviceaccount/token job_name: kubernetes-nodes-cadvisor scrape_interval: 30s scrape_timeout: 10s kubernetes_sd_configs: - role: node relabel_configs: - regex: (.+) replacement: /api/v1/nodes/$1/proxy/metrics/cadvisor source_labels: - __meta_kubernetes_node_name target_label: __metrics_path__ - replacement: kubernetes.default.svc:443 target_label: __address__ metric_relabel_configs: - source_labels: [__name__] regex: 'container_cpu_usage_seconds_total|container_memory_working_set_bytes' action: keep

擁有抓取器組態後,您必須對 base64 進行編碼,以便在步驟 2 中使用。組態是文字 YAML 檔案。若要編碼檔案,請使用網站,例如 https://www.base64encode.org/

步驟 2:建立抓取器

現在您已擁有組態檔案,您需要建立您的抓取器。根據先決條件區段中概述的變數,使用下列 AWS CLI 命令建立抓取器。您必須將來自 EKS 叢集的資訊用於 <EKS-CLUSTER-ARN><SG-SECURITY-GROUP-ID><SUBNET-ID> 欄位,將 <BASE64-CONFIGURATION-BLOB> 取代為您在上一個步驟中建立的抓取器組態,並將 <AMP_WORKSPACE_ARN> 取代為您的 Amazon Managed Service for Prometheus 工作區 ARN。

aws amp create-scraper \ --source eksConfiguration="{clusterArn=<EKS-CLUSTER-ARN>,securityGroupIds=[<SG-SECURITY-GROUP-ID>],subnetIds=[<SUBNET-ID>]}" \ --scrape-configuration configurationBlob=<BASE64-CONFIGURATION-BLOB> \ --destination ampConfiguration={workspaceArn="<AMP_WORKSPACE_ARN>"}

記下scraperId傳回供步驟 3 使用的 。

步驟 3:設定您的 EKS 叢集以允許抓取器存取指標

如果您的 EKS 叢集的身分驗證模式設定為 APIAPI_AND_CONFIG_MAP,則您的 抓取器將自動擁有正確的叢集內存取政策,而抓取器將可存取您的叢集。不需要進一步的組態,而且指標應該會流向 Amazon Managed Service for Prometheus。

如果您的 EKS 叢集的身分驗證模式未設定為 APIAPI_AND_CONFIG_MAP,您將需要手動設定叢集,以允許抓取器透過 ClusterRole 和 ClusterRoleBinding 存取您的指標。若要了解如何啟用這些許可,請參閱《Amazon Managed Service for Prometheus 使用者指南》中的手動設定 EKS 叢集以進行抓取器存取

選項 2:建立您自己的 Prometheus 代理程式

如果您無法使用 AWS 受管收集器,或已經有自己的 Prometheus 伺服器,您可以使用自己的 Prometheus 執行個體做為代理程式,從您的 EKS 叢集抓取指標,並將其傳送至 Amazon Managed Service for Prometheus。

如需如何使用您自己的 Prometheus 執行個體做為代理程式的詳細說明,請參閱《Amazon Managed Service for Prometheus 使用者指南》中的使用 Prometheus 執行個體做為收集器

以下是 Prometheus 抓取組態範例,其中包含 Prometheus 伺服器抓取間隔和分割成本分配資料所需的容器指標。如果您有短期 Pod,建議將預設 Prometheus 伺服器抓取間隔從 30 秒降至 15 秒。請注意,這可能會導致高 Prometheus 伺服器記憶體用量。

scrape_configs: - bearer_token_file: /var/run/secrets/kubernetes.io/serviceaccount/token job_name: kubernetes-nodes-cadvisor scrape_interval: 30s scrape_timeout: 10s kubernetes_sd_configs: - role: node relabel_configs: - regex: (.+) replacement: /api/v1/nodes/$1/proxy/metrics/cadvisor source_labels: - __meta_kubernetes_node_name target_label: __metrics_path__ - replacement: kubernetes.default.svc:443 target_label: __address__ metric_relabel_configs: - source_labels: [__name__] regex: 'container_cpu_usage_seconds_total|container_memory_working_set_bytes' action: keep

如果您在 Amazon Managed Service for Prometheus 使用者指南中的 中遵循使用 Helm 從新的 Prometheus 伺服器設定擷取,則可以更新抓取組態。

更新您的抓取組態
  1. my_prometheus_values_yaml 從指南編輯 ,並在 server區塊中包含範例抓取組態。

  2. 使用 Amazon Managed Service for Prometheus 使用者指南prometheus-namespace中的 prometheus-chart-name和 執行下列命令。

helm upgrade prometheus-chart-name prometheus-community/prometheus -n prometheus-namespace -f my_prometheus_values_yaml

若要進一步了解 scrape_interval或使用非全域 scrape_interval,請參閱 Prometheus 抓取組態

或者,您可以使用具有 Prometheus 接收器的 AWS Distro for OpenTelemetry 收集器、Prometheus 遠端寫入匯出器和 AWS Sigv4 身分驗證延伸,以實現對 Amazon Managed Service for Prometheus 的遠端寫入存取。

注意

設定 Prometheus 代理程式後,與 AWS 受管收集器不同,您需負責將代理程式保持在最新狀態並執行以收集指標。

估算 Amazon Managed Service for Prometheus 成本

您可以使用 AWS 定價計算器來估算使用 Amazon Managed Service for Prometheus 進行分割成本分配資料的成本。

為您的預估設定 Amazon Managed Service for Prometheus
  1. 開啟 AWS 定價計算器,網址為 https://https://calculator.aws/#/

  2. 選擇 Create estimate (建立估計)

  3. 新增服務頁面上,在搜尋欄位中輸入 Amazon Managed Service for Prometheus,然後選擇設定

  4. 描述欄位中,輸入估計值的描述。

  5. 選擇一個區域

  6. 選取使用您的基礎設施詳細資訊計算成本。此選項可讓您根據目前或提議的基礎設施設定,預估擷取、儲存和查詢範例成本。

  7. 針對 EC2 執行個體數量,輸入整個合併帳單系列 (包括所有帳戶和區域) 中所有叢集的 EC2 執行個體總數。如果您使用 AWS Fargate,請使用 Fargate 任務的數量做為 EC2 執行個體計數的代理。

  8. 分割成本分配資料需要兩個指標: container_cpu_usage_seconds_totalcontainer_memory_working_set_bytes。針對每個 EC2 執行個體的 Prometheus 指標,請輸入 2。

  9. 分割成本分配資料建議 15 秒的抓取間隔。針對指標收集間隔 (以秒為單位),輸入 15。如果您使用不同的間隔 (例如 30 秒),請將此變更為您設定的間隔。

  10. 分割成本分配資料不會對其他參數施加任何特定要求,因此請根據您的業務需求,為其餘輸入參數輸入適當的值。

  11. 選擇儲存並新增服務