0

我正在使用 AWS Firelens 将日志发送到 Cloudwatch。Firelens 默认将以下字段与日志消息一起发送到 fluentbit 容器。

{
    "container_id": "asdfasdfasdfadsf",
    "container_name": "/ecs-microservice-ms-233-microservice-ms-e8e2e9e3b3c9e5e02100",
    "ecs_cluster": "arn:aws:ecs:us-east-1:1234:cluster/app-microservices",
    "ecs_task_arn": "arn:aws:ecs:us-east-1:1234:task/asdf",
    "ecs_task_definition": "microservice-ms:233"
}

我想在输出部分做这样的事情,

[OUTPUT]
    Name cloudwatch
    Match **
    region us-east-1
    log_group_name /ecs/${microservice}
    log_stream_prefix ${TAG}
    auto_create_group true

其中,日志组名称应为 ecs 服务名称。但我没有任何带有服务名称的字段。ecs_task_definition 是最接近的,但它具有任务修订号。

是否有可能实现我想要做的事情?

4

1 回答 1

1

Fluentbit 允许我们在输出部分使用环境变量。话虽如此,解决问题的方法是将环境变量传递给 Firelens 容器。

在 AWS ECS 中,这必须从任务定义部分传递,在 firelens sidecar 容器定义中,

    {
        "name": "firelens_log_router",
        "image": "<account_id>.dkr.ecr.us-east-1.amazonaws.com/accel-aws-for-fluent-bit:latest",
        "logConfiguration": {
            "logDriver": "awslogs",
            "options": {
                "awslogs-group": "firelens-container",
                "awslogs-region": "us-east-1",
                "awslogs-create-group": "true",
                "awslogs-stream-prefix": "firelens"
            }
        },
        "memory": 100,
        "memoryReservation": 50,
        "firelensConfiguration": {
            "type": "fluentbit",
            "options": {
                "config-file-type": "file",
                "config-file-value": "/fluent-bit/etc/fluent-bit-accel.conf"
            }
        },
        "environment": [
            {
                "name": "MICROSERVICE_NAME",
                "value": "${microservice_name}"
            }
        ]
    }

然后在 Fluentbit 配置文件的 OUTPUT 部分,

[OUTPUT]
    Name cloudwatch
    Match **
    region us-east-1
    log_group_name /ecs/${MICROSERVICE_NAME}
    log_stream_prefix fluentbit.
    auto_create_group true
于 2020-06-01T08:58:05.773 回答