添加 Fluentd 作为接收器

您可以在 KubeSphere 中使用 Elasticsearch、Kafka 和 Fluentd 日志接收器。本教程演示:

  • 创建 Fluentd 部署以及对应的服务和 ConfigMap。
  • 添加 Fluentd 作为日志接收器以接收来自 Fluent Bit 的日志,并输出为 stdout(标准输出)。
  • 验证 Fluentd 能否成功接收日志。

准备工作

  • 您需要一个被授予集群管理权限的帐户。例如,您可以直接用 admin 帐户登录控制台,或创建一个具有集群管理权限的角色然后将此角色授予一个帐户。

  • 添加日志接收器前,您需要启用组件 loggingeventsauditing。有关更多信息,请参见启用可插拔组件。本教程启用 logging 作为示例。

步骤 1:创建 Fluentd 部署

由于内存消耗低,KubeSphere 选择 Fluent Bit。Fluentd 一般在 Kubernetes 中以守护进程集的形式部署,在每个节点上收集容器日志。此外,Fluentd 支持多个插件。因此,Fluentd 会以部署的形式在 KubeSphere 中创建,将从 Fluent Bit 接收到的日志发送到多个目标,例如 S3、MongoDB、Cassandra、MySQL、syslog 和 Splunk 等。

执行以下命令:

备注

  • 以下命令将在默认命名空间 default 中创建 Fluentd 部署、服务和 ConfigMap,并为该 Fluentd ConfigMap 添加 filter 以排除 default 命名空间中的日志,避免 Fluent Bit 和 Fluentd 重复日志收集。
  • 如果您想要将 Fluentd 部署至其他命名空间,请修改以下命令中的命名空间名称。
cat <<EOF | kubectl apply -f -
apiVersion: v1
kind: ConfigMap
metadata:
  name: fluentd-config
  namespace: default
data:
  fluent.conf: |-
    # Receive logs sent from Fluent Bit on port 24224
    <source>
      @type forward
      port 24224
    </source>

    # Because this will send logs Fluentd received to stdout,
    # to avoid Fluent Bit and Fluentd loop logs collection,
    # add a filter here to avoid sending logs from the default namespace to stdout again
    <filter **>
      @type grep
      <exclude>
        key $.kubernetes.namespace_name
        pattern /^default$/
      </exclude>
    </filter>

    # Send received logs to stdout for demo/test purpose only
    # Various output plugins are supported to output logs to S3, MongoDB, Cassandra, MySQL, syslog, Splunk, etc.
    <match **>
      @type stdout
    </match>
---
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: fluentd
  name: fluentd
  namespace: default
spec:
  replicas: 1
  selector:
    matchLabels:
      app: fluentd
  template:
    metadata:
      labels:
        app: fluentd
    spec:
      containers:
      - image: fluentd:v1.9.1-1.0
        imagePullPolicy: IfNotPresent
        name: fluentd
        ports:
        - containerPort: 24224
          name: forward
          protocol: TCP
        - containerPort: 5140
          name: syslog
          protocol: TCP
        volumeMounts:
        - mountPath: /fluentd/etc
          name: config
          readOnly: true
      volumes:
      - configMap:
          defaultMode: 420
          name: fluentd-config
        name: config
---
apiVersion: v1
kind: Service
metadata:
  labels:
    app: fluentd
  name: fluentd
  namespace: default
spec:
  ports:
  - name: forward
    port: 24224
    protocol: TCP
    targetPort: forward
  selector:
    app: fluentd
  sessionAffinity: None
  type: ClusterIP
EOF

步骤 2:添加 Fluentd 作为日志接收器

  1. admin 身份登录 KubeSphere 的 Web 控制台。点击左上角的平台管理,然后选择集群管理

    备注

    如果您启用了多集群功能,您可以选择一个集群。
  2. 集群管理页面,选择集群设置下的日志收集

  3. 点击添加日志接收器并选择 Fluentd

  4. 输入 Fluentd 服务地址和端口信息,如下所示:

    add-fluentd

  5. Fluentd 会显示在日志收集页面的接收器列表中,状态为收集中

步骤 3:验证 Fluentd 能否从 Fluent Bit 接收日志

  1. 集群管理页面点击应用负载

  2. 点击工作负载,并从部署选项卡下的下拉菜单中选择 default 项目。

  3. 点击 fluentd 项目并选择 fluentd-xxxxxxxxx-xxxxx Pod。

  4. 点击 fluentd 容器。

  5. fluentd 容器页面,选择容器日志选项卡。

  6. 您可以看到日志持续滚动输出。

    container-logs


感谢您的反馈。如果您有关于如何使用 KubeSphere 的具体问题,请在 Slack 上提问。如果您想报告问题或提出改进建议,请在 GitHub 存储库中打开问题。