huanggze 对的,需求是按应用名称加上日期建立索引,比如a.log.20200324,b.log.20200324

    tzghost 应用名以什么为准呢?pod name?app label?然后问下,为什么有这个需求?

      huanggze 我这边暂时考虑的是pod name ,由于我们业务应用比较多,后续都迁移到KS上来,所有日志再输出到ES,只有一个索引会比较混乱吧。目前我们是通过filebeat把不同应用的日志输出到kafka的不同topic上来区分开的

        8 天 后

        huanggze 参考相关资料折腾了几天,目前我是以DaemonSet的形式,在业务的namespace下跑了fluentd。通过在平台设置 - 日志接收者配置fluentd的地址端口,将应用日志过滤后输出到ES的。目前我遇到的问题是,在filter中grep过滤单个应用日志是正常的,但过滤多个应用日志时就有问题,麻烦帮忙看看,相关配置如下:
        fluentd-es-configmap.yaml

        kind: ConfigMap
        apiVersion: v1
        metadata:
          name: fluentd-es-config-v0.2.0
          namespace: zzb-test
          labels:
            addonmanager.kubernetes.io/mode: Reconcile
        data:
          containers.input.conf: |-
            <source>
              @id fluentd-containers.log
              @type tail
              path /var/log/containers/*.log
              pos_file /var/log/es-containers.log.pos
              tag raw.kubernetes.*
              read_from_head true
              <parse>
                @type multi_format
                <pattern>
                  format json
                  time_key time
                  time_format %Y-%m-%dT%H:%M:%S.%NZ
                </pattern>
                <pattern>
                  format /^(?<time>.+) (?<stream>stdout|stderr) [^ ]* (?<log>.*)$/
                  time_format %Y-%m-%dT%H:%M:%S.%N%:z
                </pattern>
              </parse>
            </source>
        
            # Detect exceptions in the log output and forward them as one log entry.
            <match raw.kubernetes.**>
              @id raw.kubernetes
              @type detect_exceptions
              remove_tag_prefix raw
              message log
              stream stream
              multiline_flush_interval 5
              max_bytes 500000
              max_lines 1000
            </match>
        
            # Concatenate multi-line logs
            <filter **>
              @id filter_concat
              @type concat
              key message
              multiline_end_regexp /\n$/
              separator ""
            </filter>
        
            # Enriches records with Kubernetes metadata
            <filter kubernetes.**>
              @id filter_kubernetes_metadata
              @type kubernetes_metadata
            </filter>
        
            # Fixes json fields in Elasticsearch
            <filter kubernetes.**>
              @id filter_parser
              @type parser
              key_name log
              reserve_data true
              remove_key_name_field true
              <parse>
                @type multi_format
                <pattern>
                  format json
                </pattern>
                <pattern>
                  format none
                </pattern>
              </parse>
            </filter>
        
          forward.input.conf: |-
            # Takes the messages sent over TCP
            <source>
              @id forward
              @type forward
            </source>
            <filter **>
                @type grep
                <regexp>
                        key $.kubernetes.pod_name
                        pattern /(^zzbservice)/
                </regexp>
            </filter>
        
          monitoring.conf: |-
            # Prometheus Exporter Plugin
            # input plugin that exports metrics
            <source>
              @id prometheus
              @type prometheus
            </source>
        
            <source>
              @id monitor_agent
              @type monitor_agent
            </source>
        
            # input plugin that collects metrics from MonitorAgent
            <source>
              @id prometheus_monitor
              @type prometheus_monitor
              <labels>
                host ${hostname}
              </labels>
            </source>
        
            # input plugin that collects metrics for output plugin
            <source>
              @id prometheus_output_monitor
              @type prometheus_output_monitor
              <labels>
                host ${hostname}
              </labels>
            </source>
        
            # input plugin that collects metrics for in_tail plugin
            <source>
              @id prometheus_tail_monitor
              @type prometheus_tail_monitor
              <labels>
                host ${hostname}
              </labels>
            </source>
        
          output.conf: |-
            <match **>
              @id elasticsearch
              @type elasticsearch
              @log_level info
              type_name _doc
              include_tag_key true
              host 192.168.0.95
              port 9200
              logstash_format true
              logstash_prefix zzbservice
              logstash_dateformat %Y.%m.%d
              <buffer>
                @type file
                path /var/log/fluentd-buffers/kubernetes.system.buffer
                flush_mode interval
                retry_type exponential_backoff
                flush_thread_count 2
                flush_interval 5s
                retry_forever
                retry_max_interval 30
                chunk_limit_size 2M
                total_limit_size 500M
                overflow_action block
              </buffer>
            </match>

        fluentd-es-ds.yaml:

        apiVersion: v1
        kind: ServiceAccount
        metadata:
          name: fluentd-es
          namespace: zzb-test
          labels:
            k8s-app: fluentd-es
            addonmanager.kubernetes.io/mode: Reconcile
        ---
        kind: ClusterRole
        apiVersion: rbac.authorization.k8s.io/v1
        metadata:
          name: fluentd-es
          labels:
            k8s-app: fluentd-es
            addonmanager.kubernetes.io/mode: Reconcile
        rules:
        - apiGroups:
          - ""
          resources:
          - "namespaces"
          - "pods"
          verbs:
          - "get"
          - "watch"
          - "list"
        ---
        kind: ClusterRoleBinding
        apiVersion: rbac.authorization.k8s.io/v1
        metadata:
          name: fluentd-es
          labels:
            k8s-app: fluentd-es
            addonmanager.kubernetes.io/mode: Reconcile
        subjects:
        - kind: ServiceAccount
          name: fluentd-es
          namespace: zzb-test
          apiGroup: ""
        roleRef:
          kind: ClusterRole
          name: fluentd-es
          apiGroup: ""
        ---
        apiVersion: apps/v1
        kind: DaemonSet
        metadata:
          name: fluentd-es-v3.0.0
          namespace: zzb-test
          labels:
            k8s-app: fluentd-es
            version: v3.0.0
            addonmanager.kubernetes.io/mode: Reconcile
        spec:
          selector:
            matchLabels:
              k8s-app: fluentd-es
              version: v3.0.0
          template:
            metadata:
              labels:
                k8s-app: fluentd-es
                version: v3.0.0
              # This annotation ensures that fluentd does not get evicted if the node
              # supports critical pod annotation based priority scheme.
              # Note that this does not guarantee admission on the nodes (#40573).
              annotations:
                seccomp.security.alpha.kubernetes.io/pod: 'docker/default'
            spec:
              priorityClassName: k8s-cluster-critical
              serviceAccountName: fluentd-es
              containers:
              - name: fluentd-es
                image: registry.cn-hangzhou.aliyuncs.com/google_containers/fluentd-elasticsearch:v2.4.0 
                env:
                - name: FLUENTD_ARGS
                  value: --no-supervisor -q
                resources:
                  limits:
                    memory: 500Mi
                  requests:
                    cpu: 100m
                    memory: 200Mi
                volumeMounts:
                - name: varlog
                  mountPath: /var/log
                - name: varlibdockercontainers
                  mountPath: /var/lib/docker/containers
                  readOnly: true
                - name: config-volume
                  mountPath: /etc/fluent/config.d
                ports:
                - containerPort: 24231
                  name: prometheus
                  protocol: TCP
                livenessProbe:
                  tcpSocket:
                    port: prometheus
                  initialDelaySeconds: 5
                  timeoutSeconds: 10
                readinessProbe:
                  tcpSocket:
                    port: prometheus
                  initialDelaySeconds: 5
                  timeoutSeconds: 10
              terminationGracePeriodSeconds: 30
              volumes:
              - name: varlog
                hostPath:
                  path: /var/log
              - name: varlibdockercontainers
                hostPath:
                  path: /var/lib/docker/containers
              - name: config-volume
                configMap:
                  name: fluentd-es-config-v0.2.0

        上面是我过滤单个应用日志时的正常配置,我的需求是需要过滤多个应用日志,比如有两个业务应用a 和 b,都在同一个namespace,过滤后日志分别输出到ES的不同索引中,要怎么做呢?

          tzghost

          fluentd 的具体使用估计得在 fluentd 社区问下。

          你说的需求建议,社区正在评估,最终以什么方式呈现可以在 https://github.com/kubesphere/community 讨论。我们目前还没有实践和尝试,所以只能提供一个前面给出的 basic idea 给你了