1. Mục tiêu

Tài liệu này nhằm hướng dẫn cài đặt và cấu hình Fluentd Helm để thu thập log stdout/stderr của container Kubernetes và gửi dữ liệu lên OpenSearch.

2. Thông tin kết nối OpenSearch

Khách hàng sẽ nhận được các thông tin sau:

  • Endpoint: <ID dịch vụ>.vnptplatform.vn
  • Port: 443
  • Username: <username>
  • Password: <password>

3. Cài đặt Helm chart Fluentd

3.1. Download chart từ registry

Download helm chart Fluentd từ chart repo:

helm pull oci:// ecr.vnptplatform.vn/ecr_7zqv3ezcixi/fluentd --version 1.0.0
tar -xvzf fluentd-1.0.0.tgz
cd fluentd
vi values.yaml

3.2. Tạo namespace

kubectl create namespace <namespace triển khai fluentd>

4. Sửa cấu hình Fluentd

Sửa file values.yaml phần fileConfigs như sau:

fileConfigs:
  01_sources.conf: |-
    <source>
      @type tail
      @id in_tail_container_logs
      @label @KUBERNETES
      path /var/log/containers/*.log
      pos_file /var/log/fluentd-containers.log.pos
      tag kubernetes.*
      read_from_head true
      <parse>
        @type multi_format
        <pattern>
          format json
          time_key time
          time_type string
          time_format "%Y-%m-%dT%H:%M:%S.%NZ"
          keep_time_key false
        </pattern>
        <pattern>
          format regexp
          expression /^(?<time>[^ ]+) (?<stream>stdout|stderr) (?<logtag>[FP]) (?<log>.*)$/
          time_key time
          time_type string
          time_format "%Y-%m-%dT%H:%M:%S.%N%:z"
          keep_time_key false
        </pattern>
        <pattern>
          format regexp
          expression /^(?<time>[^ ]+) (?<stream>stdout|stderr) (?<logtag>[FP]) (?<log>.*)$/
          time_key time
          time_type string
          time_format "%Y-%m-%dT%H:%M:%S.%N%z"
          keep_time_key false
        </pattern>
        <pattern>
          format regexp
          expression /^(?<stream>stdout|stderr) (?<log>.*)$/
          keep_time_key false
        </pattern>
      </parse>
      emit_unmatched_lines true
    </source>

    <source>
      @type prometheus
      bind 0.0.0.0
      port 24231
      metrics_path /metrics
    </source>

  02_filters.conf: |-
    <label @KUBERNETES>
      <match kubernetes.var.log.containers.fluentd-*_*_fluentd-*.log>
        @type relabel
        @label @FLUENT_LOG
      </match>

      <filter kubernetes.**>
        @type kubernetes_metadata
        @id filter_kube_metadata
        skip_labels true
        skip_container_metadata false
        skip_namespace_metadata true
        skip_master_url true
      </filter>

      <match **>
        @type relabel
        @label @DISPATCH
      </match>
    </label>

  03_dispatch.conf: |-
    <label @DISPATCH>
      <filter **>
        @type prometheus
        <metric>
          name fluentd_input_status_num_records_total
          type counter
          desc The total number of incoming records
          <labels>
            tag ${tag}
            hostname ${hostname}
          </labels>
        </metric>
      </filter>

      <match **>
        @type relabel
        @label @OUTPUT
      </match>
    </label>

  04_outputs.conf: |-
    <label @OUTPUT>
      <match **>
        @type opensearch
        host <endpoint opensearch>
        port 443
        user <username opensearch>
        password <password opensearch>
        scheme https
        ssl_verify false
        logstash_format true
        logstash_prefix fluentd-logs
        suppress_type_name true

        <buffer>
          @type file
          path /var/log/fluentd-buffers/opensearch-buffer
          flush_mode interval
          flush_interval 5s
          retry_type exponential_backoff
        </buffer>
      </match>
    </label>

5. Triển khai Fluentd

Nếu chart đã được tải về máy local:

helm install <tên triển khai fluentd> ./fluentd \
  -n <namespace triển khai fluentd> \
  -f values.yaml

6. Kiểm tra

kubectl get pods -n <namespace triển khai fluentd>
kubectl logs -n <namespace triển khai fluentd> -l app.kubernetes.io/name=fluentd

Nếu cấu hình đúng, Fluentd sẽ đọc log từ /var/log/containers và gửi lên OpenSearch.

7. Kiểm tra dữ liệu trên OpenSearch

7.1. Kiểm tra index

curl -k -u "<username opensearch>:<password opensearch>" \
  "https://< ID dịch vụ>.vnptplatform.vn:443/fluentd-logs-*/_search"

7.2. Kết quả dự kiến

OpenSearch trả về JSON chứa dữ liệu log đã được gửi từ Fluentd.

Discovery dữ liệu log thu thập được trên OpenSearch Dashboard:

Hình: Màn hình dữ liệu log

8. Kết luận

Với cấu hình trên, Fluentd sẽ thu thập log theo từng container trong Kubernetes, bổ sung metadata và gửi tới OpenSearch theo index fluentd-logs.

9. Tài liệu tham khảo

- Cấu hình Fluentd (source, filter, match, label)
https://docs.fluentd.org/configuration/config-file

- Kubernetes metadata filter plugin
https://github.com/fabric8io/fluent-plugin-kubernetes_metadata_filter