elk二、filebeat抓取k8s日志——k8s-filebeat.yaml(output 到redis)

5 篇文章 1 订阅

ConfigMap API资源提供了将配置数据注入容器的方式,同时保证该机制对容器来说是透明的。ConfigMap可以被用来保存单个属性,也可以用来保存整个配置文件或者JSON二进制大对象。

ConfigMap API资源存储键/值对配置数据,这些数据可以在pods里使用。ConfigMap跟Secrets类似,但是ConfigMap可以更方便的处理不包含敏感信息的字符串。

一个DaemonSet确保所有(或部分)节点上运行分离舱的副本。随着节点添加到群集中,将添加Pod。随着节点从群集中删除,这些Pod将被垃圾收集。删除DaemonSet将清除它创建的Pod。
参考官方地址
配置位置
官方git地址
yaml语法:

|:文中自动换行 + 文末新增一空行
|+:文中自动换行 + 文末新增两空行
|-:文中自动换行 + 文末不新增行
[root@k8s-master ~]# cat > k8s-filebeat.yaml << leo
---
# 创建命名空间
kind: Namespace
apiVersion: v1
metadata:
   # 不可以使用 下划线,
   name: filebeat-namespace
   labels:
     name: filebeat-namespace


---
# 定义一个\`name=filebeat-config\`的configMap,用于指定 filebeat output
apiVersion: v1
kind: ConfigMap
# 标准 object 的元数据
metadata:
  name: filebeat-config
  namespace: filebeat-namespace
  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: false

      modules:
        path: \${path.config}/modules.d/*.yml
        # Reload module configs as they change:
        reload.enabled: false

    # To enable hints based autodiscover, remove \`filebeat.config.inputs\` configuration and uncomment this:
    #filebeat.autodiscover:
    #  providers:
    #    - type: kubernetes
    #      hints.enabled: true

    processors:
      - add_cloud_metadata: ~

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

    # ElasticSearch的配置
    #output.elasticsearch:
    #  hosts: ['\${ELASTICSEARCH_HOST:elasticsearch}:\${ELASTICSEARCH_PORT:9200}']
    #  username: \${ELASTICSEARCH_USERNAME}
    #  password: \${ELASTICSEARCH_PASSWORD}

    # redis的配置
    output.redis:
      hosts: ['\${REDIS_HOST}']
      password: '\${REDIS_PASSWORD}'
      key: '\${REDIS_KEY}'
      db: '\${REDIS_DB}'
      dataytpe: '\${REDIS_DATAYTPE}'
      # 零为一直不超时
      timeout: 0

---
# 定义一个\`name=filebeat-inputs\`的configMap,用于指定 filebeat input
apiVersion: v1
kind: ConfigMap
metadata:
  name: filebeat-inputs
  namespace: filebeat-namespace
  labels:
    k8s-app: filebeat
data:
  kubernetes.yml: |-  
    - type: docker
      # 设置指定与指定模式不匹配的任何行都属于上一行
      # filebeat抓取的日志中,从头匹配 key为message对应的内容
      multiline.pattern: '^\[|^[0-9]{4}-[0-9]{2}-[0-9]{2}|^[0-9]{1,3}\.[0-9]{1,3}'
      # 是否匹配 true为将匹配的内容追加到上一行
      multiline.negate: true
      # after表示 追加到文件后面
      multiline.match: after
      multiline.timeout: 15s
      multiline.max_lines: 10000
      tags: ['prod-filebeat']
      containers.ids:
      - "*"      
      processors:
        # 为每个 event 添加来自哪个 pod 的注解
        - add_kubernetes_metadata:
            # true: filebeat 将运行在 pod 中,否则以进程方式运行在节点上
            in_cluster: true


---
# 将 filebeat 配置为 DaemonSet
apiVersion: extensions/v1beta1
kind: DaemonSet
metadata:
  name: filebeat
  namespace: filebeat-namespace
  labels:
    k8s-app: filebeat
# DaemonSetSpec
spec:
  template:
    metadata:
      labels:
        k8s-app: filebeat
    # PodSpec
    spec:
      # 指定运行在当前 pod 的 Service Account
      serviceAccountName: filebeat
      # 优雅的终止pod所需要的时间,单位秒(0表示立即删除,nil表示使用默认宽限期)
      # 宽限期是向pod中运行的进程发送终止信号到进程被终止的时间。
      # 需要设置此值的时间大于你的进程的预期清理时间。默认为30秒
      terminationGracePeriodSeconds: 30
      # 配置运行在pod中的container
      containers:
      - name: filebeat
        image: docker.elastic.co/beats/filebeat:7.2.0
        # 向入口点传送的参数,如果没有设置则使用docker images 的 CMD
        args: [
          "-c", "/etc/filebeat.yml",
          "-e",
        ]
        # 配置环境变量,在其他配置里面可以通过 \${VAR} 获取到
        env:
        # redis 环境变量
        - name: REDIS_HOST
          value: "k8s.dev-share.top:6379"
        - name: REDIS_PASSWORD
          value: "maosiyu1987"
        - name: REDIS_KEY
          value: "sinoeyes-io"
        - name: REDIS_DB
          value: "4"
        - name: REDIS_DATAYTPE
          value: "list"

        # es 环境变量
        #- name: ELASTICSEARCH_HOST
        #  value: elasticsearch
        #- 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:
        # securityContext定义Pod或Container的权限和访问控制设置
        securityContext:
          # UID 用于运行容器进程的入口点。
          # 如果没有指定,默认为镜像指定的用户。如果\`SecurityContext\`和
          # \`PodSecurityContext\`都指定了,以\`SecurityContext\`为准
          runAsUser: 0
        # 分配容器所需的资源
        resources:
          # 设置允许的最大资源——不限制内存否则占用内存过大会导致filebeat OOM
          #limits:
            # 内存: 可以使用的单位E、P、T、G、M、K,另外 Mi 和 M 的含义相同
            #memory: 200Mi
          # 设置所需的最小资源,如果省略该配置项则默认为 limits
          requests:
            # 转换为 millicore 值并乘以100。
            # 容器每100毫秒可以使用的cpu时间总量。
            cpu: 100m
            memory: 100Mi
        # 把 volume mount 到 container 的 filesystem
        volumeMounts:
        # 将 name=\`config\` 的 volume mount 到 container 的 /etc/filebeat.yml,并设为只读
        - 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
      # 属于该 pod 的容器可以 mount 的 volume 列表
      volumes:
      # 给 volume 取个名字
      - name: config
        # 引用一个 configMap object 填充 volume
        configMap:
          # 设置创建文件时默认的文件读写权限
          # 必须是0~0777之间的值。默认为 0644
          defaultMode: 0600
          # 要引用的 configMap object 的 name
          name: filebeat-config
      - name: varlibdockercontainers
        # 宿主机直接暴露给 container 的预先存在的文件或目录。
        hostPath:
          # 宿主机上的路径,如果是链接文件,它会找到真实的文件路径。
          path: /var/lib/docker/containers
      - name: inputs
        configMap:
          defaultMode: 0600
          name: filebeat-inputs
      # 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:
          path: /var/lib/filebeat-data
          # 默认为"",在 mount hostPath volume 之前不会执行任何检查
          # DirectoryOrCreate 代表如果给定路径中不存在,则会根据需要创建一个空目录(0755),
          # 并与 Kubelet 具有相同的组和所有权
          # 其他的 type 参考:https://kubernetes.io/docs/concepts/storage/volumes/#hostpath
          type: DirectoryOrCreate


---
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRoleBinding
metadata:
  name: filebeat
# 主题包含对该角色适用的对象的引用
subjects:
- kind: ServiceAccount
  name: filebeat
  namespace: filebeat-namespace
# 只能引用全局 namespace 中的 clusterRole。如果无法解析 roleRef,则 Authorizer 一定会返回错误。
roleRef:
  kind: ClusterRole
  name: filebeat
  apiGroup: rbac.authorization.k8s.io

---
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRole
metadata:
  name: filebeat
  labels:
    k8s-app: filebeat
# 此 ClusterRole 的所有策略规则
rules:
  # 包含 resources 的 apiGroup 的名字,如果指定了多个 apiGroup,
  # 那么对于任何一个 apiGroup 资源请求的任何操作都会允许
  # "" indicates the core API group
- apiGroups: [""]
  # 此 rule 适用的资源列表,\`ResourceAll\`代表所有资源
  resources:
  - namespaces
  - pods
  # 此 rule 适用的所有资源类型和属性限制的动词列表,VerbAll代表所有动词
  verbs:
  - get
  - watch
  - list

---
apiVersion: v1
kind: ServiceAccount
metadata:
  name: filebeat
  namespace: filebeat-namespace
  labels:
    k8s-app: filebeat
leo

[root@k8s-master ~]#

第二种可以直接创建k8s-filebeat.yaml文件

vim k8s-filebeat.yaml

内容如下:

---
# 创建命名空间
kind: Namespace
apiVersion: v1
metadata:
   # 不可以使用 下划线,
   name: filebeat-namespace
   labels:
     name: filebeat-namespace


---
# 定义一个`name=filebeat-config`的configMap,用于指定 filebeat output
apiVersion: v1
kind: ConfigMap
# 标准 object 的元数据
metadata:
  name: filebeat-config
  namespace: filebeat-namespace
  labels:
    k8s-app: filebeat
data:
  filebeat.yml: |-
    filebeat.config:
      inputs:
       # json.keys_under_root: true
       # json.add_error_key: true
       # json.message_key: log
       # multiline.pattern: '^Mon|Tue|Web|Thu|Fri|Sat|Sun [[:space:]] Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec'
       # multiline.negate: true
       # multiline.match: after
       # multiline.timeout: 10s

        # Mounted `filebeat-inputs` configmap:
        path: ${path.config}/inputs.d/*.yml
        # Reload inputs configs as they change:
        reload.enabled: false
      modules:
        path: ${path.config}/modules.d/*.yml
        # Reload module configs as they change:
        reload.enabled: false

    # To enable hints based autodiscover, remove `filebeat.config.inputs` configuration and uncomment this:
    #filebeat.autodiscover:
    #  providers:
    #    - type: kubernetes
    #      hints.enabled: true
    #- type: log
    #  tags: ["uat-dev15"]
    #  paths:
    #    - /var/lib/docker/containers/*/*.log


    #processors:
    #  - add_cloud_metadata:

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

    # ElasticSearch的配置
    #output.elasticsearch:
    #  hosts: ['${ELASTICSEARCH_HOST:elasticsearch}:${ELASTICSEARCH_PORT:9200}']
    #  username: ${ELASTICSEARCH_USERNAME}
    #  password: ${ELASTICSEARCH_PASSWORD}


    # redis的配置
    output.redis:
      hosts: ['${REDIS_HOST}']
      password: '${REDIS_PASSWORD}'
      key: '${REDIS_KEY}'
      db: '${REDIS_DB}'
      dataytpe: '${REDIS_DATAYTPE}'


---
# 定义一个`name=filebeat-inputs`的configMap,用于指定 filebeat input
apiVersion: v1
kind: ConfigMap
metadata:
  name: filebeat-inputs
  namespace: filebeat-namespace
  labels:
    k8s-app: filebeat
data:
  kubernetes.yml: |-
    #- type: log
    #  tags: ["uat-dev15"]
    #  paths:
    #    - /var/lib/docker/containers/*/*.log
    - type: docker
      tags: ["uat-dev15"]
     # json.keys_under_root: true
     # json.add_error_key: true
     # json.message_key: log
     # multiline.pattern: '^Mon|Tue|Web|Thu|Fri|Sat|Sun [[:space:]] Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec'
      multiline.pattern: '^\[|^[0-9]{4}-[0-9]{2}-[0-9]{2}|^[0-9]{1,3}\.[0-9]{1,3}'
      multiline.negate: true
      multiline.match: after
      multiline.timeout: 10s
      multiline.max_lines: 10000

      containers.ids:
      - "*"
     # json.keys_under_root: true
     # json.add_error_key: true
     # json.message_key: log
     # multiline.pattern: '^Mon|Tue|Web|Thu|Fri|Sat|Sun [[:space:]] Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec'
     # multiline.negate: true
     # multiline.match: after
     # multiline.timeout: 10s

      processors:
        # 为每个 event 添加来自哪个 pod 的注解
        - add_kubernetes_metadata:
            # true: filebeat 将运行在 pod 中,否则以进程方式运行在节点上
            in_cluster: true

---
# 将 filebeat 配置为 DaemonSet
apiVersion: extensions/v1beta1
kind: DaemonSet
metadata:
  name: filebeat
  namespace: filebeat-namespace
  labels:
    k8s-app: filebeat
# DaemonSetSpec
spec:
  template:
    metadata:
      labels:
        k8s-app: filebeat
    # PodSpec
    spec:
      # 指定运行在当前 pod 的 Service Account
      serviceAccountName: filebeat
      # 优雅的终止pod所需要的时间,单位秒(0表示立即删除,nil表示使用默认宽限期)
      # 宽限期是向pod中运行的进程发送终止信号到进程被终止的时间。
      # 需要设置此值的时间大于你的进程的预期清理时间。默认为30秒
      terminationGracePeriodSeconds: 30
      # 配置运行在pod中的container
      containers:
      - name: filebeat
        image: docker.elastic.co/beats/filebeat:7.2.0
        # 向入口点传送的参数,如果没有设置则使用docker images 的 CMD
        args: [
          "-c", "/etc/filebeat.yml",
          "-e",
        ]
        # 配置环境变量,在其他配置里面可以通过 ${VAR} 获取到
        env:
        # redis 环境变量
        - name: REDIS_HOST
          value: "192.168.180.6:6379"
        - name: REDIS_PASSWORD
          value: "sinoeyes"
        - name: REDIS_KEY
          value: "sinoeyes-io"
        - name: REDIS_DB
          value: "4"
        - name: REDIS_DATAYTPE
          value: "list"
        # es 环境变量
        #- name: ELASTICSEARCH_HOST
        #  value: elasticsearch
        #- 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:
        # securityContext定义Pod或Container的权限和访问控制设置
        securityContext:
          # UID 用于运行容器进程的入口点。
          # 如果没有指定,默认为镜像指定的用户。如果`SecurityContext`和
          # `PodSecurityContext`都指定了,以`SecurityContext`为准
          runAsUser: 0
        # 分配容器所需的资源
        resources:
          # 设置允许的最大资源
          limits:
            # 内存: 可以使用的单位E、P、T、G、M、K,另外 Mi 和 M 的含义相同
            memory: 200Mi
          # 设置所需的最小资源,如果省略该配置项则默认为 limits
          requests:
            # 转换为 millicore 值并乘以100。
            # 容器每100毫秒可以使用的cpu时间总量。
            cpu: 100m
            memory: 100Mi
        # 把 volume mount 到 container 的 filesystem
        volumeMounts:
        # 将 name=`config` 的 volume mount 到 container 的 /etc/filebeat.yml,并设为只读
        - 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
      # 属于该 pod 的容器可以 mount 的 volume 列表
      volumes:
      # 给 volume 取个名字
      - name: config
        # 引用一个 configMap object 填充 volume
        configMap:
          # 设置创建文件时默认的文件读写权限
          # 必须是0~0777之间的值。默认为 0644
          defaultMode: 0600
          # 要引用的 configMap object 的 name
          name: filebeat-config
      - name: varlibdockercontainers
        # 宿主机直接暴露给 container 的预先存在的文件或目录。
        hostPath:
          # 宿主机上的路径,如果是链接文件,它会找到真实的文件路径。
          path: /var/lib/docker/containers/
      - name: inputs
        configMap:
          defaultMode: 0600
          name: filebeat-inputs
      # 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:
          path: /var/lib/filebeat-data
          # 默认为"",在 mount hostPath volume 之前不会执行任何检查
          # DirectoryOrCreate 代表如果给定路径中不存在,则会根据需要创建一个空目录(0755),
          # 并与 Kubelet 具有相同的组和所有权
          # 其他的 type 参考:https://kubernetes.io/docs/concepts/storage/volumes/#hostpath
          type: DirectoryOrCreate


---
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRoleBinding
metadata:
  name: filebeat
# 主题包含对该角色适用的对象的引用
subjects:
- kind: ServiceAccount
  name: filebeat
  namespace: filebeat-namespace
# 只能引用全局 namespace 中的 clusterRole。如果无法解析 roleRef,则 Authorizer 一定会返回错误。
roleRef:
  kind: ClusterRole
  name: filebeat
  apiGroup: rbac.authorization.k8s.io

---
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRole
metadata:
  name: filebeat
  labels:
    k8s-app: filebeat
# 此 ClusterRole 的所有策略规则
rules:
  # 包含 resources 的 apiGroup 的名字,如果指定了多个 apiGroup,
  # 那么对于任何一个 apiGroup 资源请求的任何操作都会允许
  # "" indicates the core API group
- apiGroups: [""]
  # 此 rule 适用的资源列表,`ResourceAll`代表所有资源
  resources:
  - namespaces
  - pods
  # 此 rule 适用的所有资源类型和属性限制的动词列表,VerbAll代表所有动词
  verbs:
  - get
  - watch
  - list

---
apiVersion: v1
kind: ServiceAccount
metadata:
  name: filebeat
  namespace: filebeat-namespace
  labels:
    k8s-app: filebeat
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值