如何获取已激活 Amazon ECS Exec 的 Fargate 任务的 SSM Agent 日志?
我想要获取已激活 Amazon Elastic Container Service(Amazon ECS)Exec 的 AWS Fargate 任务的 AWS Systems Manager Agent(SSM Agent)日志。
简短描述
先决条件:
要获取已激活 ECS Exec 的 Fargate 任务的 SSM Agent 日志,请创建 Amazon Elastic File System(Amazon EFS)文件系统。然后,在 Fargate 容器上挂载 Amazon EFS 文件系统。最后,在 Amazon Elastic Compute Cloud(Amazon EC2)实例上安装相同的文件系统以获取 SSM Agent 日志。
**重要事项:**在挂载 EFS 文件系统之前,必须启动一个 Amazon EC2 实例。您的 Amazon EFS 文件系统、Amazon ECS 集群和 Fargate 任务必须全部位于同一个 Amazon Virtual Private Cloud(Amazon VPC)中。
解决方法
**注意:**以下解决方法仅适用于已激活 ECS Exec 的 Fargate 任务。仅使用解决步骤进行调试。为了确保不会覆盖日志,请将该任务作为独立任务启动。或者,在 Amazon ECS 服务中将任务的 desiredCount 保持为“1”。对于必须从容器中检查非 stderr/stdout 日志的情况,还可以使用以下解决方法。
创建 Amazon EFS 文件系统并将其挂载到 Fargate 容器上
要创建 Amazon EFS 文件系统并将其挂载到任务或服务中的 Fargate 容器上,请完成下面的步骤:
- 创建 Amazon EFS 文件系统。
- 记下 Amazon EFS ID 和安全组 ID。
- 编辑文件系统安全组规则,以允许通过端口 2049 从与 Fargate 任务关联的安全组进行入站连接。
- 更新 Amazon ECS 安全组,以允许通过端口 2049 与文件系统的安全组建立出站连接。
- 打开 Amazon ECS 控制台。
- 在导航窗格中,选择任务定义,然后选择任务定义。
- 选择创建新修订。
- 在存储下,对于卷,选择添加卷。然后,输入下面的信息
对于卷名,输入卷的名称。
对于卷类型,选择 EFS。
对于文件系统 ID,输入文件系统的 ID。 - 对于容器挂载点,选择添加挂载点。然后,输入下面的信息:
对于容器,选择您的容器。
对于源卷,选择源卷。
对于容器路径,输入 /var/log/amazon。 - 选择创建。
注意:在将 Amazon EFS 文件系统挂载到 Amazon EC2 实例之前,任务必须处于正在运行状态。
在 Amazon EC2 实例上挂载 Amazon EFS 文件系统并获取 SSM Agent 日志
**注意:**如果在运行 AWS 命令行界面(AWS CLI)命令时收到错误,请参阅 Troubleshoot AWS CLI errors。此外,应确保您使用的是最新版本的 AWS CLI。
完成下面的步骤:
-
要获取日志数据,请运行下面的 AWS CLI 命令:
sudo mount -t nfs4 -o nfsvers=4.1,rsize=1048576,wsize=1048576,hard,timeo=600,retrans=2,noresvport fs-01b0bxxxxxxxx.efs.ap-southeast-1.amazonaws.com:/ /efs
输出示例:
# df -h Filesystem Size Used Avail Use% Mounted on fs-01b0bxxxxxxxx.efs.us-west-2.amazonaws.com:/ 8.0E 0 8.0E 0% /efs
下面是 Fargate 容器的路径 /var/log/amazon/ssm/amazon-ssm-agent.log 中存储的日志的示例:
[root@ip-172-31-32-32 efs]# cd ssm/ [root@ip-172-31-32-32 ssm]# ls amazon-ssm-agent.log audits [root@ip-172-31-32-32 ssm]# cat amazon-ssm-agent.log | tail -n 10 2022-10-20 11:50:34 INFO [ssm-agent-worker] [MessageService] [MessageHandler] ended idempotency deletion thread 2022-10-20 11:50:37 INFO [ssm-agent-worker] [MessageService] [MGSInteractor] send failed reply thread started 2022-10-20 11:50:37 INFO [ssm-agent-worker] [MessageService] [MGSInteractor] send failed reply thread done 2022-10-20 11:55:37 INFO [ssm-agent-worker] [MessageService] [MGSInteractor] send failed reply thread started 2022-10-20 11:55:37 INFO [ssm-agent-worker] [MessageService] [MGSInteractor] send failed reply thread done 2022-10-20 12:00:34 INFO [ssm-agent-worker] [MessageService] [MessageHandler] started idempotency deletion thread 2022-10-20 12:00:34 WARN [ssm-agent-worker] [MessageService] [MessageHandler] [Idempotency] encountered error open /var/lib/amazon/ssm/170b15cacf5846ed836bcd7903cbee48-2531612879/idempotency: no such file or directory while listing replies in /var/lib/amazon/ssm/170b15cacf5846ed836bcd7903cbee48-2531612879/idempotency 2022-10-20 12:00:34 INFO [ssm-agent-worker] [MessageService] [MessageHandler] ended idempotency deletion thread 2022-10-20 12:00:37 INFO [ssm-agent-worker] [MessageService] [MGSInteractor] send failed reply thread started 2022-10-20 12:00:37 INFO [ssm-agent-worker] [MessageService] [MGSInteractor] send failed reply thread done [root@ip-172-31-32-32 ssm]#