docker - ECS容器如何保存日志?

标签 docker amazon-ecs

我正在运行 ECS 任务,最近服务 cpu 达到 100% 然后宕机。

我等待实例稳定下来并进入。 我正在寻找日志,但似乎 docker 容器重新启动并且日志都消失了(cpu 高时的日志)

下次,我如何确保至少可以看到日志来诊断问题?

我有以下内容,希望能在某处看到一些日志(安装在主机中)

     "mountPoints": [
            {
                "readOnly": null,
                "containerPath": "/var/log/uwsgi",
                "sourceVolume": "logs"
            }
        ],

但是主机中没有/var/log/uwsgi。 我可能需要 syslog 和东西..

最佳答案

就您当前的配置而言,logs 完全取决于您在卷部分中定义的路径。

     "mountPoints": [
            {
                "readOnly": null,
                "containerPath": "/var/log/uwsgi",
                "sourceVolume": "logs"
            }
        ],

卷日志 logs 中定义的源路径不是 /var/log/uwsgi,所以你正在挂载

/var/log/uwsgi(容器路径)-> logs 卷(主机路径)。您可以在 logs 卷中定义的路径中找到这些日志。但最好设置类似

            {
                "readOnly": null,
                "containerPath": "/var/log/uwsgi",
                "sourceVolume": "volume_name"
            }

然后音量配置

"volumes": [
    {
      "name": "logs",
      "host": {
        "sourcePath": "/home/ec2-user/logs"
      }
    }
]

来自文档

In the task definition volumes section, define a bind mount with name and sourcePath values.

  "volumes": [
    {
      "name": "webdata",
      "host": {
        "sourcePath": "/ecs/webdata"
      }
    }
  ]

In the containerDefinitions section, define a container with mountPoints values that reference the name of the defined bind mount and the containerPath value to mount the bind mount at on the container.

  "containerDefinitions": [
    {
      "name": "web",
      "image": "nginx",
      "cpu": 99,
      "memory": 100,
      "portMappings": [
        {
          "containerPort": 80,
          "hostPort": 80
        }
      ],
      "essential": true,
      "mountPoints": [
        {
          "sourceVolume": "webdata",
          "containerPath": "/usr/share/nginx/html"
        }
      ]
    }
  ]

bind-mounts-ECS

现在,如果我接受我的建议,我会选择 AWS 日志驱动程序。

在AWS工作,最好的办法是将所有日志推送到CW,但是AWS日志驱动只将容器stdout和stderr日志推送到CW。

使用 AWS 日志驱动程序,您无需担心实例和容器,您将登录 CW 并可以流式传输这些 logs to ELK

          "logConfiguration": {
                "logDriver": "awslogs",
                "options": {
                    "awslogs-group": "awslogs-wordpress",
                    "awslogs-region": "us-west-2",
                    "awslogs-stream-prefix": "awslogs-example"
                }
            }

enter image description here

using_awslogs

关于docker - ECS容器如何保存日志?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59062484/

相关文章:

node.js - prisma 容器中的 AWS ECS 错误 - 环境变量 PRISMA_CONFIG

amazon-web-services - 通过 ECS 运行 docker 镜像是否需要 PM2?

docker - TeamCity - docker-compose - 无法映射 URL

ssh - 如何将 SSH key 安全地传递给 Docker Build?

.net - 使用 AWS ECS 从 Linux 容器上运行的 .NET Core 应用程序访问具有集成安全性的 SQL Server

amazon-web-services - 如何将 ECS 服务注册到另一个堆栈中定义的目标组中

java vm 选项和 AWS::ECS::TaskDefinition

mysql - 构建 docker 容器后运行自定义 SQL 查询

ubuntu - 设备或资源繁忙 - Docker

sql - 将 SQL IDE 连接到 docker db 容器?