当前位置: 首页 > article >正文

k8s中部署filebeat进行日志监听并发送到es中

注意事项

1. 需要将namespace修改为自己项目中的命名空间

2. es换成对应的地址

3. filebeat-inputs中的两个配置(根据需要用任意一个就可以)

3.1 第一个配置是监听docker日志,由于系统日志太多所以这里只监听项目部署命名空间下的内容
- type: docker
  containers.ids:
  - "*"
  exclude_files: ['.*/syslog.*']  # 排除系统日志文件(示例)  
  multiline.pattern: '^\d{4}-\d{2}-\d{2}'
  multiline.negate: true
  multiline.match: after
  processors:
    - drop_fields:
        fields: ["input_type", "offset", "stream", "beat"]
    - add_kubernetes_metadata:
        in_cluster: true
    - drop_event:
        when:
          or:
            # 丢弃不属于 gateway 和 api 容器的日志
            - not:
                or:
                  - equals:
                      kubernetes.container.name: "gateway"  # 只记录 gateway 容器
                  - equals:
                      kubernetes.container.name: "api"  # 只记录 api 容器
            # 丢弃健康检查相关的日志
            - contains:
                message: "/health/status"
3.2 第二个是配置监听指定文件夹下的所有日志信息,日志有变更就会发送到es中(如果修改路径地址,需要把挂载logger-path路径一并改掉)
	- type: log
      enabled: true
      paths:
        - /home/k8s/logger/*/*.txt    # 指定文件夹下的日志
      multiline.pattern: '^\d{4}-\d{2}-\d{2}' # 长文本合并
      multiline.negate: true
      multiline.match: after
      fields_under_root: true
      exclude_lines: ['^DEBUG']  # 可选,排除 DEBUG 日志
      exclude_files: ['.gz$'] # 排除压缩文件

完整配置

---
apiVersion: v1
kind: ConfigMap
metadata:
  name: filebeat-config
  namespace: gitee
  labels:
    k8s-app: filebeat
data:
  filebeat.yml: |-
    filebeat.config:
      inputs:
        # Mounted `filebeat-inputs` configmap:
        path: ${path.config}/inputs.d/*.yml
        # Reload inputs configs as they change:
        reload.enabled: true
      modules:
        path: ${path.config}/modules.d/*.yml
        # Reload module configs as they change:
        reload.enabled: false
    output.elasticsearch:
      hosts: ['elasticsearch.gitee:9200']
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: filebeat-inputs
  namespace: gitee
  labels:
    k8s-app: filebeat
data:
  kubernetes.yml: |-
    - type: docker
      containers.ids:
        - "*"
      exclude_files: ['.*/syslog.*']
      multiline.pattern: '^\d{4}-\d{2}-\d{2}'
      multiline.negate: true
      multiline.match: after
      processors:
        - drop_fields:
            fields: ["input_type", "offset", "stream", "beat"]
        - add_kubernetes_metadata:
            in_cluster: true
        - drop_event:
            when:
              and:
                - not:
                    or:
                      - equals:
                          kubernetes.container.name: "gateway"
                      - equals:
                          kubernetes.container.name: "api"
                - contains:
                    message: "/health/status"

  file-logs.yml: |-
    - type: log
      enabled: true
      paths:
        - /home/k8s/logger/*/*.txt
      multiline.pattern: '^\d{4}-\d{2}-\d{2}'
      multiline.negate: true
      multiline.match: after
      fields_under_root: true
      exclude_lines: ['^DEBUG']
      exclude_files: ['.gz$']
---
apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: filebeat
  namespace: gitee
  labels:
    k8s-app: filebeat
spec:
  selector:
    matchLabels:
      k8s-app: filebeat
  template:
    metadata:
      labels:
        k8s-app: filebeat
    spec:
      serviceAccountName: filebeat
      terminationGracePeriodSeconds: 30
      containers:
      - name: filebeat
        image: elastic/filebeat:7.9.2
        args: [
          "-c", "/etc/filebeat.yml",
          "-e",
        ]
        securityContext:
          runAsUser: 0
        resources:
          limits:
            memory: 200Mi
          requests:
            cpu: 100m
            memory: 100Mi
        volumeMounts:
        - name: config
          mountPath: /etc/filebeat.yml
          readOnly: true
          subPath: filebeat.yml
        - name: inputs
          mountPath: /usr/share/filebeat/inputs.d
          readOnly: true
        - name: data
          mountPath: /usr/share/filebeat/data
        - name: varlibdockercontainers
          mountPath: /var/lib/docker/containers
          readOnly: true
        - name: logger-path # 持久化地址
          mountPath: /home/k8s/logger/
      volumes:
      - name: config
        configMap:
          defaultMode: 0600
          name: filebeat-config
      - name: varlibdockercontainers
        hostPath:
          path: /var/lib/docker/containers
      - name: inputs
        configMap:
          defaultMode: 0600
          name: filebeat-inputs
      - name: data
        hostPath:
          path: /var/lib/filebeat-data
          type: DirectoryOrCreate
      - name: logger-path
        hostPath:
          path: /home/k8s/logger/
          type: Directory
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: filebeat
subjects:
- kind: ServiceAccount
  name: filebeat
  namespace: gitee
roleRef:
  kind: ClusterRole
  name: filebeat
  apiGroup: rbac.authorization.k8s.io
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: filebeat
  labels:
    k8s-app: filebeat
rules:
- apiGroups: [""] # "" indicates the core API group
  resources:
  - namespaces
  - pods
  verbs:
  - get
  - watch
  - list
---
apiVersion: v1
kind: ServiceAccount
metadata:
  name: filebeat
  namespace: gitee
  labels:
    k8s-app: filebeat

http://www.kler.cn/a/410135.html

相关文章:

  • C++:当vector中存储的是自定义类型对象时注意事项
  • 项目实战:基于深度学习的人脸表情识别系统设计与实现
  • Java 8 Stream API 在数据转换中的应用 —— 将列表转换为映射
  • 【Linux学习】【Ubuntu入门】1-7 ubuntu下磁盘管理
  • Ruby 模块(Module)
  • IIFE - 立即执行函数
  • 门控循环单元(GRU)与时间序列预测应用
  • spring boot jpa中 Hibernate 注解 @Immutable 的使用场景
  • 二叉树oj题解析
  • jQuery-Word-Export 使用记录及完整修正文件下载 jquery.wordexport.js
  • 系统设计时应时刻考虑设计模式基础原则
  • flutter 专题十一 Fair原理篇Fair逻辑动态化架构设计与实现
  • 永磁同步电机末端振动抑制(输入整形)
  • 【Electron学习笔记(二)】基于Electron开发应用程序
  • 怎么规划一套电话机器人系统?
  • GitLab/GitHub 多环境配置SSH密钥
  • KADB支持arm架构Pro*c
  • 开源客户关系管理平台EspoCRM
  • 001 MATLAB介绍
  • 【Spring Cloud】 Gateway配置说明示例
  • GitHub 和 GitLab
  • SD-WAN在构建混合云架构中的作用
  • shell练习
  • 【数据分享】2001-2023年我国30米分辨率冬小麦种植分布栅格数据(免费获取)
  • 1. Klipper从安装到运行
  • 单片机结合OpenCV