

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

# 使用 Firelens 日志路由器为 Amazon ECS 创建自定义日志解析器
<a name="create-a-custom-log-parser-for-amazon-ecs-using-a-firelens-log-router"></a>

*Varun Sharma，Amazon Web Services*

## Summary
<a name="create-a-custom-log-parser-for-amazon-ecs-using-a-firelens-log-router-summary"></a>

Firelens 是适用于 Amazon Elastic Container Service （Amazon ECS）和 AWS Fargate 的日志路由器。您可以使用 Firelens 将容器日志从 Amazon ECS 路由到亚马逊 CloudWatch 和其他目的地（例如 [Splunk](https://www.splunk.com/) 或 S [um](https://www.sumologic.com/) o Logic）。Firelens 使用 [Fluentd](https://www.fluentd.org/) 或 [Fluent Bit](https://fluentbit.io/) 作为日志记录代理，这意味着您可以使用 [Amazon ECS 任务定义参数](https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task_definition_parameters.html)来路由日志。

通过选择在源级别解析日志，您可以分析您的日志记录数据并执行查询，从而更加高效和有效地响应操作问题。由于不同的应用程序具有不同的日志模式，因此您需要使用自定义解析器来构建日志，以便在最终目的地更轻松地进行搜索。

此模式使用带有自定义解析器的 Firelens 日志路由器 CloudWatch 从 Amazon ECS 上运行的示例 Spring Boot 应用程序推送日志。然后，您可以使用 Amazon CloudWatch Logs Insights 根据自定义解析器生成的自定义字段筛选日志。

## 先决条件和限制
<a name="create-a-custom-log-parser-for-amazon-ecs-using-a-firelens-log-router-prereqs"></a>

**先决条件**
+ 活跃 Amazon Web Services (AWS) account
+ AWS 命令行界面（AWS CLI）已在本地计算机上安装和配置。
+ 已在本地计算机上安装并配置的 Docker。
+ Amazon Elastic Container Registry (Amazon ECR) 上现有的基于 Spring Boot 的容器化应用程序。 

## 架构
<a name="create-a-custom-log-parser-for-amazon-ecs-using-a-firelens-log-router-architecture"></a>

![\[使用 Firelens 日志路由器将日志 CloudWatch 从 Amazon ECS 上运行的应用程序推送到。\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/images/pattern-img/e82b4992-c4e0-4af5-b87e-cb0b1c1ed8c9/images/ef60e087-965a-40e9-9f80-35edbda2befe.png)


**技术堆栈**
+ CloudWatch
+ Amazon ECR
+ Amazon ECS
+ Fargate
+ Docker
+ Fluent Bit

## 工具
<a name="create-a-custom-log-parser-for-amazon-ecs-using-a-firelens-log-router-tools"></a>
+ [Amazon ECR](https://docs.aws.amazon.com/AmazonECR/latest/userguide/what-is-ecr.html) - Amazon Elastic Container Registry (Amazon ECR) 是一项由 AWS 托管的容器映像注册表服务，该服务安全可靠，且可扩展。
+ [Amazon ECS](https://docs.aws.amazon.com/AmazonECS/latest/developerguide/Welcome.html) - Amazon Elastic Container Service (Amazon ECS) 是一项高度可扩展的快速容器管理服务，可轻松在集群上运行、停止和管理容器。
+ [AWS 身份识别和访问管理(IAM)](https://docs.aws.amazon.com/IAM/latest/UserGuide/introduction.html) - IAM 是一项 Web 服务，用于安全控制 Amazon Web Services 的访问。
+ [AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-welcome.html) - AWS 命令行界面（AWS CLI）是一种开源工具，可让您使用命令行 Shell 中的命令与 Amazon Web Services 交互。
+ [Docker](https://www.docker.com/) - Docker 是用于开发、发布和运行应用程序的开放平台。

**代码**

此模式附加了以下文件：
+ `customFluentBit.zip` - 包含用于添加自定义解析和配置的文件。
+ `firelens_policy.json` - 包含用于创建 IAM policy 的策略文档。
+ `Task.json` - 包含 Amazon ECS 的示例任务定义。

## 操作说明
<a name="create-a-custom-log-parser-for-amazon-ecs-using-a-firelens-log-router-epics"></a>

### 创建自定义 Fluent Bit 映像
<a name="create-a-custom-fluent-bit-image"></a>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 创建 Amazon ECR 存储库。 | 登录 AWS 管理控制台，打开 Amazon ECR 控制台，然后创建一个名为 `fluentbit_custom` 的存储库。有关此内容的更多信息，请参阅 Amazon ECR 文档中的[创建存储库](https://docs.aws.amazon.com/AmazonECR/latest/userguide/repository-create.html)。 | 系统管理员、开发人员 | 
| 解压 customFluentBit .zip 压缩包。 |  [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/create-a-custom-log-parser-for-amazon-ecs-using-a-firelens-log-router.html) |  | 
| 创建自定义 Docker 映像。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/create-a-custom-log-parser-for-amazon-ecs-using-a-firelens-log-router.html)有关更多信息，请参阅 Amazon ECR 文档中的[推送 Docker 映像](https://docs.aws.amazon.com/AmazonECR/latest/userguide/docker-push-ecr-image.html)。  | 系统管理员、开发人员 | 

### 设置 Amazon ECS 集群
<a name="set-up-the-amazon-ecs-cluster"></a>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 创建 Amazon ECS 集群。 | 按照 Amazon ECS 文档中[创建集群](https://docs.aws.amazon.com/AmazonECS/latest/developerguide/create_cluster.html)的*仅联网模板*部分中的说明创建 Amazon ECS 集群。请确认选择**创建 VPC** 来为您的 Amazon ECS 集群创建新的虚拟私有云（VPC）。 | 系统管理员、开发人员 | 

### 设置 Amazon ECS 任务
<a name="set-up-the-amazon-ecs-task"></a>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
|  设置 Amazon ECS 任务执行 IAM 角色。 | 使用 `AmazonECSTaskExecutionRolePolicy` 托管策略创建 Amazon ECS 任务执行 IAM 角色。有关此内容的更多信息，请参阅 Amazon ECS 文档中的 [Amazon ECS 任务执行 IAM 角色](https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task_execution_IAM_role.html)。请确认记录 IAM 角色的 Amazon 资源名称（ARN）。 | 系统管理员、开发人员 | 
|  将 IAM policy 附加到 Amazon ECS 任务执行 IAM 角色。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/create-a-custom-log-parser-for-amazon-ecs-using-a-firelens-log-router.html) | 系统管理员、开发人员 | 
| 设置 Amazon ECS 任务定义。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/create-a-custom-log-parser-for-amazon-ecs-using-a-firelens-log-router.html)有关此内容的更多信息，请参阅 Amazon ECS 文档中的[创建任务定义](https://docs.aws.amazon.com/AmazonECS/latest/developerguide/create-task-definition.html)。 | 系统管理员、开发人员 | 

### 运行 Amazon ECS 任务
<a name="run-the-amazon-ecs-task"></a>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 运行 Amazon ECS 任务。 | 在 Amazon ECS 控制台上，选择**集群**，选择您之前创建的集群，然后运行独立任务。有关此内容的更多信息，请参阅 Amazon ECS 文档中的[运行独立任务](https://docs.aws.amazon.com/AmazonECS/latest/developerguide/ecs_run_task.html)。 | 系统管理员、开发人员 | 

### 验证日 CloudWatch 志
<a name="verify-the-cloudwatch-logs"></a>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 验证日志。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/create-a-custom-log-parser-for-amazon-ecs-using-a-firelens-log-router.html) | 系统管理员、开发人员 | 

## 相关资源
<a name="create-a-custom-log-parser-for-amazon-ecs-using-a-firelens-log-router-resources"></a>
+ [Amazon ECS 的 Docker 基本信息](https://docs.aws.amazon.com/AmazonECS/latest/developerguide/docker-basics.html) 
+ [AWS Fargate 上的 Amazon ECS](https://docs.aws.amazon.com/AmazonECS/latest/developerguide/AWS_Fargate.html) 
+ [配置基本服务参数](https://docs.aws.amazon.com/AmazonECS/latest/developerguide/basic-service-params.html) 

## 附件
<a name="attachments-e82b4992-c4e0-4af5-b87e-cb0b1c1ed8c9"></a>

要访问与此文档相关联的其他内容，请解压以下文件：[attachment.zip](samples/p-attach/e82b4992-c4e0-4af5-b87e-cb0b1c1ed8c9/attachments/attachment.zip)