如何排查在将 Fluent Bit 与 Container Insights for Amazon EKS 集成时的设置问题?

3 分钟阅读
0

我想要排查在将 Fluent Bit 与 Container Insights for Amazon Elastic Kubernetes Service(Amazon EKS)集成时的设置问题。

简短描述

Fluent Bit 是一个轻量级日志处理器和转发器,用于收集 Amazon CloudWatch 中的容器日志。

最佳实践是使用 Fluent Bit 而不是 Fluentd(旧版开源程序),因为它资源占用量少并且具有 Use_Kubelet 功能。有关更多信息,请参阅为大集群启用 Use_Kubelet 功能

要允许 Fluent Bit 将容器日志传输至 Amazon CloudWatch Logs,您必须向 Fluent Bit 授予 AWS Identity and Access Management(IAM)权限。通过 Amazon EKS,有两种授予 IAM 权限的方式:

  • 为您的 Worker 节点的 IAM 角色附加策略。
  • 使用 IAM 服务账户角色。

如果您向 Fluent Bit 授予 IAM 权限,则 Fluent Bit 能够执行以下操作:

  • logs:DescribeLogGroups
  • logs:DescribeLogStreams
  • logs:CreateLogGroup
  • logs:CreateLogStream
  • logs:PutLogEvents

常见问题包括:

  • Fluent Bit 容器组崩溃。
  • Fluent Bit 未向 CloudWatch 发送日志。
  • Fluent Bit 容器组返回 CreateContainerConfigError

解决方法

为服务账户设置 IAM 角色

使用 AWS 托管式策略 CloudWatchAgentServerPolicyamazon-cloudwatchcloudwatch-agent 服务账户创建 IAM 角色。

1.    运行以下命令以设置环境变量:

export CLUSTER="clustername"
export AWS_REGION="awsregion"
export AWS_ACCOUNT="awsaccountid"

注意:请将 "clustername""awsregion""awsaccountid"(包括引号)替换为您自己的集群名称、AWS 区域和账户 ID。

2.    运行以下 eksctl 命令:

eksctl create iamserviceaccount \
    --name cloudwatch-agent \
    --namespace amazon-cloudwatch \
    --cluster $CLUSTER \
    --attach-policy-arn "arn:aws:iam::aws:policy/CloudWatchAgentServerPolicy" \
    --approve \
    --override-existing-serviceaccounts

有关 Container Insights 先决条件的更多信息,请参阅验证先决条件

设置 CloudWatch 代理以收集集群指标并启用 Container Insights

1.    要使用快速入门部署 Container Insights,请运行以下命令:

ClusterName="my-cluster-name"
RegionName="my-cluster-region"
FluentBitHttpPort='2020'
FluentBitReadFromHead='Off'
[[ ${FluentBitReadFromHead} = 'On' ]] && FluentBitReadFromTail='Off'|| FluentBitReadFromTail='On'
[[ -z ${FluentBitHttpPort} ]] && FluentBitHttpServer='Off' || 
FluentBitHttpServer='On'
curl https://raw.githubusercontent.com/aws-samples/amazon-cloudwatch-
container-insights/latest/k8s-deployment-manifest-templates/deployment-mode/daemonset/container-insights-
monitoring/quickstart/cwagent-fluent-bit-quickstart.yaml | sed 's/
{{cluster_name}}/'${ClusterName}'/;s/
{{region_name}}/'${RegionName}'/;s/
{{http_server_toggle}}/"'${FluentBitHttpServer}'"/;s/
{{http_server_port}}/"'${FluentBitHttpPort}'"/;s/
{{read_from_head}}/"'${FluentBitReadFromHead}'"/;s/
{{read_from_tail}}/"'${FluentBitReadFromTail}'"/' | kubectl apply -f -

**注意:**请将 "my-cluster-name""my-cluster-region"(包括引号)替换为您自己的集群名称和 AWS 区域。

前面的命令用于为 CloudWatch 代理和 Fluent Bit 常见命令空间、ClusterRole、ClusterRoleBinding 和 ConfigMap。

2.    该命令运行后,请运行以下命令来创建 Fluent Bit 服务账户:

eksctl create iamserviceaccount \
    --name fluent-bit \
    --namespace amazon-cloudwatch \
    --cluster $CLUSTER \
    --attach-policy-arn "arn:aws:iam::aws:policy/CloudWatchAgentServerPolicy" \
    --approve \
    --override-existing-serviceaccounts

3.    请运行以下命令以验证 CloudWatch 代理是否部署:

kubectl get pods -n amazon-cloudwatch

4.    完成后,CloudWatch 代理将创建日志组 /aws/containerinsights/Cluster_Name/performance。 然后,CloudWatch 代理会将性能日志事件发送到日志组。

问题排查

Fluent Bit 容器组崩溃

1.    检查 Fluent Bit 容器组日志中是否有错误消息。请运行以下命令以查找 Fluent Bit 容器组中的事件:

kubectl -n amazon-cloudwatch logs -l k8s-app=fluent-bit 

kubectl -n amazon-cloudwatch describe pod fluent_pod pod_name

2.    验证 cluster-info(ConfigMap 中存储的 Fluent Bit 配置文件)是否准确且没有语法错误。请确保已设置所有集群名称和区域值。要了解更多信息,请参阅 GitHub 网站上的 amazon-cloudwatch-container-insights

Fluent Bit 不向 CloudWatch 发送日志

1.    验证 Fluent Bit 配置文件中是否已正确设置输出插件。要检查是否存在任何数据传输错误,请使用以下命令检查 Fluent Bit 容器组日志:

kubectl -n amazon-cloudwatch logs fluent_pod_name

2.    确保 Fluent Bit 容器组具有必要的 IAM 权限来将日志流式传输到 CloudWatch。您的 Amazon EKS Worker 节点因为 IAM 强制实施的 CloudWatchAgentServerPolicy 而向 CloudWatch 提交指标和日志。可通过两种方法授予必要的 IAM 权限:

  • 为 Worker 节点的 IAM 角色添加策略。
  • 为集群的服务账户创建 IAM 角色,并将策略粘贴到该角色上。

有关更多信息,请参阅为服务账户设置 IAM 角色部分。

Fluent Bit 容器组卡在 CreateContainerConfigError

如果容器组状态为 CreateContainerConfigError,请运行以下命令以获取准确的错误消息:

kubectl describe pod pod_name -n amazon-cloudwatch

在命令输出的 Events(事件)部分中,查找以下类似的错误消息:

Error syncing pod ("fluent-bit-xxxxxxx"), skipping: failed to "StartContainer" with CreateContain
erConfigError: "configmap \"fluent-bit-config\" not found"

如果您看到此错误,则可能是您未为 Fluent Bit 创建 ConfigMap(fluent-bit-config)。再次执行安装步骤,以确保创建 ConfigMap。


相关信息

设置 CloudWatch 代理以收集集群指标

CloudWatch 代理和 Fluent Bit 快速入门

启用调试日志记录(GitHub 网站)

AWS 官方
AWS 官方已更新 1 年前