

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 將 Fluent Bit 設定為 DaemonSet 以將日誌傳送至 CloudWatch Logs
<a name="Container-Insights-setup-logs-FluentBit"></a>

下列各節可協助您部署 Fluent Bit，以將日誌從容器傳送至 CloudWatch Logs。

**Topics**
+ [設定 Fluent Bit](#Container-Insights-FluentBit-setup)
+ [多行日誌支援](#ContainerInsights-fluentbit-multiline)
+ [(選用) 減少 FluentD 的日誌量](#ContainerInsights-fluentbit-volume)
+ [疑難排解](#Container-Insights-FluentBit-troubleshoot)
+ [儀表板](#Container-Insights-FluentBit-dashboard)

## 設定 Fluent Bit
<a name="Container-Insights-FluentBit-setup"></a>

若要設定 Fluent Bit 以從您的容器收集日誌，您可以遵循 [Amazon EKS 和 Kubernetes 上 Container Insights 的 Quick Start 設定](Container-Insights-setup-EKS-quickstart.md) 中的步驟，或是遵循本節中的步驟。

無論使用何種方法，連接至叢集節點的 IAM 角色皆必須具有足夠的權限。如需有關執行 Amazon EKS 叢集所需許可的詳細資訊，請參閱*《Amazon EKS 使用者指南》*中的 [Amazon EKS IAM 政策、角色和許可](https://docs.aws.amazon.com/eks/latest/userguide/IAM_policies.html)。

在下列步驟中，您可以將 Fluent Bit 設為 DaemonSet 來將日誌傳送到 CloudWatch Logs。完成此步驟時，Fluent Bit 會建立下列日誌群組 (若尚未存在的話)。

**重要**  
如果您已在 Container Insights 中設定了 Fluentd，且 Fluentd DaemonSet 未如預期般執行 (如果您使用 `containerd` 執行期，則可能出現這種情況)，您必須先將其解除安裝，才能安裝 Fluent Bit，防止 Fluent Bit 處理 Fluentd 錯誤日誌訊息。否則，您必須在成功安裝 Fluent Bit 之後立即解除安裝 Fluentd。在安裝 Fluent Bit 後解除安裝 Fluentd，可確保在此移轉過程中記錄的唯一性。只需 Fluent Bit 或 Fluentd 其一即可將日誌傳送到 CloudWatch Logs。


| 日誌群組名稱 | 日誌來源 | 
| --- | --- | 
|  `/aws/containerinsights/Cluster_Name/application`  |  `/var/log/containers` 中所有日誌檔  | 
|  `/aws/containerinsights/Cluster_Name/host`  |  來自 `/var/log/dmesg`、`/var/log/secure` 以及 `/var/log/messages` 的日誌  | 
|  `/aws/containerinsights/Cluster_Name/dataplane`  |  `/var/log/journal` 中適用於 `kubelet.service`、`kubeproxy.service` 和 `docker.service` 的日誌。  | 

**若要安裝 Fluent Bit 以將日誌從容器傳送至 CloudWatch Logs**

1. 如果您還沒有名為 `amazon-cloudwatch` 的命名空間，請輸入下列命令建立一個：

   ```
   kubectl apply -f https://raw.githubusercontent.com/aws-samples/amazon-cloudwatch-container-insights/latest/k8s-deployment-manifest-templates/deployment-mode/daemonset/container-insights-monitoring/cloudwatch-namespace.yaml
   ```

1. 執行下列命令以建立為 `cluster-info` 的 ConfigMap，並提供叢集名稱和要傳送日誌到其中的區域。將 *cluster-name* 和 *cluster-region* 替換成您叢集的名稱和區域。

   ```
   ClusterName=cluster-name
   RegionName=cluster-region
   FluentBitHttpPort='2020'
   FluentBitReadFromHead='Off'
   [[ ${FluentBitReadFromHead} = 'On' ]] && FluentBitReadFromTail='Off'|| FluentBitReadFromTail='On'
   [[ -z ${FluentBitHttpPort} ]] && FluentBitHttpServer='Off' || FluentBitHttpServer='On'
   kubectl create configmap fluent-bit-cluster-info \
   --from-literal=cluster.name=${ClusterName} \
   --from-literal=http.server=${FluentBitHttpServer} \
   --from-literal=http.port=${FluentBitHttpPort} \
   --from-literal=read.head=${FluentBitReadFromHead} \
   --from-literal=read.tail=${FluentBitReadFromTail} \
   --from-literal=logs.region=${RegionName} -n amazon-cloudwatch
   ```

   在這個命令中， 用於監控外掛程式指標的 `FluentBitHttpServer` 預設為開啟。若要將其關閉，請將命令中的第三行變更為 `FluentBitHttpPort=''` (空字串)。

   此外，依預設，Fluent Bit 從尾部讀取日誌檔案，並將僅擷取部署後的新日誌。如果您想要相反結果，請設定 `FluentBitReadFromHead='On'`，它將收集檔案系統中的所有日誌。

1. 透過執行以下其中一個命令，下載 Fluent Bit daemonset 並將其部署到叢集。
   + 如果想要為 Linux 計算機最佳化 Fluent Bit 組態，請執行此命令。

     ```
     kubectl apply -f https://raw.githubusercontent.com/aws-samples/amazon-cloudwatch-container-insights/latest/k8s-deployment-manifest-templates/deployment-mode/daemonset/container-insights-monitoring/fluent-bit/fluent-bit.yaml
     ```
   + 如果想要為 Linux 計算機最佳化 Fluent Bit 組態，請執行此命令。

     ```
     kubectl apply -f https://raw.githubusercontent.com/aws-samples/amazon-cloudwatch-container-insights/latest/k8s-deployment-manifest-templates/deployment-mode/daemonset/container-insights-monitoring/fluent-bit/fluent-bit-windows.yaml
     ```
   + 如果使用 Linux 計算機，並想要更類似於 Fluentd 的 Fluent Bit 組態，請執行此命令。

     ```
     kubectl apply -f https://raw.githubusercontent.com/aws-samples/amazon-cloudwatch-container-insights/latest/k8s-deployment-manifest-templates/deployment-mode/daemonset/container-insights-monitoring/fluent-bit/fluent-bit-compatible.yaml
     ```
**重要**  
依預設，Fluent Bit daemonset 組態會將日誌層級設定為 INFO，這可能會導致 CloudWatch Logs 擷取成本較高。如果您想要減少日誌擷取量和成本，可以將日誌層級變更為 ERROR。  
如需如何減少日誌量的詳細資訊，請參閱 [(選用) 減少 FluentD 的日誌量](#ContainerInsights-fluentbit-volume)。

1. 透過輸入以下命令來驗證部署。每個節點應有一個名為 **fluent-bit-\$1** 的 pod。

   ```
   kubectl get pods -n amazon-cloudwatch
   ```

上述步驟在叢集中建立下列資源：
+ 在 `amazon-cloudwatch` 命名空間中名為 `Fluent-Bit` 的服務帳戶。此服務帳戶會用來執行 Fluent Bit daemonSet。如需詳細資訊，請參閱 Kubernetes 參考中的[管理服務帳戶](https://kubernetes.io/docs/reference/access-authn-authz/service-accounts-admin/)。
+ 在 `amazon-cloudwatch` 命名空間中名為的 `Fluent-Bit-role` 叢集角色。此叢集角色會在 pod 日誌上將 `get`、`list` 和 `watch` 許可授予給 `Fluent-Bit` 服務帳戶。如需詳細資訊，請參閱 Kubernetes 參考中的 [API 概觀](https://kubernetes.io/docs/reference/access-authn-authz/rbac/#api-overview/)。
+ `amazon-cloudwatch` 命名空間中名為 `Fluent-Bit-config` 的 ConfigMap。此 ConfigMap 包含 Fluent Bit 使用的組態。如需詳細資訊，請參閱 Kubernetes 任務文件中的[將 Pod 設定為使用 ConfigMap](https://kubernetes.io/docs/tasks/configure-pod-container/configure-pod-configmap/)。

如果您想要驗證您的 Fluent Bit 設定，請遵循下列步驟。

**驗證 Fluent Bit 設定**

1. 透過 [https://console.aws.amazon.com/cloudwatch/](https://console.aws.amazon.com/cloudwatch/) 開啟 CloudWatch 主控台。

1. 在導覽窗格中，選擇 **Log groups** (日誌群組)。

1. 請確定您位於部署 Fluent Bit 的區域。

1. 檢查區域中的日誌群組清單。請查看下列事項：
   + `/aws/containerinsights/Cluster_Name/application`
   + `/aws/containerinsights/Cluster_Name/host`
   + `/aws/containerinsights/Cluster_Name/dataplane`

1. 導覽至其中一個記錄群組，然後勾選日誌串流的 **Last Event Time** (上次事件時間)。如果它相對於您部署 Fluent Bit 的時間更近，則設定已受驗證。

   建立 `/dataplane` 日誌群組時可能會略微存在延遲。這是正常的，因為只有在 Fluent Bit 開始為該日誌組傳送日誌時，才會建立這些日誌群組。

## 多行日誌支援
<a name="ContainerInsights-fluentbit-multiline"></a>

如需如何搭配多行日誌使用 Fluent Bit 的詳細資訊，請參閱 Fluent Bit 文件的下列章節：
+ [Multiline Parsing](https://docs.fluentbit.io/manual/administration/configuring-fluent-bit/multiline-parsing)
+ [Multiline and Containers (v1.8)](https://docs.fluentbit.io/manual/pipeline/inputs/tail#multiline-and-containers-v1.8)
+ [Multiline Core (v1.8)](https://docs.fluentbit.io/manual/pipeline/inputs/tail#multiline-core-v1.8)
+ [Always use multiline in the tail input](https://github.com/aws/aws-for-fluent-bit/blob/mainline/troubleshooting/debugging.md#always-use-multiline-the-tail-input)

## (選用) 減少 FluentD 的日誌量
<a name="ContainerInsights-fluentbit-volume"></a>

根據預設，我們會將 Fluent Bit 應用程式日誌和 Kubernetes 中繼資料傳送到 CloudWatch。若您希望減少傳送到 CloudWatch 的資料量，您可以停止其中一個，或同時停止這兩個資料來源傳送到 CloudWatch。如果已遵循此頁面上的步驟來設定 Fluent Bit，請從先前執行的 kubectl `apply` 命令下載 Kubernetes 資訊清單 YAML 檔案，並使用變更進行修改，然後可以重新套用至叢集。或者，如果您使用的是 Amazon CloudWatch Observability EKS 附加元件或 Helm Chart，請參閱 [(選用) 額外組態](install-CloudWatch-Observability-EKS-addon.md#install-CloudWatch-Observability-EKS-addon-configuration) 以取得有關使用附加元件進階組態或 Helm Chart 來管理 Fluent Bit 組態的資訊。

若要停止 Fluent Bit 應用程式日誌，請從 `Fluent Bit configuration` 檔案移除以下區段。

```
[INPUT]
        Name                tail
        Tag                 application.*
        Path                /var/log/containers/fluent-bit*
        Parser              docker
        DB                  /fluent-bit/state/flb_log.db
        Mem_Buf_Limit       5MB
        Skip_Long_Lines     On
        Refresh_Interval    10
```

若要移除 Kubernetes 中繼資料，使其不會附加到傳送至 CloudWatch 的日誌事件，請將下列篩選條件新增到 Fluent Bit 組態的 `application-log.conf` 區段。將 *<Metadata\$11>* 和類似欄位取代為實際中繼資料識別符。

```
application-log.conf: |
    [FILTER]
        Name                nest
        Match               application.*
        Operation           lift
        Nested_under        kubernetes
        Add_prefix          Kube.

    [FILTER]
        Name                modify
        Match               application.*
        Remove              Kube.<Metadata_1>
        Remove              Kube.<Metadata_2>
        Remove              Kube.<Metadata_3>
    
    [FILTER]
        Name                nest
        Match               application.*
        Operation           nest
        Wildcard            Kube.*
        Nested_under        kubernetes
        Remove_prefix       Kube.
```

## 疑難排解
<a name="Container-Insights-FluentBit-troubleshoot"></a>

如果您沒有看到這些日誌群組，且在正確的區域中尋找，請檢查日誌是否有 Fluent Bit daemonSet pod 以尋找錯誤。

執行以下命令並確保狀態為 `Running`。

```
kubectl get pods -n amazon-cloudwatch
```

如果日誌有與 IAM 許可相關的錯誤，請確認 IAM 角色是否已連接到叢集節點。如需有關執行 Amazon EKS 叢集所需許可的詳細資訊，請參閱*《Amazon EKS 使用者指南》*中的 [Amazon EKS IAM 政策、角色和許可](https://docs.aws.amazon.com/eks/latest/userguide/IAM_policies.html)。

如果 pod 狀態為 `CreateContainerConfigError`，透過執行以下命令以取得確切的錯誤。

```
kubectl describe pod pod_name -n amazon-cloudwatch
```

## 儀表板
<a name="Container-Insights-FluentBit-dashboard"></a>

您可以建立一個儀表板來監控每個正在執行的外掛程式的指標。您可以查看輸入和輸出位元組、記錄處理速率以及輸出錯誤和重試/失敗率的資料。若要檢視這些指標，您需要安裝具有適用於 Amazon EKS 和 Kubernetes 叢集的 Prometheus 指標集合的 CloudWatch 代理程式。如需如何設定儀表板的詳細資訊，請參閱 [在 Amazon EKS 和 Kubernetes 叢集上安裝具有 Prometheus 指標集合的 CloudWatch 代理程式。在 Amazon EKS 和 Kubernetes 叢集上安裝具有 Prometheus 指標集合的 CloudWatch 代理程式。](ContainerInsights-Prometheus-Setup.md)。

**注意**  
您必須先設定 Prometheus 指標的 Container Insights，才能設定此儀表板。如需詳細資訊，請參閱[Container Insights Prometheus 指標監控](ContainerInsights-Prometheus.md)。

**若要建立 Fluent Bit Prometheus 指標的儀表板**

1. 建立環境變數，取代下列幾行中右側的數值，以符合您的部署。

   ```
   DASHBOARD_NAME=your_cw_dashboard_name
   REGION_NAME=your_metric_region_such_as_us-west-1
   CLUSTER_NAME=your_kubernetes_cluster_name
   ```

1. 執行以下命令建立儀表板。

   ```
   curl https://raw.githubusercontent.com/aws-samples/amazon-cloudwatch-container-insights/latest/k8s-deployment-manifest-templates/deployment-mode/service/cwagent-prometheus/sample_cloudwatch_dashboards/fluent-bit/cw_dashboard_fluent_bit.json \
   | sed "s/{{YOUR_AWS_REGION}}/${REGION_NAME}/g" \
   | sed "s/{{YOUR_CLUSTER_NAME}}/${CLUSTER_NAME}/g" \
   | xargs -0 aws cloudwatch put-dashboard --dashboard-name ${DASHBOARD_NAME} --dashboard-body
   ```