

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

# 使用 AWS 可觀測性加速器設定 Amazon Managed Service for Prometheus
<a name="obs_accelerator"></a>

AWS 為您的 Amazon Elastic Kubernetes Service (Amazon EKS) 專案提供可觀測性工具，包括監控、記錄、提醒和儀表板。這包含 Amazon Managed Service for Prometheus、[Amazon Managed Grafana](https://docs.aws.amazon.com/grafana/latest/userguide/what-is-Amazon-Managed-Service-Grafana.html)、[適用於 OpenTelemetry 的AWS Distro](https://aws-otel.github.io/) 和其他工具。為了協助您同時使用這些工具， AWS 提供 Terraform 模組來設定這些服務的可觀測性，稱為[AWS 可觀測性加速器](https://github.com/aws-observability/terraform-aws-observability-accelerator)。

AWS Observability Accelerator 為 Amazon Managed Service for Prometheus 提供兩個收集器設定檔：
+ **受管指標 （無代理程式）** – 使用 [Amazon Managed Service for Prometheus 收集器](https://docs.aws.amazon.com/prometheus/latest/userguide/AMP-collector.html)，這是在叢集外部執行的全受管、無代理程式的湊集器。沒有要管理的收集器 Pod。僅限指標。
+ **自我管理** – 透過叢集中的 Helm 部署 OpenTelemetry Collector。支援指標、追蹤 (AWS X-Ray) 和日誌 (Amazon CloudWatch)。

本節會逐步介紹這兩個選項，從建議的無代理程式方法開始。

Terraform 範本和詳細說明可在 [Terraform GitHub 頁面的AWS 可觀測性加速器](https://github.com/aws-observability/terraform-aws-observability-accelerator)上找到。

## 先決條件
<a name="obs-accelerator-prereq"></a>

若要使用 AWS 可觀測性加速器，您必須擁有現有的 Amazon EKS 叢集，以及下列先決條件：
+ [AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html) – 用來從命令列呼叫 AWS 功能。
+ [kubectl](https://kubernetes.io/docs/tasks/tools/)：用於從命令列控制您的 EKS 叢集。
+ [Terraform](https://learn.hashicorp.com/tutorials/terraform/install-cli) (>= 1.5.0) – 用來自動建立此解決方案的資源。您必須讓 AWS 提供者設定 IAM 角色，該角色有權在您的 AWS 帳戶中建立和管理 Amazon Managed Service for Prometheus、Amazon Managed Grafana 和 IAM。如需有關如何設定 Terraform AWS 提供者的詳細資訊，請參閱 *Terraform 說明文件*中的[AWS 提供者](https://registry.terraform.io/providers/hashicorp/aws/latest/docs)。

## 使用 受管指標 （無代理程式） 範例
<a name="obs-accelerator-managed-metrics"></a>

此範例使用 Amazon Managed Service for Prometheus 收集器從您的 Amazon EKS 叢集中抓取 Prometheus 指標，而不部署任何收集器 Pod。收集器在兩個不同的可用區域中至少需要兩個子網路。如需詳細資訊，請參閱 GitHub 上的 [eks-amp-managed](https://github.com/aws-observability/terraform-aws-observability-accelerator/tree/main/examples/eks-amp-managed) 範例。

**使用無代理程式基礎設施監控 Terraform 模組**

1. 從您要在其中建立專案的資料夾中，使用以下命令複製儲存庫。

   ```
   git clone https://github.com/aws-observability/terraform-aws-observability-accelerator.git
   ```

1. 使用以下命令初始化 Terraform。

   ```
   cd examples/eks-amp-managed
   
   terraform init
   ```

1. 建立新 `terraform.tfvars` 檔案，如下列範例所示。使用 Amazon EKS AWS 叢集的區域、叢集 ID 和 VPC 網路詳細資訊。收集器在兩個不同的可用區域中至少需要兩個子網路。

   ```
   # (mandatory) AWS Region where your resources will be located
   aws_region = "{{eu-west-1}}"
   
   # (mandatory) EKS Cluster name
   eks_cluster_id = "{{my-eks-cluster}}"
   
   # (mandatory) Subnets for the managed scraper (>= 2 AZs)
   scraper_subnet_ids = ["{{subnet-aaa}}", "{{subnet-bbb}}"]
   
   # (mandatory) Security group allowing scraper access to the EKS API
   scraper_security_group_ids = ["{{sg-xxx}}"]
   ```

1. 若您尚無想要使用的工作區，請建立 Amazon Managed Grafana 工作區。如需有關如何建立新工作區的詳細資訊，請參閱 *Amazon Managed Grafana 使用者指南*中的[建立您的第一個工作區](https://docs.aws.amazon.com/grafana/latest/userguide/getting-started-with-AMG.html#AMG-getting-started-workspace-create)。

1. 在命令列中執行下列命令，為 Terraform 建立兩個變數以使用 Grafana 工作區。您需要將 {{grafana-workspace-id}} 替換為 Grafana 工作區的 ID。

   ```
   export TF_VAR_managed_grafana_workspace_id={{grafana-workspace-id}}
   export TF_VAR_grafana_api_key=`aws grafana create-workspace-api-key --key-name "observability-accelerator-$(date +%s)" --key-role ADMIN --seconds-to-live 1200 --workspace-id $TF_VAR_managed_grafana_workspace_id --query key --output text`
   ```

1. [選用] 若要使用現有 Amazon Managed Service for Prometheus 工作區，請將 ID 新增至 `terraform.tfvars` 檔案，如下列範例所示，將 {{prometheus-workspace-id}} 替換為您的 Prometheus 工作區 ID。如果您未指定現有的工作區，則會為您建立新的 Prometheus 工作區。

   ```
   # (optional) Leave it empty for a new workspace to be created
   managed_prometheus_workspace_id = "{{prometheus-workspace-id}}"
   ```

1. 使用下列命令部署解決方案。

   ```
   terraform apply -var-file=terraform.tfvars
   ```

這將在您的帳戶中建立資源 AWS ，包括下列項目：
+ 全新 Amazon Managed Service for Prometheus 工作區 (除非您選擇使用現有的工作區)。
+ Amazon Managed Service for Prometheus 收集器 （無代理程式湊集器），設定為從 Amazon EKS 叢集中抓取 Prometheus 指標。
+ Amazon Managed Service for Prometheus 工作區中的 Prometheus 記錄和提醒規則。
+ 部署在 Amazon EKS 叢集中用於基礎設施指標的 kube-state-metrics 和 node-exporter。
+ 您目前工作區中的全新 Amazon Managed Grafana 資料來源和儀表板。儀表板將列在 **EKS 監控**下。

## 替代方案：自我管理的 OpenTelemetry Collector
<a name="obs-accelerator-self-managed"></a>

如果您需要追蹤、日誌或完全控制集合管道，請使用自我管理的設定檔。這會在 Amazon EKS 叢集中透過 Helm 部署 OpenTelemetry Collector，設定為抓取 Prometheus 指標並遠端寫入 Amazon Managed Service for Prometheus。它也支援追蹤 (AWS X-Ray) 和日誌 (Amazon CloudWatch)。如需詳細資訊，請參閱 GitHub 上的 [eks-amp-otel](https://github.com/aws-observability/terraform-aws-observability-accelerator/tree/main/examples/eks-amp-otel) 範例。

**使用自我管理的 Terraform 模組**

1. 複製儲存庫並初始化 Terraform。

   ```
   git clone https://github.com/aws-observability/terraform-aws-observability-accelerator.git
   cd examples/eks-amp-otel
   terraform init
   ```

1. 建立新 `terraform.tfvars` 檔案，如下列範例所示。

   ```
   # (mandatory) AWS Region where your resources will be located
   aws_region = "{{eu-west-1}}"
   
   # (mandatory) EKS Cluster name
   eks_cluster_id = "{{my-eks-cluster}}"
   ```

1. 使用與受管指標範例相同的步驟來設定 Amazon Managed Grafana 工作區和 API 金鑰 （上述步驟 4-6)。

1. 使用下列命令部署解決方案。

   ```
   terraform apply -var-file=terraform.tfvars
   ```

這會在您的帳戶中建立下列資源 AWS （與無代理程式方法不同，收集器會在叢集中執行）：
+ Amazon Managed Service for Prometheus 工作區 （若未提供）。
+ 具有資料來源和儀表板的 Amazon Managed Grafana 工作區。
+ 在 Amazon EKS 叢集中透過 Helm 部署的 OpenTelemetry Collector，設定為抓取 Prometheus 指標並遠端寫入 Amazon Managed Service for Prometheus。
+ OpenTelemetry Collector 的服務帳戶 (IRSA) 的 IAM 角色。
+ 追蹤管道到 AWS X-Ray （預設為啟用）。
+ 將管道記錄到 Amazon CloudWatch （預設為啟用）。

## 檢視儀表板
<a name="obs-accelerator-dashboards"></a>

若要檢視新的儀表板，請在 Amazon Managed Grafana 工作區中開啟特定儀表板。Terraform 會自動佈建基礎設施儀表板。如需有關使用 Amazon Managed Grafana 的詳細資訊，請參閱 *Amazon Managed Grafana 使用者指南*中的[在 Grafana 工作區中工作](https://docs.aws.amazon.com/grafana/latest/userguide/AMG-working-with-Grafana-workspace.html)。