Add Fluentd as a Receiver (i.e. Collector)

You can use Elasticsearch, Kafka and Fluentd as log receivers in KubeSphere. This tutorial demonstrates:

  • How to deploy Fluentd as a Deployment and create the corresponding Service and ConfigMap.
  • How to add Fluentd as a log receiver to receive logs sent from Fluent Bit and then output to stdout.
  • How to verify if Fluentd receives logs successfully.

Prerequisites

  • You need an account granted a role including the authorization of Cluster Management. For example, you can log in to the console as admin directly or create a new role with the authorization and assign it to an account.

  • Before adding a log receiver, you need to enable any of the logging, events or auditing components. For more information, see Enable Pluggable Components. logging is enabled as an example in this tutorial.

Step 1: Deploy Fluentd as a Deployment

Usually, Fluentd is deployed as a DaemonSet in Kubernetes to collect container logs on each node. KubeSphere chooses Fluent Bit because of its low memory footprint. Besides, Fluentd features numerous output plugins. Hence, KubeSphere chooses to deploy Fluentd as a Deployment to forward logs it receives from Fluent Bit to more destinations such as S3, MongoDB, Cassandra, MySQL, syslog and Splunk.

Run the following commands:

Note

  • The following commands create the Fluentd Deployment, Service and ConfigMap in the default namespace and add a filter to the Fluentd ConfigMap to exclude logs from the default namespace to avoid Fluent Bit and Fluentd loop log collections.
  • Change the namespace if you want to deploy Fluentd into a different namespace.
  1. cat <<EOF | kubectl apply -f -
  2. apiVersion: v1
  3. kind: ConfigMap
  4. metadata:
  5. name: fluentd-config
  6. namespace: default
  7. data:
  8. fluent.conf: |-
  9. # Receive logs sent from Fluent Bit on port 24224
  10. <source>
  11. @type forward
  12. port 24224
  13. </source>
  14. # Because this will send logs Fluentd received to stdout,
  15. # to avoid Fluent Bit and Fluentd loop logs collection,
  16. # add a filter here to avoid sending logs from the default namespace to stdout again
  17. <filter **>
  18. @type grep
  19. <exclude>
  20. key $.kubernetes.namespace_name
  21. pattern /^default$/
  22. </exclude>
  23. </filter>
  24. # Send received logs to stdout for demo/test purpose only
  25. # Various output plugins are supported to output logs to S3, MongoDB, Cassandra, MySQL, syslog, Splunk, etc.
  26. <match **>
  27. @type stdout
  28. </match>
  29. ---
  30. apiVersion: apps/v1
  31. kind: Deployment
  32. metadata:
  33. labels:
  34. app: fluentd
  35. name: fluentd
  36. namespace: default
  37. spec:
  38. replicas: 1
  39. selector:
  40. matchLabels:
  41. app: fluentd
  42. template:
  43. metadata:
  44. labels:
  45. app: fluentd
  46. spec:
  47. containers:
  48. - image: fluentd:v1.9.1-1.0
  49. imagePullPolicy: IfNotPresent
  50. name: fluentd
  51. ports:
  52. - containerPort: 24224
  53. name: forward
  54. protocol: TCP
  55. - containerPort: 5140
  56. name: syslog
  57. protocol: TCP
  58. volumeMounts:
  59. - mountPath: /fluentd/etc
  60. name: config
  61. readOnly: true
  62. volumes:
  63. - configMap:
  64. defaultMode: 420
  65. name: fluentd-config
  66. name: config
  67. ---
  68. apiVersion: v1
  69. kind: Service
  70. metadata:
  71. labels:
  72. app: fluentd-svc
  73. name: fluentd-svc
  74. namespace: default
  75. spec:
  76. ports:
  77. - name: forward
  78. port: 24224
  79. protocol: TCP
  80. targetPort: forward
  81. selector:
  82. app: fluentd
  83. sessionAffinity: None
  84. type: ClusterIP
  85. EOF

Step 2: Add Fluentd as a Log Receiver (i.e. Collector)

  1. Log in to KubeSphere as admin. Click Platform in the top left corner and select Cluster Management.

  2. If you have enabled the multi-cluster feature, you can select a specific cluster. If you have not enabled the feature, refer to the next step directly.

  3. On the Cluster Management page, go to Log Collections in Cluster Settings.

  4. Click Add Log Collector and choose Fluentd.

    add-receiver

  5. Provide the Fluentd service address and port as below:

    add-fluentd

  6. Fluentd will appear in the receiver list on the Log Collections page, the status of which is Collecting.

    receiver-list

Step 3: Verify Fluentd is Receiving Logs Sent from Fluent Bit

  1. Click Application Workloads on the Cluster Management page.

  2. Select Workloads and then select the default project from the drop-down list in the Deployments tab.

  3. Click the fluentd item and then select the fluentd-xxxxxxxxx-xxxxx Pod.

  4. Click the fluentd container.

  5. On the fluentd container page, select the Container Logs tab.

  6. You can see logs begin to scroll up continuously.

    container-logs