0

我正在使用 AWS 批处理和 ECS。

ECS 任务可以在任务定义中配置为使用 awslogs logDriver 并将日志发送到 cloudwatch,从而防止它们占用 EC2 实例中的空间。但是 ECS 容器代理本身也存在于 docker 容器中,并且它的所有 docker 容器日志都存储在 EC2 实例中,这很快就会填满内存。无论如何要为 ECS 容器代理本身设置 logDriver 吗?

ECS代理也将日志存储在/var/log/ecs/ecs-agent.log.timestamp中,这也占用了大量空间。知道如何将它们重定向到 cloudwatch 吗?

4

1 回答 1

0

您可以使用小脚本UserData(作为 LaunchConfiguration 的一部分)来安装awslogs和配置相同的内容。请在示例代码段下方找到。

        # Install awslogs and the jq JSON parser
        yum install -y awslogs jq

        # Inject the CloudWatch Logs configuration file contents
        cat > /etc/awslogs/awslogs.conf <<- ''EOF''
        [general]
        state_file = /var/lib/awslogs/agent-state

        [/var/log/dmesg]
        file = /var/log/dmesg
        log_group_name = ${EnvName}-${EnvNumber}#ecs#dmesg
        log_stream_name = {cluster}/{container_instance_id}

        [/var/log/messages]
        file = /var/log/messages
        log_group_name = ${EnvName}-${EnvNumber}#ecs#messages
        log_stream_name = {cluster}/{container_instance_id}

        datetime_format = %b %d %H:%M:%S

        [/var/log/docker]
        file = /var/log/docker
        log_group_name = ${EnvName}-${EnvNumber}#ecs#docker
        log_stream_name = {cluster}/{container_instance_id}
        datetime_format = %Y-%m-%dT%H:%M:%S.%f

        [/var/log/ecs/ecs-init.log]
        file = /var/log/ecs/ecs-init.log.*
        log_group_name = ${EnvName}-${EnvNumber}#ecs#ecs-init.log
        log_stream_name = {cluster}/{container_instance_id}
        datetime_format = %Y-%m-%dT%H:%M:%SZ

        [/var/log/ecs/ecs-agent.log]
        file = /var/log/ecs/ecs-agent.log.*
        log_group_name = ${EnvName}-${EnvNumber}#ecs#ecs-agent.log
        log_stream_name = {cluster}/{container_instance_id}
        datetime_format = %Y-%m-%dT%H:%M:%SZ
于 2018-09-13T10:38:12.343 回答