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

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

示例任务定义

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

将日志转发到CloudWatch Logs

注意

有关更多示例,请参阅Amazon ECSFireLens 示例(位于 GitHub 上)。

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

在日志配置选项中,指定日志组名称及其所在的区域。要让 Fluent Bit 代表您创建日志组,请指定 "auto_create_group":"true"。您还可以将任务 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": "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 集成所需的日志选项。有关更多信息,请参阅 。适用于的 Fluent BitAmazon Elasticsearch Service

{ "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对于流利位版本 1.3,有一个 JSON 解析器包含在AWS用于流利位图像。以下示例说明如何在任务定义的 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 解析的详细信息,请参阅分析器在流利位文档中。

转发到外部 Fluentd 或 Fluent Bit

注意

有关更多示例,请参阅Amazon ECSFireLens 示例(位于 GitHub 上)。

以下任务定义示例演示如何指定用于将日志转发到外部 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: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 } ] }