Add Fluentd as Receiver (aka Collector)

KubeSphere supports using Elasticsearch, Kafka and Fluentd as log receivers. This doc will demonstrate:

  • How to deploy Fluentd as deployment and create 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

  • Before adding a log receiver, you need to enable any of the logging, events or auditing components following Enable Pluggable Components. The logging component is enabled as an example in this doc.
  • To configure log collection, you should use an account of platform-admin role.

Step 1: Deploy Fluentd as a deployment

Usually, Fluentd is deployed as a daemonset in K8s to collect container logs on each node. KubeSphere chooses Fluent Bit for this purpose 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.

To deploy Fluentd as a deployment, you simply need to open the kubectl console in KubeSphere Toolbox and run the following command:

Note

  • The following command will deploy Fluentd deployment, service and configmap into the default namespace and add a filter to Fluentd configmap to exclude logs from the default namespace to avoid Fluent Bit and Fluentd loop logs collection.
  • You’ll need to change all these default to the namespace you selected if you want to deploy to 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 and 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 log receiver (aka collector)

  1. To add a log receiver:
  • Login KubeSphere with an account of platform-admin role
  • Click Platform -> Clusters Management
  • Select a cluster if multiple clusters exist
  • Click Cluster Settings -> Log Collections
  • Log receivers can be added by clicking Add Log Collector

Add receiver

  1. Choose Fluentd and fill in the Fluentd service address and port like below:

Add Fluentd

  1. Fluentd appears in the receiver list of Log Collections UI and its status shows Collecting.

Receiver List

  1. Verify whether Fluentd is receiving logs sent from Fluent Bit:
  • Click Application Workloads in the Cluster Management UI.
  • Select Workloads and then select the default namespace in the Workload - Deployments tab
  • Click the fluentd item and then click the fluentd-xxxxxxxxx-xxxxx pod
  • Click the fluentd container
  • In the fluentd container page, select the Container Logs tab

You’ll see logs begin to scroll up continuously.

Container Logs