Get Hands-on with Amazon EKS - Workshop Event Series
Whether you're taking your first steps with Kubernetes or you're an experienced practitioner looking to sharpen your skills, our Amazon EKS workshop series delivers practical, real-world experience that moves you forward. Learn directly from AWS solutions architects and EKS specialists through hands-on sessions designed to build your confidence with Kubernetes. Register now and start building with Amazon EKS!
我該如何將容器日誌同時傳送到 AWS Fargate 上 Amazon ECS 中的多個目的地?
我希望在 AWS Fargate 上執行的應用程式容器能將日誌轉送到多個目的地,例如 Amazon CloudWatch、Amazon Data Firehose 或 Splunk。
簡短說明
Amazon Elastic Container Service (Amazon ECS) 任務定義可讓您為特定容器指定單一的日誌組態物件。此限制表示您只能將日誌轉送到一個目的地。若要在 Amazon ECS 的 Fargate 上將日誌轉送至多個目的地,您可以使用 FireLens。
**注意:**FireLens 可與 Fluent Bit 和 Fluentd 日誌轉送工具搭配使用。此解決方法使用 Fluent Bit,因為 Fluent Bit 比 Fluentd 更節省資源。
解決方法
**注意:**如果您在執行 AWS Command Line Interface (AWS CLI) 命令時收到錯誤,請參閱AWS CLI 錯誤疑難排解。此外,請確定您使用的是最新的 AWS CLI 版本。
在開始前,請先確認您已了解下列事項:
- 為了產生 Fluent Bit 輸出定義,FireLens 會使用 ECS 任務定義中 logConfiguration 物件內指定為選項的索引鍵值組。FireLens 將日誌路由至的目的地,會在 Fluent Bit 組態檔中的 [OUTPUT] 定義區段中指定。如需詳細資訊,請參閱 Fluent Bit 網站上的輸出。若您未指定任何選項,系統將不會產生任何輸出。
- FireLens 會代您建立組態檔,但您也可以指定自訂組態檔。您可以在 Amazon Simple Storage Service (Amazon S3) 中託管此組態檔案。或者,建立一個自訂的 Fluent Bit Docker 映像,並將自訂輸出組態檔加入該映像中。
- 除非您需要 Fluent Bit 的自訂外掛程式,否則您可以使用 Amazon ECS 與 Fargate 從 Amazon S3 提取自訂組態檔。
建立 Fluent Bit 自訂輸出組態檔並將其上傳至 Amazon S3
建立名為 logDestinations.conf 的自訂 Fluent Bit 組態檔,並加入您選擇的 [OUTPUT] 定義。例如,該組態檔包含為 CloudWatch、Amazon Data Firehose 與 Splunk 定義的組態。Match 指示詞會路由您的資料。當 Amazon ECS 填入 Fluent Bit 組態時,會以容器名稱作為首碼,為輸入串流加上自動產生的標記。範例中使用 service 作為首碼:
[OUTPUT] Name firehose Match service* region us-west-2 delivery_stream nginx-stream [OUTPUT] Name cloudwatch Match service* region us-east-1 log_group_name firelens-nginx-container log_stream_prefix from-fluent-bit auto_create_group true [OUTPUT] Name splunk Match service* Host 127.0.0.1 Splunk_Token xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx Splunk_Send_Raw On
**注意:**不同的目的地需要在 [OUTPUT] 定義中指定不同的欄位。如需範例,請參閱 GitHub 網站上的 amazon-ecs-firelens-examples。
若要將此檔案上傳至您控制的 Amazon S3 儲存貯體,請執行 AWS CLI cp 命令:
aws s3 cp logDestinations.conf s3://example-bucket/logDestinations.conf
建立 IAM 權限
建立 AWS Identity and Access Management (IAM) 權限,以允許您的任務角色取得 S3 組態檔。允許您的任務角色將日誌路由至不同的目的地。例如,若您的目的地是 Amazon Data Firehose,則您必須授與任務呼叫 firehose:PutRecordBatch API 的權限。
**注意:**Fluent Bit 支援將外掛程式作為日誌目的地。CloudWatch 與 Kinesis 等目的地需要包含 logs:CreateLogGroup、logs:CreateLogStream、logs:DescribeLogStreams、logs:PutLogEvents 以及 kinesis:PutRecords 的權限。如需詳細資訊,請參閱 GitHub 網站上的 CloudWatch 與 Kinesis 權限。
在範例 IAM 政策中,任務角色授與組態檔的 s3:GetObject 存取權。最佳實務是授與任務必要的權限,使其能夠將任務本身的輸出記錄到另一個 CloudWatch 日誌群組。
{ "Version": "2012-10-17", "Statement": [ { "Sid": "ConfigurationFileAccess", "Action": [ "s3:GetObject" ], "Effect": "Allow", "Resource": "arn:aws:s3:::example-bucket/logDestinations.conf" }, { "Sid": "CloudWatchLogGroupPermissions", "Action": [ "logs:CreateLogGroup", "logs:CreateLogStream" ], "Effect": "Allow", "Resource": "arn:aws:logs:us-east-1:555555555555:log-group:firelens-log-router" }, { "Sid": "CloudWatchLogStreamPermissions", "Action": [ "logs:CreateLogStream", "logs:PutLogEvents" ], "Effect": "Allow", "Resource": "arn:aws:logs:us-east-1:555555555555:log-group:firelens-log-router:log-stream:*" } ] }
**注意:**如需關於如何建立任務 IAM 角色與所需權限的詳細資訊,請參閱 Amazon ECS 任務執行 IAM 角色。
範例 IAM 任務角色授與範例目的地中使用的 CloudWatch 與 Amazon Data Firehose 驅動程式所需的權限:
{ "Version": "2012-10-17", "Statement": [ { "Sid": "DataFirehosePermissions", "Action": [ "firehose:PutRecordBatch" ], "Effect": "Allow", "Resource": "arn:aws:firehose:us-west-2:555555555555:deliverystream/nginx-stream" }, { "Sid": "CloudWatchLogGroupPermissions", "Action": [ "logs:CreateLogGroup", "logs:CreateLogStream" ], "Effect": "Allow", "Resource": "arn:aws:logs:us-east-1:555555555555:log-group:firelens-nginx-container" }, { "Sid": "CloudWatchLogStreamPermissions", "Action": [ "logs:CreateLogStream", "logs:PutLogEvents" ], "Effect": "Allow", "Resource": "arn:aws:logs:us-east-1:555555555555:log-group:firelens-nginx-container:log-stream:*" } ] }
建立任務定義
在任務定義中,使用 AWS for Fluent Bit 映像以包含額外的必要容器。此範例會執行官方的 nginx 容器。使用 AWS CLI 或 Amazon ECS 主控台來建立完整的任務定義:
{ "family": "firelens-example-task", "taskRoleArn": "arn:aws:iam::012345678901:role/exampleTaskRole", "taskExecutionRoleArn": "arn:aws:iam::444455556666:role/exampleTaskExecutionRole", "networkMode": "awsvpc", "requiresCompatibilities": ["FARGATE"], "cpu": "256", "memory": "512", "containerDefinitions": [ { "essential": true, "image": "public.ecr.aws/docker/library/nginx:latest", "name": "service_web_app", "logConfiguration": { "logDriver": "awsfirelens" } }, { "essential": true, "image": "public.ecr.aws/aws-observability/aws-for-fluent-bit:stable", "name": "log_router", "logConfiguration": { "logDriver": "awslogs", "options": { "awslogs-group": "firelens-log-router", "awslogs-region": "us-east-1", "awslogs-create-group": "true", "awslogs-stream-prefix": "firelens-router-logs" } }, "firelensConfiguration": { "type": "fluentbit", "options": { "config-file-type": "s3", "config-file-value": "arn:aws:s3:::example-bucket/logDestinations.conf" } } } ] }
**注意:**為了對問題進行疑難排解,範例組態會將 Fluent Bit 容器的輸出導向另一個具有不同首碼的 CloudWatch 日誌群組。
- 語言
- 中文 (繁體)
