DHorse日志收集原理

时间:2022-12-12 13:08:35

基于k8s的日志收集主要有两种方案,一是使用daemoset,另一种是基于sidecar。两种方式各有优缺点,目前DHorse是基于daemoset实现的。如图1所示:

DHorse日志收集原理

图1

在每个k8s集群中启动一个daemoset组件,即Filebeat的服务,监控/var/log/containers目录下的日志文件变动,然后把日志内容推送到ELK集群。

DHorse日志配置

在DHorse的安装目录conf子目录下,可以通过filebeat-k8s.yml文件进行日志收集的相关配置,如下:

---
apiVersion: v1
kind: ConfigMap
metadata:
  # 默认名称,不允许修改
  name: filebeat-config
  # 默认命名空间,不允许修改
  namespace: dhorse-system
  labels:
    # 默认标签名,不允许修改
    app: filebeat
data:
  filebeat.yml: |-
    filebeat.inputs:
    - type: container
      paths:
        - /var/log/containers/*.log
      processors:
        - add_kubernetes_metadata:
            host: ${NODE_NAME}
            matchers:
            - logs_path:
                logs_path: "/var/log/containers/"

    # To enable hints based autodiscover, remove `filebeat.inputs` configuration and uncomment this:
    #filebeat.autodiscover:
    #  providers:
    #    - type: kubernetes
    #      node: ${NODE_NAME}
    #      hints.enabled: true
    #      hints.default_config:
    #        type: container
    #        paths:
    #          - /var/log/containers/*${data.kubernetes.container.id}.log

    processors:
      - add_cloud_metadata:
      - add_host_metadata:

    cloud.id: ${ELASTIC_CLOUD_ID}
    cloud.auth: ${ELASTIC_CLOUD_AUTH}

    output.elasticsearch:
      hosts: ['${ELASTICSEARCH_HOST:elasticsearch}:${ELASTICSEARCH_PORT:9200}']
      username: ${ELASTICSEARCH_USERNAME}
      password: ${ELASTICSEARCH_PASSWORD}
---
apiVersion: apps/v1
kind: DaemonSet
metadata:
  # 默认名称,不允许修改
  name: filebeat
  # 默认命名空间,不允许修改
  namespace: dhorse-system
  labels:
    # 默认标签名,不允许修改
    app: filebeat
spec:
  selector:
    matchLabels:
      # 默认标签名,不允许修改
      app: filebeat
  template:
    metadata:
      labels:
        # 默认标签名,不允许修改
        app: filebeat
    spec:
      terminationGracePeriodSeconds: 30
      hostNetwork: true
      dnsPolicy: ClusterFirstWithHostNet
      containers:
      - name: filebeat
        # 替换成你自己的filebeat镜像
        image: docker.elastic.co/beats/filebeat:8.1.0
        args: [
          "-c", "/etc/filebeat.yml",
          "-e",
        ]
        #替换成你自己的es地址和账号
        env:
        - name: ELASTICSEARCH_HOST
          value: 127.0.0.1
        - name: ELASTICSEARCH_PORT
          value: "9200"
        - name: ELASTICSEARCH_USERNAME
          value: elastic
        - name: ELASTICSEARCH_PASSWORD
          value: changeme
        - name: ELASTIC_CLOUD_ID
          value:
        - name: ELASTIC_CLOUD_AUTH
          value:
        - name: NODE_NAME
          valueFrom:
            fieldRef:
              fieldPath: spec.nodeName
        securityContext:
          runAsUser: 0
          # If using Red Hat OpenShift uncomment this:
          #privileged: true
        resources:
          limits:
            memory: 200Mi
          requests:
            cpu: 100m
            memory: 100Mi
        volumeMounts:
        - name: config
          mountPath: /etc/filebeat.yml
          readOnly: true
          subPath: filebeat.yml
        - name: data
          mountPath: /usr/share/filebeat/data
        - name: varlibdockercontainers
          mountPath: /var/lib/docker/containers
          readOnly: true
        - name: varlog
          mountPath: /var/log
          readOnly: true
        - name: hosttime
          mountPath: /etc/localtime
          readOnly: true
      volumes:
      - name: config
        configMap:
          defaultMode: 0640
          name: filebeat-config
      - name: varlibdockercontainers
        hostPath:
          path: /var/lib/docker/containers
      - name: varlog
        hostPath:
          path: /var/log
      - name: hosttime
        hostPath:
          path: /etc/localtime
      # data folder stores a registry of read status for all files, so we don't send everything again on a Filebeat pod restart
      - name: data
        hostPath:
          # When filebeat runs as non-root user, this directory needs to be writable by group (g+w).
          path: /var/lib/filebeat-data
          type: DirectoryOrCreate

然后,需要开启目标集群的日志开关即可,如图2所示:

DHorse日志收集原理

图2