如何解决从ECS容器中的Docker日志记录到Cloudwatch的问题?

时间:2018-12-02 21:07:13

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

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

    EC2实例上的
  • ECS容器包含
  • 运行ECS的ECS任务定义
  • 带有记录到stderr的python脚本的Docker实例

我看到创建ECS任务的Cloudwatch日志组,但没有打印到stderr的内容。 Cloudwatch log group

我的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"
            }
        }
    ]
}

1 个答案:

答案 0 :(得分:0)

我知道了问题所在,但不知道为什么能解决。 当我将命令从sleep infinity更改为仅运行一段记录python script.py的代码时,CloudWatch中的日志记录开始工作。

在我将sleep infinity ssh-sing到容器中并从shell中启动script.py启动容器之前,这不是日志记录。