View a markdown version of this page

新增 Prometheus 湊集目標的教學:Amazon ECS 上的 Memcached - Amazon CloudWatch

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

新增 Prometheus 湊集目標的教學:Amazon ECS 上的 Memcached

本教學課程提供實作介紹,讓您在具有 EC2 啟動類型的 Amazon ECS 叢集上湊集範例 Memcached 應用程式的 Prometheus 指標。CloudWatch 代理程式會透過 ECS 任務定義型服務探索自動探索 Memcached Prometheus 匯出工具目標。

Memcached 是一個通用的分佈式記憶體快取系統。它通常用於透過在 RAM 中快取資料和物件,來加速動態資料庫驅動的網站,進而減少必須讀取外部資料來源 (例如資料庫或 API) 的次數。如需詳細資訊,請參閱什麼是 Memcached?

memchached_exporter (Apache License 2.0) 是 Prometheus 其中一個正式匯出工具。memcache_exporter 預設會在 /metrics. 的連接埠 0.0.0.0:9150 上提供服務

本教學課程會使用下列兩個 Docker Hub 儲存庫中的 Docker 影像:

必要條件

若要從 Amazon ECS 的範例 Prometheus 工作負載收集指標,您必須在叢集中執行 Container Insights。如需安裝 Container Insights 的相關資訊,請參閱 Amazon ECS

設定 Amazon ECS EC2 叢集環境變數

如要設定 Amazon ECS EC2 叢集環境變數
  1. 安裝 Amazon ECS CLI (如果您尚未安裝)。如需詳細資訊,請參閱安裝 Amazon ECS CLI

  2. 設定新的 Amazon ECS 叢集名稱和區域。例如:

    ECS_CLUSTER_NAME=ecs-ec2-memcached-tutorial AWS_DEFAULT_REGION=ca-central-1
  3. (選用) 如果您還沒有要在其中安裝範例 Memcached 工作負載和 CloudWatch 代理程式的 EC2 啟動類型的 Amazon ECS 叢集,您可以輸入下列命令,進而建立一個叢集。

    ecs-cli up --capability-iam --size 1 \ --instance-type t3.medium \ --cluster $ECS_CLUSTER_NAME \ --region $AWS_REGION

    此命令的預期結果如下所示:

    WARN[0000] You will not be able to SSH into your EC2 instances without a key pair. INFO[0000] Using recommended Amazon Linux 2 AMI with ECS Agent 1.44.4 and Docker version 19.03.6-ce INFO[0001] Created cluster cluster=ecs-ec2-memcached-tutorial region=ca-central-1 INFO[0002] Waiting for your cluster resources to be created... INFO[0002] Cloudformation stack status stackStatus=CREATE_IN_PROGRESS INFO[0063] Cloudformation stack status stackStatus=CREATE_IN_PROGRESS INFO[0124] Cloudformation stack status stackStatus=CREATE_IN_PROGRESS VPC created: vpc-xxxxxxxxxxxxxxxxx Security Group created: sg-xxxxxxxxxxxxxxxxx Subnet created: subnet-xxxxxxxxxxxxxxxxx Subnet created: subnet-xxxxxxxxxxxxxxxxx Cluster creation succeeded.

安裝範例 Memcached 工作負載

若要安裝公開 Prometheus 指標的範例 Memcached 工作負載
  1. 輸入下列命令來下載 Memcached CloudFormation 範本。

    curl -O https://raw.githubusercontent.com/aws-samples/amazon-cloudwatch-container-insights/latest/ecs-task-definition-templates/deployment-mode/replica-service/cwagent-prometheus/sample_traffic/memcached/memcached-traffic-sample.yaml
  2. 輸入下列指令,設定要為 Memcached 建立的 IAM 角色名稱。

    MEMCACHED_ECS_TASK_ROLE_NAME=memcached-prometheus-demo-ecs-task-role-name MEMCACHED_ECS_EXECUTION_ROLE_NAME=memcached-prometheus-demo-ecs-execution-role-name
  3. 輸入下列命令,以安裝範例 Memcached 工作負載。此範例會在 host 網路模式中安裝工作負載。

    MEMCACHED_ECS_NETWORK_MODE=host aws cloudformation create-stack --stack-name Memcached-Prometheus-Demo-ECS-$ECS_CLUSTER_NAME-EC2-$MEMCACHED_ECS_NETWORK_MODE \ --template-body file://memcached-traffic-sample.yaml \ --parameters ParameterKey=ECSClusterName,ParameterValue=$ECS_CLUSTER_NAME \ ParameterKey=ECSNetworkMode,ParameterValue=$MEMCACHED_ECS_NETWORK_MODE \ ParameterKey=TaskRoleName,ParameterValue=$MEMCACHED_ECS_TASK_ROLE_NAME \ ParameterKey=ExecutionRoleName,ParameterValue=$MEMCACHED_ECS_EXECUTION_ROLE_NAME \ --capabilities CAPABILITY_NAMED_IAM \ --region $AWS_REGION

CloudFormation 堆疊會建立四個資源:

  • 一個 ECS 任務角色

  • 一個 ECS 任務執行角色

  • 一個 Memcached 任務定義

  • 一個 Memcached 服務

在 Memcached 任務定義中,定義了兩個容器:

  • 主要容器執行簡易的 Memcached 應用程式,並開啟連接埠 11211 以進行存取。

  • 另一個容器會執行 Redis OSS 匯出工具程序,以公開連接埠 9150 上的 Prometheus 指標。這是由 CloudWatch 代理程式探索及湊集的容器。

設定 CloudWatch 代理程式以湊集 Memcached Prometheus 指標

若要設定 CloudWatch 代理程式以湊集 Memcached Prometheus 指標
  1. 輸入下列命令,以下載最新版本的 cwagent-ecs-prometheus-metric-for-awsvpc.yaml

    curl -O https://raw.githubusercontent.com/aws-samples/amazon-cloudwatch-container-insights/latest/ecs-task-definition-templates/deployment-mode/replica-service/cwagent-prometheus/cloudformation-quickstart/cwagent-ecs-prometheus-metric-for-awsvpc.yaml
  2. 使用文字編輯器開啟檔案,然後在 resource:CWAgentConfigSSMParameter 區段中的 value 金鑰查找完整的 CloudWatch 代理程式組態。

    然後,在 ecs_service_discovery 區段中,將下列組態新增至 task_definition_list 區段。

    { "sd_job_name": "ecs-memcached", "sd_metrics_ports": "9150", "sd_task_definition_arn_pattern": ".*:task-definition/memcached-prometheus-demo.*:[0-9]+" },

    對於 metric_declaration 區段中,預設設定不允許任何 Memcached 指標。新增下列區段,以允許 Memcached 指標。請務必遵循現有的縮排模式。

    { "source_labels": ["container_name"], "label_matcher": "memcached-exporter-.*", "dimensions": [["ClusterName", "TaskDefinitionFamily"]], "metric_selectors": [ "^memcached_current_(bytes|items|connections)$", "^memcached_items_(reclaimed|evicted)_total$", "^memcached_(written|read)_bytes_total$", "^memcached_limit_bytes$", "^memcached_commands_total$" ] }, { "source_labels": ["container_name"], "label_matcher": "memcached-exporter-.*", "dimensions": [["ClusterName", "TaskDefinitionFamily","status","command"], ["ClusterName", "TaskDefinitionFamily","command"]], "metric_selectors": [ "^memcached_commands_total$" ] },
  3. 如果您已在 Amazon ECS 叢集中部署 CloudWatch 代理程式 CloudFormation,您可以輸入下列命令來建立變更集。

    ECS_NETWORK_MODE=bridge CREATE_IAM_ROLES=True ECS_TASK_ROLE_NAME=your_selected_ecs_task_role_name ECS_EXECUTION_ROLE_NAME=your_selected_ecs_execution_role_name aws cloudformation create-change-set --stack-name CWAgent-Prometheus-ECS-${ECS_CLUSTER_NAME}-EC2-${ECS_NETWORK_MODE} \ --template-body file://cwagent-ecs-prometheus-metric-for-bridge-host.yaml \ --parameters ParameterKey=ECSClusterName,ParameterValue=$ECS_CLUSTER_NAME \ ParameterKey=CreateIAMRoles,ParameterValue=$CREATE_IAM_ROLES \ ParameterKey=ECSNetworkMode,ParameterValue=$ECS_NETWORK_MODE \ ParameterKey=TaskRoleName,ParameterValue=$ECS_TASK_ROLE_NAME \ ParameterKey=ExecutionRoleName,ParameterValue=$ECS_EXECUTION_ROLE_NAME \ --capabilities CAPABILITY_NAMED_IAM \ --region $AWS_REGION \ --change-set-name memcached-scraping-support
  4. 在 https://https://console.aws.amazon.com/cloudformation 開啟 CloudFormation 主控台。

  5. 檢閱新建立的變更集 memcached-scraping-support。您應該會看到一個可套用至 CWAgentConfigSSMParameter 資源的變更。輸入下列命令,以執行變更集並重新執行 CloudWatch 代理程式任務。

    aws ecs update-service --cluster $ECS_CLUSTER_NAME \ --desired-count 0 \ --service cwagent-prometheus-replica-service-EC2-$ECS_NETWORK_MODE \ --region $AWS_REGION
  6. 請等候約 10 秒鐘,然後輸入下列命令。

    aws ecs update-service --cluster $ECS_CLUSTER_NAME \ --desired-count 1 \ --service cwagent-prometheus-replica-service-EC2-$ECS_NETWORK_MODE \ --region $AWS_REGION
  7. 如果您是第一次在叢集上安裝具有 Prometheus 指標收集的 CloudWatch 代理程式,請輸入下列命令:

    ECS_NETWORK_MODEE=bridge CREATE_IAM_ROLES=True ECS_TASK_ROLE_NAME=your_selected_ecs_task_role_name ECS_EXECUTION_ROLE_NAME=your_selected_ecs_execution_role_name aws cloudformation create-stack --stack-name CWAgent-Prometheus-ECS-${ECS_CLUSTER_NAME}-EC2-${ECS_NETWORK_MODE} \ --template-body file://cwagent-ecs-prometheus-metric-for-bridge-host.yaml \ --parameters ParameterKey=ECSClusterName,ParameterValue=$ECS_CLUSTER_NAME \ ParameterKey=CreateIAMRoles,ParameterValue=$CREATE_IAM_ROLES \ ParameterKey=ECSNetworkMode,ParameterValue=$ECS_NETWORK_MODE \ ParameterKey=TaskRoleName,ParameterValue=$ECS_TASK_ROLE_NAME \ ParameterKey=ExecutionRoleName,ParameterValue=$ECS_EXECUTION_ROLE_NAME \ --capabilities CAPABILITY_NAMED_IAM \ --region $AWS_REGION

檢視您的 Memcached 指標

此教學課程會將下列指標傳送至 CloudWatch 中的 ECS/ContainerInsights/Prometheus 命名空間。您可以使用 CloudWatch 主控台查看該命名空間中的指標。

指標名稱 維度

memcached_current_items

ClusterName, TaskDefinitionFamily

memcached_current_connections

ClusterName, TaskDefinitionFamily

memcached_limit_bytes

ClusterName, TaskDefinitionFamily

memcached_current_bytes

ClusterName, TaskDefinitionFamily

memcached_written_bytes_total

ClusterName, TaskDefinitionFamily

memcached_read_bytes_total

ClusterName, TaskDefinitionFamily

memcached_items_evicted_total

ClusterName, TaskDefinitionFamily

memcached_items_reclaimed_total

ClusterName, TaskDefinitionFamily

memcached_commands_total

ClusterName, TaskDefinitionFamily

ClusterName、TaskDefinitionFamily、命令

ClusterName、TaskDefinitionFamily、狀態、命令

注意

command (命令) 維度的數值可以是:deletegetcassetdecrtouchincrflush

status (狀態) 維度的數值可以是 hitmissbadval

您也可以為您的 Memcached Prometheus 指標建立 CloudWatch 儀表板。

若要建立 Memcached Prometheus 指標的儀表板
  1. 建立環境變數,取代下面的數值,以符合您的部署。

    DASHBOARD_NAME=your_memcached_cw_dashboard_name ECS_TASK_DEF_FAMILY=memcached-prometheus-demo-$ECS_CLUSTER_NAME-EC2-$MEMCACHED_ECS_NETWORK_MOD
  2. 輸入下列命令建立儀表板。

    curl https://raw.githubusercontent.com/aws-samples/amazon-cloudwatch-container-insights/latest/ecs-task-definition-templates/deployment-mode/replica-service/cwagent-prometheus/sample_cloudwatch_dashboards/memcached/cw_dashboard_memcached.json \ | sed "s/{{YOUR_AWS_REGION}}/$AWS_REGION/g" \ | sed "s/{{YOUR_CLUSTER_NAME}}/$ECS_CLUSTER_NAME/g" \ | sed "s/{{YOUR_TASK_DEF_FAMILY}}/$ECS_TASK_DEF_FAMILY/g" \ | xargs -0 aws cloudwatch put-dashboard --dashboard-name ${DASHBOARD_NAME} --region $AWS_REGION --dashboard-body