示例任务定义 - Amazon Elastic Container Service
AWS 文档中描述的 AWS 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅中国的 AWS 服务入门

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

示例任务定义

以下是一些演示常用自定义日志路由选项的示例任务定义。有关更多示例,请参阅上Amazon ECSFireLens的示例GitHub。

将日志转发到 CloudWatch Logs

注意

有关更多示例,请参阅上Amazon ECSFireLens的示例GitHub。

以下任务定义示例演示如何指定用于将日志转发到 CloudWatch Logs 日志组的日志配置。有关更多信息,请参阅 中的什么是 Amazon CloudWatch Amazon CloudWatch Logs User GuideLogs?

在日志配置选项中,指定日志组名称及其所在的区域。要让 Fluent Bit 代表您创建日志组,请指定 "auto_create_group":"true"。 您还可以将任务 ID 指定为日志流前缀,这有助于进行筛选。有关更多信息,请参阅适用于 日志CloudWatch的 Fluent Bit 插件。

{ "family": "firelens-example-cloudwatch", "taskRoleArn": "arn:aws:iam::123456789012:role/ecs_task_iam_role", "containerDefinitions": [ { "essential": true, "image": "906394416424.dkr.ecr.us-west-2.amazonaws.com/aws-for-fluent-bit:latest", "name": "log_router", "firelensConfiguration": { "type": "fluentbit" }, "logConfiguration": { "logDriver": "awslogs", "options": { "awslogs-group": "firelens-container", "awslogs-region": "us-west-2", "awslogs-create-group": "true", "awslogs-stream-prefix": "firelens" } }, "memoryReservation": 50 }, { "essential": true, "image": "nginx", "name": "app", "logConfiguration": { "logDriver":"awsfirelens", "options": { "Name": "cloudwatch", "region": "us-west-2", "log_key": "log", "log_group_name": "/aws/ecs/containerinsights/$(ecs_cluster)/application", "auto_create_group": "true", "log_stream_prefix": "log_stream_name" } }, "memoryReservation": 100 } ] }

将日志转发到 Amazon Kinesis Data Firehose 传输流

注意

有关更多示例,请参阅上Amazon ECSFireLens的示例GitHub。

以下任务定义示例演示如何指定用于将日志转发到 Amazon Kinesis Data Firehose 传输流的日志配置。Kinesis Data Firehose 传输流必须已存在。有关更多信息,请参阅 中的创建 Amazon Kinesis Data Firehose Amazon Kinesis Data Firehose 开发人员指南传输流

在日志配置选项中,指定传输流名称及其所在的区域。有关更多信息,请参阅适用于 Amazon Kinesis Firehose 的 Fluent Bit 插件.

{ "family": "firelens-example-firehose", "taskRoleArn": "arn:aws:iam::123456789012:role/ecs_task_iam_role", "containerDefinitions": [ { "essential": true, "image": "906394416424.dkr.ecr.us-west-2.amazonaws.com/aws-for-fluent-bit:latest", "name": "log_router", "firelensConfiguration": { "type": "fluentbit" }, "logConfiguration": { "logDriver": "awslogs", "options": { "awslogs-group": "firelens-container", "awslogs-region": "us-west-2", "awslogs-create-group": "true", "awslogs-stream-prefix": "firelens" } }, "memoryReservation": 50 }, { "essential": true, "image": "httpd", "name": "app", "logConfiguration": { "logDriver":"awsfirelens", "options": { "Name": "firehose", "region": "us-west-2", "delivery_stream": "my-stream" } }, "memoryReservation": 100 } ] }

将日志转发到 Amazon Elasticsearch Service 域

注意

有关更多示例,请参阅上Amazon ECSFireLens的示例GitHub。

以下任务定义示例演示如何指定将日志转发到 Amazon Elasticsearch Service 域的日志配置。Amazon Elasticsearch Service 域必须已存在。有关更多信息,请参阅 Amazon Elasticsearch Service 中的什么是 Amazon Elasticsearch Service 开发人员指南.

在日志配置选项中,指定 Elasticsearch 集成所需的日志选项。有关更多信息,请参阅适用于 Amazon Elasticsearch Service的 Fluent Bit。

{ "family": "firelens-example-elasticsearch", "taskRoleArn": "arn:aws:iam::123456789012:role/ecs_task_iam_role", "containerDefinitions": [ { "essential": true, "image": "906394416424.dkr.ecr.us-west-2.amazonaws.com/aws-for-fluent-bit:latest", "name": "log_router", "firelensConfiguration": { "type": "fluentbit" }, "logConfiguration": { "logDriver": "awslogs", "options": { "awslogs-group": "firelens-container", "awslogs-region": "us-west-2", "awslogs-create-group": "true", "awslogs-stream-prefix": "firelens" } }, "memoryReservation": 50 }, { "essential": true, "image": "httpd", "name": "app", "logConfiguration": { "logDriver": "awsfirelens", "options": { "Name": "es", "Host": "vpc-fake-domain-ke7thhzo07jawrhmz6mb7ite7y.us-west-2.es.amazonaws.com", "Port": "443", "Index": "my_index", "Type": "my_type", "AWS_Auth": "On", "AWS_Region": "us-west-2", "tls": "On" } }, "memoryReservation": 100 } ] }

解析序列化 JSON 的容器日志

注意

有关更多示例,请参阅上Amazon ECSFireLens的示例GitHub。

从 AWS for Fluent Bit 版本 1.3 开始,AWS for Fluent Bit 映像中包含了 JSON 解析程序。以下示例说明如何在任务定义的FireLens配置中引用 JSON 解析程序。

"firelensConfiguration": { "type": "fluentbit", "options": { "config-file-type": "file", "config-file-value": "/fluent-bit/configs/parse-json.conf" } },

Fluent Bit 配置文件将解析 JSON 格式的任何日志。例如,如果目标处的日志如下所示而不进行 JSON 解析:

{ "source": "stdout", "log": "{\"requestID\": \"b5d716fca19a4252ad90e7b8ec7cc8d2\", \"requestInfo\": {\"ipAddress\": \"204.16.5.19\", \"path\": \"/activate\", \"user\": \"TheDoctor\"}}", "container_id": "e54cccfac2b87417f71877907f67879068420042828067ae0867e60a63529d35", "container_name": "/ecs-demo-6-container2-a4eafbb3d4c7f1e16e00" "ecs_cluster": "mycluster", "ecs_task_arn": "arn:aws:ecs:us-east-2:01234567891011:task/mycluster/3de392df-6bfa-470b-97ed-aa6f482cd7a6", "ecs_task_definition": "demo:7" "ec2_instance_id": "i-06bc83dbc2ac2fdf8" }

借助 JSON 解析,日志将如下所示:

{ "source": "stdout", "container_id": "e54cccfac2b87417f71877907f67879068420042828067ae0867e60a63529d35", "container_name": "/ecs-demo-6-container2-a4eafbb3d4c7f1e16e00" "ecs_cluster": "mycluster", "ecs_task_arn": "arn:aws:ecs:us-east-2:01234567891011:task/mycluster/3de392df-6bfa-470b-97ed-aa6f482cd7a6", "ecs_task_definition": "demo:7" "ec2_instance_id": "i-06bc83dbc2ac2fdf8" "requestID": "b5d716fca19a4252ad90e7b8ec7cc8d2", "requestInfo": { "ipAddress": "204.16.5.19", "path": "/activate", "user": "TheDoctor" } }

序列化的 JSON 将扩展到最终 JSON 输出中的顶级字段。有关 JSON 解析的更多信息,请参阅 Fluent Bit 文档中的分析器

转发到外部 Fluentd 或 Fluent Bit

注意

有关更多示例,请参阅上Amazon ECSFireLens的示例GitHub。

以下任务定义示例演示如何指定用于将日志转发到外部 Fluentd 或 Fluent Bit 机的日志配置。host 为您的环境指定 port 和 。

{ "family": "firelens-example-forward", "taskRoleArn": "arn:aws:iam::123456789012:role/ecs_task_iam_role", "containerDefinitions": [ { "essential": true, "image": "906394416424.dkr.ecr.us-west-2.amazonaws.com/aws-for-fluent-bit:latest", "name": "log_router", "firelensConfiguration": { "type": "fluentbit" }, "logConfiguration": { "logDriver": "awslogs", "options": { "awslogs-group": "firelens-container", "awslogs-region": "us-west-2", "awslogs-create-group": "true", "awslogs-stream-prefix": "firelens" } }, "memoryReservation": 50 }, { "essential": true, "image": "httpd", "name": "app", "logConfiguration": { "logDriver":"awsfirelens", "options": { "Name": "forward", "Host": "fluentdhost", "Port": "24224" } }, "memoryReservation": 100 } ] }