【问题标题】:How to troubleshoot logging to cloudwatch from docker in an ECS container?如何解决从 ECS 容器中的 docker 到 cloudwatch 的日志记录问题?
【发布时间】:2018-12-02 21:07:12
【问题描述】:

我正在为此撕毁我的头发。 这是我的设置:

  • EC2 实例上的 ECS 容器包含一个
  • 运行的 ECS 任务定义
  • 带有 python 脚本的 Docker 实例记录到标准错误

我看到创建了 ECS 任务的 Cloudwatch 日志组,但没有出现我打印到 stderr 的任何内容。

我的 ECS 容器具有默认的 ecsInstanceRole。 ecsInstanceRole 的 AmazonEC2ContainerServiceforEC2Role 策略如下:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "ecs:CreateCluster",
                "ecs:DeregisterContainerInstance",
                "ecs:DiscoverPollEndpoint",
                "ecs:Poll",
                "ecs:RegisterContainerInstance",
                "ecs:StartTelemetrySession",
                "ecs:UpdateContainerInstancesState",
                "ecs:Submit*",
                "ecr:GetAuthorizationToken",
                "ecr:BatchCheckLayerAvailability",
                "ecr:GetDownloadUrlForLayer",
                "ecr:BatchGetImage",
                "logs:CreateLogStream",
                "logs:PutLogEvents"
                ],
                "Resource": "*"
            }
        ]
    }

任务定义

{
    "containerDefinitions": [
        {
            "dnsSearchDomains": [],
            "logConfiguration": {
                "logDriver": "awslogs",
                "options": {
                    "awslogs-group": "torres",
                    "awslogs-region": "us-west-2",
                    "awslogs-stream-prefix": "torres"
                }
            },
            "command": [
                "sleep",
                "infinity"
            ],
            "cpu": 100,
            "mountPoints": [
                {
                    "readOnly": null,
                    "containerPath": "/mnt/efs",
                    "sourceVolume": "efs"
                }
            ],
            "memoryReservation": 512,
            "image": "X.dkr.ecr.us-west-2.amazonaws.com/torres-docker:latest",
            "interactive": true,
            "essential": true,
            "pseudoTerminal": true,
            "readonlyRootFilesystem": false,
            "privileged": false,
            "name": "torres"
        }
    ],
    "family": "torres",
    "volumes": [
        {
            "name": "efs",
            "host": {
                "sourcePath": "/mnt/efs"
            }
        }
    ]
}

【问题讨论】:

  • 您能否也分享您的任务任务定义详细信息?您是否确保 ECS 代理启用了 awslogs 驱动程序。 ECS_AVAILABLE_LOGGING_DRIVERS=["json-file","awslogs"] 。另外,请确保您的应用程序(python)正在登录到控制台。
  • 感谢伊姆兰的回复。我添加了任务定义。该应用程序正在登录到控制台,ECS 代理是 1.22 版,因此它不需要按照这里的配置 docs.aws.amazon.com/AmazonECS/latest/developerguide/…

标签: amazon-cloudwatch amazon-ecs amazon-cloudwatchlogs


【解决方案1】:

我发现了这个问题,但我不知道为什么它可以解决它。 当我将命令从sleep infinity 更改为只运行一段记录python script.py 的代码时,CloudWatch 中的日志记录开始工作。

在我使用 sleep infinity ssh-ing 启动容器并从 shell 启动 script.py 之前,这没有记录。

【讨论】:

  • 我的猜测是您的程序在sleep infinity 之前写入的输出只是位于缓冲区中并且从未被写出。您可以在任务定义的环境部分尝试PYTHONUNBUFFERED=1
猜你喜欢
  • 2019-07-06
  • 1970-01-01
  • 1970-01-01
  • 2019-12-01
  • 2017-08-14
  • 2023-03-23
  • 2020-10-23
  • 2019-03-27
  • 1970-01-01
相关资源
最近更新 更多