要解决 AWS ECS Fargate 使用 Firelens sidecar 容器方法将应用程序容器中存在的日志文件推送到 CloudWatch 遇到的问题,可以按照以下步骤操作:
确保您已经按照 AWS 官方文档设置好 CloudWatch 日志记录。确保您分配了 Task iam 角色,该角色具有访问 CloudWatch 日志记录所需的最小权限。
确保您在 ECS task 定义文件中正确配置了 Firelens sidecar 容器。例如:
"containerDefinitions": [
{
"name": "myapp-container",
"image": "myapp:latest",
"cpu": 256,
"memory": 512,
"essential": true,
"logConfiguration": {
"logDriver": "awslogs",
"options": {
"awslogs-group": "/ecs/myapp",
"awslogs-region": "us-east-1",
"awslogs-stream-prefix": "myapp"
}
}
},
{
"name": "firelens",
"image": "aws-for-fluent-bit:latest",
"essential": true,
"firelensConfiguration": {
"type": "fluentbit",
"options": {
"config-file-type": "s3",
"config-file-value": "s3://mybucket/fluent-bit.conf"
}
}
}
]
在上述 ECS Task 定义文件中,我们的应用程序容器名称为 "myapp-container",我们为其配置了 CloudWatch 日志记录。我们使用 Firelens sidecar 容器,将位于 S3 存储桶中的 fluent-bit 配置文件加载到它的容器中。
修改 fluent-bit 配置文件以统一管理主、副容器的日志记录配置。例如: