• DHorse系列文章之日志收集


    实现原理

    基于k8s的日志收集主要有两种方案,一是使用daemoset,另一种是基于sidecar。两种方式各有优缺点,目前DHorse是基于daemoset实现的。如图1所示:
    在这里插入图片描述
    图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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143

    然后,需要开启目标集群的日志开关即可,如图2所示:
    在这里插入图片描述
    图2

  • 相关阅读:
    Protobuf应用层协议设计
    【C++】解引用 (及指针) 和 引用 的概念区别
    Nginx 实用配置技巧,99%用过的是老司机
    Springboot实验室自主预约系统毕业设计源码111953
    了解GPT:ChatGPT的终极指南
    Java刷题面试系列习题(十一)
    hadoop环境配置错误 Error:Invalid HADOOP_COMMON_HOME
    求最大公约数的几种常见的方法 【详解】
    Django数据库orm操作以list形式获取数据库中某列所有值
    06_openstack之创建云主机和常见错误
  • 原文地址:https://blog.csdn.net/huashetianzu/article/details/127697038