zoukankan      html  css  js  c++  java
  • k8s filebeat收集容器中的日志文件

    k8s-filebeat收集容器中的日志文件

    1. K8s-收集容器中的日志文件

    • 收集容器文件图解
      image

      针对容器中日志文件:在Pod中增加一个容器运行日志采集器,使用emtyDir共享日志目录让日志采集器读取到日志文件

    • 示例代码

      [root@k8s-master elk]# vi app-log-logfile.yaml
      [root@k8s-master elk]# cat app-log-logfile.yaml 
      apiVersion: apps/v1
      kind: Deployment
      metadata:
        name: app-log-logfile
      spec:
        replicas: 3
        selector:
          matchLabels:
            project: microservice
            app: nginx-logfile
        template:
          metadata:
            labels:
              project: microservice
              app: nginx-logfile
          spec:
            containers:
            # 应用容器
            - name: nginx 
              image: lizhenliang/nginx-php
              # 将数据卷挂载到日志目录
              volumeMounts:
              - name: nginx-logs 
                mountPath: /usr/local/nginx/logs
            # 日志采集器容器
            - name: filebeat
              image: elastic/filebeat:7.10.1 
              args: [
                "-c", "/etc/filebeat.yml",
                "-e",
              ]
              resources:
                requests:
                  cpu: 100m
                  memory: 100Mi
                limits:
                  memory: 500Mi
              securityContext:
                runAsUser: 0
              volumeMounts:
              # 挂载filebeat配置文件
              - name: filebeat-config
                mountPath: /etc/filebeat.yml
                subPath: filebeat.yml
              # 将数据卷挂载到日志目录
              - name: nginx-logs 
                mountPath: /usr/local/nginx/logs
            # 数据卷共享日志目录
            volumes:
            - name: nginx-logs
              emptyDir: {}
            - name: filebeat-config
              configMap:
                name: filebeat-nginx-config
      ---
      apiVersion: v1
      kind: Service
      metadata:
        name: app-log-logfile
      spec:
        ports:
        - port: 80
          protocol: TCP
          targetPort: 80
        selector:
          project: microservice
          app: nginx-logfile
      ---
      apiVersion: v1
      kind: ConfigMap
      metadata:
        name: filebeat-nginx-config
        
      data:
        # 配置文件保存在ConfigMap
        filebeat.yml: |-
          filebeat.inputs:
            - type: log
              paths:
                - /usr/local/nginx/logs/access.log
              # tags: ["access"]
              fields_under_root: true
              fields:
                project: microservice
                app: nginx
      
          setup.ilm.enabled: false
          setup.template.name: "nginx-access"
          setup.template.pattern: "nginx-access-*"
      
          output.elasticsearch:
            hosts: ['127.0.0.1:9200']
            username: "admin"
            password: "12345678"
            index: "nginx-access-%{+yyyy.MM.dd}"
      

    2. 案例

    • 编写配置文件

      [root@k8s-master elk]# vi app-log-logfile.yaml
      [root@k8s-master elk]# cat app-log-logfile.yaml 
      apiVersion: apps/v1
      kind: Deployment
      metadata:
        name: app-log-logfile
      spec:
        replicas: 3
        selector:
          matchLabels:
            project: microservice
            app: nginx-logfile
        template:
          metadata:
            labels:
              project: microservice
              app: nginx-logfile
          spec:
            containers:
            # 应用容器
            - name: nginx 
              image: lizhenliang/nginx-php
              # 将数据卷挂载到日志目录
              volumeMounts:
              - name: nginx-logs 
                mountPath: /usr/local/nginx/logs
            # 日志采集器容器
            - name: filebeat
              image: elastic/filebeat:7.10.1 
              args: [
                "-c", "/etc/filebeat.yml",
                "-e",
              ]
              resources:
                requests:
                  cpu: 100m
                  memory: 100Mi
                limits:
                  memory: 500Mi
              securityContext:
                runAsUser: 0
              volumeMounts:
              # 挂载filebeat配置文件
              - name: filebeat-config
                mountPath: /etc/filebeat.yml
                subPath: filebeat.yml
              # 将数据卷挂载到日志目录
              - name: nginx-logs 
                mountPath: /usr/local/nginx/logs
            # 数据卷共享日志目录
            volumes:
            - name: nginx-logs
              emptyDir: {}
            - name: filebeat-config
              configMap:
                name: filebeat-nginx-config
      ---
      apiVersion: v1
      kind: Service
      metadata:
        name: app-log-logfile
      spec:
        ports:
        - port: 80
          protocol: TCP
          targetPort: 80
        selector:
          project: microservice
          app: nginx-logfile
      ---
      apiVersion: v1
      kind: ConfigMap
      metadata:
        name: filebeat-nginx-config
        
      data:
        # 配置文件保存在ConfigMap
        filebeat.yml: |-
          filebeat.inputs:
            - type: log
              paths:
                - /usr/local/nginx/logs/access.log
              # tags: ["access"]
              fields_under_root: true
              fields:
                project: microservice
                app: nginx
      
          setup.ilm.enabled: false
          setup.template.name: "nginx-access"
          setup.template.pattern: "nginx-access-*"
      
          output.elasticsearch:
            hosts: ['127.0.0.1:9200']
            username: "admin"
            password: "12345678"
            index: "nginx-access-%{+yyyy.MM.dd}"
      
    • 运行配置文件

      [root@k8s-master elk]# kubectl apply -f app-log-logfile.yaml 
      deployment.apps/app-log-logfile created
      service/app-log-logfile created
      configmap/filebeat-nginx-config created
      
    • 查看容器是否启动

      [root@k8s-master elk]# kubectl get pods
      NAME                               READY   STATUS    RESTARTS   AGE
      app-log-logfile-68b99bf447-fvvdj   2/2     Running   0          2m21s
      
    • 访问nginx

      [root@k8s-master elk]# kubectl get pods -o wide
      NAME                               READY   STATUS    RESTARTS   AGE     IP              NODE         NOMINATED NODE   READINESS GATES
      app-log-logfile-68b99bf447-fvvdj   2/2     Running   0          5m53s   10.244.85.199   k8s-node01   <none>           <none>
      nginx                              1/1     Running   0          33h     10.244.85.196   k8s-node01   <none>           <none>
      tomcat                             1/1     Running   0          33h     10.244.85.197   k8s-node01   <none>           <none>
      web-5df8b97c79-hksfc               1/1     Running   0          3d4h    10.244.85.195   k8s-node01   <none>           <none>
      [root@k8s-master elk]# curl -I 10.244.85.199/status.html
      HTTP/1.1 200 OK
      Server: nginx
      Date: Thu, 08 Jul 2021 14:41:48 GMT
      Content-Type: text/html
      Content-Length: 3
      Last-Modified: Tue, 01 Jan 2019 20:08:29 GMT
      Connection: close
      ETag: "5c2bc8bd-3"
      Accept-Ranges: bytes
      [root@k8s-master elk]# curl -I 10.244.85.199/status.html
      HTTP/1.1 200 OK
      Server: nginx
      Date: Thu, 08 Jul 2021 14:41:48 GMT
      Content-Type: text/html
      Content-Length: 3
      Last-Modified: Tue, 01 Jan 2019 20:08:29 GMT
      Connection: close
      ETag: "5c2bc8bd-3"
      Accept-Ranges: bytes
      
    • kibana验证是否有索引
      image

    • 创建索引验证数据
      image
      image
      image
      image
      image
      image

  • 相关阅读:
    安全相关小知识
    http小知识
    跨域资源共享 CORS
    Django——CRSF攻击及处理
    Django——XSS攻击及处理
    Django——模版层(前后端交互编码方式,django模板使用的两种方式,模板语法之变量&深度查询句点符,模板渲染成标签还是原字符串,过滤器+自定义,标签+自定义)
    Django——Postman介绍及安装, 测试项目
    Django——视图层(请求&响应对象,cbv和fbv,文件上传)
    一个http请求从浏览器发出去,经历的过程(即上网流程)
    Django——有名分组 无名分组,反向解析,名称空间
  • 原文地址:https://www.cnblogs.com/scajy/p/15543601.html
Copyright © 2011-2022 走看看