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

示例任务定义

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

将日志转发到 CloudWatch Logs

注意

有关更多示例,请参阅 GitHub 上的 Amazon ECS FireLens 示例

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

在日志配置选项中,指定日志组名称及其所在的Amazon Web Services 区域。要让 Fluent Bit 代表您创建日志组,请指定 "auto_create_group":"true",以设置 fluentd-buffer-limit 使用 log-driver-buffer-limit。您还可以将任务 ID 指定为有助于筛选的日志流前缀。有关更多信息,请参阅适用于 CloudWatch Logs 的 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": "httpd", "name": "app", "logConfiguration": { "logDriver":"awsfirelens", "options": { "Name": "cloudwatch", "region": "us-west-2", "log_group_name": "firelens-blog", "auto_create_group": "true", "log_stream_prefix": "from-fluent-bit", "log-driver-buffer-limit": "2097152" } }, "memoryReservation": 100 } ] }

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

注意

有关更多示例,请参阅 GitHub 上的 Amazon ECS FireLens 示例

以下任务定义示例演示如何指定用于将日志转发到 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:stable", "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 OpenSearch Service 域

注意

有关更多示例,请参阅 GitHub 上的 Amazon ECS FireLens 示例

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

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

{ "family": "firelens-example-opensearch", "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:stable", "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 的容器日志

注意

有关更多示例,请参阅 GitHub 上的 Amazon ECS FireLens 示例

从 Amazon for Fluent Bit 版本 1.3 开始,Amazon 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

注意

有关更多示例,请参阅 GitHub 上的 Amazon ECS FireLens 示例

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

{ "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:stable", "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 } ] }