如何在 Amazon EKS 叢集上開啟 Container Insights 指標?

4 分的閱讀內容
0

我想要設定 Amazon CloudWatch Container Insights,以查看我的 Amazon Elastic Kubernetes Service (Amazon EKS) 叢集指標。

解決方法

只有 Linux 執行個體才支援 Container Insights。Amazon 在 Amazon Elastic Container Registry (Amazon ECR) 上提供了 CloudWatch 代理程式容器映像檔。如需詳細資訊,請參閱 cloudwatch-agent/cloudwatch-agent

在開始之前,請確認您符合 CloudWatch 中 Container Insights 的先決條件。對於 AWS Fargate 叢集,您必須定義 Fargate 設定檔來排程 Pod。此外,Amazon EKS Pod 身分識別和 Access Management (IAM) 角色必須允許在 Fargate 基礎架構上執行的元件呼叫 AWS API。例如,IAM 角色必須能夠從 Amazon ECR 中提取容器映像檔。

使用 CloudWatch 代理程式在 EKS 叢集上設定 Container Insights 指標

CloudWatch 代理程式會建立一個名為 aws/containerinsights/Cluster_Name/performance 的日誌群組,然後將效能日誌事件傳送到該日誌群組。當您設定 Container Insights 來收集指標時,必須從 Docker Hub 將 CloudWatch 代理程式容器映像檔部署為 DaemonSet。預設情況下,您會以匿名使用者身分部署容器映像檔。

**注意:**Docker 可能會限制您可以提取的映像檔數量。

若要使用 CloudWatch 代理程式設定 Container Insights,請完成以下步驟:

  1. 執行以下命令建立 amazon-cloudwatch 命名空間:

    kubectl apply -f https://raw.githubusercontent.com/aws-samples/amazon-cloudwatch-container-insights/latest/k8s-deployment-manifest-templates/deployment-mode/daemonset/container-insights-monitoring/cloudwatch-namespace.yaml
  2. 執行以下命令,為名為 cloudwatch-agent 的 CloudWatch 代理程式建立服務帳戶:

    kubectl apply -f https://raw.githubusercontent.com/aws-samples/amazon-cloudwatch-container-insights/latest/k8s-deployment-manifest-templates/deployment-mode/daemonset/container-insights-monitoring/cwagent/cwagent-serviceaccount.yaml
  3. 執行以下命令,以建立 configmap 作為 CloudWatch 代理程式的組態檔案:

    ClusterName= my-cluster-name curl https://raw.githubusercontent.com/aws-samples/amazon-cloudwatch-container-insights/latest/k8s-deployment-manifest-templates/deployment-mode/daemonset/container-insights-monitoring/cwagent/cwagent-configmap.yaml | sed 's/cluster_name/'${ClusterName}'/' | kubectl apply -f -

    **注意:**將 my-cluster-name 替換為您 EKS 叢集的名稱。如需詳細資訊,請參閱為 CloudWatch 代理程式建立 ConfigMap

  4. 執行以下命令來部署 cloudwatch-agent DaemonSet:

    kubectl apply -f https://raw.githubusercontent.com/aws-samples/amazon-cloudwatch-container-insights/latest/k8s-deployment-manifest-templates/deployment-mode/daemonset/container-insights-monitoring/cwagent/cwagent-daemonset.yaml
  5. (選用) 執行以下命令來修補 cloudwatch-agent DaemonSet,使其從 ECR 中提取 CloudWatch 代理程式:

    kubectl patch ds cloudwatch-agent -n amazon-cloudwatch -p \ '{"spec":{"template":{"spec":{"containers":[{"name":"cloudwatch-agent","image":"public.ecr.aws/cloudwatch-agent/cloudwatch-agent:latest"}]}}}}'
    

    **注意:**ECR 上的 CloudWatch 代理程式 Docker 映像檔支援 ARM 和 AMD64 架構。根據映像檔版本和架構替換最新的映像檔標籤。如需詳細資訊,請參閱 cloudwatch-agent/cloudwatch-agent

  6. 對於服務帳戶的 IAM 角色,請建立 OIDC 供應商以及 IAM 角色和政策。然後,執行以下命令將 IAM 角色關聯至 cloudwatch-agent 服務帳戶:

    kubectl annotate serviceaccounts cloudwatch-agent -n amazon-cloudwatch "eks.amazonaws.com/role-arn=arn:aws:iam::ACCOUNT_ID:role/IAM_ROLE_NAME"

    **注意:**將 ACCOUNT_ID 替換為您的帳戶 ID,將 IAM_ROLE_NAME 替換為您用於服務帳戶的 IAM 角色。

對 CloudWatch 代理程式進行疑難排解

請完成下列步驟:

  1. 執行以下命令來擷取 Pod 清單:

    kubectl get pods -n amazon-cloudwatch
  2. 執行以下命令以查看輸出底部的事件:

    kubectl describe pod pod-name -n amazon-cloudwatch
  3. 執行以下命令以查看日誌:

    kubectl logs pod-name -n amazon-cloudwatch
    
  4. 如果您看到 CloudWatch 代理程式出現 CrashLoopBackOff 錯誤,請確認您已正確設定 IAM 權限。

刪除 CloudWatch 代理程式

執行下列命令以刪除 CloudWatch 代理程式:

kubectl delete -f https://raw.githubusercontent.com/aws-samples/amazon-cloudwatch-container-insights/latest/k8s-deployment-manifest-templates/deployment-mode/daemonset/container-insights-monitoring/cloudwatch-namespace.yaml

**注意:**如果您刪除命名空間,則也會刪除相關資源。

使用 Distro for OpentTelemetry 在 EKS 叢集上設定 Container Insights 指標

AWS Distro for OpenTelemetry 會建立一個名為 aws/containerinsights/Cluster_Name/performance 的日誌群組,然後將效能日誌事件傳送到該日誌群組。

請完成下列步驟:

  1. 執行下列命令,將 AWS OpenTelemetry Collector 部署為 DaemonSet:

    curl https://raw.githubusercontent.com/aws-observability/aws-otel-collector/main/deployment-template/eks/otel-container-insights-infra.yaml | kubectl apply -f -
    

    **注意:**如需詳細資訊,請參閱 Container Insights EKS 基礎架構指標

  2. 執行以下命令確認收集器正在運作:

    kubectl get pods -l name=aws-otel-eks-ci -n aws-otel-eks
    
  3. (選用) 執行以下命令來修補 aws-otel-eks-ci DaemonSet,使其在 ECR 上提取 aws-otel-collector Docker 映像檔:

    kubectl patch ds aws-otel-eks-ci -n aws-otel-eks -p \'{"spec":{"template":{"spec":{"containers":[{"name":"aws-otel-collector","image":"public.ecr.aws/aws-observability/aws-otel-collector:latest"}]}}}}'

    注意: ECR 上的 Cloudwatch-agent Docker 映像檔支援 ARM 和 AMD64 架構。根據映像檔版本和架構替換最新的映像檔標籤。

  4. (選用) 對於服務帳戶的 IAM 角色,請建立 OIDC 供應商以及 IAM 角色和政策。然後,執行以下命令將 IAM 角色關聯至 aws-otel-sa 服務帳戶:

    kubectl annotate serviceaccounts aws-otel-sa -n aws-otel-eks "eks.amazonaws.com/role-arn=arn:aws:iam::ACCOUNT_ID:role/IAM_ROLE_NAME"
    

    **注意:**將 ACCOUNT_ID 替換為您的帳戶 ID,將 IAM_ROLE_NAME 替換為您用於服務帳戶的 IAM 角色。

刪除 Distro for OpenTelemetry

執行下列命令以刪除 Distro for OpenTelemetry:

curl https://raw.githubusercontent.com/aws-observability/aws-otel-collector/main/deployment-template/eks/otel-container-insights-infra.yaml |kubectl delete -f -

使用 Distro for OpenTelemetry 在 EKS Fargate 叢集上設定 Container Insights 指標

對於在 EKS 和 Fargate 上執行的應用程式,您可以使用 Distro for OpenTelemetry 來設定 Container Insights
AWS OpenTelemetry Collector 會針對 EKS Fargate 上執行的每個工作負載,向 CloudWatch 傳送下列指標:

  • pod_cpu_utilization_over_pod_limit
  • pod_cpu_usage_total
  • pod_cpu_limit
  • pod_memory_utilization_over_pod_limit
  • pod_memory_working_set
  • pod_memory_limit
  • pod_network_rx_bytes
  • pod_network_tx_bytes

AWS OpenTelemetry Collector 會收集名為 ContainerInsights 的 CloudWatch 命名空間下的每個指標。從 CloudWatch 主控台中,選擇 Metrics (指標),然後選擇 All metrics (所有指標)。您可以導覽至 Custom Namespace (自訂命名空間) 下的 ContainerInsights

每個指標都與以下維度集相關聯:

  • ClusterNameLaunchType
  • ClusterNameNamespaceLaunchType
  • ClusterNameNamespacePodNameLaunchType

如需詳細資訊,請參閱 Container Insights EKS Fargate

若要在 EKS Fargate 中部署 Distro for OpenTelemetry,請完成下列步驟:

  1. 建立一個名為 fargate-container-insights 的命名空間。

  2. 使用下列指令碼建立名為 EKS-ADOT-ServiceAccount-Role 的 IAM 角色,該角色與名為 adot-collector 的 Kubernetes 服務帳戶關聯。下列輔助程式指令碼需要 eksctl

    #!/bin/bashCLUSTER_NAME=YOUR-EKS-CLUSTER-NAMEREGION=YOUR-EKS-CLUSTER-REGION
    SERVICE_ACCOUNT_NAMESPACE=fargate-container-insights
    SERVICE_ACCOUNT_NAME=adot-collector
    SERVICE_ACCOUNT_IAM_ROLE=EKS-Fargate-ADOT-ServiceAccount-Role
    SERVICE_ACCOUNT_IAM_POLICY=arn:aws:iam::aws:policy/CloudWatchAgentServerPolicy
    
    kubectl create ns fargate-container-insights
    
    eksctl utils associate-iam-oidc-provider \
    --cluster=$CLUSTER_NAME \
    --approve
    
    eksctl create iamserviceaccount \
    --cluster=$CLUSTER_NAME \
    --region=$REGION \
    --name=$SERVICE_ACCOUNT_NAME \
    --namespace=$SERVICE_ACCOUNT_NAMESPACE \
    --role-name=$SERVICE_ACCOUNT_IAM_ROLE \
    --attach-policy-arn=$SERVICE_ACCOUNT_IAM_POLICY \
    --approve

    **注意:**將 CLUSTER_NAME 替換為您的叢集名稱,將 REGION 替換為您的 AWS 區域。

  3. 執行下列命令,將 AWS OpenTelemetry Collector 部署為 Kubernetes StatefulSet:

    ClusterName= your-cluster-name Region= your-cluster-region curl https://raw.githubusercontent.com/aws-observability/aws-otel-collector/main/deployment-template/eks/otel-fargate-container-insights.yaml | sed 's/YOUR-EKS-CLUSTER-NAME/'${ClusterName}'/;s/us-east-1/'${Region}'/' | kubectl apply -f -
    

    **注意:**將 your-cluster-name 替換為您叢集的名稱,將 your-cluster-region 替換為您叢集所在的區域。確認您有相符的 Fargate 設定檔,以在 Fargate 上佈建 StatefulSet Pod。

  4. 執行以下命令來驗證 Distro for Open Telemetry Collector Pod 是否正在執行:

    kubectl get pods -n fargate-container-insights
  5. (選用) 執行下列命令來修補 adot-collector StatefulSet,使其在 ECR 上提取 aws-otel-collector Docker 映像檔:

    kubectl patch sts adot-collector -n fargate-container-insights -p \'{"spec":{"template":{"spec":{"containers":[{"name":"adot-collector","image":"public.ecr.aws/aws-observability/aws-otel-collector:latest"}]}}}}'
    

刪除 Distro for OpenTelemetry

執行下列命令以刪除 Distro for OpenTelemetry:

eksctl delete iamserviceaccount --cluster CLUSTER_NAME --name adot-collector
ClusterName= your-cluster-name Region= your-cluster-region
curl https://raw.githubusercontent.com/aws-observability/aws-otel-collector/main/deployment-template/eks/otel-fargate-container-insights.yaml | sed 's/YOUR-EKS-CLUSTER-NAME/'${ClusterName}'/;s/us-east-1/'${Region}'/' | kubectl delete -f -

**注意:**將 your-cluster-name 替換為您的叢集名稱,將 your-cluster-region 替換為您叢集所在的區域。

AWS 官方已更新 3 個月前