本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
在 Amazon ECS 叢集上安裝含有 Prometheus 指標收集的 CloudWatch 代理程式
本節說明如何在執行 Amazon ECS 的叢集中設定具有 Prometheus 監控的 CloudWatch 代理程式。執行這項操作之後,代理程式會自動湊集和匯入該叢集中執行的下列工作負載的指標。
-
AWS App Mesh
-
Java/JMX
您也可以設定代理程式,以從其他 Prometheus 工作負載和來源湊集和匯入指標。
設定 IAM 角色
您需要兩個 IAM 角色來執行 CloudWatch 代理程式任務定義。如果您在 AWS CloudFormation 堆疊CreateIAMRoles=True
中指定 讓 Container Insights 為您建立這些角色,則會使用正確的許可來建立角色。如果您想要自行建立或使用現有角色,則需要下列角色和許可。
-
CloudWatch 代理程式 ECS 任務角色 — CloudWatch 代理程式容器會使用此角色。它必須包含 CloudWatchAgentServerPolicy 政策和包含下列唯讀許可的客戶受管政策:
-
ec2:DescribeInstances
-
ecs:ListTasks
-
ecs:ListServices
-
ecs:DescribeContainerInstances
-
ecs:DescribeServices
-
ecs:DescribeTasks
-
ecs:DescribeTaskDefinition
-
-
CloudWatch 代理程式 ECS 任務執行角色 — 這是 Amazon ECS 啟動和執行容器所需的角色。確保您的任務執行角色已連接至 AmazonSSMReadOnlyAccess、AmazonECSTaskExecutionRolePolicy 和 CloudWatchAgentServerPolicy 政策。如果您要存放更敏感的資料以供 Amazon ECS 使用,請參閱指定敏感資料。
使用 安裝具有 Prometheus 監控的 CloudWatch 代理程式 AWS CloudFormation
您可以使用 AWS CloudFormation 為 Amazon ECS 叢集安裝具有 Prometheus 監控的 CloudWatch 代理程式。下列清單顯示了您將在 AWS CloudFormation 範本中使用的參數。
-
ECSClusterName— 指定目標 Amazon ECS 叢集。
-
CreateIAMRoles— 指定
True
,以為 Amazon ECS 任務角色和 Amazon ECS 任務執行角色建立新角色。指定False
以重複使用現有角色。 -
TaskRoleName— 如果您將 CreateIAMRoles 指定為
True
,則會指定要用於新 Amazon ECS 任務角色的名稱。如果您將 CreateIAMRoles 指定為False
,則會指定要作為 Amazon ECS 任務角色使用的現有角色。 -
TaskRoleName— 如果您將 CreateIAMRoles 指定為
True
,則會指定要用於新 Amazon ECS 任務角色的名稱。如果您將 CreateIAMRoles 指定為False
,則會指定要作為 Amazon ECS 任務執行角色使用的現有角色。 -
ECSNetworkMode— 如果您使用 EC2 啟動類型,請在此處指定網路模式。其必須為
bridge
或host
。 -
ECSLaunchType— 指定
fargate
或EC2
。 -
SecurityGroupID— 如果 ECSNetworkMode 是
awsvpc
,請在此指定安全群組 ID。 -
SubneId— 如果 ECSNetworkMode 是
awsvpc
,請在此處指定子網路 ID。
命令範例
本節包含在各種情況下使用 Prometheus 監控安裝 Container Insights 的範例 AWS CloudFormation 命令。
在橋接網路模式下為 Amazon ECS 叢集建立 AWS CloudFormation 堆疊
export AWS_PROFILE=
your_aws_config_profile_eg_default
export AWS_DEFAULT_REGION=your_aws_region_eg_ap-southeast-1
export ECS_CLUSTER_NAME=your_ec2_ecs_cluster_name
export ECS_NETWORK_MODE=bridge export CREATE_IAM_ROLES=True export ECS_TASK_ROLE_NAME=your_selected_ecs_task_role_name
export ECS_EXECUTION_ROLE_NAME=your_selected_ecs_execution_role_name
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-bridge-host.yaml 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_DEFAULT_REGION} \ --profile ${AWS_PROFILE}
在主機網路模式中為 Amazon ECS 叢集建立 AWS CloudFormation 堆疊
export AWS_PROFILE=
your_aws_config_profile_eg_default
export AWS_DEFAULT_REGION=your_aws_region_eg_ap-southeast-1
export ECS_CLUSTER_NAME=your_ec2_ecs_cluster_name
export ECS_NETWORK_MODE=host export CREATE_IAM_ROLES=True export ECS_TASK_ROLE_NAME=your_selected_ecs_task_role_name
export ECS_EXECUTION_ROLE_NAME=your_selected_ecs_execution_role_name
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-bridge-host.yaml 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_DEFAULT_REGION} \ --profile ${AWS_PROFILE}
在 awsvpc 網路模式中為 Amazon ECS 叢集建立 AWS CloudFormation 堆疊
export AWS_PROFILE=
your_aws_config_profile_eg_default
export AWS_DEFAULT_REGION=your_aws_region_eg_ap-southeast-1
export ECS_CLUSTER_NAME=your_ec2_ecs_cluster_name
export ECS_LAUNCH_TYPE=EC2 export CREATE_IAM_ROLES=True export ECS_CLUSTER_SECURITY_GROUP=your_security_group_eg_sg-xxxxxxxxxx
export ECS_CLUSTER_SUBNET=your_subnet_eg_subnet-xxxxxxxxxx
export ECS_TASK_ROLE_NAME=your_selected_ecs_task_role_name
export ECS_EXECUTION_ROLE_NAME=your_selected_ecs_execution_role_name
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 aws cloudformation create-stack --stack-name CWAgent-Prometheus-ECS-${ECS_CLUSTER_NAME}-${ECS_LAUNCH_TYPE}-awsvpc \ --template-body file://cwagent-ecs-prometheus-metric-for-awsvpc.yaml \ --parameters ParameterKey=ECSClusterName,ParameterValue=${ECS_CLUSTER_NAME} \ ParameterKey=CreateIAMRoles,ParameterValue=${CREATE_IAM_ROLES} \ ParameterKey=ECSLaunchType,ParameterValue=${ECS_LAUNCH_TYPE} \ ParameterKey=SecurityGroupID,ParameterValue=${ECS_CLUSTER_SECURITY_GROUP} \ ParameterKey=SubnetID,ParameterValue=${ECS_CLUSTER_SUBNET} \ ParameterKey=TaskRoleName,ParameterValue=${ECS_TASK_ROLE_NAME} \ ParameterKey=ExecutionRoleName,ParameterValue=${ECS_EXECUTION_ROLE_NAME} \ --capabilities CAPABILITY_NAMED_IAM \ --region ${AWS_DEFAULT_REGION} \ --profile ${AWS_PROFILE}
在 awsvpc 網路模式中為 Fargate 叢集建立 AWS CloudFormation 堆疊
export AWS_PROFILE=
your_aws_config_profile_eg_default
export AWS_DEFAULT_REGION=your_aws_region_eg_ap-southeast-1
export ECS_CLUSTER_NAME=your_ec2_ecs_cluster_name
export ECS_LAUNCH_TYPE=FARGATE export CREATE_IAM_ROLES=True export ECS_CLUSTER_SECURITY_GROUP=your_security_group_eg_sg-xxxxxxxxxx
export ECS_CLUSTER_SUBNET=your_subnet_eg_subnet-xxxxxxxxxx
export ECS_TASK_ROLE_NAME=your_selected_ecs_task_role_name
export ECS_EXECUTION_ROLE_NAME=your_selected_ecs_execution_role_name
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 aws cloudformation create-stack --stack-name CWAgent-Prometheus-ECS-${ECS_CLUSTER_NAME}-${ECS_LAUNCH_TYPE}-awsvpc \ --template-body file://cwagent-ecs-prometheus-metric-for-awsvpc.yaml \ --parameters ParameterKey=ECSClusterName,ParameterValue=${ECS_CLUSTER_NAME} \ ParameterKey=CreateIAMRoles,ParameterValue=${CREATE_IAM_ROLES} \ ParameterKey=ECSLaunchType,ParameterValue=${ECS_LAUNCH_TYPE} \ ParameterKey=SecurityGroupID,ParameterValue=${ECS_CLUSTER_SECURITY_GROUP} \ ParameterKey=SubnetID,ParameterValue=${ECS_CLUSTER_SUBNET} \ ParameterKey=TaskRoleName,ParameterValue=${ECS_TASK_ROLE_NAME} \ ParameterKey=ExecutionRoleName,ParameterValue=${ECS_EXECUTION_ROLE_NAME} \ --capabilities CAPABILITY_NAMED_IAM \ --region ${AWS_DEFAULT_REGION} \ --profile ${AWS_PROFILE}
AWSAWS CloudFormation 堆疊建立的資源
下表列出當您使用 AWS CloudFormation 在 Amazon ECS 叢集上設定 Container Insights with Prometheus 監控時所建立 AWS 的資源。
資源類型 | 資源名稱 | 說明 |
---|---|---|
AWS::SSM::Parameter |
AmazonCloudWatch-CWAgentConfig-$ |
這是具有預設 App Mesh 和 Java/JMX 內嵌指標格式定義的 CloudWatch 代理程式。 |
AWS::SSM::Parameter |
AmazonCloudWatch-PrometheusConfigName-$ |
這是 Prometheus 湊集組態。 |
AWS::IAM::Role |
$ECS_TASK_ROLE_NAME。 |
Amazon ECS 任務角色。這僅在您為 |
AWS::IAM::Role |
${ECS_EXECUTION_ROLE_NAME} |
Amazon ECS 任務執行角色。這僅在您為 |
AWS::ECS::TaskDefinition |
cwagent-prometheus-$ |
|
AWS::ECS::Service |
cwagent-prometheus-replica-service-$ |
使用 Prometheus 監控刪除 CloudWatch 代理程式的 AWS CloudFormation 堆疊
若要從 Amazon ECS 叢集刪除 CloudWatch 代理程式,請輸入這些命令。
export AWS_PROFILE=
your_aws_config_profile_eg_default
export AWS_DEFAULT_REGION=your_aws_region_eg_ap-southeast-1
export CLOUDFORMATION_STACK_NAME=your_cloudformation_stack_name
aws cloudformation delete-stack \ --stack-name ${CLOUDFORMATION_STACK_NAME} \ --region ${AWS_DEFAULT_REGION} \ --profile ${AWS_PROFILE}