filebeat作为daemonSet

本文详细描述了在Kubernetes环境中设置Filebeat服务,包括创建ServiceAccount、ClusterRole、ClusterRoleBinding以及创建ConfigMap来配置输入源、输出到Elasticsearch,最后部署filebeatDaemonSet,确保文件日志收集和传输的完整流程。
摘要由CSDN通过智能技术生成

创建 Filebeat 服务账户和 ClusterRole

apiVersion: v1
kind: Namespace
metadata:
  name: logging
---
apiVersion: v1
kind: ServiceAccount
metadata:
  name: filebeat
  namespace: logging
  labels:
    k8s-app: filebeat
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: filebeat
  namespace: logging
  labels:
    k8s-app: filebeat
rules:
- apiGroups: [""]
  resources:
  - namespaces
  - pods
  - nodes
  verbs:
  - get
  - watch
  - list
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: filebeat
  namespace: logging
subjects:
- kind: ServiceAccount
  name: filebeat
  namespace: logging
roleRef:
  kind: ClusterRole
  name: filebeat
  apiGroup: rbac.authorization.k8s.io

2. 创建 Filebeat ConfigMap

kind: ConfigMap
apiVersion: v1
metadata:
  name: filebeat-config
  namespace: logging



  labels:
    k8s-app: filebeat
data:
  filebeat.yml: |-
    filebeat.inputs:
    - type: filestream
      enabled: true
      paths:
        - /home/leve/logs/*/*.log
        - /var/log/leve/test/*/*.log
      tags: ["test-log"]
      fields:
        source: test-log
      multiline.pattern: '^\d{4}-\d{2}-\d{2}'
      multiline.negate: true
      multiline.match: after
      index: filebeat-service-test
    - type: log
      enabled: true
      paths:
        - /var/log/leve/*/*.log
        - /var/log/leve/dev/*/*.log
      tags: ["dev-log"]
      fields:
        source: dev-log
      multiline.pattern: '^\d{4}-\d{2}-\d{2}'
      multiline.negate: true
      multiline.match: after
      index: filebeat-service-dev
    processors:
      - add_host_metadata:
      - script:
          lang: javascript
          source: >
            function process(event) {
              try {
                var message = event.Get("message")
                var arr = message.match(/^(\d{4}-\d{2}-\d{2}T\S+)\s+([a-zA-Z]+)\s+\[([a-zA-Z]+)(.*)/)
                var date = arr[1]
                var level = arr[2]
                var service = arr[3]
                var content = arr[4]
                var fields = event.Get("fields")
                fields["date"] = date
                fields["level"] = level
                fields["service"] = service
                fields["content"] = content
                event.Put("fields", fields)
              } catch(err) {
                var fields = event.Get("fields")
                fields["js_err"] = err.message
                event.Put("fields", fields)
              }
            }
           
    filebeat.config.modules:
      path: ${path.config}/modules.d/*.yml
      reload.enabled: false
    output.elasticsearch:
      hosts: ["es1.leve.com:9202", "es2.leve.com:9202", "es3.leve.com:9202"]
      username: "elastic"
      password: "123456"

3. 创建filebeat DaemonSet

apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: filebeat
  namespace: logging
  labels:
    k8s-app: filebeat
spec:
  selector:
    matchLabels:
      k8s-app: filebeat
  template:
    metadata:
      labels:
        k8s-app: filebeat
    spec:
      affinity:
        nodeAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            nodeSelectorTerms:
            - matchExpressions:
              - key: zone
                operator: Exists
      serviceAccountName: filebeat
      terminationGracePeriodSeconds: 30
      hostNetwork: true
      dnsPolicy: ClusterFirstWithHostNet
      tolerations:
      - effect: NoSchedule
        key: node-role.kubernetes.io/master
      containers:
      - name: filebeat
        image: elastic/filebeat:8.12.2
        args: [
          "-c", "/root/filebeat.yml",
          "-e",
        ]
        env:
        - name: NODE_NAME
          valueFrom:
            fieldRef:
              fieldPath: spec.nodeName
        securityContext:
          runAsUser: 0
        resources:
          limits:
            memory: 300Mi
          requests:
            cpu: 100m
            memory: 100Mi
        volumeMounts:
        - name: config
          mountPath: /root/filebeat.yml
          readOnly: true
          subPath: filebeat.yml
        - name: data
          mountPath: /usr/share/filebeat/data
        - name: dev-log
          mountPath: /var/log/leve
        - name: test-log
          mountPath: /home/leve/logs
        - name: varlibdockercontainers
          mountPath: /var/lib/docker/containers
          readOnly: true
      volumes:
      - name: config
        configMap:
          defaultMode: 0600
          name: filebeat-config
      - name: varlibdockercontainers
        hostPath:
          path: /var/lib/docker/containers
      - name: dev-log
        hostPath:
          path: /var/log/leve
      - name: test-log
        hostPath:
          path: /home/leve/logs
      - 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
---

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xiegwei

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值